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 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;
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.
23 * @param <T> canonical value type
24 * @author Robert Varga
28 public abstract class AbstractCanonicalValueSupport<T extends CanonicalValue<T>> implements CanonicalValueSupport<T> {
29 private static final ClassValue<Boolean> SUPPORTS = new ClassValue<Boolean>() {
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",
37 final int modifiers = type.getModifiers();
38 checkArgument(Modifier.isFinal(modifiers), "%s must be final", type);
43 private static final ClassValue<Boolean> VALUES = new AbstractCanonicalValueImplementationValidator() {
45 void checkCompareTo(@NonNull final Class<?> type) {
46 checkFinalMethod(type, "compareTo", type);
50 private final Class<T> representationClass;
52 protected AbstractCanonicalValueSupport(final Class<T> representationClass) {
53 VALUES.get(representationClass);
54 this.representationClass = representationClass;
55 SUPPORTS.get(getClass());
59 public final Class<T> getRepresentationClass() {
60 return representationClass;
64 public final Class<T> getValidatedRepresentationClass() {
65 return representationClass;
69 public final Variant<T, CanonicalValueViolation> validateRepresentation(final T value) {
70 return Variant.ofFirst(value);
74 public final Variant<T, CanonicalValueViolation> validateRepresentation(final T value,
75 final String canonicalString) {
76 return Variant.ofFirst(value);