Represent large number types as strings
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONCodecFactory.java
index 15fe09f1fcf2775d90f30b73d210f9dccd1da386..87ee320406e2d368874b458ebc3d04b97b512b81 100644 (file)
@@ -7,12 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.data.codec.gson;
 
-import static com.google.common.base.Verify.verifyNotNull;
-import static java.util.Objects.requireNonNull;
-
 import com.google.common.annotations.Beta;
 import java.util.List;
-import java.util.function.BiFunction;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
 import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
@@ -50,112 +47,104 @@ import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
  * a particular {@link SchemaContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
  */
 @Beta
-public final class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
-    private final BiFunction<SchemaContext, JSONCodecFactory, JSONInstanceIdentifierCodec> iidCodecSupplier;
-    private final JSONCodec<?> iidCodec;
-
-    JSONCodecFactory(final SchemaContext context, final CodecCache<JSONCodec<?>> cache,
-            final BiFunction<SchemaContext, JSONCodecFactory, JSONInstanceIdentifierCodec> iidCodecSupplier) {
+public abstract class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
+    JSONCodecFactory(final @NonNull SchemaContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
         super(context, cache);
-        this.iidCodecSupplier = requireNonNull(iidCodecSupplier);
-        iidCodec = verifyNotNull(iidCodecSupplier.apply(context, this));
     }
 
     @Override
-    protected JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
+    protected final JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
         return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
+    protected final JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
         return new BooleanJSONCodec(BooleanStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
+    protected final JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
         return new QuotedJSONCodec<>(BitsStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
-        return new NumberJSONCodec<>(DecimalStringCodec.from(type));
+    protected final JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
+        return wrapDecimalCodec(DecimalStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
+    protected final JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
         return EmptyJSONCodec.INSTANCE;
     }
 
     @Override
-    protected JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
+    protected final JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
         return new QuotedJSONCodec<>(EnumStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
+    protected final JSONCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
         return new IdentityrefJSONCodec(getSchemaContext(), module);
     }
 
     @Override
-    protected JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
-        return iidCodec;
-    }
-
-    @Override
-    protected JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
+    protected final JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
+    protected final JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
+    protected final JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
-        return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
+    protected final JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
+        return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> stringCodec(final StringTypeDefinition type) {
+    protected final JSONCodec<?> stringCodec(final StringTypeDefinition type) {
         return new QuotedJSONCodec<>(StringStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
+    protected final JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
+    protected final JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
+    protected final JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
-        return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
+    protected final JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
+        return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
     }
 
     @Override
-    protected JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
+    protected final JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
         return UnionJSONCodec.create(type, codecs);
     }
 
     @Override
-    protected JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
+    protected final JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
         return NullJSONCodec.INSTANCE;
     }
 
+    @Override
+    protected abstract JSONCodec<?> instanceIdentifierCodec(InstanceIdentifierTypeDefinition type);
+
     // Returns a one-off factory for the purposes of normalizing an anydata tree.
     //
     // FIXME: 5.0.0: this is really ugly, as we should be able to tell if the new context is the same as ours and
@@ -165,7 +154,13 @@ public final class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
     //
     //               The above is not currently possible, as we cannot reference JSONCodecFactorySupplier from the
     //               factory due to that potentially creating a circular reference.
-    JSONCodecFactory rebaseTo(final SchemaContext newSchemaContext) {
-        return new JSONCodecFactory(newSchemaContext, new LazyCodecCache<>(), iidCodecSupplier);
+    final JSONCodecFactory rebaseTo(final SchemaContext newSchemaContext) {
+        return rebaseTo(newSchemaContext, new LazyCodecCache<>());
     }
+
+    abstract JSONCodecFactory rebaseTo(SchemaContext newSchemaContext, CodecCache<JSONCodec<?>> newCache);
+
+    abstract JSONCodec<?> wrapDecimalCodec(DecimalStringCodec decimalCodec);
+
+    abstract JSONCodec<?> wrapIntegerCodec(AbstractIntegerStringCodec<?, ?> integerCodec);
 }