Merge "BUG-532: implemented copy constructor for generated builder classes."
[mdsal.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / ClassTemplate.xtend
index fd033e4c74ade1dd93512759a0ed3740aef76c97..eb5b94c5da742df982350567d99606116e351d2a 100644 (file)
@@ -125,9 +125,9 @@ class ClassTemplate extends BaseTemplate {
 
             «generateToString(genTO.toStringIdentifiers)»
 
-            «generateGetLength»
+            «generateLengthMethod("length", genTO, genTO.importedName, "_length")»
 
-            «generateGetRange»
+            «generateRangeMethod("range", genTO.restrictions, genTO.importedName, "_range", allProperties)»
 
         }
     '''
@@ -164,7 +164,7 @@ class ClassTemplate extends BaseTemplate {
             «parentConstructor»
         «ENDIF»
     '''
-    
+
     def protected allValuesConstructor() '''
     «IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
         @«ConstructorProperties.importedName»("value")
@@ -365,6 +365,17 @@ class ClassTemplate extends BaseTemplate {
      * @return string with the class attributes in JAVA format
      */
     def protected generateFields() '''
+        «IF restrictions != null»
+            «val prop = getPropByName("value")»
+            «IF prop != null»
+                «IF !(restrictions.lengthConstraints.empty)»
+                    private static «List.importedName»<«Range.importedName»<«prop.returnType.importedNumber»>> _length;
+                «ENDIF»
+                «IF !(restrictions.rangeConstraints.empty)»
+                    private static «List.importedName»<«Range.importedName»<«prop.returnType.importedNumber»>> _range;
+                «ENDIF»
+            «ENDIF»
+        «ENDIF»
         «IF !properties.empty»
             «FOR f : properties»
                 «IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»;
@@ -372,7 +383,6 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
-
     /**
      * Template method which generates the method <code>hashCode()</code>.
      *
@@ -434,30 +444,13 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
-    def private generateGetLength() '''
-        «IF restrictions != null && !(restrictions.lengthConstraints.empty)»
-            «val clazz = restrictions.lengthConstraints.iterator.next.min.class»
-            public static «List.importedName»<«Range.importedName»<«clazz.importedName»>> length() {
-                final «List.importedName»<«Range.importedName»<«clazz.importedName»>> result = new «ArrayList.importedName»<>();
-                «FOR r : restrictions.lengthConstraints»
-                    result.add(«Range.importedName».closed(new «clazz.importedName»("«r.min»"), new «clazz.importedName»("«r.max»")));
-                «ENDFOR»
-                return result;
+    def GeneratedProperty getPropByName(String name) {
+        for (GeneratedProperty prop : allProperties) {
+            if (prop.name.equals(name)) {
+                return prop;
             }
-        «ENDIF»
-    '''
-
-    def private generateGetRange() '''
-        «IF restrictions != null && !(restrictions.rangeConstraints.empty)»
-            «val clazz = restrictions.rangeConstraints.iterator.next.min.class»
-            public static «List.importedName»<«Range.importedName»<«clazz.importedName»>> range() {
-                final «List.importedName»<«Range.importedName»<«clazz.importedName»>> result = new «ArrayList.importedName»<>();
-                «FOR r : restrictions.rangeConstraints»
-                    result.add(«Range.importedName».closed(new «clazz.importedName»("«r.min»"), new «clazz.importedName»("«r.max»")));
-                «ENDFOR»
-                return result;
-            }
-        «ENDIF»
-    '''
+        }
+        return null;
+    }
 
 }