X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-codec-gson%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fcodec%2Fgson%2FJSONCodecFactory.java;h=fd1bf502d90c70471a1d09683464570d209fa865;hb=a78442e67b5b67976ebc8291534cf004780a2446;hp=1962556feac5ba6a75ba7fe1a1713cf8cd09603a;hpb=65b95496775cd06d576f59b3ca3948636e5c8f60;p=yangtools.git diff --git a/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java b/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java index 1962556fea..fd1bf502d9 100644 --- a/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java +++ b/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java @@ -10,13 +10,8 @@ package org.opendaylight.yangtools.yang.data.codec.gson; import com.google.common.annotations.Beta; import com.google.common.base.Preconditions; import com.google.common.base.Verify; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; import com.google.gson.stream.JsonWriter; import java.io.IOException; -import javax.annotation.Nonnull; -import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; @@ -36,7 +31,7 @@ import org.slf4j.LoggerFactory; * a particular {@link SchemaContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s. */ @Beta -public final class JSONCodecFactory { +public abstract class JSONCodecFactory { private static final Logger LOG = LoggerFactory.getLogger(JSONCodecFactory.class); private static final JSONCodec NULL_CODEC = new JSONCodec() { @Override @@ -56,19 +51,10 @@ public final class JSONCodecFactory { } }; - private final LoadingCache> codecs = CacheBuilder.newBuilder().softValues() - .build(new CacheLoader>() { - @Override - public JSONCodec load(@Nonnull final TypedSchemaNode key) throws Exception { - final TypeDefinition type = key.getType(); - return createCodec(key, type); - } - }); - private final SchemaContext schemaContext; private final JSONCodec iidCodec; - private JSONCodecFactory(final SchemaContext context) { + JSONCodecFactory(final SchemaContext context) { this.schemaContext = Preconditions.checkNotNull(context); iidCodec = new JSONStringInstanceIdentifierCodec(context, this); } @@ -80,67 +66,55 @@ public final class JSONCodecFactory { * @return A codec factory instance. */ public static JSONCodecFactory create(final SchemaContext context) { - return new JSONCodecFactory(context); + return SharedJSONCodecFactory.get(context); } - private JSONCodec createCodec(final DataSchemaNode key, final TypeDefinition type) { + final JSONCodec createCodec(final DataSchemaNode key, final TypeDefinition type) { if (type instanceof LeafrefTypeDefinition) { return createReferencedTypeCodec(key, (LeafrefTypeDefinition) type); } else if (type instanceof IdentityrefTypeDefinition) { - return createIdentityrefTypeCodec(key); + return new JSONStringIdentityrefCodec(schemaContext, key.getQName().getModule()); } else if (type instanceof UnionTypeDefinition) { return createUnionTypeCodec(key, (UnionTypeDefinition) type); - } - return createFromSimpleType(key, type); - } - - private JSONCodec createReferencedTypeCodec(final DataSchemaNode schema, - final LeafrefTypeDefinition type) { - // FIXME: Verify if this does indeed support leafref of leafref - final TypeDefinition referencedType = - SchemaContextUtil.getBaseTypeForLeafRef(type, getSchemaContext(), schema); - Verify.verifyNotNull(referencedType, "Unable to find base type for leafref node '%s'.", schema.getPath()); - return createCodec(schema, referencedType); - } - - private JSONCodec createIdentityrefTypeCodec(final DataSchemaNode schema) { - final JSONCodec jsonStringIdentityrefCodec = - new JSONStringIdentityrefCodec(schemaContext, schema.getQName().getModule()); - return jsonStringIdentityrefCodec; - } - - private JSONCodec createUnionTypeCodec(final DataSchemaNode schema, final UnionTypeDefinition type) { - final JSONCodec jsonStringUnionCodec = new JSONStringUnionCodec(schema, type, this); - return jsonStringUnionCodec; - } - - private JSONCodec createFromSimpleType(final DataSchemaNode schema, final TypeDefinition type) { - if (type instanceof InstanceIdentifierTypeDefinition) { + } else if (type instanceof InstanceIdentifierTypeDefinition) { return iidCodec; - } - if (type instanceof EmptyTypeDefinition) { + } else if (type instanceof EmptyTypeDefinition) { return JSONEmptyCodec.INSTANCE; } final TypeDefinitionAwareCodec codec = TypeDefinitionAwareCodec.from(type); if (codec == null) { - LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName() - .getLocalName()); + // catches anyxml + LOG.debug("Codec for {} is not implemented yet", type); return NULL_CODEC; } return AbstractJSONCodec.create(codec); } - SchemaContext getSchemaContext() { + final SchemaContext getSchemaContext() { return schemaContext; } JSONCodec codecFor(final DataSchemaNode schema) { Preconditions.checkArgument(schema instanceof TypedSchemaNode, "Unsupported node type %s", schema.getClass()); - return codecs.getUnchecked((TypedSchemaNode) schema); + return codecFor((TypedSchemaNode) schema); } - JSONCodec codecFor(final DataSchemaNode schema, final TypeDefinition unionSubType) { + abstract JSONCodec codecFor(final TypedSchemaNode schema); + + final JSONCodec codecFor(final DataSchemaNode schema, final TypeDefinition unionSubType) { return createCodec(schema, unionSubType); } + + private JSONCodec createReferencedTypeCodec(final DataSchemaNode schema, final LeafrefTypeDefinition type) { + // FIXME: Verify if this does indeed support leafref of leafref + final TypeDefinition referencedType = SchemaContextUtil.getBaseTypeForLeafRef(type, schemaContext, schema); + Verify.verifyNotNull(referencedType, "Unable to find base type for leafref node '%s'.", schema.getPath()); + return createCodec(schema, referencedType); + } + + private JSONCodec createUnionTypeCodec(final DataSchemaNode schema, final UnionTypeDefinition type) { + final JSONCodec jsonStringUnionCodec = new JSONStringUnionCodec(schema, type, this); + return jsonStringUnionCodec; + } }