Specialize relative leafref types during instantiation
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / mdsal / binding / model / util / Types.java
index c52c95001d84e65212c8f851a4426c54763c0553..97835bd02a04dd8c5718ead0e79716067604e36c 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.mdsal.binding.model.util;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.annotations.Beta;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -24,7 +25,9 @@ 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.AbstractBaseType;
 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
@@ -33,14 +36,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<Class<?>, ConcreteType> TYPE_LOADER = new CacheLoader<Class<?>, ConcreteType>() {
+    private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER = new CacheLoader<>() {
         @Override
         public ConcreteType load(final Class<?> key) {
             return new ConcreteTypeImpl(JavaTypeName.create(key), null);
@@ -49,21 +50,23 @@ public final class Types {
     private static final LoadingCache<Class<?>, 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 BYTE_ARRAY = typeForClass(byte[].class);
+    public static final @NonNull ConcreteType CLASS = typeForClass(Class.class);
+    public static final @NonNull ConcreteType STRING = typeForClass(String.class);
+    public static final @NonNull ConcreteType VOID = typeForClass(Void.class);
+
+    private static final @NonNull ConcreteType BUILDER = typeForClass(Builder.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_BOOLEAN = typeForClass(boolean.class);
+    private static final @NonNull ConcreteType PRIMITIVE_INT = typeForClass(int.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);
+    private static final @NonNull ParameterizedType LIST_TYPE_WILDCARD = parameterizedTypeFor(LIST_TYPE);
 
     /**
      * It is not desirable to create instance of this class.
@@ -79,7 +82,7 @@ public final class Types {
      * @return A parameterized type corresponding to {@code Class<Type>}
      * @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);
     }
 
@@ -88,7 +91,7 @@ public final class Types {
      *
      * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
      */
-    public static ConcreteType voidType() {
+    public static @NonNull ConcreteType voidType() {
         return VOID;
     }
 
@@ -97,16 +100,34 @@ public final class Types {
      *
      * @return <code>ConcreteType</code> instance which represents {@link Object}
      */
-    public static ConcreteType objectType() {
+    public static @NonNull ConcreteType objectType() {
         return OBJECT;
     }
 
+    /**
+     * Returns an instance of {@link ConcreteType} which represents JAVA <code>boolean</code> type.
+     *
+     * @return <code>ConcreteType</code> instance which represents JAVA <code>boolean</code>
+     */
+    public static @NonNull ConcreteType primitiveBooleanType() {
+        return PRIMITIVE_BOOLEAN;
+    }
+
+    /**
+     * Returns an instance of {@link ConcreteType} which represents JAVA <code>int</code> type.
+     *
+     * @return <code>ConcreteType</code> instance which represents JAVA <code>int</code>
+     */
+    public static @NonNull ConcreteType primitiveIntType() {
+        return PRIMITIVE_INT;
+    }
+
     /**
      * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
      *
      * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
      */
-    public static ConcreteType primitiveVoidType() {
+    public static @NonNull ConcreteType primitiveVoidType() {
         return PRIMITIVE_VOID;
     }
 
@@ -115,7 +136,7 @@ public final class Types {
      *
      * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
      */
-    public static ConcreteType serializableType() {
+    public static @NonNull ConcreteType serializableType() {
         return SERIALIZABLE;
     }
 
@@ -125,20 +146,24 @@ public final class Types {
      * @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) {
-        if (restrictions == null) {
-            return typeForClass(cls);
-        }
+    public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls,
+            final @Nullable Restrictions restrictions) {
+        return restrictions == null ? typeForClass(cls) : restrictedType(JavaTypeName.create(cls), restrictions);
+    }
 
-        final JavaTypeName identifier = JavaTypeName.create(cls);
-        if (restrictions instanceof DefaultRestrictions) {
-            return new ConcreteTypeImpl(identifier, restrictions);
-        }
-        return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
+    @Beta
+    public static @NonNull ConcreteType restrictedType(final Type type, final @NonNull Restrictions restrictions) {
+        return restrictedType(type.getIdentifier(), restrictions);
+    }
+
+    private static @NonNull ConcreteType restrictedType(final JavaTypeName identifier,
+            final @NonNull Restrictions restrictions) {
+        return restrictions instanceof DefaultRestrictions ? new ConcreteTypeImpl(identifier, restrictions)
+                : new BaseTypeWithRestrictionsImpl(identifier, restrictions);
     }
 
     /**
@@ -148,10 +173,18 @@ public final class Types {
      * @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}&lt;V&gt; with concrete type
      * of value.
@@ -159,7 +192,7 @@ public final class Types {
      * @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);
     }
 
@@ -170,12 +203,25 @@ public final class Types {
      * @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);
     }
 
+    /**
+     * Returns an instance of {@link ParameterizedType} describing the typed {@link List}&lt;?&gt;.
+     *
+     * @return Description of type instance of List
+     */
+    public static @NonNull ParameterizedType listTypeWildcard() {
+        return LIST_TYPE_WILDCARD;
+    }
+
+    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);
     }
 
     /**
@@ -185,7 +231,7 @@ public final class Types {
      * @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);
     }
 
@@ -196,7 +242,7 @@ 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);
     }
 
@@ -208,7 +254,7 @@ public final class 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) {
+    public static @NonNull ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
         return new ParametrizedTypeImpl(type, parameters);
     }
 
@@ -222,36 +268,18 @@ public final class Types {
         return new WildcardTypeImpl(identifier);
     }
 
-    /**
-     * Creates instance of
-     * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
-     * ParameterizedType} where raw type is
-     * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
-     * parameter is <code>valueType</code>.
-     *
-     * @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>
-     */
-    public static ParameterizedType augmentableTypeFor(final Type valueType) {
-        final Type augmentable = typeForClass(Augmentable.class);
-        return parameterizedTypeFor(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 <code>valueType</code>.
-     *
-     * @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>
-     */
-    public static ParameterizedType augmentationTypeFor(final Type valueType) {
-        final Type augmentation = typeForClass(Augmentation.class);
-        return parameterizedTypeFor(augmentation, valueType);
+    public static boolean strictTypeEquals(final Type type1, final Type type2) {
+        if (!type1.equals(type2)) {
+            return false;
+        }
+        if (type1 instanceof ParameterizedType) {
+            if (type2 instanceof ParameterizedType) {
+                return Arrays.equals(((ParameterizedType) type1).getActualTypeArguments(),
+                    ((ParameterizedType) type2).getActualTypeArguments());
+            }
+            return false;
+        }
+        return !(type2 instanceof ParameterizedType);
     }
 
     public static @Nullable String getOuterClassName(final Type valueType) {