equalsHelper (MoreObjects2) 13/42513/12
authorMichael Vorburger <vorburger@redhat.com>
Mon, 25 Jul 2016 23:26:14 +0000 (01:26 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 5 Oct 2016 09:33:55 +0000 (09:33 +0000)
Moving this class "downwards" from mdsalutil, because this really is a
generic utility helper class, which probably best fits into
yangtools.utils.

Change-Id: I0d8c6dd44790a85f7efccd6f83d42e6bd7e084bb
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
common/util/pom.xml
common/util/src/main/java/org/opendaylight/yangtools/util/EvenMoreObjects.java [new file with mode: 0644]
common/util/src/test/java/org/opendaylight/yangtools/util/EvenMoreObjectsTest.java [new file with mode: 0644]

index f3e71bb582ab6a71b303f97186878f48bc5842bc..538c31b1a0cc077aa8dfd455cf16a62995dc6d62 100644 (file)
             <artifactId>mockito-configuration</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+          <groupId>com.google.guava</groupId>
+          <artifactId>guava-testlib</artifactId>
+          <scope>test</scope>
+        </dependency>
     </dependencies>
 
   <!--
diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/EvenMoreObjects.java b/common/util/src/main/java/org/opendaylight/yangtools/util/EvenMoreObjects.java
new file mode 100644 (file)
index 0000000..c7988c1
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.util;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import java.util.function.BiFunction;
+
+/**
+ * Utility helping to implement readable equals() methods.
+ *
+ * <p>Usage:
+ * <pre>
+ *{@literal @}Override
+ * public boolean equals(Object obj) {
+ *     return EvenMoreObjects.equalsHelper(this, obj,
+ *        (one, another) -&gt; Objects.equals(one.name, another.name) &amp;&amp; Objects.equals(one.age, another.age));
+ * }
+ * </pre>
+ *
+ * <p>See <a href="https://github.com/google/guava/issues/2521">Guava issue proposing contributing this</a>.
+ *
+ * @see MoreObjects
+ *
+ * @author Michael Vorburger, Red Hat
+ */
+@Beta
+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 (self.getClass() != other.getClass()) {
+            return false;
+        }
+        return equals.apply(self, (T) other);
+    }
+
+    @FunctionalInterface
+    public interface BooleanEqualsFunction<T> extends BiFunction<T, T, Boolean> { }
+
+    private EvenMoreObjects() { }
+}
diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/EvenMoreObjectsTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/EvenMoreObjectsTest.java
new file mode 100644 (file)
index 0000000..644f6e3
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.util;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.testing.EqualsTester;
+import java.util.Objects;
+import org.junit.Test;
+
+public class EvenMoreObjectsTest {
+
+    @Test
+    public void test() {
+        new EqualsTester()
+                .addEqualityGroup(new Thing("hello", 123), new Thing("hello", 123))
+                .addEqualityGroup(new Thing("hoi", 123), new Thing("hoi", 123))
+                .addEqualityGroup(new Thing("hoi", null))
+                .addEqualityGroup(new Thing(null, null))
+                .testEquals();
+    }
+
+    static class Thing {
+
+        String name;
+        Integer age;
+
+        @Override
+        public boolean equals(Object obj) {
+            return EvenMoreObjects.equalsHelper(this, obj,
+                (one, another) -> Objects.equals(one.name, another.name) && Objects.equals(one.age, another.age));
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(name, age);
+        }
+
+        @Override
+        public String toString() {
+            return MoreObjects.toStringHelper(this).add("name", name).add("age", age).toString();
+        }
+
+        Thing(String name, Integer age) {
+            super();
+            this.name = name;
+            this.age = age;
+        }
+
+    }
+
+}