accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mmil...@apache.org
Subject [accumulo] branch master updated: ACCUMULO-4722 Use Objects.equals in Pairs class" (#309)
Date Thu, 19 Oct 2017 19:51:21 GMT
This is an automated email from the ASF dual-hosted git repository.

mmiller pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/master by this push:
     new 255aa8f  ACCUMULO-4722 Use Objects.equals in Pairs class" (#309)
255aa8f is described below

commit 255aa8f4faa5382194b10e02b2e7b6bf28440e32
Author: Mark Owens <jmarkowe@gmail.com>
AuthorDate: Thu Oct 19 15:51:19 2017 -0400

    ACCUMULO-4722 Use Objects.equals in Pairs class" (#309)
    
    Removed private equals and hashCode methods in Pairs class and instead made use of the
    java.util.Objects.equals method. Updated the test class to verify
    several variations of the equals and hashCode method.
---
 .../java/org/apache/accumulo/core/util/Pair.java   | 20 +++--------
 .../org/apache/accumulo/core/util/PairTest.java    | 40 ++++++++++++++++++++--
 2 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/core/src/main/java/org/apache/accumulo/core/util/Pair.java b/core/src/main/java/org/apache/accumulo/core/util/Pair.java
index 37fb04f..55333d5 100644
--- a/core/src/main/java/org/apache/accumulo/core/util/Pair.java
+++ b/core/src/main/java/org/apache/accumulo/core/util/Pair.java
@@ -18,6 +18,7 @@ package org.apache.accumulo.core.util;
 
 import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.Map.Entry;
+import java.util.Objects;
 
 public class Pair<A,B> {
   A first;
@@ -28,29 +29,16 @@ public class Pair<A,B> {
     this.second = s;
   }
 
-  private int hashCode(Object o) {
-    if (o == null)
-      return 0;
-    return o.hashCode();
-  }
-
   @Override
   public int hashCode() {
-    return hashCode(first) + hashCode(second);
-  }
-
-  private boolean equals(Object o1, Object o2) {
-    if (o1 == null || o2 == null)
-      return o1 == o2;
-
-    return o1.equals(o2);
+    return Objects.hashCode(first) + Objects.hashCode(second);
   }
 
   @Override
   public boolean equals(Object o) {
     if (o instanceof Pair<?,?>) {
-      Pair<?,?> op = (Pair<?,?>) o;
-      return equals(first, op.first) && equals(second, op.second);
+      Pair<?,?> other = (Pair<?,?>) o;
+      return Objects.equals(first, other.first) && Objects.equals(second, other.second);
     }
     return false;
   }
diff --git a/core/src/test/java/org/apache/accumulo/core/util/PairTest.java b/core/src/test/java/org/apache/accumulo/core/util/PairTest.java
index 6effc9e..b6c0e82 100644
--- a/core/src/test/java/org/apache/accumulo/core/util/PairTest.java
+++ b/core/src/test/java/org/apache/accumulo/core/util/PairTest.java
@@ -17,6 +17,10 @@
 package org.apache.accumulo.core.util;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertTrue;
 
 import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.Map.Entry;
@@ -26,13 +30,45 @@ import org.junit.Test;
 public class PairTest {
 
   /**
+   * Test method for {@link org.apache.accumulo.core.util.Pair#hashCode()}.
+   */
+  @Test
+  public void testHashMethod() {
+    Pair<Integer,String> pair1 = new Pair<>(25, "twenty-five");
+    Pair<Integer,String> pair2 = new Pair<>(25, "twenty-five");
+    Pair<Integer,String> pair3 = new Pair<>(null, null);
+    Pair<Integer,String> pair4 = new Pair<>(25, "twentyfive");
+    Pair<Integer,String> pair5 = new Pair<>(225, "twenty-five");
+    assertNotSame(pair1, pair2);
+    assertEquals(pair1.hashCode(), pair2.hashCode());
+    assertNotSame(pair2, pair3);
+    assertNotEquals(pair1.hashCode(), pair4.hashCode());
+    assertNotEquals(pair1.hashCode(), pair5.hashCode());
+  }
+
+  /**
    * Test method for {@link org.apache.accumulo.core.util.Pair#equals(java.lang.Object)}.
    */
   @Test
   public void testEqualsObject() {
-    Pair<Integer,String> pair = new Pair<>(25, "twenty-five");
+    Pair<Integer,String> pair1 = new Pair<>(25, "twenty-five");
     Pair<Integer,String> pair2 = new Pair<>(25, "twenty-five");
-    assertEquals(pair, pair2);
+    Pair<Integer,String> pair3 = new Pair<>(25, "twentyfive");
+    Pair<Integer,String> null1 = null;
+
+    assertEquals(pair1, pair1);
+    assertEquals(pair2, pair1);
+    assertNotEquals(pair1, pair3);
+
+    // verify direct calls
+    assertTrue(pair1.equals(pair2));
+    assertTrue(pair2.equals(pair1));
+    assertFalse(pair1.equals(pair3));
+
+    // check null
+    assertEquals(null1, null1);
+    assertEquals(null1, null);
+    assertNotEquals(pair1, null1);
   }
 
   /**

-- 
To stop receiving notification emails like this one, please contact
['"commits@accumulo.apache.org" <commits@accumulo.apache.org>'].

Mime
View raw message