X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-dom-codec%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fdom%2Fcodec%2Fimpl%2FValueTypeCodec.java;h=55e5d1a50c67cc2932e1d8196a9ca787610e1458;hb=0150ddd6c4e3c61a761dacb9161e84b3f38b748a;hp=e0a4162b8c55db829399a7fbb9f71ce64373faec;hpb=04280830979caa7550928315b32b35e2edf4836d;p=mdsal.git diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/ValueTypeCodec.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/ValueTypeCodec.java index e0a4162b8c..55e5d1a50c 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/ValueTypeCodec.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/ValueTypeCodec.java @@ -11,36 +11,44 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; -import org.opendaylight.yangtools.concepts.Codec; -import org.opendaylight.yangtools.yang.binding.util.BindingReflections; -import org.opendaylight.yangtools.yang.common.Empty; +import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections; +import org.opendaylight.yangtools.concepts.IllegalArgumentCodec; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; /** * Value codec, which serializes / deserializes values from DOM simple values. */ -abstract class ValueTypeCodec implements Codec { - - private static final Cache, SchemaUnawareCodec> STATIC_CODECS = CacheBuilder.newBuilder().weakKeys() - .build(); +// FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior +abstract class ValueTypeCodec implements IllegalArgumentCodec { + /* + * Use identity comparison for keys and allow classes to be GCd themselves. + * + * Since codecs can (and typically do) hold a direct or indirect strong reference to the class, they need to be also + * accessed via reference. Using a weak reference could be problematic, because the codec would quite often be only + * weakly reachable. We therefore use a soft reference, whose implementation guidance is suitable to our use case: + * + * "Virtual machine implementations are, however, encouraged to bias against clearing recently-created or + * recently-used soft references." + */ + private static final Cache, SchemaUnawareCodec> STATIC_CODECS = CacheBuilder.newBuilder() + .weakKeys().softValues().build(); /** * Marker interface for codecs, which functionality will not be affected by schema change (introduction of new YANG * modules) they may have one static instance generated when first time needed. */ - interface SchemaUnawareCodec extends Codec { + // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior + interface SchemaUnawareCodec extends IllegalArgumentCodec { } /** * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers, - * binary and strings. + * binary, strings and empty. */ public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() { - @Override public Object serialize(final Object input) { return input; @@ -52,31 +60,11 @@ abstract class ValueTypeCodec implements Codec { } }; - public static final SchemaUnawareCodec EMPTY_CODEC = new SchemaUnawareCodec() { - - @Override - public Object serialize(final Object input) { - // Empty type has Empty value in NormalizedNode and Composite Node representation - return Empty.getInstance(); - } - - @Override - public Object deserialize(final Object input) { - /* Empty type has boolean.TRUE representation in Binding-aware world otherwise it is null / false. - * So when codec is triggered, empty leaf is present, that means we are safe to return true. - */ - return Boolean.TRUE; - } - }; - - private static final Callable EMPTY_LOADER = () -> EMPTY_CODEC; - - public static SchemaUnawareCodec getCodecFor(final Class typeClz, final TypeDefinition def) { if (BindingReflections.isBindingClass(typeClz)) { return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def)); } - return def instanceof EmptyTypeDefinition ? EMPTY_CODEC : NOOP_CODEC; + return NOOP_CODEC; } private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class typeClz, @@ -105,10 +93,9 @@ abstract class ValueTypeCodec implements Codec { @SuppressWarnings("rawtypes") static ValueTypeCodec encapsulatedValueCodecFor(final Class typeClz, final TypeDefinition typeDef, - final Codec delegate) { + final IllegalArgumentCodec delegate) { SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz, EncapsulatedValueCodec.loader(typeClz, typeDef)); return new CompositeValueCodec(extractor, delegate); } - }