Import exception references
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / ClassTemplate.xtend
index 3b18d18815c34424358ee347cc25b915f4a5c9af..be2f6e8e034c345e3395036b13c957f869e4ad87 100644 (file)
@@ -8,23 +8,25 @@
 package org.opendaylight.mdsal.binding.java.api.generator
 
 import static java.util.Objects.requireNonNull
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.BINARY_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.BOOLEAN_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.EMPTY_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.INSTANCE_IDENTIFIER
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.INT16_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.INT32_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.INT64_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.INT8_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.STRING_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.UINT16_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.UINT32_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.UINT64_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.UINT8_TYPE
-import static org.opendaylight.mdsal.binding.model.util.BindingTypes.SCALAR_TYPE_OBJECT
-import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN
-import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BINARY_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BOOLEAN_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.DECIMAL64_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.EMPTY_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.INSTANCE_IDENTIFIER
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.INT16_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.INT32_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.INT64_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.INT8_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.STRING_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT16_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT32_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT64_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT8_TYPE
+import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.SCALAR_TYPE_OBJECT
+import static org.opendaylight.mdsal.binding.model.ri.Types.BOOLEAN
+import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
 import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
+import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isBitsType
 
 import com.google.common.base.Preconditions
 import com.google.common.collect.ImmutableList
@@ -33,6 +35,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
 import java.beans.ConstructorProperties
 import java.util.ArrayList
 import java.util.Base64;
+import java.util.Collection
 import java.util.Comparator
 import java.util.List
 import java.util.Map
@@ -46,10 +49,9 @@ import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
 import org.opendaylight.mdsal.binding.model.api.Restrictions
 import org.opendaylight.mdsal.binding.model.api.Type
-import org.opendaylight.mdsal.binding.model.util.TypeConstants
+import org.opendaylight.mdsal.binding.model.ri.TypeConstants
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
 import org.opendaylight.yangtools.yang.common.Empty
-import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
 
 /**
  * Template for generating JAVA class.
@@ -59,6 +61,7 @@ class ClassTemplate extends BaseTemplate {
     static val Comparator<GeneratedProperty> PROP_COMPARATOR = Comparator.comparing([prop | prop.name])
     static val VALUEOF_TYPES = Set.of(
         BOOLEAN_TYPE,
+        DECIMAL64_TYPE,
         INT8_TYPE,
         INT16_TYPE,
         INT32_TYPE,
@@ -146,8 +149,11 @@ class ClassTemplate extends BaseTemplate {
      * @return string with class source code in JAVA format
      */
     def protected generateBody(boolean isInnerClass) '''
-        «wrapToDocumentation(formatDataForJavaDoc(type))»
+        «type.formatDataForJavaDoc.wrapToDocumentation»
         «annotationDeclaration»
+        «IF !isInnerClass»
+            «generatedAnnotation»
+        «ENDIF»
         «generateClassDeclaration(isInnerClass)» {
             «suidDeclaration»
             «innerClassesDeclarations»
@@ -171,7 +177,7 @@ class ClassTemplate extends BaseTemplate {
 
             «propertyMethods»
 
-            «IF (genTO.isTypedef() && genTO.getBaseType instanceof BitsTypeDefinition)»
+            «IF genTO.isBitsType»
                 «generateGetValueForBitsTypeDef»
             «ENDIF»
 
@@ -212,7 +218,7 @@ class ClassTemplate extends BaseTemplate {
     def private scalarTypeObjectValue(GeneratedProperty field) '''
         @«OVERRIDE.importedName»
         public «field.returnType.importedName» «BindingMapping.SCALAR_TYPE_OBJECT_GET_VALUE_NAME»() {
-            return «field.fieldName»«IF field.returnType.name.endsWith("[]")».clone()«ENDIF»;
+            return «field.fieldName»«field.cloneCall»;
         }
     '''
 
@@ -251,10 +257,8 @@ class ClassTemplate extends BaseTemplate {
             «genUnionConstructor»
         «ELSEIF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals(TypeConstants.VALUE_PROP)»
             «typedefConstructor»
-            «legacyConstructor»
         «ELSE»
             «allValuesConstructor»
-            «legacyConstructor»
         «ENDIF»
 
         «IF !allProperties.empty»
@@ -265,7 +269,7 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
-    def private allValuesConstructor() '''
+    def allValuesConstructor() '''
     public «type.name»(«allProperties.asArgumentsDeclaration») {
         «IF !parentProperties.empty»
             super(«parentProperties.asArguments»);
@@ -304,38 +308,11 @@ class ClassTemplate extends BaseTemplate {
 
         «FOR p : properties»
             «val fieldName = p.fieldName»
-            «IF p.returnType.name.endsWith("[]")»
-                this.«fieldName» = «fieldName».clone();
-            «ELSE»
-                this.«fieldName» = «fieldName»;
-            «ENDIF»
+            this.«fieldName» = «fieldName»«p.cloneCall»;
         «ENDFOR»
     }
     '''
 
-    def private legacyConstructor() {
-        if (!hasUintProperties) {
-            return ""
-        }
-
-        val compatUint = CODEHELPERS.importedName + ".compatUint("
-        return '''
-
-            /**
-             * Utility migration constructor.
-             *
-             «FOR prop : allProperties»
-             * @param «prop.fieldName» «prop.name»«IF prop.isUintType» in legacy Java type«ENDIF»
-             «ENDFOR»
-             * @deprecated Use {#link «type.name»(«FOR prop : allProperties SEPARATOR ", "»«prop.returnType.importedJavadocName»«ENDFOR»)} instead.
-             */
-            @Deprecated(forRemoval = true)
-            public «type.getName»(«FOR prop : allProperties SEPARATOR ", "»«prop.legacyType.importedName» «prop.fieldName»«ENDFOR») {
-                this(«FOR prop : allProperties SEPARATOR ", "»«IF prop.isUintType»«compatUint»«prop.fieldName»)«ELSE»«prop.fieldName»«ENDIF»«ENDFOR»);
-            }
-        '''
-    }
-
     def protected genUnionConstructor() '''
     «FOR p : allProperties»
         «val List<GeneratedProperty> other = new ArrayList(properties)»
@@ -377,7 +354,7 @@ class ClassTemplate extends BaseTemplate {
         }
     }
 
-    def private generateRestrictions(Type type, String paramName, Type returnType) '''
+    def generateRestrictions(Type type, String paramName, Type returnType) '''
         «val restrictions = type.restrictions»
         «IF restrictions !== null»
             «IF restrictions.lengthConstraint.present || restrictions.rangeConstraint.present»
@@ -438,7 +415,7 @@ class ClassTemplate extends BaseTemplate {
                     return new «genTO.name»(«Base64.importedName».getDecoder().decode(defaultValue));
                 «ELSEIF EMPTY_TYPE.equals(propType)»
                     «Preconditions.importedName».checkArgument(defaultValue.isEmpty(), "Invalid value %s", defaultValue);
-                    return new «genTO.name»(«Empty.importedName».getInstance());
+                    return new «genTO.name»(«Empty.importedName».value());
                 «ELSE»
                     return new «genTO.name»(new «propType.importedName»(defaultValue));
                 «ENDIF»
@@ -451,7 +428,7 @@ class ClassTemplate extends BaseTemplate {
     def protected bitsArgs() '''
         «JU_LIST.importedName»<«STRING.importedName»> properties = «Lists.importedName».newArrayList(«allProperties.propsAsArgs»);
         if (!properties.contains(defaultValue)) {
-            throw new «IllegalArgumentException.importedName»("invalid default parameter");
+            throw new «IAE.importedName»("invalid default parameter");
         }
         int i = 0;
         return new «genTO.name»(
@@ -579,7 +556,11 @@ class ClassTemplate extends BaseTemplate {
             @«OVERRIDE.importedName»
             public int hashCode() {
                 «IF size != 1»
-                    «hashCodeResult(genTO.hashCodeIdentifiers)»
+                    final int prime = 31;
+                    int result = 1;
+                    «FOR property : genTO.hashCodeIdentifiers»
+                        result = prime * result + «property.importedUtilClass».hashCode(«property.fieldName»);
+                    «ENDFOR»
                     return result;
                 «ELSE»
                     return «CODEHELPERS.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
@@ -615,6 +596,19 @@ class ClassTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
+    def private generateToString(Collection<? extends GeneratedProperty> properties) '''
+        «IF !properties.empty»
+            @«OVERRIDE.importedName»
+            public «STRING.importedName» toString() {
+                final var helper = «MOREOBJECTS.importedName».toStringHelper(«type.importedName».class);
+                «FOR property : properties»
+                    «CODEHELPERS.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
+                «ENDFOR»
+                return helper.toString();
+            }
+        «ENDIF»
+    '''
+
     def GeneratedProperty getPropByName(String name) {
         for (GeneratedProperty prop : allProperties) {
             if (prop.name.equals(name)) {
@@ -623,26 +617,4 @@ class ClassTemplate extends BaseTemplate {
         }
         return null
     }
-
-    def private hasUintProperties() {
-        for (GeneratedProperty prop : allProperties) {
-            if (prop.isUintType) {
-                return true
-            }
-        }
-        return false
-    }
-
-    def private static isUintType(GeneratedProperty prop) {
-        UINT_TYPES.containsKey(prop.returnType)
-    }
-
-    def private static legacyType(GeneratedProperty prop) {
-        val type = prop.returnType
-        val uint = UINT_TYPES.get(type)
-        if (uint !== null) {
-            return uint
-        }
-        return type
-    }
 }