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