Map identities to proper objects
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / InterfaceTemplate.xtend
index 799a0f85f5420c60ac55c38fcb6e55981f7afbfe..95f963372ddd49e36be2a61b0d333ab968c0d3f6 100644 (file)
@@ -8,18 +8,22 @@
 package org.opendaylight.mdsal.binding.java.api.generator
 
 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.getGetterMethodForNonnull
+import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.getGetterMethodForRequire
 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isGetterMethodName
 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isNonnullMethodName
-import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
+import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isRequireMethodName
+import static org.opendaylight.mdsal.binding.model.ri.Types.BOOLEAN
+import static org.opendaylight.mdsal.binding.model.ri.Types.STRING
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.REQUIRE_PREFIX
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_EQUALS_NAME
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_HASHCODE_NAME
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_TO_STRING_NAME
-import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.MoreObjects
 import java.util.List
+import java.util.Locale
 import java.util.Map.Entry
 import java.util.Set
 import org.gaul.modernizer_maven_annotations.SuppressModernizer
@@ -27,10 +31,12 @@ import org.opendaylight.mdsal.binding.model.api.AnnotationType
 import org.opendaylight.mdsal.binding.model.api.Constant
 import org.opendaylight.mdsal.binding.model.api.Enumeration
 import org.opendaylight.mdsal.binding.model.api.GeneratedType
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName
 import org.opendaylight.mdsal.binding.model.api.MethodSignature
+import org.opendaylight.mdsal.binding.model.api.ParameterizedType
 import org.opendaylight.mdsal.binding.model.api.Type
-import org.opendaylight.mdsal.binding.model.util.Types
-import org.opendaylight.mdsal.binding.model.util.TypeConstants
+import org.opendaylight.mdsal.binding.model.ri.Types
+import org.opendaylight.mdsal.binding.model.ri.TypeConstants
 
 /**
  * Template for generating JAVA interfaces.
@@ -81,6 +87,7 @@ class InterfaceTemplate extends BaseTemplate {
     override body() '''
         «type.formatDataForJavaDoc.wrapToDocumentation»
         «type.annotations.generateAnnotations»
+        «generatedAnnotation»
         public interface «type.name»
             «superInterfaces»
         {
@@ -105,6 +112,17 @@ class InterfaceTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
+    def private generateAccessorAnnotations(MethodSignature method) '''
+         «val annotations = method.annotations»
+         «IF annotations !== null && !annotations.empty»
+             «FOR annotation : annotations»
+                  «IF method.returnType != BOOLEAN || !(annotation.identifier == OVERRIDE)»
+                      «annotation.generateAnnotation»
+                  «ENDIF»
+             «ENDFOR»
+        «ENDIF»
+    '''
+
     /**
      * Template method which generates the interface name declaration.
      *
@@ -186,9 +204,11 @@ class InterfaceTemplate extends BaseTemplate {
     def private generateDefaultMethod(MethodSignature method) {
         if (method.name.isNonnullMethodName) {
             generateNonnullMethod(method)
+        } else if (method.name.isRequireMethodName) {
+            generateRequireMethod(method)
         } else {
             switch method.name {
-                case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
+                case BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
                 default :
                     if (VOID == method.returnType.identifier) {
                         generateNoopVoidInterfaceMethod(method)
@@ -202,6 +222,7 @@ class InterfaceTemplate extends BaseTemplate {
             case BINDING_EQUALS_NAME : generateBindingEquals
             case BINDING_HASHCODE_NAME : generateBindingHashCode
             case BINDING_TO_STRING_NAME : generateBindingToString
+            default : ""
         }
     }
 
@@ -219,29 +240,36 @@ class InterfaceTemplate extends BaseTemplate {
         }
     '''
 
-    def private static accessorJavadoc(MethodSignature method, String orString) {
-        val reference = method.comment?.referenceDescription
-        val propReturn = method.propertyNameFromGetter + ", or " + orString + " if it is not present."
+    def private accessorJavadoc(MethodSignature method, String orString) {
+        accessorJavadoc(method, orString, null)
+    }
+
+    def private accessorJavadoc(MethodSignature method, String orString, JavaTypeName exception) {
+        val propName = method.propertyNameFromGetter
+        val propReturn = propName + orString
 
         return wrapToDocumentation('''
-            Return «propReturn».
+            Return «propReturn»
 
-            «reference.formatReference»
-            @return {@code «method.returnType.fullyQualifiedName»} «propReturn»
+            «method.comment?.referenceDescription.formatReference»
+            @return {@code «method.returnType.importedName»} «propReturn»
+            «IF exception !== null»
+                @throws «exception.importedName» if «propName» is not present
+            «ENDIF»
         ''')
     }
 
     def private generateAccessorMethod(MethodSignature method) {
         return '''
-            «accessorJavadoc(method, "{@code null}")»
-            «method.annotations.generateAnnotations»
+            «accessorJavadoc(method, ", or {@code null} if it is not present.")»
+            «method.generateAccessorAnnotations»
             «method.returnType.nullableType» «method.name»();
         '''
     }
 
     def private generateDefaultImplementedInterface() '''
         @«OVERRIDE.importedName»
-        default «CLASS.importedName»<«type.fullyQualifiedName»> «DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»() {
+        default «CLASS.importedName»<«type.fullyQualifiedName»> «BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»() {
             return «type.fullyQualifiedName».class;
         }
     '''
@@ -266,7 +294,9 @@ class InterfaceTemplate extends BaseTemplate {
                     result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
                 «ENDFOR»
                 «IF augmentable»
-                    result = prime * result + obj.augmentations().hashCode();
+                    for (var augmentation : obj.augmentations().values()) {
+                        result += augmentation.hashCode();
+                    }
                 «ENDIF»
                 return result;
             }
@@ -316,12 +346,12 @@ class InterfaceTemplate extends BaseTemplate {
          * @throws «NPE.importedName» if {@code obj} is null
          */
         static «STRING.importedName» «BINDING_TO_STRING_NAME»(final «type.fullyQualifiedNonNull» obj) {
-            final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«type.name»");
+            final var helper = «MOREOBJECTS.importedName».toStringHelper("«type.name»");
             «FOR property : typeAnalysis.value»
                 «CODEHELPERS.importedName».appendValue(helper, "«property.name»", obj.«property.getterName»());
             «ENDFOR»
             «IF augmentable»
-                «CODEHELPERS.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", obj.augmentations().values());
+                «CODEHELPERS.importedName».appendAugmentations(helper, "«AUGMENTATION_FIELD»", obj);
             «ENDIF»
             return helper.toString();
         }
@@ -330,16 +360,29 @@ class InterfaceTemplate extends BaseTemplate {
     def private generateNonnullMethod(MethodSignature method) '''
         «val ret = method.returnType»
         «val name = method.name»
-        «accessorJavadoc(method, "an empty list")»
+        «accessorJavadoc(method, ", or an empty list if it is not present.")»
         «method.annotations.generateAnnotations»
         default «ret.importedNonNull» «name»() {
             return «CODEHELPERS.importedName».nonnull(«name.getGetterMethodForNonnull»());
         }
     '''
 
+    def private generateRequireMethod(MethodSignature method) '''
+        «val ret = method.returnType»
+        «val name = method.name»
+        «val fieldName = name.toLowerCase(Locale.ROOT).replace(REQUIRE_PREFIX, "")»
+        «accessorJavadoc(method, ", guaranteed to be non-null.", NSEE)»
+        default «ret.importedNonNull» «name»() {
+            return «CODEHELPERS.importedName».require(«getGetterMethodForRequire(name)»(), "«fieldName»");
+        }
+    '''
+
     def private String nullableType(Type type) {
-        if (type.isObject && (Types.isMapType(type) || Types.isListType(type))) {
-            return type.importedNullable
+        if (type.isObject && type instanceof ParameterizedType) {
+            val param = type as ParameterizedType
+            if (Types.isMapType(param) || Types.isListType(param) || Types.isSetType(param)) {
+                return type.importedNullable
+            }
         }
         return type.importedName
     }