BUG-1485: store patterns in an array
[yangtools.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / ClassTemplate.xtend
index 8d20831242fc1be9149a80811227def001523ecc..6da9c0a7b199e216d5aade24feec09e65e49d57d 100644 (file)
@@ -26,6 +26,7 @@ import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
 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
 
 /**
@@ -126,6 +127,10 @@ class ClassTemplate extends BaseTemplate {
                 «ENDIF»
             «ENDFOR»
 
+            «IF (genTO.isTypedef() && genTO.getBaseType instanceof BitsTypeDefinition)»
+                «generateGetValueForBitsTypeDef»
+            «ENDIF»
+
             «generateHashCode»
 
             «generateEquals»
@@ -137,11 +142,33 @@ class ClassTemplate extends BaseTemplate {
             «generateRangeMethod("range", "_range")»
 
         }
+
+    '''
+
+    /**
+     * Template method which generates the method <code>getValue()</code> for typedef,
+     * which base type is BitsDefinition.
+     *
+     * @return string with the <code>getValue()</code> method definition in JAVA format
+     */
+    def protected generateGetValueForBitsTypeDef() '''
+
+        public boolean[] getValue() {
+            return new boolean[]{
+            «FOR property: genTO.properties SEPARATOR ','»
+                 «property.fieldName»
+            «ENDFOR»
+            };
+        }
     '''
 
     def private generateLengthMethod(String methodName, String varName) '''
         «IF restrictions != null && !(restrictions.lengthConstraints.empty)»
             «val numberClass = restrictions.lengthConstraints.iterator.next.min.class»
+            /**
+             * @deprecated This method is slated for removal in a future release. See BUG-1485 for details.
+             */
+            @Deprecated
             public static «List.importedName»<«Range.importedName»<«numberClass.importedNumber»>> «methodName»() {
                 return «varName»;
             }
@@ -151,6 +178,10 @@ class ClassTemplate extends BaseTemplate {
     def private generateRangeMethod(String methodName, String varName) '''
         «IF restrictions != null && !(restrictions.rangeConstraints.empty)»
             «val returnType = allProperties.iterator.next.returnType»
+            /**
+             * @deprecated This method is slated for removal in a future release. See BUG-1485 for details.
+             */
+            @Deprecated
             public static «List.importedName»<«Range.importedName»<«returnType.importedNumber»>> «methodName»() {
                 return «varName»;
             }
@@ -166,7 +197,7 @@ class ClassTemplate extends BaseTemplate {
         «IF !enclosedGeneratedTypes.empty»
             «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
                 «IF (innerClass instanceof GeneratedTransferObject)»
-                    «val classTemplate = new ClassTemplate(innerClass as GeneratedTransferObject
+                    «val classTemplate = new ClassTemplate(innerClass)»
                     «classTemplate.generateAsInnerClass»
 
                 «ENDIF»
@@ -273,7 +304,11 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
 
         «FOR p : properties»
+            «IF p.returnType.importedName.contains("[]")»
+            this.«p.fieldName» = «p.fieldName» == null ? null : «p.fieldName».clone();
+            «ELSE»
             this.«p.fieldName» = «p.fieldName»;
+            «ENDIF»
         «ENDFOR»
     }
 
@@ -437,12 +472,11 @@ class ClassTemplate extends BaseTemplate {
                 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
                     «val cValue = c.value»
                     «IF cValue instanceof List<?>»
-                        «val cValues = cValue as List<?>»
-                        private static final «List.importedName»<«Pattern.importedName»> «Constants.MEMBER_PATTERN_LIST»;
+                        private static final «Pattern.importedName»[] «Constants.MEMBER_PATTERN_LIST»;
                         public static final «List.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «ImmutableList.importedName».of(«
-                        FOR v : cValues SEPARATOR ", "»«
+                        FOR v : cValue SEPARATOR ", "»«
                             IF v instanceof String»"«
-                                v as String»"«
+                                v»"«
                             ENDIF»«
                         ENDFOR»);
 
@@ -462,12 +496,13 @@ class ClassTemplate extends BaseTemplate {
      */
     def protected generateStaticInicializationBlock() '''
         static {
-            final «List.importedName»<«Pattern.importedName»> l = new «ArrayList.importedName»<«Pattern.importedName»>();
+            final «Pattern.importedName» a[] = new «Pattern.importedName»[«TypeConstants.PATTERN_CONSTANT_NAME».size()];
+            int i = 0;
             for (String regEx : «TypeConstants.PATTERN_CONSTANT_NAME») {
-                l.add(Pattern.compile(regEx));
+                a[i++] = Pattern.compile(regEx);
             }
 
-            «Constants.MEMBER_PATTERN_LIST» = «ImmutableList.importedName».copyOf(l);
+            «Constants.MEMBER_PATTERN_LIST» = a;
         }
     '''
 
@@ -490,7 +525,7 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
         «IF !properties.empty»
             «FOR f : properties»
-                «IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»;
+                private«IF f.readOnly» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
             «ENDFOR»
         «ENDIF»
     '''