Fix findbugs in yang-common and enable enforcement
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / AbstractCanonicalValueSupport.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.yang.common;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import java.lang.reflect.Modifier;
14 import javax.annotation.concurrent.ThreadSafe;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.concepts.Variant;
19
20 /**
21  * Base implementation of {@link CanonicalValueSupport}. This class should be used as superclass to all implementations
22  * of {@link CanonicalValueSupport}, as doing so provides a simpler base and enforces some aspects of the subclass.
23  *
24  * @param <T> canonical value type
25  * @author Robert Varga
26  */
27 @Beta
28 @NonNullByDefault
29 @ThreadSafe
30 public abstract class AbstractCanonicalValueSupport<T extends CanonicalValue<T>> implements CanonicalValueSupport<T> {
31     private static final ClassValue<Boolean> SUPPORTS = new ClassValue<Boolean>() {
32         @Override
33         protected Boolean computeValue(final @Nullable Class<?> type) {
34             // Every DerivedStringSupport representation class must:
35             checkArgument(CanonicalValueSupport.class.isAssignableFrom(type), "%s is not a CanonicalValueSupport",
36                 type);
37
38             // be final
39             final int modifiers = type.getModifiers();
40             checkArgument(Modifier.isFinal(modifiers), "%s must be final", type);
41
42             return Boolean.TRUE;
43         }
44     };
45     private static final ClassValue<Boolean> VALUES = new AbstractCanonicalValueImplementationValidator() {
46         @Override
47         void checkCompareTo(@NonNull final Class<?> type) {
48             checkFinalMethod(type, "compareTo", type);
49         }
50     };
51
52     private final Class<T> representationClass;
53
54     protected AbstractCanonicalValueSupport(final Class<T> representationClass) {
55         VALUES.get(representationClass);
56         this.representationClass = representationClass;
57         SUPPORTS.get(getClass());
58     }
59
60     @Override
61     public final Class<T> getRepresentationClass() {
62         return representationClass;
63     }
64
65     @Override
66     public final Class<T> getValidatedRepresentationClass() {
67         return representationClass;
68     }
69
70     @Override
71     public final Variant<T, CanonicalValueViolation> validateRepresentation(final T value) {
72         return Variant.ofFirst(value);
73     }
74
75     @Override
76     public final Variant<T, CanonicalValueViolation> validateRepresentation(final T value,
77             final String canonicalString) {
78         return Variant.ofFirst(value);
79     }
80 }