BUG-1425: fix builder's implementation of equals() 68/9868/2
authorRobert Varga <rovarga@cisco.com>
Mon, 11 Aug 2014 21:39:36 +0000 (23:39 +0200)
committerRobert Varga <rovarga@cisco.com>
Tue, 12 Aug 2014 12:56:15 +0000 (14:56 +0200)
With the advent of LazyDataObject, we have two implementation which need
to compare as equal. LazyObject.equals(builderObject) already works, but
we need the revers to be true, too. This patch sets out to do exactly
that.

Change-Id: I931f66a9088451524a842f6a67e8492ac922fd3d
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend

index d818c11d3d04e17cad071320888dbd7846dfdfb1..74a5ef379315dfdb53752fc26877bfc002d2a4c6 100644 (file)
@@ -655,35 +655,51 @@ class BuilderTemplate extends BaseTemplate {
                 if (this == obj) {
                     return true;
                 }
-                if (obj == null) {
+                if (!(obj instanceof «DataObject.importedName»)) {
                     return false;
                 }
-                if (getClass() != obj.getClass()) {
+                if (!«type.importedName».class.equals(((«DataObject.importedName»)obj).getImplementedInterface())) {
                     return false;
                 }
-                «type.name»«IMPL» other = («type.name»«IMPL») obj;
+                «type.importedName» other = («type.importedName»)obj;
                 «FOR property : properties»
                     «val fieldName = property.fieldName»
                     if («fieldName» == null) {
-                        if (other.«fieldName» != null) {
+                        if (other.«property.getterMethodName»() != null) {
                             return false;
                         }
                     «IF property.returnType.name.contains("[")»
-                    } else if(!«Arrays.importedName».equals(«fieldName», other.«fieldName»)) {
+                    } else if(!«Arrays.importedName».equals(«fieldName», other.«property.getterMethodName»())) {
                     «ELSE»
-                    } else if(!«fieldName».equals(other.«fieldName»)) {
+                    } else if(!«fieldName».equals(other.«property.getterMethodName»())) {
                     «ENDIF»
                         return false;
                     }
                 «ENDFOR»
                 «IF augmentField != null»
-                    «val fieldName = augmentField.name»
-                    if («fieldName» == null) {
-                        if (other.«fieldName» != null) {
+                    if (getClass() == obj.getClass()) {
+                        // Simple case: we are comparing against self
+                        «type.name»«IMPL» otherImpl = («type.name»«IMPL») obj;
+                        «val fieldName = augmentField.name»
+                        if («fieldName» == null) {
+                            if (otherImpl.«fieldName» != null) {
+                                return false;
+                            }
+                        } else if(!«fieldName».equals(otherImpl.«fieldName»)) {
+                            return false;
+                        }
+                    } else {
+                        // Hard case: compare our augments with presence there...
+                        for («Map.importedName».Entry<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> e : «augmentField.name».entrySet()) {
+                            final Object oa = other.getAugmentation(e.getKey());
+                            if (!e.getValue().equals(oa)) {
+                                return false;
+                            }
+                        }
+                        // .. and give the other one the chance to do the same
+                        if (!obj.equals(this)) {
                             return false;
                         }
-                    } else if(!«fieldName».equals(other.«fieldName»)) {
-                        return false;
                     }
                 «ENDIF»
                 return true;