Support testing null against null in equalsHelper() 99/46599/4
authorStephen Kitt <skitt@redhat.com>
Thu, 6 Oct 2016 08:13:58 +0000 (10:13 +0200)
committerStephen Kitt <skitt@redhat.com>
Fri, 7 Oct 2016 07:56:48 +0000 (09:56 +0200)
In its intended context, equalsHelper() should never be called with
two null parameters, but it's easy to support the general case — and
because this isn't SQL, null equals itself.

Change-Id: I826435e81a4dd59aaac72ad9ab07a80069f7a17f
Signed-off-by: Stephen Kitt <skitt@redhat.com>
common/util/src/main/java/org/opendaylight/yangtools/util/EvenMoreObjects.java
common/util/src/test/java/org/opendaylight/yangtools/util/EvenMoreObjectsTest.java

index c7988c1bf8950d015024db895a8765113ad647aa..137ff43e78aee6bf0951e463f82648454d853803 100644 (file)
@@ -34,12 +34,12 @@ public final class EvenMoreObjects {
 
     @SuppressWarnings("unchecked")
     public static <T> boolean equalsHelper(T self, Object other, BooleanEqualsFunction<T> equals) {
-        if (other == null) {
-            return false;
-        }
         if (other == self) {
             return true;
         }
+        if (other == null) {
+            return false;
+        }
         if (self.getClass() != other.getClass()) {
             return false;
         }
index 644f6e32df53ee61236bbde81770269e39ab023a..59bcd8521763bbe9e8e53e20d42ec8707461debf 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.yangtools.util;
 
+import static org.junit.Assert.assertTrue;
+
 import com.google.common.base.MoreObjects;
 import com.google.common.testing.EqualsTester;
 import java.util.Objects;
@@ -15,7 +17,7 @@ import org.junit.Test;
 public class EvenMoreObjectsTest {
 
     @Test
-    public void test() {
+    public void thingPassesEqualsTester() {
         new EqualsTester()
                 .addEqualityGroup(new Thing("hello", 123), new Thing("hello", 123))
                 .addEqualityGroup(new Thing("hoi", 123), new Thing("hoi", 123))
@@ -24,7 +26,12 @@ public class EvenMoreObjectsTest {
                 .testEquals();
     }
 
-    static class Thing {
+    @Test
+    public void nullEqualsNull() {
+        assertTrue(EvenMoreObjects.equalsHelper(null, null, (one, another) -> true));
+    }
+
+    private static class Thing {
 
         String name;
         Integer age;