Add Types.isListType(ParameterizedType)
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / mdsal / binding / model / util / Types.java
index 763c1e6f392362a6b106975347cf42b442584d1f..82e4b422d1b09df96072175139fed7447c34f58e 100644 (file)
  */
 package org.opendaylight.mdsal.binding.model.util;
 
-import com.google.common.base.CharMatcher;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Splitter;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
-import com.google.common.collect.Iterables;
+import com.google.common.collect.ImmutableRangeSet;
+import com.google.common.collect.Range;
+import com.google.common.collect.RangeSet;
+import com.google.common.util.concurrent.ListenableFuture;
+import java.io.Serializable;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
-import java.util.concurrent.Future;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
 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;
-import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
 
 public final class Types {
-    private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
-            new CacheLoader<Class<?>, ConcreteType>() {
-                @Override
-                public ConcreteType load(final Class<?> key) {
-                    return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
-                }
+    private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER = new CacheLoader<Class<?>, ConcreteType>() {
+        @Override
+        public ConcreteType load(final Class<?> key) {
+            return new ConcreteTypeImpl(JavaTypeName.create(key), null);
+        }
     };
     private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
             CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
 
-    private static final Type SET_TYPE = typeForClass(Set.class);
-    private static final Type LIST_TYPE = typeForClass(List.class);
-    private static final Type MAP_TYPE = typeForClass(Map.class);
 
     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
-    public static final ConcreteType FUTURE = typeForClass(Future.class);
     public static final ConcreteType STRING = typeForClass(String.class);
     public static final ConcreteType VOID = typeForClass(Void.class);
-    public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
-    public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
-    private static final Splitter DOT_SPLITTER = Splitter.on('.');
+    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);
 
     /**
-     * It is not desirable to create instance of this class
+     * It is not desirable to create instance of this class.
      */
     private Types() {
     }
 
     /**
-     * Creates the instance of type
-     * {@link org.opendaylight.mdsal.binding.model.api.ConcreteType
-     * ConcreteType} which represents JAVA <code>void</code> type.
+     * Returns an instance of {@link ParameterizedType} which represents JAVA <code>java.lang.Class</code> type
+     * specialized to specified type.
+     *
+     * @param type Type for which to specialize
+     * @return A parameterized type corresponding to {@code Class<Type>}
+     * @throws NullPointerException if {@code type} is null
+     */
+    public static ParameterizedType classType(final Type type) {
+        return parameterizedTypeFor(CLASS, type);
+    }
+
+    /**
+     * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
      *
-     * @return <code>ConcreteType</code> instance which represents JAVA
-     *         <code>void</code>
+     * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
      */
     public static ConcreteType voidType() {
         return VOID;
     }
 
     /**
-     * Creates the instance of type
-     * {@link org.opendaylight.mdsal.binding.model.api.ConcreteType
-     * ConcreteType} which represents primitive JAVA type for which package
-     * doesn't exist.
+     * Returns an instance of {@link ConcreteType} which represents {@link Object} type.
      *
-     * @param primitiveType
-     *            string containing programmatic construction based on
-     *            primitive type (e.g byte[])
-     * @param restrictions
-     *            restrictions object
-     * @return <code>ConcreteType</code> instance which represents programmatic
-     *         construction with primitive JAVA type
+     * @return <code>ConcreteType</code> instance which represents {@link Object}
      */
-    public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
-        return new ConcreteTypeImpl("", primitiveType, restrictions);
+    public static ConcreteType objectType() {
+        return OBJECT;
     }
 
     /**
-     * Returns an instance of {@link ConcreteType} describing the class
+     * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
      *
-     * @param cls
-     *            Class to describe
+     * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
+     */
+    public static ConcreteType primitiveVoidType() {
+        return PRIMITIVE_VOID;
+    }
+
+    /**
+     * Returns an instance of {@link ConcreteType} which represents {@link Serializable} type.
+     *
+     * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
+     */
+    public static ConcreteType serializableType() {
+        return SERIALIZABLE;
+    }
+
+    /**
+     * Returns an instance of {@link ConcreteType} describing the class.
+     *
+     * @param cls Class to describe
      * @return Description of class
      */
     public static ConcreteType typeForClass(final Class<?> cls) {
@@ -105,25 +130,22 @@ public final class Types {
     }
 
     public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
-        if (restrictions != null) {
-            if (restrictions instanceof DefaultRestrictions) {
-                return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
-            } else {
-                return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
-            }
-        } else {
+        if (restrictions == null) {
             return typeForClass(cls);
         }
+
+        final JavaTypeName identifier = JavaTypeName.create(cls);
+        if (restrictions instanceof DefaultRestrictions) {
+            return new ConcreteTypeImpl(identifier, restrictions);
+        }
+        return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
     }
 
     /**
-     * Returns an instance of {@link ParameterizedType} describing the typed
-     * {@link Map}&lt;K,V&gt;
+     * Returns an instance of {@link ParameterizedType} describing the typed {@link Map}&lt;K,V&gt;.
      *
-     * @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) {
@@ -131,11 +153,10 @@ public final class Types {
     }
 
     /**
-     * Returns an instance of {@link ParameterizedType} describing the typed
-     * {@link Set}&lt;V&gt; with concrete type of value.
+     * Returns an instance of {@link ParameterizedType} describing the typed {@link Set}&lt;V&gt; 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) {
@@ -143,47 +164,66 @@ public final class Types {
     }
 
     /**
-     * Returns an instance of {@link ParameterizedType} describing the typed
-     * {@link List}&lt;V&gt; with concrete type of value.
+     * Returns an instance of {@link ParameterizedType} describing the typed {@link List}&lt;V&gt; 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) {
         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 && isListType((ParameterizedType) type);
+    }
+
     /**
-     * Creates instance of type
-     * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
-     * ParameterizedType}
+     * Returns an instance of {@link ParameterizedType} describing the typed {@link ListenableFuture}&lt;V&gt;
+     * with concrete type of value.
+     *
+     * @param valueType Value Type
+     * @return Description of type instance of ListenableFuture
+     */
+    public static ParameterizedType listenableFutureTypeFor(final Type valueType) {
+        return parameterizedTypeFor(LISTENABLE_FUTURE, valueType);
+    }
+
+    /**
+     * Returns an instance of {@link ParameterizedType} describing the typed
+     * {@link Builder}&lt;V&gt; with concrete type of value.
+     *
+     * @param valueType Value Type
+     * @return Description of type instance of Builder
+     */
+    public static ParameterizedType builderTypeFor(final Type valueType) {
+        return parameterizedTypeFor(BUILDER, valueType);
+    }
+
+    /**
+     * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType ParameterizedType}.
      *
-     * @param type
-     *            JAVA <code>Type</code> for raw type
-     * @param parameters
-     *            JAVA <code>Type</code>s for actual parameter types
-     * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
-     *         and its parameters <code>parameters</code>
+     * @param type JAVA <code>Type</code> for raw type
+     * @param parameters JAVA <code>Type</code>s for actual parameter types
+     * @return <code>ParametrizedType</code> representation of <code>type</code> and its <code>parameters</code>
+     * @throws NullPointerException if any argument or any member of {@code parameters} is null
      */
     public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
         return new ParametrizedTypeImpl(type, parameters);
     }
 
     /**
-     * Creates instance of type
-     * {@link org.opendaylight.mdsal.binding.model.api.WildcardType
-     * WildcardType}
+     * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
      *
-     * @param packageName
-     *            string with the package name
-     * @param typeName
-     *            string with the type name
-     * @return <code>WildcardType</code> representation of
-     *         <code>packageName</code> and <code>typeName</code>
+     * @param identifier JavaTypeName of the type
+     * @return <code>WildcardType</code> representation of specified identifier
      */
-    public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
-        return new WildcardTypeImpl(packageName, typeName);
+    public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
+        return new WildcardTypeImpl(identifier);
     }
 
     /**
@@ -193,8 +233,7 @@ public final class Types {
      * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
      * parameter is <code>valueType</code>.
      *
-     * @param valueType
-     *            JAVA <code>Type</code> with actual parameter
+     * @param valueType JAVA <code>Type</code> with actual parameter
      * @return <code>ParametrizedType</code> representation of raw type
      *         <code>Augmentable</code> with actual parameter
      *         <code>valueType</code>
@@ -205,14 +244,11 @@ public final class Types {
     }
 
     /**
-     * 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 <code>valueType</code>.
+     * 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 <code>valueType</code>.
      *
-     * @param valueType
-     *            JAVA <code>Type</code> with actual parameter
+     * @param valueType JAVA <code>Type</code> with actual parameter
      * @return <code>ParametrizedType</code> reprezentation of raw type
      *         <code>Augmentation</code> with actual parameter
      *         <code>valueType</code>
@@ -222,35 +258,24 @@ public final class Types {
         return parameterizedTypeFor(augmentation, valueType);
     }
 
-
-    public static  @Nullable String getOuterClassName(final Type valueType) {
-        final String pkgName = valueType.getPackageName();
-        if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) {
-            // It is inner class.
-            return Iterables.getLast(DOT_SPLITTER.split(pkgName));
-        }
-        return null;
+    public static @Nullable String getOuterClassName(final Type valueType) {
+        return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
     }
 
     /**
-     *
      * Represents concrete JAVA type.
-     *
      */
     private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
         private final Restrictions restrictions;
 
         /**
-         * Creates instance of this class with package <code>pkName</code> and
-         * with the type name <code>name</code>.
+         * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
          *
-         * @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
          */
-        private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
-            super(pkName, name);
+        ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
+            super(identifier);
             this.restrictions = restrictions;
         }
 
@@ -261,25 +286,21 @@ 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 <code>pkName</code> and
-         * with the type name <code>name</code>.
+         * Creates instance of this class with package <code>pkName</code> and with the type name <code>name</code>.
          *
-         * @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
          */
-        private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
-            super(pkName, name);
-            this.restrictions = Preconditions.checkNotNull(restrictions);
+        BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
+            super(identifier);
+            this.restrictions = requireNonNull(restrictions);
         }
 
         @Override
@@ -289,9 +310,7 @@ public final class Types {
     }
 
     /**
-     *
      * Represents parametrized JAVA type.
-     *
      */
     private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
         /**
@@ -300,10 +319,25 @@ 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;
 
+        /**
+         * Creates instance of this class with concrete rawType and array of actual parameters.
+         *
+         * @param rawType JAVA <code>Type</code> for raw type
+         * @param actTypes array of actual parameters
+         */
+        ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
+            super(rawType.getIdentifier());
+            this.rawType = requireNonNull(rawType);
+            actualTypes = actTypes.clone();
+            if (Arrays.stream(actualTypes).anyMatch(Objects::isNull)) {
+                throw new NullPointerException("actTypes contains a null");
+            }
+        }
+
         @Override
         public Type[] getActualTypeArguments() {
 
@@ -314,57 +348,33 @@ public final class Types {
         public Type getRawType() {
             return this.rawType;
         }
-
-        /**
-         * Creates instance of this class with concrete rawType and array of
-         * actual parameters.
-         *
-         * @param rawType
-         *            JAVA <code>Type</code> for raw type
-         * @param actTypes
-         *            array of actual parameters
-         */
-        public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
-            super(rawType.getPackageName(), rawType.getName());
-            this.rawType = rawType;
-            this.actualTypes = actTypes.clone();
-        }
-
     }
 
     /**
-     *
      * Represents JAVA bounded wildcard type.
-     *
      */
     private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
         /**
          * 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
          */
-        public WildcardTypeImpl(final String packageName, final String typeName) {
-            super(packageName, typeName);
+        WildcardTypeImpl(final JavaTypeName identifier) {
+            super(identifier);
         }
     }
 
-    public static <T extends Number> DefaultRestrictions<T> getDefaultRestrictions(final T min, final T max) {
+    public static <T extends Number & Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
+            final T max) {
         return new DefaultRestrictions<>(min, max);
     }
 
-    private static final class DefaultRestrictions<T extends Number> implements Restrictions {
-        private final T min;
-        private final T max;
-        private final List<RangeConstraint> rangeConstraints;
+    private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
+        private final RangeConstraint<?> rangeConstraint;
 
-        private DefaultRestrictions(final T min, final T max) {
-            this.min = Preconditions.checkNotNull(min);
-            this.max = Preconditions.checkNotNull(max);
-            this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
-                    .<String>absent(), Optional.<String>absent()));
+        DefaultRestrictions(final T min, final T max) {
+            this.rangeConstraint = new DefaultRangeConstraint<>(min, max);
         }
 
         @Override
@@ -373,8 +383,8 @@ public final class Types {
         }
 
         @Override
-        public List<RangeConstraint> getRangeConstraints() {
-            return this.rangeConstraints;
+        public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
+            return Optional.of(rangeConstraint);
         }
 
         @Override
@@ -383,8 +393,43 @@ public final class Types {
         }
 
         @Override
-        public List<LengthConstraint> getLengthConstraints() {
-            return Collections.emptyList();
+        public Optional<LengthConstraint> getLengthConstraint() {
+            return Optional.empty();
+        }
+    }
+
+    private static final class DefaultRangeConstraint<T extends Number & Comparable<T>> implements RangeConstraint<T> {
+        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<String> getErrorAppTag() {
+            return Optional.empty();
+        }
+
+        @Override
+        public Optional<String> getErrorMessage() {
+            return Optional.empty();
+        }
+
+        @Override
+        public Optional<String> getDescription() {
+            return Optional.empty();
+        }
+
+        @Override
+        public Optional<String> getReference() {
+            return Optional.empty();
+        }
+
+        @Override
+        public RangeSet<T> getAllowedRanges() {
+            return ImmutableRangeSet.of(Range.closed(min, max));
         }
     }
-}
\ No newline at end of file
+}