Refactored yang-model-parser-impl to improve readability. SchemaContextImpl moved...
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / controller / sal / java / api / generator / GeneratorUtil.java
index 9e9a3e5df1dcabb339e250ff1ec0ba5c11e78153..b85e2b0ed7ed78f1a43c23a2bf207f8c56df5b0e 100644 (file)
@@ -7,28 +7,13 @@
  */
 package org.opendaylight.controller.sal.java.api.generator;
 
-import static org.opendaylight.controller.sal.java.api.generator.Constants.CLASS;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.COMMA;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.ENUM;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.FINAL;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.GAP;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.IFC;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.LB;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.LCB;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.NL;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.PKG;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.PRIVATE;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.PUBLIC;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.RB;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.RCB;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.SC;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.STATIC;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.TAB;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.EXTENDS;
-import static org.opendaylight.controller.sal.java.api.generator.Constants.IMPLEMENTS;
+import static org.opendaylight.controller.sal.java.api.generator.Constants.*;
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.opendaylight.controller.sal.binding.model.api.AnnotationType;
 import org.opendaylight.controller.sal.binding.model.api.Constant;
@@ -47,23 +32,30 @@ public class GeneratorUtil {
     private GeneratorUtil() {
     }
 
-    public static String createIfcDeclarationWithPkgName(
-            final GeneratedType genType, final String indent) {
-        return createFileDeclarationWithPkgName(IFC, genType, indent);
+    public static String createIfcDeclaration(final GeneratedType genType,
+            final String indent,
+            final Map<String, LinkedHashMap<String, Integer>> availableImports) {
+        return createFileDeclaration(IFC, genType, indent, availableImports);
     }
 
-    public static String createClassDeclarationWithPkgName(
-            final GeneratedTransferObject genTransferObject, final String indent) {
-        return createFileDeclarationWithPkgName(CLASS, genTransferObject,
-                indent);
+    public static String createClassDeclaration(
+            final GeneratedTransferObject genTransferObject,
+            final String indent,
+            final Map<String, LinkedHashMap<String, Integer>> availableImports) {
+        return createFileDeclaration(CLASS, genTransferObject, indent,
+                availableImports);
     }
 
-    private static String createFileDeclarationWithPkgName(final String type,
-            final GeneratedType genType, final String indent) {
+    public static String createPackageDeclaration(final String packageName) {
+        return PKG + GAP + packageName + SC;
+    }
+
+    private static String createFileDeclaration(final String type,
+            final GeneratedType genType, final String indent,
+            final Map<String, LinkedHashMap<String, Integer>> availableImports) {
         final StringBuilder builder = new StringBuilder();
-        builder.append(PKG + GAP + genType.getPackageName() + SC);
-        builder.append(NL);
-        builder.append(NL);
+        final String currentPkg = genType.getPackageName();
+
         createComment(builder, genType.getComment(), indent);
 
         if (!genType.getAnnotations().isEmpty()) {
@@ -73,7 +65,6 @@ public class GeneratorUtil {
         }
         builder.append(PUBLIC + GAP + type + GAP + genType.getName() + GAP);
 
-        final List<Type> genImplements = genType.getImplements();
         if (genType instanceof GeneratedTransferObject) {
             GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
 
@@ -82,16 +73,21 @@ public class GeneratorUtil {
                 builder.append(genTO.getExtends() + GAP);
             }
         }
+
+        final List<Type> genImplements = genType.getImplements();
         if (!genImplements.isEmpty()) {
             if (genType instanceof GeneratedTransferObject) {
                 builder.append(IMPLEMENTS + GAP);
             } else {
                 builder.append(EXTENDS + GAP);
             }
-            builder.append(getExplicitType(genImplements.get(0)));
+            builder.append(getExplicitType(genImplements.get(0),
+                    availableImports, currentPkg));
+
             for (int i = 1; i < genImplements.size(); ++i) {
                 builder.append(", ");
-                builder.append(getExplicitType(genImplements.get(i)));
+                builder.append(getExplicitType(genImplements.get(i),
+                        availableImports, currentPkg));
             }
         }
 
@@ -155,18 +151,22 @@ public class GeneratorUtil {
     }
 
     public static String createConstant(final Constant constant,
-            final String indent) {
+            final String indent,
+            final Map<String, LinkedHashMap<String, Integer>> availableImports,
+            final String currentPkg) {
         final StringBuilder builder = new StringBuilder();
         builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);
-        builder.append(getExplicitType(constant.getType()) + GAP
-                + constant.getName());
+        builder.append(getExplicitType(constant.getType(), availableImports,
+                currentPkg) + GAP + constant.getName());
         builder.append(GAP + "=" + GAP);
         builder.append(constant.getValue() + SC);
         return builder.toString();
     }
 
     public static String createField(final GeneratedProperty property,
-            final String indent) {
+            final String indent,
+            Map<String, LinkedHashMap<String, Integer>> availableImports,
+            final String currentPkg) {
         final StringBuilder builder = new StringBuilder();
         builder.append(indent);
         if (!property.getAnnotations().isEmpty()) {
@@ -175,23 +175,28 @@ public class GeneratorUtil {
             builder.append(NL);
         }
         builder.append(indent + PRIVATE + GAP);
-        builder.append(getExplicitType(property.getReturnType()) + GAP
-                + property.getName());
+        builder.append(getExplicitType(property.getReturnType(),
+                availableImports, currentPkg) + GAP + property.getName());
         builder.append(SC);
         return builder.toString();
     }
 
     /**
      * Create method declaration in interface.
-     * 
+     *
      * @param method
      * @param indent
      * @return
      */
     public static String createMethodDeclaration(final MethodSignature method,
-            final String indent) {
+            final String indent,
+            Map<String, LinkedHashMap<String, Integer>> availableImports,
+            final String currentPkg) {
+        final StringBuilder builder = new StringBuilder();
+
         if (method == null) {
-            throw new IllegalArgumentException("Method Signature parameter MUST be specified and cannot be NULL!");
+            throw new IllegalArgumentException(
+                    "Method Signature parameter MUST be specified and cannot be NULL!");
         }
 
         final String comment = method.getComment();
@@ -202,12 +207,12 @@ public class GeneratorUtil {
 
         final Type type = method.getReturnType();
         if (type == null) {
-            throw new IllegalStateException("Method Return type cannot be NULL!");
+            throw new IllegalStateException(
+                    "Method Return type cannot be NULL!");
         }
 
         final List<Parameter> parameters = method.getParameters();
 
-        final StringBuilder builder = new StringBuilder();
         createComment(builder, comment, indent);
         builder.append(NL);
         builder.append(indent);
@@ -218,7 +223,9 @@ public class GeneratorUtil {
             builder.append(NL);
         }
 
-        builder.append(indent + getExplicitType(type) + GAP + name);
+        builder.append(indent
+                + getExplicitType(type, availableImports, currentPkg) + GAP
+                + name);
         builder.append(LB);
         for (int i = 0; i < parameters.size(); i++) {
             Parameter p = parameters.get(i);
@@ -226,8 +233,8 @@ public class GeneratorUtil {
             if (i + 1 == parameters.size()) {
                 separator = "";
             }
-            builder.append(getExplicitType(p.getType()) + GAP + p.getName()
-                    + separator);
+            builder.append(getExplicitType(p.getType(), availableImports,
+                    currentPkg) + GAP + p.getName() + separator);
         }
         builder.append(RB);
         builder.append(SC);
@@ -236,9 +243,11 @@ public class GeneratorUtil {
     }
 
     public static String createConstructor(
-            GeneratedTransferObject genTransferObject, final String indent) {
+            GeneratedTransferObject genTransferObject, final String indent,
+            Map<String, LinkedHashMap<String, Integer>> availableImports) {
         final StringBuilder builder = new StringBuilder();
 
+        final String currentPkg = genTransferObject.getPackageName();
         final List<GeneratedProperty> properties = genTransferObject
                 .getProperties();
         final List<GeneratedProperty> ctorParams = new ArrayList<GeneratedProperty>();
@@ -247,21 +256,23 @@ public class GeneratorUtil {
                 ctorParams.add(property);
             }
         }
-        
+
         builder.append(indent);
         builder.append(PUBLIC);
         builder.append(GAP);
         builder.append(genTransferObject.getName());
         builder.append(LB);
-        
+
         if (!ctorParams.isEmpty()) {
-            builder.append(getExplicitType(ctorParams.get(0).getReturnType()));
+            builder.append(getExplicitType(ctorParams.get(0).getReturnType(),
+                    availableImports, currentPkg));
             builder.append(" ");
             builder.append(ctorParams.get(0).getName());
             for (int i = 1; i < ctorParams.size(); ++i) {
                 final GeneratedProperty param = ctorParams.get(i);
                 builder.append(", ");
-                builder.append(getExplicitType(param.getReturnType()));
+                builder.append(getExplicitType(param.getReturnType(),
+                        availableImports, currentPkg));
                 builder.append(GAP);
                 builder.append(param.getName());
             }
@@ -285,7 +296,9 @@ public class GeneratorUtil {
     }
 
     public static String createGetter(final GeneratedProperty property,
-            final String indent) {
+            final String indent,
+            Map<String, LinkedHashMap<String, Integer>> availableImports,
+            final String currentPkg) {
         final StringBuilder builder = new StringBuilder();
 
         final Type type = property.getReturnType();
@@ -293,7 +306,8 @@ public class GeneratorUtil {
         final char first = Character.toUpperCase(varName.charAt(0));
         final String methodName = "get" + first + varName.substring(1);
 
-        builder.append(indent + PUBLIC + GAP + getExplicitType(type) + GAP
+        builder.append(indent + PUBLIC + GAP
+                + getExplicitType(type, availableImports, currentPkg) + GAP
                 + methodName);
         builder.append(LB + RB + LCB + NL);
 
@@ -304,9 +318,11 @@ public class GeneratorUtil {
         builder.append(indent + RCB);
         return builder.toString();
     }
-    
+
     public static String createSetter(final GeneratedProperty property,
-            final String indent) {
+            final String indent,
+            Map<String, LinkedHashMap<String, Integer>> availableImports,
+            String currentPkg) {
         final StringBuilder builder = new StringBuilder();
 
         final Type type = property.getReturnType();
@@ -314,11 +330,12 @@ public class GeneratorUtil {
         final char first = Character.toUpperCase(varName.charAt(0));
         final String methodName = "set" + first + varName.substring(1);
 
-        builder.append(indent + PUBLIC + GAP + "void" + GAP
-                + methodName);
-        builder.append(LB + getExplicitType(type) + GAP + varName + RB + LCB + NL);
+        builder.append(indent + PUBLIC + GAP + "void" + GAP + methodName);
+        builder.append(LB + getExplicitType(type, availableImports, currentPkg)
+                + GAP + varName + RB + LCB + NL);
         String currentIndent = indent + TAB;
-        builder.append(currentIndent + "this." + varName + " = " + varName + SC + NL);
+        builder.append(currentIndent + "this." + varName + " = " + varName + SC
+                + NL);
         builder.append(indent + RCB);
         return builder.toString();
     }
@@ -329,7 +346,7 @@ public class GeneratorUtil {
         builder.append(indent + "public int hashCode() {" + NL);
         builder.append(indent + TAB + "final int prime = 31;" + NL);
         builder.append(indent + TAB + "int result = 1;" + NL);
-        
+
         for (GeneratedProperty property : properties) {
             String fieldName = property.getName();
             builder.append(indent + TAB + "result = prime * result + (("
@@ -346,8 +363,8 @@ public class GeneratorUtil {
             final List<GeneratedProperty> properties, final String indent) {
         StringBuilder builder = new StringBuilder();
         final String indent1 = indent + TAB;
-        final String indent2 = indent + TAB + TAB;
-        final String indent3 = indent + TAB + TAB + TAB;
+        final String indent2 = indent1 + TAB;
+        final String indent3 = indent2 + TAB;
 
         builder.append(indent + "public boolean equals(Object obj) {" + NL);
         builder.append(indent1 + "if (this == obj) {" + NL);
@@ -360,7 +377,7 @@ public class GeneratorUtil {
         builder.append(indent2 + "return false;" + NL);
         builder.append(indent1 + "}" + NL);
 
-        String typeStr = type.getPackageName() + "." + type.getName();
+        String typeStr = type.getName();
         builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;"
                 + NL);
 
@@ -461,31 +478,59 @@ public class GeneratorUtil {
         return builder.toString();
     }
 
-    private static String getExplicitType(final Type type) {
+    private static String getExplicitType(final Type type,
+            Map<String, LinkedHashMap<String, Integer>> availableImports,
+            final String currentPkg) {
         if (type == null) {
-            throw new IllegalArgumentException("Type parameter MUST be specified and cannot be NULL!");
+            throw new IllegalArgumentException(
+                    "Type parameter MUST be specified and cannot be NULL!");
         }
         String packageName = type.getPackageName();
-        if (packageName.endsWith(".")) {
-            packageName = packageName.substring(0, packageName.length() - 1);
-        }
 
-        final StringBuilder builder = new StringBuilder(packageName + "."
-                + type.getName());
-        if (type instanceof ParameterizedType) {
-            final ParameterizedType pType = (ParameterizedType) type;
-            Type[] pTypes = pType.getActualTypeArguments();
-            builder.append("<");
-            builder.append(getParameters(pTypes));
-            builder.append(">");
-        }
-        if (builder.toString().equals("java.lang.Void")) {
-            return "void";
+        LinkedHashMap<String, Integer> imports = availableImports.get(type
+                .getName());
+
+        if ((imports != null && packageName
+                .equals(findMaxValue(imports).get(0)))
+                || packageName.equals(currentPkg)) {
+            final StringBuilder builder = new StringBuilder(type.getName());
+            if (type instanceof ParameterizedType) {
+                ParameterizedType pType = (ParameterizedType) type;
+                Type[] pTypes = pType.getActualTypeArguments();
+                builder.append("<");
+                builder.append(getParameters(pTypes, availableImports,
+                        currentPkg));
+                builder.append(">");
+            }
+            if (builder.toString().equals("Void")) {
+                return "void";
+            }
+            return builder.toString();
+        } else {
+            final StringBuilder builder = new StringBuilder();
+            if (packageName.startsWith("java.lang")) {
+                builder.append(type.getName());
+            } else {
+                builder.append(packageName + "." + type.getName());
+            }
+            if (type instanceof ParameterizedType) {
+                ParameterizedType pType = (ParameterizedType) type;
+                Type[] pTypes = pType.getActualTypeArguments();
+                builder.append("<");
+                builder.append(getParameters(pTypes, availableImports,
+                        currentPkg));
+                builder.append(">");
+            }
+            if (builder.toString().equals("Void")) {
+                return "void";
+            }
+            return builder.toString();
         }
-        return builder.toString();
     }
 
-    private static String getParameters(final Type[] pTypes) {
+    private static String getParameters(final Type[] pTypes,
+            Map<String, LinkedHashMap<String, Integer>> availableImports,
+            String currentPkg) {
         final StringBuilder builder = new StringBuilder();
         for (int i = 0; i < pTypes.length; i++) {
             Type t = pTypes[i];
@@ -494,11 +539,30 @@ public class GeneratorUtil {
             if (i + 1 == pTypes.length) {
                 separator = "";
             }
-            builder.append(getExplicitType(t) + separator);
+            builder.append(getExplicitType(t, availableImports, currentPkg)
+                    + separator);
         }
         return builder.toString();
     }
 
+    private static List<String> findMaxValue(
+            LinkedHashMap<String, Integer> imports) {
+        final List<String> result = new ArrayList<String>();
+
+        int maxValue = 0;
+        int currentValue = 0;
+        for (Map.Entry<String, Integer> entry : imports.entrySet()) {
+            currentValue = entry.getValue();
+            if (currentValue > maxValue) {
+                result.clear();
+                result.add(entry.getKey());
+            } else if (currentValue == maxValue) {
+                result.add(entry.getKey());
+            }
+        }
+        return result;
+    }
+
     private static void createComment(final StringBuilder builder,
             final String comment, final String indent) {
         if (comment != null && comment.length() > 0) {
@@ -508,4 +572,101 @@ public class GeneratorUtil {
         }
     }
 
+    public static Map<String, LinkedHashMap<String, Integer>> createImports(
+            GeneratedType genType) {
+        final Map<String, LinkedHashMap<String, Integer>> imports = new HashMap<String, LinkedHashMap<String, Integer>>();
+        final String genTypePkg = genType.getPackageName();
+
+        final List<Constant> constants = genType.getConstantDefinitions();
+        final List<MethodSignature> methods = genType.getMethodDefinitions();
+        List<Type> impl = genType.getImplements();
+
+        // IMPLEMENTATIONS
+        if (impl != null) {
+            for (Type t : impl) {
+                addTypeToImports(t, imports, genTypePkg);
+            }
+        }
+
+        // CONSTANTS
+        if (constants != null) {
+            for (Constant c : constants) {
+                Type ct = c.getType();
+                addTypeToImports(ct, imports, genTypePkg);
+            }
+        }
+
+        // METHODS
+        if (methods != null) {
+            for (MethodSignature m : methods) {
+                Type ct = m.getReturnType();
+                addTypeToImports(ct, imports, genTypePkg);
+                for (MethodSignature.Parameter p : m.getParameters()) {
+                    addTypeToImports(p.getType(), imports, genTypePkg);
+                }
+            }
+        }
+
+        // PROPERTIES
+        if (genType instanceof GeneratedTransferObject) {
+            GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
+
+            List<GeneratedProperty> props = genTO.getProperties();
+            if (props != null) {
+                for (GeneratedProperty prop : props) {
+                    Type pt = prop.getReturnType();
+                    addTypeToImports(pt, imports, genTypePkg);
+                }
+            }
+        }
+
+        return imports;
+    }
+
+    private static void addTypeToImports(Type type,
+            Map<String, LinkedHashMap<String, Integer>> importedTypes,
+            String genTypePkg) {
+        String typeName = type.getName();
+        String typePkg = type.getPackageName();
+        if (typePkg.startsWith("java.lang") || typePkg.equals(genTypePkg)) {
+            return;
+        }
+        LinkedHashMap<String, Integer> packages = importedTypes.get(typeName);
+        if (packages == null) {
+            packages = new LinkedHashMap<String, Integer>();
+            packages.put(typePkg, 1);
+            importedTypes.put(typeName, packages);
+        } else {
+            Integer occurrence = packages.get(typePkg);
+            if (occurrence == null) {
+                packages.put(typePkg, 1);
+            } else {
+                occurrence++;
+                packages.put(typePkg, occurrence);
+            }
+        }
+
+        if (type instanceof ParameterizedType) {
+            ParameterizedType pt = (ParameterizedType) type;
+            Type[] params = pt.getActualTypeArguments();
+            for (Type param : params) {
+                addTypeToImports(param, importedTypes, genTypePkg);
+            }
+        }
+    }
+
+    public static List<String> createImportLines(
+            Map<String, LinkedHashMap<String, Integer>> imports) {
+        List<String> importLines = new ArrayList<String>();
+
+        for (Map.Entry<String, LinkedHashMap<String, Integer>> entry : imports
+                .entrySet()) {
+            String typeName = entry.getKey();
+            LinkedHashMap<String, Integer> typePkgMap = entry.getValue();
+            String typePkg = typePkgMap.keySet().iterator().next();
+            importLines.add("import " + typePkg + "." + typeName + SC);
+        }
+        return importLines;
+    }
+
 }