Integrate JSONCodecFactory specializations 96/101996/7
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 7 Aug 2022 18:33:31 +0000 (20:33 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 7 Aug 2022 21:26:54 +0000 (23:26 +0200)
There is no point in keeping simple subclasses separate, move them
to be nest-mates and tighen them up. This also integrates
JSONInstanceIdentifierCodec, as it provides some brevity.

Change-Id: Iaea1cb529603976324516516a7f172b33f68e127
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java
codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactorySupplier.java
codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONInstanceIdentifierCodec.java
codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/Lhotka02JSONCodecFactory.java [deleted file]
codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/Lhotka02JSONInstanceIdentifierCodec.java [deleted file]
codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/RFC7951JSONCodecFactory.java [deleted file]
codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/RFC7951JSONInstanceIdentifierCodec.java [deleted file]

index 1de48b3b58f8a9942308398f6fcbdc30dd476c98..1aa7192e6b0328e1dcf493407d0f00798a921b77 100644 (file)
@@ -7,8 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.data.codec.gson;
 
+import static com.google.common.base.Verify.verifyNotNull;
+
 import com.google.common.annotations.Beta;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 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;
@@ -47,10 +51,58 @@ import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
  * a particular {@link EffectiveModelContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
  */
 @Beta
-public abstract sealed class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>>
-        permits Lhotka02JSONCodecFactory, RFC7951JSONCodecFactory {
-    JSONCodecFactory(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
+public abstract sealed class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
+    static final class Lhotka02 extends JSONCodecFactory {
+        Lhotka02(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
+            super(context, cache, JSONInstanceIdentifierCodec.Lhotka02::new);
+        }
+
+        @Override
+        Lhotka02 rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
+            return new Lhotka02(newSchemaContext, newCache);
+        }
+
+        @Override
+        JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
+            return new NumberJSONCodec<>(decimalCodec);
+        }
+
+        @Override
+        JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
+            return new NumberJSONCodec<>(integerCodec);
+        }
+    }
+
+    static final class RFC7951 extends JSONCodecFactory {
+        RFC7951(final @NonNull  EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
+            super(context, cache, JSONInstanceIdentifierCodec.RFC7951::new);
+        }
+
+        @Override
+        RFC7951 rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
+            return new RFC7951(newSchemaContext, newCache);
+        }
+
+        @Override
+        JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
+            return new QuotedJSONCodec<>(decimalCodec);
+        }
+
+        @Override
+        JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
+            return new QuotedJSONCodec<>(integerCodec);
+        }
+    }
+
+    private final @NonNull JSONInstanceIdentifierCodec iidCodec;
+
+    @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR",
+        justification = "https://github.com/spotbugs/spotbugs/issues/1867")
+    private JSONCodecFactory(final @NonNull EffectiveModelContext context,
+            final @NonNull CodecCache<JSONCodec<?>> cache,
+            final BiFunction<EffectiveModelContext, JSONCodecFactory, @NonNull JSONInstanceIdentifierCodec> iidCodec) {
         super(context, cache);
+        this.iidCodec = verifyNotNull(iidCodec.apply(context, this));
     }
 
     @Override
@@ -88,6 +140,11 @@ public abstract sealed class JSONCodecFactory extends AbstractCodecFactory<JSONC
         return new IdentityrefJSONCodec(getEffectiveModelContext(), module);
     }
 
+    @Override
+    protected final JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
+        return iidCodec;
+    }
+
     @Override
     protected final JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
@@ -143,9 +200,6 @@ public abstract sealed class JSONCodecFactory extends AbstractCodecFactory<JSONC
         return NullJSONCodec.INSTANCE;
     }
 
-    @Override
-    protected abstract JSONCodec<?> instanceIdentifierCodec(InstanceIdentifierTypeDefinition type);
-
     // Returns a one-off factory for the purposes of normalizing an anydata tree.
     //
     // FIXME: 7.0.0: this is really ugly, as we should be able to tell if the new context is the same as ours and
index be187642aa6dd25f556fa60ca75d292a51dd1cc4..a87d96b4326e3ec32d7cd7dc3467dba87cade916 100644 (file)
@@ -46,7 +46,7 @@ public enum JSONCodecFactorySupplier {
     RFC7951() {
         @Override
         JSONCodecFactory createFactory(final EffectiveModelContext context, final CodecCache<JSONCodec<?>> cache) {
-            return new RFC7951JSONCodecFactory(context, cache);
+            return new JSONCodecFactory.RFC7951(context, cache);
         }
     },
     /**
@@ -55,7 +55,7 @@ public enum JSONCodecFactorySupplier {
     DRAFT_LHOTKA_NETMOD_YANG_JSON_02() {
         @Override
         JSONCodecFactory createFactory(final EffectiveModelContext context, final CodecCache<JSONCodec<?>> cache) {
-            return new Lhotka02JSONCodecFactory(context, cache);
+            return new JSONCodecFactory.Lhotka02(context, cache);
         }
     };
 
index 419dc12f7faea0d76858774054a19ead07e07e41..17832f940ed0e08fee90fee1c66723c0afac3d33 100644 (file)
@@ -13,6 +13,8 @@ import static java.util.Objects.requireNonNull;
 import com.google.gson.stream.JsonWriter;
 import java.io.IOException;
 import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
@@ -23,8 +25,32 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.util.LeafrefResolver;
 
-abstract class JSONInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
+abstract sealed class JSONInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
         implements JSONCodec<YangInstanceIdentifier> {
+    static final class Lhotka02 extends JSONInstanceIdentifierCodec {
+        Lhotka02(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
+            super(context, jsonCodecFactory);
+        }
+    }
+
+    static final class RFC7951 extends JSONInstanceIdentifierCodec {
+        RFC7951(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
+            super(context, jsonCodecFactory);
+        }
+
+        @Override
+        protected StringBuilder appendQName(final StringBuilder sb, final QName qname, final QNameModule lastModule) {
+            return qname.getModule().equals(lastModule) ? sb.append(qname.getLocalName())
+                : super.appendQName(sb, qname, lastModule);
+        }
+
+        @Override
+        protected QName createQName(final QNameModule lastModule, final String localName) {
+            checkArgument(lastModule != null, "Unprefixed leading name %s", localName);
+            return QName.create(lastModule, localName);
+        }
+    }
+
     private final @NonNull DataSchemaContextTree dataContextTree;
     private final JSONCodecFactory codecFactory;
     private final EffectiveModelContext context;
@@ -57,8 +83,7 @@ abstract class JSONInstanceIdentifierCodec extends AbstractModuleStringInstanceI
             final String value) {
         requireNonNull(schemaNode, "schemaNode cannot be null");
         checkArgument(schemaNode instanceof LeafSchemaNode, "schemaNode must be of type LeafSchemaNode");
-        final JSONCodec<?> objectJSONCodec = codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver);
-        return objectJSONCodec.parseValue(null, value);
+        return codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver).parseValue(null, value);
     }
 
     @Override
diff --git a/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/Lhotka02JSONCodecFactory.java b/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/Lhotka02JSONCodecFactory.java
deleted file mode 100644 (file)
index 87701c5..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
-import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
-import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
-
-final class Lhotka02JSONCodecFactory extends JSONCodecFactory {
-    private final JSONInstanceIdentifierCodec iidCodec;
-
-    Lhotka02JSONCodecFactory(final EffectiveModelContext context, final CodecCache<JSONCodec<?>> cache) {
-        super(context, cache);
-        iidCodec = new Lhotka02JSONInstanceIdentifierCodec(context, this);
-    }
-
-    @Override
-    protected JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
-        return iidCodec;
-    }
-
-    @Override
-    Lhotka02JSONCodecFactory rebaseTo(final EffectiveModelContext newSchemaContext,
-            final CodecCache<JSONCodec<?>> newCache) {
-        return new Lhotka02JSONCodecFactory(newSchemaContext, newCache);
-    }
-
-    @Override
-    JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
-        return new NumberJSONCodec<>(decimalCodec);
-    }
-
-    @Override
-    JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
-        return new NumberJSONCodec<>(integerCodec);
-    }
-}
diff --git a/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/Lhotka02JSONInstanceIdentifierCodec.java b/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/Lhotka02JSONInstanceIdentifierCodec.java
deleted file mode 100644 (file)
index 4c83a2d..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-
-final class Lhotka02JSONInstanceIdentifierCodec extends JSONInstanceIdentifierCodec {
-    Lhotka02JSONInstanceIdentifierCodec(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
-        super(context, jsonCodecFactory);
-    }
-}
diff --git a/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/RFC7951JSONCodecFactory.java b/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/RFC7951JSONCodecFactory.java
deleted file mode 100644 (file)
index f60fff6..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
-import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
-import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
-
-final class RFC7951JSONCodecFactory extends JSONCodecFactory {
-    private final RFC7951JSONInstanceIdentifierCodec iidCodec;
-
-    RFC7951JSONCodecFactory(final EffectiveModelContext context, final CodecCache<JSONCodec<?>> cache) {
-        super(context, cache);
-        iidCodec = new RFC7951JSONInstanceIdentifierCodec(context, this);
-    }
-
-    @Override
-    protected JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
-        return iidCodec;
-    }
-
-    @Override
-    JSONCodecFactory rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
-        return new RFC7951JSONCodecFactory(newSchemaContext, newCache);
-    }
-
-    @Override
-    JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
-        return new QuotedJSONCodec<>(decimalCodec);
-    }
-
-    @Override
-    JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
-        return new QuotedJSONCodec<>(integerCodec);
-    }
-}
diff --git a/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/RFC7951JSONInstanceIdentifierCodec.java b/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/RFC7951JSONInstanceIdentifierCodec.java
deleted file mode 100644 (file)
index 6ac2c6f..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import static com.google.common.base.Preconditions.checkArgument;
-
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-
-final class RFC7951JSONInstanceIdentifierCodec extends JSONInstanceIdentifierCodec {
-    RFC7951JSONInstanceIdentifierCodec(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
-        super(context, jsonCodecFactory);
-    }
-
-    @Override
-    protected StringBuilder appendQName(final StringBuilder sb, final QName qname, final QNameModule lastModule) {
-        if (qname.getModule().equals(lastModule)) {
-            return sb.append(qname.getLocalName());
-        }
-
-        return super.appendQName(sb, qname, lastModule);
-    }
-
-    @Override
-    protected QName createQName(final QNameModule lastModule, final String localName) {
-        checkArgument(lastModule != null, "Unprefixed leading name %s", localName);
-        return QName.create(lastModule, localName);
-    }
-}