X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-generator-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fmodel%2Futil%2FTypes.java;h=eefeaa26b035b2fa9456d68c0ab7ee7168db7924;hb=2f8f56af98be1f58d14256e780e2a9a77c0cfc49;hp=0574008ffa41c2f7711a3c88a604744c0c3e5c60;hpb=0f351bbc28ddf2cddfe30c8d018646d81953fa17;p=mdsal.git diff --git a/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/mdsal/binding/model/util/Types.java b/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/mdsal/binding/model/util/Types.java index 0574008ffa..eefeaa26b0 100644 --- a/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/mdsal/binding/model/util/Types.java +++ b/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/mdsal/binding/model/util/Types.java @@ -9,7 +9,6 @@ package org.opendaylight.mdsal.binding.model.util; import static java.util.Objects.requireNonNull; -import com.google.common.base.Preconditions; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -25,6 +24,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions; import org.opendaylight.mdsal.binding.model.api.ConcreteType; @@ -34,14 +34,12 @@ import org.opendaylight.mdsal.binding.model.api.Restrictions; import org.opendaylight.mdsal.binding.model.api.Type; import org.opendaylight.mdsal.binding.model.api.WildcardType; import org.opendaylight.yangtools.concepts.Builder; -import org.opendaylight.yangtools.yang.binding.Augmentable; -import org.opendaylight.yangtools.yang.binding.Augmentation; import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; public final class Types { - private static final CacheLoader, ConcreteType> TYPE_LOADER = new CacheLoader, ConcreteType>() { + private static final CacheLoader, ConcreteType> TYPE_LOADER = new CacheLoader<>() { @Override public ConcreteType load(final Class key) { return new ConcreteTypeImpl(JavaTypeName.create(key), null); @@ -50,24 +48,23 @@ public final class Types { private static final LoadingCache, ConcreteType> TYPE_CACHE = CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER); - - public static final ConcreteType BOOLEAN = typeForClass(Boolean.class); - public static final ConcreteType STRING = typeForClass(String.class); - public static final ConcreteType VOID = typeForClass(Void.class); - public static final ConcreteType BYTE_ARRAY = typeForClass(byte[].class); - - private static final ConcreteType BUILDER = typeForClass(Builder.class); - private static final ConcreteType CLASS = typeForClass(Class.class); - private static final ConcreteType LIST_TYPE = typeForClass(List.class); - private static final ConcreteType LISTENABLE_FUTURE = typeForClass(ListenableFuture.class); - private static final ConcreteType MAP_TYPE = typeForClass(Map.class); - private static final ConcreteType OBJECT = typeForClass(Object.class); - private static final ConcreteType PRIMITIVE_VOID = typeForClass(void.class); - private static final ConcreteType SERIALIZABLE = typeForClass(Serializable.class); - private static final ConcreteType SET_TYPE = typeForClass(Set.class); + public static final @NonNull ConcreteType BOOLEAN = typeForClass(Boolean.class); + public static final @NonNull ConcreteType STRING = typeForClass(String.class); + public static final @NonNull ConcreteType VOID = typeForClass(Void.class); + public static final @NonNull ConcreteType BYTE_ARRAY = typeForClass(byte[].class); + + private static final @NonNull ConcreteType BUILDER = typeForClass(Builder.class); + private static final @NonNull ConcreteType CLASS = typeForClass(Class.class); + private static final @NonNull ConcreteType LIST_TYPE = typeForClass(List.class); + private static final @NonNull ConcreteType LISTENABLE_FUTURE = typeForClass(ListenableFuture.class); + private static final @NonNull ConcreteType MAP_TYPE = typeForClass(Map.class); + private static final @NonNull ConcreteType OBJECT = typeForClass(Object.class); + private static final @NonNull ConcreteType PRIMITIVE_VOID = typeForClass(void.class); + private static final @NonNull ConcreteType SERIALIZABLE = typeForClass(Serializable.class); + private static final @NonNull ConcreteType SET_TYPE = typeForClass(Set.class); /** - * It is not desirable to create instance of this class + * It is not desirable to create instance of this class. */ private Types() { } @@ -80,7 +77,7 @@ public final class Types { * @return A parameterized type corresponding to {@code Class} * @throws NullPointerException if {@code type} is null */ - public static ParameterizedType classType(final Type type) { + public static @NonNull ParameterizedType classType(final Type type) { return parameterizedTypeFor(CLASS, type); } @@ -89,7 +86,7 @@ public final class Types { * * @return ConcreteType instance which represents JAVA java.lang.Void */ - public static ConcreteType voidType() { + public static @NonNull ConcreteType voidType() { return VOID; } @@ -98,7 +95,7 @@ public final class Types { * * @return ConcreteType instance which represents {@link Object} */ - public static ConcreteType objectType() { + public static @NonNull ConcreteType objectType() { return OBJECT; } @@ -107,7 +104,7 @@ public final class Types { * * @return ConcreteType instance which represents JAVA void */ - public static ConcreteType primitiveVoidType() { + public static @NonNull ConcreteType primitiveVoidType() { return PRIMITIVE_VOID; } @@ -116,22 +113,22 @@ public final class Types { * * @return ConcreteType instance which represents JAVA {@link Serializable} */ - public static ConcreteType serializableType() { + public static @NonNull ConcreteType serializableType() { return SERIALIZABLE; } /** - * Returns an instance of {@link ConcreteType} describing the class + * Returns an instance of {@link ConcreteType} describing the class. * - * @param cls - * Class to describe + * @param cls Class to describe * @return Description of class */ - public static ConcreteType typeForClass(final Class cls) { + public static @NonNull ConcreteType typeForClass(final @NonNull Class cls) { return TYPE_CACHE.getUnchecked(cls); } - public static ConcreteType typeForClass(final Class cls, final Restrictions restrictions) { + public static @NonNull ConcreteType typeForClass(final @NonNull Class cls, + final @Nullable Restrictions restrictions) { if (restrictions == null) { return typeForClass(cls); } @@ -144,56 +141,62 @@ public final class Types { } /** - * Returns an instance of {@link ParameterizedType} describing the typed - * {@link Map}<K,V> + * Returns an instance of {@link ParameterizedType} describing the typed {@link Map}<K,V>. * - * @param keyType - * Key Type - * @param valueType - * Value Type + * @param keyType Key Type + * @param valueType Value Type * @return Description of generic type instance */ - public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) { + public static @NonNull ParameterizedType mapTypeFor(final Type keyType, final Type valueType) { return parameterizedTypeFor(MAP_TYPE, keyType, valueType); } + public static boolean isMapType(final ParameterizedType type) { + return MAP_TYPE.equals(type.getRawType()); + } + + public static boolean isMapType(final Type type) { + return type instanceof ParameterizedType && isMapType((ParameterizedType) type); + } + /** - * Returns an instance of {@link ParameterizedType} describing the typed - * {@link Set}<V> with concrete type of value. + * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}<V> with concrete type + * of value. * - * @param valueType - * Value Type + * @param valueType Value Type * @return Description of generic type instance of Set */ - public static ParameterizedType setTypeFor(final Type valueType) { + public static @NonNull ParameterizedType setTypeFor(final Type valueType) { return parameterizedTypeFor(SET_TYPE, valueType); } /** - * Returns an instance of {@link ParameterizedType} describing the typed - * {@link List}<V> with concrete type of value. + * Returns an instance of {@link ParameterizedType} describing the typed {@link List}<V> with concrete type + * of value. * - * @param valueType - * Value Type + * @param valueType Value Type * @return Description of type instance of List */ - public static ParameterizedType listTypeFor(final Type valueType) { + public static @NonNull ParameterizedType listTypeFor(final Type valueType) { return parameterizedTypeFor(LIST_TYPE, valueType); } + public static boolean isListType(final ParameterizedType type) { + return LIST_TYPE.equals(type.getRawType()); + } + public static boolean isListType(final Type type) { - return type instanceof ParameterizedType && LIST_TYPE.equals(((ParameterizedType) type).getRawType()); + return type instanceof ParameterizedType && isListType((ParameterizedType) type); } /** - * Returns an instance of {@link ParameterizedType} describing the typed - * {@link ListenableFuture}<V> with concrete type of value. + * Returns an instance of {@link ParameterizedType} describing the typed {@link ListenableFuture}<V> + * with concrete type of value. * - * @param valueType - * Value Type + * @param valueType Value Type * @return Description of type instance of ListenableFuture */ - public static ParameterizedType listenableFutureTypeFor(final Type valueType) { + public static @NonNull ParameterizedType listenableFutureTypeFor(final Type valueType) { return parameterizedTypeFor(LISTENABLE_FUTURE, valueType); } @@ -204,24 +207,19 @@ public final class Types { * @param valueType Value Type * @return Description of type instance of Builder */ - public static ParameterizedType builderTypeFor(final Type valueType) { + public static @NonNull ParameterizedType builderTypeFor(final Type valueType) { return parameterizedTypeFor(BUILDER, valueType); } /** - * Creates instance of type - * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType - * ParameterizedType} + * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType}. * - * @param type - * JAVA Type for raw type - * @param parameters - * JAVA Types for actual parameter types - * @return ParametrizedType representation of type - * and its parameters parameters + * @param type JAVA Type for raw type + * @param parameters JAVA Types for actual parameter types + * @return ParametrizedType representation of type and its parameters * @throws NullPointerException if any argument or any member of {@code parameters} is null */ - public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) { + public static @NonNull ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) { return new ParametrizedTypeImpl(type, parameters); } @@ -242,33 +240,31 @@ public final class Types { * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual * parameter is valueType. * - * @param valueType - * JAVA Type with actual parameter + * @param valueType JAVA Type with actual parameter * @return ParametrizedType representation of raw type * Augmentable with actual parameter * valueType + * @deprecated Use {@link BindingTypes#augmentable(Type)} instead. */ - public static ParameterizedType augmentableTypeFor(final Type valueType) { - final Type augmentable = typeForClass(Augmentable.class); - return parameterizedTypeFor(augmentable, valueType); + @Deprecated(forRemoval = true) + public static @NonNull ParameterizedType augmentableTypeFor(final Type valueType) { + return BindingTypes.augmentable(valueType); } /** - * Creates instance of - * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType - * ParameterizedType} where raw type is - * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual - * parameter is valueType. + * Creates instance of {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType} where + * raw type is {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual parameter + * is valueType. * - * @param valueType - * JAVA Type with actual parameter - * @return ParametrizedType reprezentation of raw type + * @param valueType JAVA Type with actual parameter + * @return ParametrizedType representation of raw type * Augmentation with actual parameter * valueType + * @deprecated Use {@link BindingTypes#augmentation(Type)} instead. */ - public static ParameterizedType augmentationTypeFor(final Type valueType) { - final Type augmentation = typeForClass(Augmentation.class); - return parameterizedTypeFor(augmentation, valueType); + @Deprecated(forRemoval = true) + public static @NonNull ParameterizedType augmentationTypeFor(final Type valueType) { + return BindingTypes.augmentation(valueType); } public static @Nullable String getOuterClassName(final Type valueType) { @@ -276,21 +272,16 @@ public final class Types { } /** - * * Represents concrete JAVA type. - * */ private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType { private final Restrictions restrictions; /** - * Creates instance of this class with package pkName and - * with the type name name. + * Creates instance of this class with package pkName and with the type name name. * - * @param pkName - * string with package name - * @param name - * string with the name of the type + * @param pkName string with package name + * @param name string with the name of the type */ ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) { super(identifier); @@ -306,21 +297,19 @@ public final class Types { /** * Represents concrete JAVA type with changed restriction values. */ - private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions { + private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements + BaseTypeWithRestrictions { private final Restrictions restrictions; /** - * Creates instance of this class with package pkName and - * with the type name name. + * Creates instance of this class with package pkName and with the type name name. * - * @param pkName - * string with package name - * @param name - * string with the name of the type + * @param pkName string with package name + * @param name string with the name of the type */ BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) { super(identifier); - this.restrictions = Preconditions.checkNotNull(restrictions); + this.restrictions = requireNonNull(restrictions); } @Override @@ -339,31 +328,17 @@ public final class Types { private final Type[] actualTypes; /** - * JAVA raw type (like List, Set, Map...) + * JAVA raw type (like List, Set, Map...). */ private final Type rawType; - @Override - public Type[] getActualTypeArguments() { - - return this.actualTypes; - } - - @Override - public Type getRawType() { - return this.rawType; - } - /** - * Creates instance of this class with concrete rawType and array of - * actual parameters. + * Creates instance of this class with concrete rawType and array of actual parameters. * - * @param rawType - * JAVA Type for raw type - * @param actTypes - * array of actual parameters + * @param rawType JAVA Type for raw type + * @param actTypes array of actual parameters */ - public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) { + ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) { super(rawType.getIdentifier()); this.rawType = requireNonNull(rawType); actualTypes = actTypes.clone(); @@ -371,6 +346,17 @@ public final class Types { throw new NullPointerException("actTypes contains a null"); } } + + @Override + public Type[] getActualTypeArguments() { + + return this.actualTypes; + } + + @Override + public Type getRawType() { + return this.rawType; + } } /** @@ -380,57 +366,24 @@ public final class Types { /** * Creates instance of this class with concrete package and type name. * - * @param packageName - * string with the package name - * @param typeName - * string with the name of type + * @param packageName string with the package name + * @param typeName string with the name of type */ WildcardTypeImpl(final JavaTypeName identifier) { super(identifier); } } - public static > DefaultRestrictions getDefaultRestrictions(final T min, + public static > DefaultRestrictions getDefaultRestrictions(final T min, final T max) { return new DefaultRestrictions<>(min, max); } private static final class DefaultRestrictions> implements Restrictions { - private final T min; - private final T max; private final RangeConstraint rangeConstraint; - private DefaultRestrictions(final T min, final T max) { - this.min = Preconditions.checkNotNull(min); - this.max = Preconditions.checkNotNull(max); - - this.rangeConstraint = new RangeConstraint() { - - @Override - public Optional getErrorAppTag() { - return Optional.empty(); - } - - @Override - public Optional getErrorMessage() { - return Optional.empty(); - } - - @Override - public Optional getDescription() { - return Optional.empty(); - } - - @Override - public Optional getReference() { - return Optional.empty(); - } - - @Override - public RangeSet getAllowedRanges() { - return ImmutableRangeSet.of(Range.closed(min, max)); - } - }; + DefaultRestrictions(final T min, final T max) { + this.rangeConstraint = new DefaultRangeConstraint<>(min, max); } @Override @@ -453,4 +406,39 @@ public final class Types { return Optional.empty(); } } + + private static final class DefaultRangeConstraint> implements RangeConstraint { + private final T min; + private final T max; + + DefaultRangeConstraint(final T min, final T max) { + this.min = requireNonNull(min); + this.max = requireNonNull(max); + } + + @Override + public Optional getErrorAppTag() { + return Optional.empty(); + } + + @Override + public Optional getErrorMessage() { + return Optional.empty(); + } + + @Override + public Optional getDescription() { + return Optional.empty(); + } + + @Override + public Optional getReference() { + return Optional.empty(); + } + + @Override + public RangeSet getAllowedRanges() { + return ImmutableRangeSet.of(Range.closed(min, max)); + } + } }