Flatten IllegalArgumentCodec class hierarchy 54/97654/3
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 27 Sep 2021 14:39:02 +0000 (16:39 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 27 Sep 2021 15:05:56 +0000 (17:05 +0200)
IllegalArgumentCodec is the only specialization which is seeing a
wide use. Integrate all its superinterfaces and superclasses of
AbstractIllegalArgumentCodec into these two constructs.

JIRA: YANGTOOLS-1333
Change-Id: I75078910693e912d5313b7eec43c14ca22eefe2f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
12 files changed:
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractCodec.java [deleted file]
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractIllegalArgumentCodec.java
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractUncheckedCodec.java [deleted file]
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Codec.java [deleted file]
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Deserializer.java [deleted file]
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/IllegalArgumentCodec.java
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Serializer.java [deleted file]
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedCodec.java [deleted file]
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedDeserializer.java [deleted file]
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedSerializer.java [deleted file]
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/BitsStringCodec.java
data/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/TypeDefinitionAwareCodecTestHelper.java

diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractCodec.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractCodec.java
deleted file mode 100644 (file)
index 47d4f3d..0000000
+++ /dev/null
@@ -1,42 +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.concepts;
-
-import static com.google.common.base.Verify.verifyNotNull;
-import static java.util.Objects.requireNonNull;
-
-import com.google.common.annotations.Beta;
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.NonNullByDefault;
-
-/**
- * An abstract base class enforcing nullness contract around {@link Codec} interface.
- *
- * @param <P> Product type
- * @param <I> Input type
- * @param <X> Error exception type
- */
-@Beta
-@NonNullByDefault
-public abstract class AbstractCodec<P, I, X extends Exception> implements Codec<P, I, X> {
-    @Override
-    public final @NonNull I deserialize(@NonNull final P input) throws X {
-        return verifyNotNull(deserializeImpl(requireNonNull(input)), "Codec %s returned null on %s", this, input);
-    }
-
-    @Override
-    public final @NonNull P serialize(@NonNull final I input) throws X {
-        return verifyNotNull(serializeImpl(requireNonNull(input)), "Codec %s returned null on %s", this, input);
-    }
-
-    // implementation is guarded from nulls and verified not to return null
-    protected abstract @NonNull I deserializeImpl(@NonNull P product) throws X;
-
-    // implementation is guarded from nulls and verified not to return null
-    protected abstract @NonNull P serializeImpl(@NonNull I input) throws X;
-}
index eed595ae5734046646707390f746f0c952d9fd96..6be4f95a372f3bb076eee6541f84f5beb07a0939 100644 (file)
@@ -7,18 +7,38 @@
  */
 package org.opendaylight.yangtools.concepts;
 
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.Beta;
-import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 
 /**
  * An abstract base class enforcing nullness contract around {@link IllegalArgumentCodec} interface.
  *
- * @param <P> Product type
- * @param <I> Input type
+ * @param <S> Serializied (external) type
+ * @param <D> Deserialized (internal) type
  */
 @Beta
-@NonNullByDefault
-public abstract class AbstractIllegalArgumentCodec<P, I> extends AbstractUncheckedCodec<P, I, IllegalArgumentException>
-        implements IllegalArgumentCodec<P, I> {
+public abstract class AbstractIllegalArgumentCodec<S, D> implements IllegalArgumentCodec<S, D> {
+    @Override
+    public final D deserialize(final S input) {
+        return verifyResult(deserializeImpl(requireNonNull(input)), input);
+    }
+
+    @Override
+    public final S serialize(final D input) {
+        return verifyResult(serializeImpl(requireNonNull(input)), input);
+    }
+
+    // implementation is guarded from nulls and verified not to return null
+    protected abstract @NonNull D deserializeImpl(@NonNull S product);
+
+    // implementation is guarded from nulls and verified not to return null
+    protected abstract @NonNull S serializeImpl(@NonNull D input);
 
+    private <X> X verifyResult(final @Nullable X result, final Object input) {
+        return verifyNotNull(result, "Codec %s returned null on %s", this, input);
+    }
 }
diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractUncheckedCodec.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractUncheckedCodec.java
deleted file mode 100644 (file)
index 0edb216..0000000
+++ /dev/null
@@ -1,31 +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.concepts;
-
-import com.google.common.annotations.Beta;
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.NonNullByDefault;
-
-/**
- * An abstract base class enforcing nullness contract around {@link UncheckedCodec} interface.
- *
- * @param <P> Product type
- * @param <I> Input type
- * @param <X> Error exception type
- */
-@Beta
-@NonNullByDefault
-public abstract class AbstractUncheckedCodec<P, I, X extends RuntimeException> extends AbstractCodec<P, I, X>
-        implements UncheckedCodec<P, I, X> {
-    @Override
-    protected abstract @NonNull I deserializeImpl(@NonNull P product);
-
-    // implementation is guarded from nulls and verified not to return null
-    @Override
-    protected abstract @NonNull P serializeImpl(@NonNull I input);
-}
diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Codec.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Codec.java
deleted file mode 100644 (file)
index 91060a1..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. 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.concepts;
-
-/**
- * The concept of a combined {@link Serializer} and {@link Deserializer}, which produces an object from some input.
- * Implementations should consider subclassing {@link AbstractCodec}.
- *
- * @param <P> Product type
- * @param <I> Input type
- * @param <X> Error exception type
- */
-public interface Codec<P, I, X extends Exception> extends Serializer<P, I, X>, Deserializer<I, P, X> {
-    @Override
-    I deserialize(P input) throws X;
-
-    @Override
-    P serialize(I input) throws X;
-}
diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Deserializer.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Deserializer.java
deleted file mode 100644 (file)
index d327bea..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. 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.concepts;
-
-import org.eclipse.jdt.annotation.NonNull;
-
-/**
- * The concept of a deserializer, which produces an object from some input.
- *
- * @param <P> Product type
- * @param <I> Input type
- * @param <X> Error exception type
- */
-public interface Deserializer<P, I, X extends Exception> {
-    /**
-     * Produce an object base on input.
-     *
-     * @param input Input object
-     * @return Product derived from input
-     * @throws NullPointerException if input is null
-     * @throws X when input is not valid
-     */
-    @NonNull P deserialize(@NonNull I input) throws X;
-}
index e2b072ad8ea447d463acb70b6b97f35ef0a9cf57..87b69b936b7cc1d117224e7ba29a078e584f83b9 100644 (file)
@@ -8,15 +8,34 @@
 package org.opendaylight.yangtools.concepts;
 
 import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNull;
 
 /**
- * Utility interface, which specializes {@link UncheckedCodec} to {@link IllegalArgumentException}. This is useful
- * for migration purposes. Implementations should consider subclassing {@link AbstractIllegalArgumentCodec}.
+ * Utility interface for translation between a external form and an internal form. Implementations should consider
+ * subclassing {@link AbstractIllegalArgumentCodec}.
  *
- * @param <P> Product type
- * @param <I> Input type
+ * @param <S> Serialized (external) type
+ * @param <D> Deserialized (internal) type
  */
 @Beta
-public interface IllegalArgumentCodec<P, I> extends UncheckedCodec<P, I, IllegalArgumentException> {
+public interface IllegalArgumentCodec<S, D> {
+    /**
+     * Produce an internal object based on an external object.
+     *
+     * @param input Input object
+     * @return Product derived from input
+     * @throws NullPointerException if input is null
+     * @throws IllegalArgumentException when input is not valid
+     */
+    @NonNull D deserialize(@NonNull S input);
 
+    /**
+     * Produce an external object based on an internal object.
+     *
+     * @param input Input
+     * @return An external form object
+     * @throws NullPointerException if input is null
+     * @throws IllegalArgumentException when input is not valid
+     */
+    @NonNull S serialize(@NonNull D input);
 }
diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Serializer.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Serializer.java
deleted file mode 100644 (file)
index f7a85c0..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. 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.concepts;
-
-import org.eclipse.jdt.annotation.NonNull;
-
-/**
- * An entity which is able to convert some input into a product.
- *
- * @param <P> Product type
- * @param <I> Input type
- * @param <X> Error exception type
- */
-public interface Serializer<P, I, X extends Exception> {
-    /**
-     * Convert an input into a product.
-     *
-     * @param input Input
-     * @return A product
-     * @throws NullPointerException if input is null
-     * @throws X when input is not valid
-     */
-    @NonNull P serialize(@NonNull I input) throws X;
-}
diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedCodec.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedCodec.java
deleted file mode 100644 (file)
index c1196a7..0000000
+++ /dev/null
@@ -1,24 +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.concepts;
-
-import com.google.common.annotations.Beta;
-
-/**
- * The concept of a combined {@link UncheckedSerializer} and {@link UncheckedDeserializer}, which produces an object
- * from some input. Implementations should consider subclassing {@link AbstractUncheckedCodec}.
- *
- * @param <P> Product type
- * @param <I> Input type
- * @param <X> Error exception type
- */
-@Beta
-public interface UncheckedCodec<P, I, X extends RuntimeException>
-    extends UncheckedSerializer<I, P, X>, UncheckedDeserializer<P, I, X>, Codec<P, I, X> {
-
-}
diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedDeserializer.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedDeserializer.java
deleted file mode 100644 (file)
index e6ffd6c..0000000
+++ /dev/null
@@ -1,23 +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.concepts;
-
-import com.google.common.annotations.Beta;
-
-/**
- * A specialization of {@link Serializer}, which is guaranteed to throws unchecked exceptions.
- *
- * @param <P> Product type
- * @param <I> Input type
- * @param <X> Error exception type
- */
-@Beta
-public interface UncheckedDeserializer<P, I, X extends RuntimeException> extends Serializer<P, I, X> {
-    @Override
-    P serialize(I input);
-}
diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedSerializer.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedSerializer.java
deleted file mode 100644 (file)
index a8683e5..0000000
+++ /dev/null
@@ -1,23 +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.concepts;
-
-import com.google.common.annotations.Beta;
-
-/**
- * A specialization of {@link Deserializer}, which is guaranteed to throws unchecked exceptions.
- *
- * @param <P> Product type
- * @param <I> Input type
- * @param <X> Error exception type
- */
-@Beta
-public interface UncheckedSerializer<P, I, X extends RuntimeException> extends Deserializer<P, I, X> {
-    @Override
-    P deserialize(I input);
-}
index 37d3944e1e2b808b064160778691b6ae942e20d5..509110cb84e5eb595d3f70e33f19cde126a69006 100644 (file)
@@ -46,7 +46,7 @@ public final class BitsStringCodec extends TypeDefinitionAwareCodec<Set<String>,
     }
 
     @Override
-    protected @NonNull Set<String> deserializeImpl(@NonNull final String product) {
+    protected Set<String> deserializeImpl(final String product) {
         final Set<String> strings = ImmutableSet.copyOf(SPLITTER.split(product));
 
         // Normalize strings to schema first, retaining definition order
index 4b0b330f43a5ffc8fdcac7429b49f529023e94bd..ed6199507bc24353159f8a67d26547d3494830db 100644 (file)
@@ -14,7 +14,7 @@ import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
 import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.concepts.Codec;
+import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
@@ -33,7 +33,7 @@ public final class TypeDefinitionAwareCodecTestHelper {
         return clazz.cast(codec);
     }
 
-    public static void deserializeWithExpectedIllegalArgEx(final Codec<String, ?, IllegalArgumentException> codec,
+    public static void deserializeWithExpectedIllegalArgEx(final IllegalArgumentCodec<String, ?> codec,
             final @NonNull String param) {
         assertThrows(IllegalArgumentException.class, () -> codec.deserialize(param));
     }