Added support to generate interfaces from Choices and Cases.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / controller / sal / java / api / generator / ClassCodeGenerator.java
index aaeec40604a61c264afef7d23e5f7111b115a1ef..b8f988bff395354d46e7cd4049f434cd400bf070 100644 (file)
@@ -12,33 +12,28 @@ import static org.opendaylight.controller.sal.java.api.generator.Constants.*;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.opendaylight.controller.sal.binding.model.api.CodeGenerator;
-import org.opendaylight.controller.sal.binding.model.api.Enumeration;
-import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
-import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferIdentityObject;
-import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
-import org.opendaylight.controller.sal.binding.model.api.Type;
+import org.opendaylight.controller.binding.generator.util.TypeConstants;
+import org.opendaylight.controller.sal.binding.model.api.*;
 
 public final class ClassCodeGenerator implements CodeGenerator {
 
-    private Map<String, LinkedHashMap<String, Integer>> imports;
+    private Map<String, String> imports;
 
     @Override
     public Writer generate(Type type) throws IOException {
         final Writer writer = new StringWriter();
-        boolean isIdentity = type instanceof GeneratedTransferIdentityObject;
 
         if (type instanceof GeneratedTransferObject) {
-            GeneratedTransferObject genTO = (GeneratedTransferObject) type;            
+            GeneratedTransferObject genTO = (GeneratedTransferObject) type;
             imports = GeneratorUtil.createImports(genTO);
-            
+
             final String currentPkg = genTO.getPackageName();
             final List<GeneratedProperty> fields = genTO.getProperties();
-            final List<Enumeration> enums = genTO.getEnumDefintions();
+            final List<Enumeration> enums = genTO.getEnumerations();
+            final List<Constant> consts = genTO.getConstantDefinitions();
 
             writer.write(GeneratorUtil.createPackageDeclaration(currentPkg));
             writer.write(NL);
@@ -49,61 +44,66 @@ public final class ClassCodeGenerator implements CodeGenerator {
             }
             writer.write(NL);
 
-            writer.write(GeneratorUtil.createClassDeclaration(genTO, "",
-                    imports, isIdentity));
+            writer.write(GeneratorUtil.createClassDeclaration(genTO, "", imports, genTO.isAbstract()));
             writer.write(NL);
             writer.write(NL);
-            
-            if (enums != null) {           
-                       EnumGenerator enumGenerator = new EnumGenerator();
-               for ( Enumeration e : enums ) {                         
-                       writer.write(enumGenerator.generateInnerEnumeration(e, TAB).toString());
-                       writer.write(NL);
-               }
+
+            if (consts != null) {
+                for (Constant con : consts) {
+                    writer.write(GeneratorUtil.createConstant(con, TAB, imports, currentPkg));
+                    writer.write(NL);
+                }
             }
 
-            if (fields != null) {
-                for (GeneratedProperty field : fields) {
-                    writer.write(GeneratorUtil.createField(field, TAB, imports,
-                            currentPkg) + NL);
+            if (enums != null) {
+                EnumGenerator enumGenerator = new EnumGenerator();
+                for (Enumeration e : enums) {
+                    writer.write(enumGenerator.generateInnerEnumeration(e, TAB).toString());
+                    writer.write(NL);
                 }
-                writer.write(NL);
-                writer.write(GeneratorUtil.createConstructor(genTO, TAB,
-                        imports, isIdentity) + NL);
-                writer.write(NL);
-                for (GeneratedProperty field : fields) {
-                    writer.write(GeneratorUtil.createGetter(field, TAB,
-                            imports, currentPkg) + NL);
-                    if (!field.isReadOnly()) {
-                        writer.write(GeneratorUtil.createSetter(field, TAB,
-                                imports, currentPkg) + NL);
+            }
+
+            boolean memberPatternListCodeRequired = false;
+            memberPatternListCodeRequired = (GeneratorUtil.isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, genTO));
+            if (fields != null || memberPatternListCodeRequired) {
+                if (fields != null) {
+                    for (GeneratedProperty field : fields) {
+                        writer.write(GeneratorUtil.createField(field, TAB, imports, currentPkg) + NL);
                     }
                 }
-                writer.write(NL);
+                if (memberPatternListCodeRequired) {
+                    writer.write(TAB + PRIVATE + GAP + "List<Pattern>" + GAP + MEMBER_PATTERN_LIST + GAP + ASSIGN + GAP
+                            + "new ArrayList<Pattern>()" + GAP + SC + NL);
 
-                if (!genTO.getHashCodeIdentifiers().isEmpty()) {
-                    writer.write(GeneratorUtil.createHashCode(
-                            genTO.getHashCodeIdentifiers(), TAB)
-                            + NL);
                 }
+                writer.write(NL);
+                writer.write(GeneratorUtil.createConstructor(genTO, TAB, imports, genTO.isAbstract()) + NL);
+                writer.write(NL);
+                if (fields != null) {
+                    for (GeneratedProperty field : fields) {
+                        writer.write(GeneratorUtil.createGetter(field, TAB, imports, currentPkg) + NL);
+                        if (!field.isReadOnly()) {
+                            writer.write(GeneratorUtil.createSetter(field, TAB, imports, currentPkg) + NL);
+                        }
+                    }
+                    writer.write(NL);
 
-                if (!genTO.getEqualsIdentifiers().isEmpty()) {
-                    writer.write(GeneratorUtil.createEquals(genTO,
-                            genTO.getEqualsIdentifiers(), TAB)
-                            + NL);
-                }
+                    if (!genTO.getHashCodeIdentifiers().isEmpty()) {
+                        writer.write(GeneratorUtil.createHashCode(genTO.getHashCodeIdentifiers(), TAB) + NL);
+                    }
 
-                if (!genTO.getToStringIdentifiers().isEmpty()) {
-                    writer.write(GeneratorUtil.createToString(genTO,
-                            genTO.getToStringIdentifiers(), TAB)
-                            + NL);
+                    if (!genTO.getEqualsIdentifiers().isEmpty()) {
+                        writer.write(GeneratorUtil.createEquals(genTO, genTO.getEqualsIdentifiers(), TAB) + NL);
+                    }
 
-                }
+                    if (!genTO.getToStringIdentifiers().isEmpty()) {
+                        writer.write(GeneratorUtil.createToString(genTO, genTO.getToStringIdentifiers(), TAB) + NL);
+                    }
 
-                writer.write(RCB);
+                    writer.write(RCB);
+                }
             }
         }
         return writer;
     }
-
 }