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 * Support for a {@link CanonicalValue} subclasses. An implementation of this interface must be registered
18 * in the system and be available from each CanonicalValue object.
21 * Note: never implement this interface directly, subclass {@link AbstractCanonicalValueSupport} instead.
24 * This interface allows a {@link CanonicalValue} to be instantiated from a String. The implementation is expected
25 * to perform all checks implied by the corresponding YANG data model.
27 * @param <T> canonical value type
28 * @author Robert Varga
32 public interface CanonicalValueSupport<T extends CanonicalValue<T>> extends CanonicalValueValidator<T, T> {
34 * Create a instance for a string representation. Implementations of this method are required to perform checks
35 * equivalent to the YANG data model restrictions attached to the corresponding YANG type. Non-canonical format
36 * strings must be accepted and result in objects equal to objects obtained from the corresponding canonical format.
38 * @param str String representation
39 * @return A {@link CanonicalValue} instance or CanonicalValueViolation if {@code str} does not conform
40 * @throws NullPointerException if {@code str} is null
42 Either<T, CanonicalValueViolation> fromString(String str);
45 * Create a instance for the canonical string representation. Implementations of this method may perform
46 * optimizations based on the assumption the string is canonical, but should still report errors when a mismatch
49 * @param str String representation
50 * @return A {@link CanonicalValue} instance or CanonicalValueViolation if {@code str} does not conform
51 * @throws NullPointerException if {@code str} is null
53 default Either<T, CanonicalValueViolation> fromCanonicalString(final String str) {
54 return fromString(requireNonNull(str));
58 * Unsafe cast to a factory type.
60 * @return This instance cast to specified type
62 @SuppressWarnings("unchecked")
63 default <X extends CanonicalValue<X>> CanonicalValueSupport<X> unsafe() {
64 return (CanonicalValueSupport<X>) this;