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 9f7351e9d0cdd6a77e3adfac6094b525d8c10def..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;
@@ -26,6 +27,7 @@ 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;
@@ -34,8 +36,6 @@ 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;
@@ -51,22 +51,22 @@ public final class Types {
             CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
 
     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);
-    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_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 ConcreteType AUGMENTABLE = typeForClass(Augmentable.class);
-    private static final @NonNull ConcreteType AUGMENTATION = typeForClass(Augmentation.class);
+    private static final @NonNull ParameterizedType LIST_TYPE_WILDCARD = parameterizedTypeFor(LIST_TYPE);
 
     /**
      * It is not desirable to create instance of this class.
@@ -104,6 +104,24 @@ public final class Types {
         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.
      *
@@ -134,15 +152,18 @@ public final class Types {
 
     public static @NonNull ConcreteType typeForClass(final @NonNull Class<?> cls,
             final @Nullable Restrictions restrictions) {
-        if (restrictions == null) {
-            return typeForClass(cls);
-        }
+        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);
     }
 
     /**
@@ -186,6 +207,15 @@ public final class Types {
         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());
     }
@@ -238,34 +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 @NonNull ParameterizedType augmentableTypeFor(final Type valueType) {
-        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 @NonNull ParameterizedType augmentationTypeFor(final Type valueType) {
-        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) {