Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / AbstractCanonicalValueValidator.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 java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.opendaylight.yangtools.concepts.Variant;
15
16 /**
17  * Abstract base class for implementing validators.
18  *
19  * @param <T> string representation class
20  * @param <V> validated string representation class
21  * @author Robert Varga
22  */
23 @Beta
24 @NonNullByDefault
25 public abstract class AbstractCanonicalValueValidator<T extends DerivedString<T>, V extends T>
26         implements CanonicalValueValidator<T, V> {
27     private static final ClassValue<Boolean> IMPLEMENTATIONS = new AbstractCanonicalValueImplementationValidator() {
28         @Override
29         void checkCompareTo(final Class<?> type) {
30             // Intentional no-op, as we'd need a type capture of the representation
31         }
32     };
33
34     private final CanonicalValueSupport<T> representationSupport;
35     private final Class<V> validatedClass;
36
37     protected AbstractCanonicalValueValidator(final CanonicalValueSupport<T> representationSupport,
38             final Class<V> validatedClass) {
39         this.representationSupport = requireNonNull(representationSupport);
40         IMPLEMENTATIONS.get(validatedClass);
41         this.validatedClass = validatedClass;
42     }
43
44     @Override
45     public final Class<T> getRepresentationClass() {
46         return representationSupport.getRepresentationClass();
47     }
48
49     @Override
50     public final Class<V> getValidatedRepresentationClass() {
51         return validatedClass;
52     }
53
54     @Override
55     public final Variant<T, CanonicalValueViolation> validateRepresentation(final T value) {
56         return validatedClass.isAssignableFrom(value.validator().getValidatedRepresentationClass())
57                 ? Variant.ofFirst(validatedClass.cast(value)) : validate(value);
58     }
59
60     @Override
61     public final Variant<T, CanonicalValueViolation> validateRepresentation(final T value,
62             final String canonicalString) {
63         return validatedClass.isAssignableFrom(value.validator().getValidatedRepresentationClass())
64                 ? Variant.ofFirst(validatedClass.cast(value)) : validate(value, requireNonNull(canonicalString));
65     }
66
67     /**
68      * Validate a {@link DerivedString} representation. Subclasses should override this method if they can
69      * provide a validation algorithm which does not rely on canonical strings but works on representation state only.
70      *
71      * @param value Representation value
72      * @return Validated representation or CanonicalValueViolation
73      * @throws NullPointerException if {@code value} is null
74      */
75     protected Variant<T, CanonicalValueViolation> validate(final T value) {
76         return validate(value, value.toCanonicalString());
77     }
78
79     /**
80      * Validate a {@link DerivedString} representation. Subclasses can chose whether they operate on representation
81      * state or canonical string -- both are considered equivalent.
82      *
83      * @param value Representation value
84      * @param canonicalString Canonical string matching the representation value
85      * @return Validated representation
86      * @throws NullPointerException if {@code value} or {@code canonicalString} is null.
87      */
88     protected abstract Variant<T, CanonicalValueViolation> validate(T value, String canonicalString);
89 }