+++ /dev/null
-/*
- * 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;
-}
*/
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);
+ }
}
+++ /dev/null
-/*
- * 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);
-}
+++ /dev/null
-/*
- * 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;
-}
+++ /dev/null
-/*
- * 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;
-}
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);
}
+++ /dev/null
-/*
- * 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;
-}
+++ /dev/null
-/*
- * 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> {
-
-}
+++ /dev/null
-/*
- * 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);
-}
+++ /dev/null
-/*
- * 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);
-}
}
@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
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;
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));
}