All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opendaylight.yangtools.yang.common.AbstractCanonicalValueSupport Maven / Gradle / Ivy

There is a newer version: 14.0.4
Show newest version
/*
 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.common;

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.annotations.Beta;
import java.lang.reflect.Modifier;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.concepts.Either;

/**
 * Base implementation of {@link CanonicalValueSupport}. This class should be used as superclass to all implementations
 * of {@link CanonicalValueSupport}, as doing so provides a simpler base and enforces some aspects of the subclass.
 *
 * @param  canonical value type
 * @author Robert Varga
 */
@Beta
@NonNullByDefault
public abstract class AbstractCanonicalValueSupport> implements CanonicalValueSupport {
    private static final ClassValue SUPPORTS = new ClassValue<>() {
        @Override
        protected Boolean computeValue(final @Nullable Class type) {
            // Every DerivedStringSupport representation class must:
            checkArgument(CanonicalValueSupport.class.isAssignableFrom(type), "%s is not a CanonicalValueSupport",
                type);

            // be final
            final int modifiers = type.getModifiers();
            checkArgument(Modifier.isFinal(modifiers), "%s must be final", type);

            return Boolean.TRUE;
        }
    };
    private static final ClassValue VALUES = new AbstractCanonicalValueImplementationValidator() {
        @Override
        void checkCompareTo(@NonNull final Class type) {
            checkFinalMethod(type, "compareTo", type);
        }
    };

    private final Class representationClass;

    protected AbstractCanonicalValueSupport(final Class representationClass) {
        VALUES.get(representationClass);
        this.representationClass = representationClass;
        SUPPORTS.get(getClass());
    }

    @Override
    public final Class getRepresentationClass() {
        return representationClass;
    }

    @Override
    public final Class getValidatedRepresentationClass() {
        return representationClass;
    }

    @Override
    public final Either validateRepresentation(final T value) {
        return Either.ofFirst(value);
    }

    @Override
    public final Either validateRepresentation(final T value,
            final String canonicalString) {
        return Either.ofFirst(value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy