Share Serializable Type
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / mdsal / binding / model / util / Types.java
index 70a7db6d77522aa92393bf682bab176fcd5ac611..4d8edcf1846c8a5398c59e2d27de6952bf391a00 100644 (file)
@@ -7,25 +7,24 @@
  */
 package org.opendaylight.mdsal.binding.model.util;
 
-import com.google.common.base.CharMatcher;
 import com.google.common.base.Preconditions;
-import com.google.common.base.Splitter;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
 import com.google.common.collect.ImmutableRangeSet;
-import com.google.common.collect.Iterables;
 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.Collections;
 import java.util.List;
 import java.util.Map;
 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;
@@ -41,23 +40,25 @@ public final class Types {
             new CacheLoader<Class<?>, ConcreteType>() {
                 @Override
                 public ConcreteType load(final Class<?> key) {
-                    return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
+                    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 Type SET_TYPE = typeForClass(Set.class);
+    public static final Type LIST_TYPE = typeForClass(List.class);
+    public 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 FUTURE = typeForClass(ListenableFuture.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);
+    public static final ConcreteType CHAR_ARRAY = typeForClass(char[].class);
+
+    private static final ConcreteType PRIMITIVE_VOID = typeForClass(void.class);
+    private static final ConcreteType SERIALIZABLE = typeForClass(Serializable.class);
 
     /**
      * It is not desirable to create instance of this class
@@ -66,33 +67,30 @@ public final class 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 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 JAVA <code>void</code> type.
+     *
+     * @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.
      *
-     * @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 JAVA <code>{@link Serializable}</code>
      */
-    public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
-        return new ConcreteTypeImpl("", primitiveType, restrictions);
+    public static ConcreteType serializableType() {
+        return SERIALIZABLE;
     }
 
     /**
@@ -110,10 +108,12 @@ public final class Types {
         if (restrictions == null) {
             return typeForClass(cls);
         }
+
+        final JavaTypeName identifier = JavaTypeName.create(cls);
         if (restrictions instanceof DefaultRestrictions) {
-            return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+            return new ConcreteTypeImpl(identifier, restrictions);
         }
-        return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+        return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
     }
 
     /**
@@ -165,25 +165,20 @@ public final class Types {
      *            JAVA <code>Type</code>s for actual parameter types
      * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
      *         and its parameters <code>parameters</code>
+     * @throws NullPointerException if any argument 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);
     }
 
     /**
@@ -222,14 +217,8 @@ public final class Types {
         return parameterizedTypeFor(augmentation, valueType);
     }
 
-
-    public static  @Nullable String getOuterClassName(final Type valueType) {
-        final String pkgName = valueType.getPackageName();
-        if (CharMatcher.javaUpperCase().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);
     }
 
     /**
@@ -249,8 +238,8 @@ public final class Types {
          * @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;
         }
 
@@ -275,8 +264,8 @@ public final class Types {
          * @param name
          *            string with the name of the type
          */
-        private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
-            super(pkName, name);
+        BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
+            super(identifier);
             this.restrictions = Preconditions.checkNotNull(restrictions);
         }
 
@@ -321,11 +310,10 @@ public final class Types {
          *            array of actual parameters
          */
         public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
-            super(rawType.getPackageName(), rawType.getName());
+            super(rawType.getIdentifier());
             this.rawType = rawType;
             this.actualTypes = actTypes.clone();
         }
-
     }
 
     /**
@@ -340,8 +328,8 @@ public final class Types {
          * @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);
         }
     }
 
@@ -408,4 +396,4 @@ public final class Types {
             return Optional.empty();
         }
     }
-}
\ No newline at end of file
+}