Fix a minor typo
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / GeneratorUtil.java
index fcd5217722db6a8b3e0774e68b4060db17d2031d..48ff6c3957ab40a3f97b87c29c75e6034904fe66 100644 (file)
@@ -8,50 +8,47 @@
 package org.opendaylight.mdsal.binding.java.api.generator;
 
 import static com.google.common.base.Preconditions.checkArgument;
-import static org.opendaylight.mdsal.binding.java.api.generator.Constants.COMMA;
 
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
+import org.opendaylight.mdsal.binding.model.api.ConcreteType;
 import org.opendaylight.mdsal.binding.model.api.Constant;
 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
 import org.opendaylight.mdsal.binding.model.api.Type;
 import org.opendaylight.mdsal.binding.model.api.WildcardType;
-import org.opendaylight.mdsal.binding.model.util.TypeConstants;
-import org.opendaylight.mdsal.binding.model.util.Types;
+import org.opendaylight.mdsal.binding.model.ri.TypeConstants;
+import org.opendaylight.mdsal.binding.model.ri.Types;
 
 public final class GeneratorUtil {
+    private static final ConcreteType PATTERN = Types.typeForClass(Pattern.class);
 
-    /**
-     * It doesn't have the sense to create the instances of this class.
-     */
     private GeneratorUtil() {
-        throw new UnsupportedOperationException();
+        // Hidden on purpose
     }
 
     /**
-     * Returns the map of imports. The map maps the type name to the package
-     * name. To the map are added packages for <code>genType</code> and for all
-     * enclosed types, constants, methods (parameter types, return values),
+     * Returns the map of imports. The map maps the type name to the package name. To the map are added packages
+     * for <code>genType</code> and for all enclosed types, constants, methods (parameter types, return values),
      * implemented types.
      *
-     * @param genType
-     *            generated type for which the map of the imports is created
+     * @param genType generated type for which the map of the imports is created
      * @return map of the necessary imports
-     * @throws IllegalArgumentException
-     *             if <code>genType</code> equals <code>null</code>
+     * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
      */
-    static Map<String, String> createImports(final GeneratedType genType) {
+    static Map<String, JavaTypeName> createImports(final GeneratedType genType) {
         if (genType == null) {
             throw new IllegalArgumentException("Generated Type cannot be NULL!");
         }
-        final Map<String, String> imports = new LinkedHashMap<>();
+        final Map<String, JavaTypeName> imports = new LinkedHashMap<>();
 
         List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();
         if (!childGeneratedTypes.isEmpty()) {
@@ -63,7 +60,7 @@ public final class GeneratorUtil {
         // REGULAR EXPRESSION
         if (genType instanceof GeneratedTransferObject
                 && isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {
-            putTypeIntoImports(genType, Types.typeForClass(java.util.regex.Pattern.class), imports);
+            putTypeIntoImports(genType, PATTERN, imports);
         }
 
         final List<MethodSignature> methods = genType.getMethodDefinitions();
@@ -96,18 +93,12 @@ public final class GeneratorUtil {
     }
 
     /**
-     * Evaluates if it is necessary to add the package name for
-     * <code>type</code> to the map of imports for <code>parentGenType</code>.
-     * If it is so the package name is saved to the map <code>imports</code>.
+     * Evaluates if it is necessary to add the package name for <code>type</code> to the map of imports for
+     * <code>parentGenType</code>. If it is so the package name is saved to the map <code>imports</code>.
      *
-     * @param parentGenType
-     *            generated type for which is the map of the necessary imports
-     *            built
-     * @param type
-     *            JAVA <code>Type</code> for which is the necessary of the
-     *            package import evaluated
-     * @param imports
-     *            map of the imports for <code>parentGenType</code>
+     * @param parentGenType generated type for which is the map of the necessary imports built
+     * @param type JAVA <code>Type</code> for which is the necessary of the package import evaluated
+     * @param imports map of the imports for <code>parentGenType</code>
      * @throws IllegalArgumentException
      *             <ul>
      *             <li>if the <code>parentGenType</code> equals
@@ -124,7 +115,7 @@ public final class GeneratorUtil {
      *             </ul>
      */
     static void putTypeIntoImports(final GeneratedType parentGenType, final Type type,
-                                   final Map<String, String> imports) {
+                                   final Map<String, JavaTypeName> imports) {
         checkArgument(parentGenType != null, "Parent Generated Type parameter MUST be specified and cannot be "
                 + "NULL!");
         checkArgument(parentGenType.getName() != null, "Parent Generated Type name cannot be NULL!");
@@ -142,7 +133,7 @@ public final class GeneratorUtil {
             return;
         }
         if (!imports.containsKey(typeName)) {
-            imports.put(typeName, typePackageName);
+            imports.put(typeName, type.getIdentifier());
         }
         if (type instanceof ParameterizedType) {
             final ParameterizedType paramType = (ParameterizedType) type;
@@ -156,14 +147,11 @@ public final class GeneratorUtil {
     }
 
     /**
-     * Checks if the constant with the name <code>constName</code> is in the
-     * list of the constant definition for <code>genTO</code>.
+     * Checks if the constant with the name <code>constName</code> is in the list of the constant definition for
+     * <code>genTO</code>.
      *
-     * @param constName
-     *            string with the name of constant which is sought
-     * @param genTO
-     *            generated transfer object in which is <code>constName</code>
-     *            sought
+     * @param constName string with the name of constant which is sought
+     * @param genTO generated transfer object in which is <code>constName</code> sought
      * @return boolean value
      *         <ul>
      *         <li>true - if <code>constName</code> is in the list of the
@@ -190,41 +178,29 @@ public final class GeneratorUtil {
     }
 
     /**
-     * Creates the map which maps the type name to package name and contains
-     * only package names for enclosed types of <code>genType</code> and
-     * recursivelly their enclosed types.
+     * Creates the map which maps the type name to package name and contains only package names for enclosed types
+     * of <code>genType</code> and recursively their enclosed types.
      *
-     * @param genType
-     *            JAVA <code>Type</code> for which is the map created
-     * @return map of the package names for all the enclosed types and
-     *         recursivelly their enclosed types
+     * @param genType JAVA <code>Type</code> for which is the map created
+     * @return map of the package names for all the enclosed types and recursively their enclosed types
      */
     static Map<String, String> createChildImports(final GeneratedType genType) {
         Map<String, String> childImports = new LinkedHashMap<>();
-        List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();
-        if (!childGeneratedTypes.isEmpty()) {
-            for (GeneratedType genTypeChild : childGeneratedTypes) {
-                createChildImports(genTypeChild);
-                childImports.put(genTypeChild.getName(), genTypeChild.getPackageName());
-            }
+        for (GeneratedType genTypeChild : genType.getEnclosedTypes()) {
+            createChildImports(genTypeChild);
+            childImports.put(genTypeChild.getName(), genTypeChild.getPackageName());
         }
         return childImports;
     }
 
     /**
-     * Builds the string which contains either the full path to the type
-     * (package name with type) or only type name if the package is among
-     * <code>imports</code>.
+     * Builds the string which contains either the full path to the type (package name with type) or only type name
+     * if the package is among <code>imports</code>.
      *
-     * @param parentGenType
-     *            generated type which contains <code>type</code>
-     * @param type
-     *            JAVA <code>Type</code> for which is the string with type info
-     *            generated
-     * @param imports
-     *            map of necessary imports for <code>parentGenType</code>
-     * @return string with type name for <code>type</code> in the full format or
-     *         in the short format
+     * @param parentGenType generated type which contains <code>type</code>
+     * @param type JAVA <code>Type</code> for which is the string with type info generated
+     * @param imports map of necessary imports for <code>parentGenType</code>
+     * @return string with type name for <code>type</code> in the full format or in the short format
      * @throws IllegalArgumentException
      *             <ul>
      *             <li>if the <code>type</code> equals <code>null</code></li>
@@ -236,139 +212,88 @@ public final class GeneratorUtil {
      *             </ul>
      */
     static String getExplicitType(final GeneratedType parentGenType, final Type type,
-                                  final Map<String, String> imports) {
-
+                                  final Map<String, JavaTypeName> imports) {
         checkArgument(type != null, "Type parameter MUST be specified and cannot be NULL!");
-        checkArgument(type.getName() != null, "Type name cannot be NULL!");
-        checkArgument(type.getPackageName() != null, "Type cannot have Package Name referenced as NULL!");
         checkArgument(imports != null, "Imports Map cannot be NULL!");
 
-        final String typePackageName = type.getPackageName();
-        final String typeName = type.getName();
-        final String importedPackageName = imports.get(typeName);
-        final StringBuilder builder;
-
-        if (typePackageName.equals(importedPackageName)) {
-            builder = new StringBuilder(type.getName());
+        final JavaTypeName importedType = imports.get(type.getName());
+        final StringBuilder builder = new StringBuilder();
+        if (type.getIdentifier().equals(importedType)) {
+            builder.append(type.getName());
             addActualTypeParameters(builder, type, parentGenType, imports);
             if (builder.toString().equals("Void")) {
                 return "void";
             }
         } else {
-            builder = new StringBuilder();
-            if (!typePackageName.isEmpty()) {
-                builder.append(typePackageName).append(Constants.DOT).append(type.getName());
-            } else {
-                builder.append(type.getName());
-            }
             if (type.equals(Types.voidType())) {
                 return "void";
             }
+            builder.append(type.getFullyQualifiedName());
             addActualTypeParameters(builder, type, parentGenType, imports);
         }
         return builder.toString();
     }
 
     /**
-     * Adds actual type parameters from <code>type</code> to
-     * <code>builder</code> if <code>type</code> is
+     * Adds actual type parameters from <code>type</code> to <code>builder</code> if <code>type</code> is
      * <code>ParametrizedType</code>.
      *
-     * @param builder
-     *            string builder which contains type name
-     * @param type
-     *            JAVA <code>Type</code> for which is the string with type info
-     *            generated
-     * @param parentGenType
-     *            generated type which contains <code>type</code>
-     * @param imports
-     *            map of necessary imports for <code>parentGenType</code>
+     * @param builder string builder which contains type name
+     * @param type JAVA <code>Type</code> for which is the string with type info generated
+     * @param parentGenType generated type which contains <code>type</code>
+     * @param imports map of necessary imports for <code>parentGenType</code>
      * @return if <code>type</code> is of the type <code>ParametrizedType</code> <br />
      *         <li>then <code>builder</code> + actual <code>type</code>
      *         parameters</li> <li>else only <code>builder</code></li>
      */
     private static StringBuilder addActualTypeParameters(final StringBuilder builder, final Type type,
-                                                         final GeneratedType parentGenType, final Map<String, String> imports) {
+            final GeneratedType parentGenType, final Map<String, JavaTypeName> imports) {
         if (type instanceof ParameterizedType) {
             final ParameterizedType pType = (ParameterizedType) type;
             final Type[] pTypes = pType.getActualTypeArguments();
-            builder.append("<");
-            builder.append(getParameters(parentGenType, pTypes, imports));
-            builder.append(">");
+            builder.append('<').append(getParameters(parentGenType, pTypes, imports)).append('>');
         }
         return builder;
     }
 
     /**
-     * Generates the string with all actual type parameters from
-     * <code>pTypes</code>
+     * Generates the string with all actual type parameters from <code>pTypes</code>.
      *
-     * @param parentGenType
-     *            generated type for which is the JAVA code generated
-     * @param pTypes
-     *            array of <code>Type</code> instances = actual type parameters
-     * @param availableImports
-     *            map of imports for <code>parentGenType</code>
+     * @param parentGenType generated type for which is the JAVA code generated
+     * @param paramTypes array of <code>Type</code> instances = actual type parameters
+     * @param availableImports map of imports for <code>parentGenType</code>
      * @return string with all actual type parameters from <code>pTypes</code>
      */
-    private static String getParameters(final GeneratedType parentGenType, final Type[] pTypes,
-                                        final Map<String, String> availableImports) {
+    private static String getParameters(final GeneratedType parentGenType, final Type[] paramTypes,
+                                        final Map<String, JavaTypeName> availableImports) {
 
-        if (pTypes == null || pTypes.length == 0) {
+        if (paramTypes == null || paramTypes.length == 0) {
             return "?";
         }
         final StringBuilder builder = new StringBuilder();
-        for (int i = 0; i < pTypes.length; i++) {
-            final Type t = pTypes[i];
-
-            String separator = COMMA;
-            if (i == pTypes.length - 1) {
-                separator = "";
-            }
+        for (int i = 0; i < paramTypes.length; i++) {
+            final Type t = paramTypes[i];
 
             if (Types.voidType().equals(t)) {
-                builder.append("java.lang.Void").append(separator);
+                builder.append("java.lang.Void");
             } else {
                 if (t instanceof WildcardType) {
                     builder.append("? extends ");
                 }
-                builder.append(getExplicitType(parentGenType, t, availableImports)).append(separator);
+                builder.append(getExplicitType(parentGenType, t, availableImports));
+            }
+            if (i != paramTypes.length - 1) {
+                builder.append(',');
             }
         }
         return builder.toString();
     }
 
     /**
-     * Returns the reference to highest (top parent) Generated Transfer Object.
-     *
-     * @param childTransportObject
-     *            is generated transfer object which can be extended by other
-     *            generated transfer object
-     * @return in first case that <code>childTransportObject</code> isn't
-     *         extended then <code>childTransportObject</code> is returned. In
-     *         second case the method is recursive called until first case.
-     * @throws IllegalArgumentException
-     *             if <code>childTransportObject</code> equals <code>null</code>
-     */
-    static GeneratedTransferObject getTopParentTransportObject(final GeneratedTransferObject childTransportObject) {
-        if (childTransportObject == null) {
-            throw new IllegalArgumentException("Parameter childTransportObject can't be null.");
-        }
-        if (childTransportObject.getSuperType() == null) {
-            return childTransportObject;
-        } else {
-            return getTopParentTransportObject(childTransportObject.getSuperType());
-        }
-    }
-
-    /**
-     * Selects from input list of properties only those which have read only
-     * attribute set to true.
+     * Selects from input list of properties only those which have read only attribute set to true.
      *
-     * @param properties
-     *            list of properties of generated transfer object
-     * @return subset of <code>properties</code> which have read only attribute
-     *         set to true
+     * @param properties list of properties of generated transfer object
+     * @return subset of <code>properties</code> which have read only attribute set to true
      */
     static List<GeneratedProperty> resolveReadOnlyPropertiesFromTO(final List<GeneratedProperty> properties) {
         List<GeneratedProperty> readOnlyProperties = new ArrayList<>();
@@ -383,16 +308,12 @@ public final class GeneratorUtil {
     }
 
     /**
-     * Returns the list of the read only properties of all extending generated
-     * transfer object from <code>genTO</code> to highest parent generated
-     * transfer object
+     * Returns the list of the read only properties of all extending generated transfer object from <code>genTO</code>
+     * to highest parent generated transfer object.
      *
-     * @param genTO
-     *            generated transfer object for which is the list of read only
-     *            properties generated
-     * @return list of all read only properties from actual to highest parent
-     *         generated transfer object. In case when extension exists the
-     *         method is recursive called.
+     * @param genTO generated transfer object for which is the list of read only properties generated
+     * @return list of all read only properties from actual to highest parent generated transfer object. In case when
+     *         extension exists the method is recursive called.
      */
     static List<GeneratedProperty> getPropertiesOfAllParents(final GeneratedTransferObject genTO) {
         List<GeneratedProperty> propertiesOfAllParents = new ArrayList<>();
@@ -404,4 +325,4 @@ public final class GeneratorUtil {
         }
         return propertiesOfAllParents;
     }
-}
\ No newline at end of file
+}