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 java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.opendaylight.yangtools.concepts.Either;
17 * Abstract base class for implementing validators.
19 * @param <T> string representation class
20 * @param <V> validated string representation class
21 * @author Robert Varga
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() {
29 void checkCompareTo(final Class<?> type) {
30 // Intentional no-op, as we'd need a type capture of the representation
34 private final CanonicalValueSupport<T> representationSupport;
35 private final Class<V> validatedClass;
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;
45 public final Class<T> getRepresentationClass() {
46 return representationSupport.getRepresentationClass();
50 public final Class<V> getValidatedRepresentationClass() {
51 return validatedClass;
55 public final Either<T, CanonicalValueViolation> validateRepresentation(final T value) {
56 return validatedClass.isAssignableFrom(value.validator().getValidatedRepresentationClass())
57 ? Either.ofFirst(validatedClass.cast(value)) : validate(value);
61 public final Either<T, CanonicalValueViolation> validateRepresentation(final T value,
62 final String canonicalString) {
63 return validatedClass.isAssignableFrom(value.validator().getValidatedRepresentationClass())
64 ? Either.ofFirst(validatedClass.cast(value)) : validate(value, requireNonNull(canonicalString));
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.
71 * @param value Representation value
72 * @return Validated representation or CanonicalValueViolation
73 * @throws NullPointerException if {@code value} is null
75 protected Either<T, CanonicalValueViolation> validate(final T value) {
76 return validate(value, value.toCanonicalString());
80 * Validate a {@link DerivedString} representation. Subclasses can chose whether they operate on representation
81 * state or canonical string -- both are considered equivalent.
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.
88 protected abstract Either<T, CanonicalValueViolation> validate(T value, String canonicalString);