From 7c91d19ebe393468b33328f4e1a26bfa55a3ea0e Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 27 Sep 2021 16:39:02 +0200 Subject: [PATCH] Flatten IllegalArgumentCodec class hierarchy 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 --- .../yangtools/concepts/AbstractCodec.java | 42 ------------------- .../AbstractIllegalArgumentCodec.java | 32 +++++++++++--- .../concepts/AbstractUncheckedCodec.java | 31 -------------- .../yangtools/concepts/Codec.java | 24 ----------- .../yangtools/concepts/Deserializer.java | 29 ------------- .../concepts/IllegalArgumentCodec.java | 29 ++++++++++--- .../yangtools/concepts/Serializer.java | 29 ------------- .../yangtools/concepts/UncheckedCodec.java | 24 ----------- .../concepts/UncheckedDeserializer.java | 23 ---------- .../concepts/UncheckedSerializer.java | 23 ---------- .../yang/data/impl/codec/BitsStringCodec.java | 2 +- .../TypeDefinitionAwareCodecTestHelper.java | 4 +- 12 files changed, 53 insertions(+), 239 deletions(-) delete mode 100644 common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractCodec.java delete mode 100644 common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractUncheckedCodec.java delete mode 100644 common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Codec.java delete mode 100644 common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Deserializer.java delete mode 100644 common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Serializer.java delete mode 100644 common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedCodec.java delete mode 100644 common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedDeserializer.java delete mode 100644 common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedSerializer.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 index 47d4f3de7f..0000000000 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractCodec.java +++ /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

Product type - * @param Input type - * @param Error exception type - */ -@Beta -@NonNullByDefault -public abstract class AbstractCodec implements Codec { - @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; -} diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractIllegalArgumentCodec.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractIllegalArgumentCodec.java index eed595ae57..6be4f95a37 100644 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractIllegalArgumentCodec.java +++ b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractIllegalArgumentCodec.java @@ -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

Product type - * @param Input type + * @param Serializied (external) type + * @param Deserialized (internal) type */ @Beta -@NonNullByDefault -public abstract class AbstractIllegalArgumentCodec extends AbstractUncheckedCodec - implements IllegalArgumentCodec { +public abstract class AbstractIllegalArgumentCodec implements IllegalArgumentCodec { + @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 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 index 0edb216da9..0000000000 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractUncheckedCodec.java +++ /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

Product type - * @param Input type - * @param Error exception type - */ -@Beta -@NonNullByDefault -public abstract class AbstractUncheckedCodec extends AbstractCodec - implements UncheckedCodec { - @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 index 91060a1b66..0000000000 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Codec.java +++ /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

Product type - * @param Input type - * @param Error exception type - */ -public interface Codec extends Serializer, Deserializer { - @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 index d327beafdd..0000000000 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Deserializer.java +++ /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

Product type - * @param Input type - * @param Error exception type - */ -public interface Deserializer { - /** - * 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; -} diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/IllegalArgumentCodec.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/IllegalArgumentCodec.java index e2b072ad8e..87b69b936b 100644 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/IllegalArgumentCodec.java +++ b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/IllegalArgumentCodec.java @@ -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

Product type - * @param Input type + * @param Serialized (external) type + * @param Deserialized (internal) type */ @Beta -public interface IllegalArgumentCodec extends UncheckedCodec { +public interface IllegalArgumentCodec { + /** + * 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 index f7a85c00a9..0000000000 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Serializer.java +++ /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

Product type - * @param Input type - * @param Error exception type - */ -public interface Serializer { - /** - * 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 index c1196a7d56..0000000000 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedCodec.java +++ /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

Product type - * @param Input type - * @param Error exception type - */ -@Beta -public interface UncheckedCodec - extends UncheckedSerializer, UncheckedDeserializer, Codec { - -} 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 index e6ffd6c9c6..0000000000 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedDeserializer.java +++ /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

Product type - * @param Input type - * @param Error exception type - */ -@Beta -public interface UncheckedDeserializer extends Serializer { - @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 index a8683e5aac..0000000000 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/UncheckedSerializer.java +++ /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

Product type - * @param Input type - * @param Error exception type - */ -@Beta -public interface UncheckedSerializer extends Deserializer { - @Override - P deserialize(I input); -} diff --git a/data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/BitsStringCodec.java b/data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/BitsStringCodec.java index 37d3944e1e..509110cb84 100644 --- a/data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/BitsStringCodec.java +++ b/data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/BitsStringCodec.java @@ -46,7 +46,7 @@ public final class BitsStringCodec extends TypeDefinitionAwareCodec, } @Override - protected @NonNull Set deserializeImpl(@NonNull final String product) { + protected Set deserializeImpl(final String product) { final Set strings = ImmutableSet.copyOf(SPLITTER.split(product)); // Normalize strings to schema first, retaining definition order diff --git a/data/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/TypeDefinitionAwareCodecTestHelper.java b/data/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/TypeDefinitionAwareCodecTestHelper.java index 4b0b330f43..ed6199507b 100644 --- a/data/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/TypeDefinitionAwareCodecTestHelper.java +++ b/data/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/TypeDefinitionAwareCodecTestHelper.java @@ -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 codec, + public static void deserializeWithExpectedIllegalArgEx(final IllegalArgumentCodec codec, final @NonNull String param) { assertThrows(IllegalArgumentException.class, () -> codec.deserialize(param)); } -- 2.36.6