Flatten IllegalArgumentCodec class hierarchy
[yangtools.git] / common / concepts / src / main / java / org / opendaylight / yangtools / concepts / AbstractIllegalArgumentCodec.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.concepts;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16
17 /**
18  * An abstract base class enforcing nullness contract around {@link IllegalArgumentCodec} interface.
19  *
20  * @param <S> Serializied (external) type
21  * @param <D> Deserialized (internal) type
22  */
23 @Beta
24 public abstract class AbstractIllegalArgumentCodec<S, D> implements IllegalArgumentCodec<S, D> {
25     @Override
26     public final D deserialize(final S input) {
27         return verifyResult(deserializeImpl(requireNonNull(input)), input);
28     }
29
30     @Override
31     public final S serialize(final D input) {
32         return verifyResult(serializeImpl(requireNonNull(input)), input);
33     }
34
35     // implementation is guarded from nulls and verified not to return null
36     protected abstract @NonNull D deserializeImpl(@NonNull S product);
37
38     // implementation is guarded from nulls and verified not to return null
39     protected abstract @NonNull S serializeImpl(@NonNull D input);
40
41     private <X> X verifyResult(final @Nullable X result, final Object input) {
42         return verifyNotNull(result, "Codec %s returned null on %s", this, input);
43     }
44 }