Do not generate prime when not needed
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / InterfaceTemplate.xtend
index ac67e9655575e70d676f5535b5d69da6026a0169..a6d549069fbf7f51ac7221f05655c546c7c6f605 100644 (file)
@@ -7,19 +7,23 @@
  */
 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.isGetterMethodName
-import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isNonnullMethodName
-import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
-import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
-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 static extension org.opendaylight.yangtools.yang.binding.contract.Naming.getGetterMethodForNonnull
+import static extension org.opendaylight.yangtools.yang.binding.contract.Naming.getGetterMethodForRequire
+import static extension org.opendaylight.yangtools.yang.binding.contract.Naming.isGetterMethodName
+import static extension org.opendaylight.yangtools.yang.binding.contract.Naming.isNonnullMethodName
+import static extension org.opendaylight.yangtools.yang.binding.contract.Naming.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.yangtools.yang.binding.contract.Naming.REQUIRE_PREFIX
+import static org.opendaylight.yangtools.yang.binding.contract.Naming.AUGMENTATION_FIELD
+import static org.opendaylight.yangtools.yang.binding.contract.Naming.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME
+import static org.opendaylight.yangtools.yang.binding.contract.Naming.BINDING_EQUALS_NAME
+import static org.opendaylight.yangtools.yang.binding.contract.Naming.BINDING_HASHCODE_NAME
+import static org.opendaylight.yangtools.yang.binding.contract.Naming.BINDING_TO_STRING_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,16 +31,16 @@ 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.api.TypeMember
+import org.opendaylight.mdsal.binding.model.ri.Types
+import org.opendaylight.mdsal.binding.model.ri.TypeConstants
 
 /**
  * Template for generating JAVA interfaces.
  */
- @SuppressModernizer
 class InterfaceTemplate extends BaseTemplate {
     /**
      * List of constant instances which are generated as JAVA public static final attributes.
@@ -82,6 +86,7 @@ class InterfaceTemplate extends BaseTemplate {
     override body() '''
         «type.formatDataForJavaDoc.wrapToDocumentation»
         «type.annotations.generateAnnotations»
+        «generatedAnnotation»
         public interface «type.name»
             «superInterfaces»
         {
@@ -106,6 +111,17 @@ class InterfaceTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
+    def private generateAccessorAnnotations(MethodSignature method) '''
+         «val annotations = method.annotations»
+         «IF annotations !== null && !annotations.empty»
+             «FOR annotation : annotations»
+                  «IF !BOOLEAN.equals(method.returnType) || !OVERRIDE.equals(annotation.identifier)»
+                      «annotation.generateAnnotation»
+                  «ENDIF»
+             «ENDFOR»
+        «ENDIF»
+    '''
+
     /**
      * Template method which generates the interface name declaration.
      *
@@ -149,7 +165,7 @@ class InterfaceTemplate extends BaseTemplate {
     '''
 
     /**
-     * Template method wich generates JAVA constants.
+     * Template method which generates JAVA constants.
      *
      * @return string with constants in JAVA format
      */
@@ -177,6 +193,8 @@ class InterfaceTemplate extends BaseTemplate {
                     «generateStaticMethod(m)»
                 «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
                     «generateAccessorMethod(m)»
+                «ELSEIF m.parameters.empty && m.name.isNonnullMethodName»
+                    «generateNonnullAccessorMethod(m)»
                 «ELSE»
                     «generateMethod(m)»
                 «ENDIF»
@@ -184,21 +202,30 @@ class InterfaceTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
+    @SuppressModernizer
     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.equals(method.returnType.identifier)) {
+                        generateNoopVoidInterfaceMethod(method)
+                    }
             }
         }
     }
 
+    @SuppressModernizer
     def private generateStaticMethod(MethodSignature method) {
         switch method.name {
             case BINDING_EQUALS_NAME : generateBindingEquals
             case BINDING_HASHCODE_NAME : generateBindingHashCode
             case BINDING_TO_STRING_NAME : generateBindingToString
+            default : ""
         }
     }
 
@@ -208,29 +235,52 @@ class InterfaceTemplate extends BaseTemplate {
         «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
     '''
 
-    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 generateNoopVoidInterfaceMethod(MethodSignature method) '''
+        «method.comment.asJavadoc»
+        «method.annotations.generateAnnotations»
+        default «VOID.importedName» «method.name»(«method.parameters.generateParameters») {
+            // No-op
+        }
+    '''
+
+    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 generateNonnullAccessorMethod(MethodSignature method) {
+        return '''
+            «accessorJavadoc(method, ", or an empty instance if it is not present.")»
+            «method.annotations.generateAnnotations»
+            «method.returnType.importedNonNull» «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;
         }
     '''
@@ -246,16 +296,21 @@ class InterfaceTemplate extends BaseTemplate {
              *
              * @param obj Object for which to generate hashCode() result.
              * @return Hash code value of data modeled by this interface.
-             * @throws «NPE.importedName» if {@code obj} is null
+             * @throws «NPE.importedName» if {@code obj} is {@code null}
              */
             static int «BINDING_HASHCODE_NAME»(final «type.fullyQualifiedNonNull» obj) {
-                final int prime = 31;
                 int result = 1;
-                «FOR property : typeAnalysis.value»
-                    result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
-                «ENDFOR»
+                «val props = typeAnalysis.value»
+                «IF !props.empty»
+                    final int prime = 31;
+                    «FOR property : props»
+                        result = prime * result + «property.importedUtilClass».hashCode(obj.«property.getterMethodName»());
+                    «ENDFOR»
+                «ENDIF»
                 «IF augmentable»
-                    result = prime * result + obj.augmentations().hashCode();
+                    for (var augmentation : obj.augmentations().values()) {
+                        result += augmentation.hashCode();
+                    }
                 «ENDIF»
                 return result;
             }
@@ -273,22 +328,18 @@ class InterfaceTemplate extends BaseTemplate {
              * @param thisObj Object acting as the receiver of equals invocation
              * @param obj Object acting as argument to equals invocation
              * @return True if thisObj and obj are considered equal
-             * @throws «NPE.importedName» if {@code thisObj} is null
+             * @throws «NPE.importedName» if {@code thisObj} is {@code null}
              */
             static boolean «BINDING_EQUALS_NAME»(final «type.fullyQualifiedNonNull» thisObj, final «Types.objectType().importedName» obj) {
                 if (thisObj == obj) {
                     return true;
                 }
-                final «type.fullyQualifiedName» other = «CODEHELPERS.importedName».checkCast(«type.fullyQualifiedName».class, obj);
-                if (other == null) {
-                    return false;
-                }
-                «FOR property : ByTypeMemberComparator.sort(typeAnalysis.value)»
-                    if (!«property.importedUtilClass».equals(thisObj.«property.getterName»(), other.«property.getterName»())) {
-                        return false;
-                    }
-                «ENDFOR»
-                return «IF augmentable»thisObj.augmentations().equals(other.augmentations())«ELSE»true«ENDIF»;
+                final var other = «CODEHELPERS.importedName».checkCast(«type.fullyQualifiedName».class, obj);
+                return other != null
+                    «FOR property : ByTypeMemberComparator.sort(typeAnalysis.value)»
+                        && «property.importedUtilClass».equals(thisObj.«property.getterName»(), other.«property.getterName»())
+                    «ENDFOR»
+                    «IF augmentable»&& thisObj.augmentations().equals(other.augmentations())«ENDIF»;
             }
         «ENDIF»
     '''
@@ -302,15 +353,15 @@ class InterfaceTemplate extends BaseTemplate {
          *
          * @param obj Object for which to generate toString() result.
          * @return {@link «STRING.importedName»} value of data modeled by this interface.
-         * @throws «NPE.importedName» if {@code obj} is null
+         * @throws «NPE.importedName» if {@code obj} is {@code 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();
         }
@@ -319,16 +370,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) {
-            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
     }