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 ba99afceaa2f367773d2b89597c94f90dba19499..95f963372ddd49e36be2a61b0d333ab968c0d3f6 100644 (file)
@@ -7,19 +7,42 @@
  */
 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 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 com.google.common.annotations.VisibleForTesting;
 import java.util.List
+import java.util.Locale
+import java.util.Map.Entry
+import java.util.Set
+import org.gaul.modernizer_maven_annotations.SuppressModernizer
 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.util.TypeConstants
+import org.opendaylight.mdsal.binding.model.api.ParameterizedType
+import org.opendaylight.mdsal.binding.model.api.Type
+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.
      */
@@ -40,18 +63,16 @@ class InterfaceTemplate extends BaseTemplate {
      */
     val List<GeneratedType> enclosedGeneratedTypes
 
+    var Entry<Type, Set<BuilderGeneratedProperty>> typeAnalysis
+
     /**
      * Creates the instance of this class which is used for generating the interface file source
      * code from <code>genType</code>.
      *
-     * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
+     * @throws NullPointerException if <code>genType</code> is <code>null</code>
      */
     new(GeneratedType genType) {
         super(genType)
-        if (genType === null) {
-            throw new IllegalArgumentException("Generated type reference cannot be NULL!")
-        }
-
         consts = genType.constantDefinitions
         methods = genType.methodDefinitions
         enums = genType.enumerations
@@ -64,8 +85,9 @@ class InterfaceTemplate extends BaseTemplate {
      * @return string with code for interface body in JAVA format
      */
     override body() '''
-        «wrapToDocumentation(formatDataForJavaDoc(type))»
+        «type.formatDataForJavaDoc.wrapToDocumentation»
         «type.annotations.generateAnnotations»
+        «generatedAnnotation»
         public interface «type.name»
             «superInterfaces»
         {
@@ -82,22 +104,25 @@ class InterfaceTemplate extends BaseTemplate {
 
     '''
 
-
     def private generateAnnotations(List<AnnotationType> annotations) '''
         «IF annotations !== null && !annotations.empty»
             «FOR annotation : annotations»
-                @«annotation.importedName»
-                «IF annotation.parameters !== null && !annotation.parameters.empty»
-                (
-                «FOR param : annotation.parameters SEPARATOR ","»
-                    «param.name»=«param.value»
-                «ENDFOR»
-                )
-                «ENDIF»
+                «annotation.generateAnnotation»
             «ENDFOR»
         «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.
      *
@@ -163,17 +188,214 @@ class InterfaceTemplate extends BaseTemplate {
     def private generateMethods() '''
         «IF !methods.empty»
             «FOR m : methods SEPARATOR "\n"»
-                «IF !m.isAccessor»
-                    «m.comment.asJavadoc»
+                «IF m.isDefault»
+                    «generateDefaultMethod(m)»
+                «ELSEIF m.isStatic»
+                    «generateStaticMethod(m)»
+                «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
+                    «generateAccessorMethod(m)»
                 «ELSE»
-                    «formatDataForJavaDoc(m, "@return " + asCode(m.returnType.fullyQualifiedName) + " "
-                    + asCode(propertyNameFromGetter(m)) + ", or " + asCode("null") + " if not present")»
+                    «generateMethod(m)»
                 «ENDIF»
-                «m.annotations.generateAnnotations»
-                «m.returnType.importedName» «m.name»(«m.parameters.generateParameters»);
             «ENDFOR»
         «ENDIF»
     '''
 
-}
+    def private generateDefaultMethod(MethodSignature method) {
+        if (method.name.isNonnullMethodName) {
+            generateNonnullMethod(method)
+        } else if (method.name.isRequireMethodName) {
+            generateRequireMethod(method)
+        } else {
+            switch method.name {
+                case BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
+                default :
+                    if (VOID == method.returnType.identifier) {
+                        generateNoopVoidInterfaceMethod(method)
+                    }
+            }
+        }
+    }
+
+    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 : ""
+        }
+    }
+
+    def private generateMethod(MethodSignature method) '''
+        «method.comment.asJavadoc»
+        «method.annotations.generateAnnotations»
+        «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
+    '''
+
+    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»
+
+            «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, ", 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»> «BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»() {
+            return «type.fullyQualifiedName».class;
+        }
+    '''
+
+    @VisibleForTesting
+    def generateBindingHashCode() '''
+        «val augmentable = analyzeType»
+        «IF augmentable || !typeAnalysis.value.empty»
+            /**
+             * Default implementation of {@link «Object.importedName»#hashCode()} contract for this interface.
+             * Implementations of this interface are encouraged to defer to this method to get consistent hashing
+             * results across all implementations.
+             *
+             * @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
+             */
+            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»
+                «IF augmentable»
+                    for (var augmentation : obj.augmentations().values()) {
+                        result += augmentation.hashCode();
+                    }
+                «ENDIF»
+                return result;
+            }
+        «ENDIF»
+    '''
+
+    def private generateBindingEquals() '''
+        «val augmentable = analyzeType»
+        «IF augmentable || !typeAnalysis.value.isEmpty»
+            /**
+             * Default implementation of {@link «Object.importedName»#equals(«Object.importedName»)} contract for this interface.
+             * Implementations of this interface are encouraged to defer to this method to get consistent equality
+             * results across all implementations.
+             *
+             * @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
+             */
+            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»;
+            }
+        «ENDIF»
+    '''
+
+    def generateBindingToString() '''
+        «val augmentable = analyzeType»
+        /**
+         * Default implementation of {@link «Object.importedName»#toString()} contract for this interface.
+         * Implementations of this interface are encouraged to defer to this method to get consistent string
+         * representations across all implementations.
+         *
+         * @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
+         */
+        static «STRING.importedName» «BINDING_TO_STRING_NAME»(final «type.fullyQualifiedNonNull» obj) {
+            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».appendAugmentations(helper, "«AUGMENTATION_FIELD»", obj);
+            «ENDIF»
+            return helper.toString();
+        }
+    '''
+
+    def private generateNonnullMethod(MethodSignature method) '''
+        «val ret = method.returnType»
+        «val name = method.name»
+        «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 && type instanceof ParameterizedType) {
+            val param = type as ParameterizedType
+            if (Types.isMapType(param) || Types.isListType(param) || Types.isSetType(param)) {
+                return type.importedNullable
+            }
+        }
+        return type.importedName
+    }
+
+    def private static boolean isObject(Type type) {
+        // The return type has a package, so it's not a primitive type
+        return !type.getPackageName().isEmpty()
+    }
+
+    private def boolean analyzeType() {
+        if (typeAnalysis === null) {
+            typeAnalysis = analyzeTypeHierarchy(type)
+        }
+        typeAnalysis.key !== null
+    }
+}