+ 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
+ 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»> «DATA_CONTAINER_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»
+ '''