BUG-1485: Move restriction generation 61/21961/2
authorRobert Varga <rovarga@cisco.com>
Wed, 27 May 2015 19:24:59 +0000 (21:24 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 5 Jun 2015 21:08:32 +0000 (21:08 +0000)
It is only used in the class template, so move it and make it private to
increase clarity.

Change-Id: I751c4a5a0af06dd89a79ce43546ec9c12490f166
Signed-off-by: Robert Varga <rovarga@cisco.com>
(cherry picked from commit 76c57ce16f7b1b90325214e05f916bc486ad792d)

code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BaseTemplate.xtend
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend

index d98b52fc471bd2b6ac4f152cae09be14d1dc6a20..c93f428131430a53f47be3459e83f4c502b700be 100644 (file)
@@ -328,50 +328,6 @@ abstract class BaseTemplate {
         return sb.toString
     }
 
-    def generateRestrictions(Type type, String paramName, Type returnType) '''
-        «val restrictions = type.getRestrictions»
-        «IF restrictions !== null»
-            «val boolean isNestedType = !(returnType instanceof ConcreteType)»
-            «IF !restrictions.lengthConstraints.empty»
-                «generateLengthRestriction(returnType, restrictions, paramName, isNestedType)»
-            «ENDIF»
-            «IF !restrictions.rangeConstraints.empty»
-                «generateRangeRestriction(returnType, paramName, isNestedType)»
-            «ENDIF»
-        «ENDIF»
-    '''
-
-    def private generateLengthRestriction(Type returnType, Restrictions restrictions, String paramName, boolean isNestedType) '''
-        «val clazz = restrictions.lengthConstraints.iterator.next.min.class»
-        if («paramName» != null) {
-            «printLengthConstraint(returnType, clazz, paramName, isNestedType, returnType.name.contains("["))»
-            boolean isValidLength = false;
-            for («Range.importedName»<«clazz.importedNumber»> r : «IF isNestedType»«returnType.importedName».«ENDIF»length()) {
-                if (r.contains(_constraint)) {
-                    isValidLength = true;
-                }
-            }
-            if (!isValidLength) {
-                throw new IllegalArgumentException(String.format("Invalid length: %s, expected: %s.", «paramName», «IF isNestedType»«returnType.importedName».«ENDIF»length()));
-            }
-        }
-    '''
-
-    def private generateRangeRestriction(Type returnType, String paramName, boolean isNestedType) '''
-        if («paramName» != null) {
-            «printRangeConstraint(returnType, paramName, isNestedType)»
-            boolean isValidRange = false;
-            for («Range.importedName»<«returnType.importedNumber»> r : «IF isNestedType»«returnType.importedName».«ENDIF»range()) {
-                if (r.contains(_constraint)) {
-                    isValidRange = true;
-                }
-            }
-            if (!isValidRange) {
-                throw new IllegalArgumentException(String.format("Invalid range: %s, expected: %s.", «paramName», «IF isNestedType»«returnType.importedName».«ENDIF»range()));
-            }
-        }
-    '''
-
     /**
      * Print length constraint.
      * This should always be a BigInteger (only string and binary can have length restriction)
index 0a4d242f52f5b67062504b00e082af59c35c5e4e..a3b7827ab44b10c12afbc574a9c5d2c910336bc5 100644 (file)
@@ -28,6 +28,8 @@ import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
 import com.google.common.base.Preconditions
+import org.opendaylight.yangtools.sal.binding.model.api.Type
+import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
 
 /**
  * Template for generating JAVA class.
@@ -334,6 +336,50 @@ class ClassTemplate extends BaseTemplate {
     }
     '''
 
+    def private generateRestrictions(Type type, String paramName, Type returnType) '''
+        «val restrictions = type.getRestrictions»
+        «IF restrictions !== null»
+            «val boolean isNestedType = !(returnType instanceof ConcreteType)»
+            «IF !restrictions.lengthConstraints.empty»
+                «generateLengthRestriction(returnType, restrictions, paramName, isNestedType)»
+            «ENDIF»
+            «IF !restrictions.rangeConstraints.empty»
+                «generateRangeRestriction(returnType, paramName, isNestedType)»
+            «ENDIF»
+        «ENDIF»
+    '''
+
+    def private generateLengthRestriction(Type returnType, Restrictions restrictions, String paramName, boolean isNestedType) '''
+        «val clazz = restrictions.lengthConstraints.iterator.next.min.class»
+        if («paramName» != null) {
+            «printLengthConstraint(returnType, clazz, paramName, isNestedType, returnType.name.contains("["))»
+            boolean isValidLength = false;
+            for («Range.importedName»<«clazz.importedNumber»> r : «IF isNestedType»«returnType.importedName».«ENDIF»length()) {
+                if (r.contains(_constraint)) {
+                    isValidLength = true;
+                }
+            }
+            if (!isValidLength) {
+                throw new IllegalArgumentException(String.format("Invalid length: %s, expected: %s.", «paramName», «IF isNestedType»«returnType.importedName».«ENDIF»length()));
+            }
+        }
+    '''
+
+    def private generateRangeRestriction(Type returnType, String paramName, boolean isNestedType) '''
+        if («paramName» != null) {
+            «printRangeConstraint(returnType, paramName, isNestedType)»
+            boolean isValidRange = false;
+            for («Range.importedName»<«returnType.importedNumber»> r : «IF isNestedType»«returnType.importedName».«ENDIF»range()) {
+                if (r.contains(_constraint)) {
+                    isValidRange = true;
+                }
+            }
+            if (!isValidRange) {
+                throw new IllegalArgumentException(String.format("Invalid range: %s, expected: %s.", «paramName», «IF isNestedType»«returnType.importedName».«ENDIF»range()));
+            }
+        }
+    '''
+
     def protected copyConstructor() '''
     /**
      * Creates a copy from Source Object.