2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static com.google.common.base.Preconditions.checkArgument;
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;
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.
24 * @param <T> canonical value type
25 * @author Robert Varga
30 public abstract class AbstractCanonicalValueSupport<T extends CanonicalValue<T>> implements CanonicalValueSupport<T> {
31 private static final ClassValue<Boolean> SUPPORTS = new ClassValue<Boolean>() {
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",
39 final int modifiers = type.getModifiers();
40 checkArgument(Modifier.isFinal(modifiers), "%s must be final", type);
45 private static final ClassValue<Boolean> VALUES = new AbstractCanonicalValueImplementationValidator() {
47 void checkCompareTo(@NonNull final Class<?> type) {
48 checkFinalMethod(type, "compareTo", type);
52 private final Class<T> representationClass;
54 protected AbstractCanonicalValueSupport(final Class<T> representationClass) {
55 VALUES.get(representationClass);
56 this.representationClass = representationClass;
57 SUPPORTS.get(getClass());
61 public final Class<T> getRepresentationClass() {
62 return representationClass;
66 public final Class<T> getValidatedRepresentationClass() {
67 return representationClass;
71 public final Variant<T, CanonicalValueViolation> validateRepresentation(final T value) {
72 return Variant.ofFirst(value);
76 public final Variant<T, CanonicalValueViolation> validateRepresentation(final T value,
77 final String canonicalString) {
78 return Variant.ofFirst(value);