hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject svn commit: r1053488 - in /hbase/branches/0.90/src: main/java/org/apache/hadoop/hbase/util/Pair.java test/java/org/apache/hadoop/hbase/util/TestPairEquals.java
Date Wed, 29 Dec 2010 00:10:08 GMT
Author: stack
Date: Wed Dec 29 00:10:08 2010
New Revision: 1053488

URL: http://svn.apache.org/viewvc?rev=1053488&view=rev
Log:
HBASE-3387 Pair does not deep check arrays for equality

Added:
    hbase/branches/0.90/src/test/java/org/apache/hadoop/hbase/util/TestPairEquals.java
Modified:
    hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/util/Pair.java

Modified: hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/util/Pair.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/util/Pair.java?rev=1053488&r1=1053487&r2=1053488&view=diff
==============================================================================
--- hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/util/Pair.java (original)
+++ hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/util/Pair.java Wed Dec 29 00:10:08
2010
@@ -21,6 +21,7 @@
 package org.apache.hadoop.hbase.util;
 
 import java.io.Serializable;
+import java.lang.reflect.Array;
 
 /**
  * A generic class for pairs.
@@ -87,9 +88,35 @@ public class Pair<T1, T2> implements Ser
     return second;
   }
 
-  private static boolean equals(Object x, Object y)
-  {
-     return (x == null && y == null) || (x != null && x.equals(y));
+  private static boolean equals(Object x, Object y) {
+    if (x == null && y == null)
+      return true;
+
+    if (x != null && y != null) {
+      if (x.getClass().equals(y.getClass())) {
+        if (x.getClass().isArray() && y.getClass().isArray()) {
+
+          int len = Array.getLength(x) == Array.getLength(y) ? Array
+              .getLength(x) : -1;
+          if (len < 0)
+            return false;
+
+          for (int i = 0; i < len; i++) {
+
+            Object xi = Array.get(x, i);
+            Object yi = Array.get(y, i);
+
+            if (!xi.equals(yi))
+              return false;
+          }
+          return true;
+        } else {
+          return x.equals(y);
+        }
+      }
+    }
+    return false;
+
   }
 
   @Override

Added: hbase/branches/0.90/src/test/java/org/apache/hadoop/hbase/util/TestPairEquals.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.90/src/test/java/org/apache/hadoop/hbase/util/TestPairEquals.java?rev=1053488&view=auto
==============================================================================
--- hbase/branches/0.90/src/test/java/org/apache/hadoop/hbase/util/TestPairEquals.java (added)
+++ hbase/branches/0.90/src/test/java/org/apache/hadoop/hbase/util/TestPairEquals.java Wed
Dec 29 00:10:08 2010
@@ -0,0 +1,158 @@
+/**
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+/**
+ * Testing Testing {@link Pair#equals(Object)} for deep checking of arrays
+ */
+public class TestPairEquals {
+
+  /**
+   * Testing {@link Pair#equals(Object)} for deep checking of arrays
+   */
+  @Test
+  public void testEquals() {
+    Pair<String, String> p1 = new Pair<String, String>("Hello", "World");
+    Pair<String, String> p1a = new Pair<String, String>("Hello", "World");
+    assertTrue(p1.equals(p1a));
+
+    Pair<String, byte[]> p2 = new Pair<String, byte[]>("Hello", new byte[] {
1,
+        0, 5 });
+    Pair<String, byte[]> p2a = new Pair<String, byte[]>("Hello", new byte[] {
+        1, 0, 5 });
+    // Previously this test would fail as they are two different pointers to
+    // arrays that inherently the same.
+    assertTrue(p2.equals(p2a));
+
+    Pair<char[], String> p3 = new Pair<char[], String>(new char[] { 'h', 'e'
},
+        "world");
+    assertTrue(p3.equals(p3));
+
+    // These kinds of tests will still fail as they have fundamentally different
+    // elements
+    Pair<Character[], String> p4 = new Pair<Character[], String>(
+        new Character[] { new Character('h'), new Character('e') }, "world");
+    // checking for autoboxing non-equality to the original class
+    assertFalse(p3.equals(p4));
+
+    // still fail for a different autoboxing situation (just to prove that it
+    // is not just chars)
+    Pair<String, Integer[]> p5 = new Pair<String, Integer[]>("hello",
+        new Integer[] { new Integer(1), new Integer(982) });
+    Pair<String, int[]> p5a = new Pair<String, int[]>("hello", new int[] { 1,
+        982 });
+    assertFalse(p5.equals(p5a));
+
+    // will still fail for that different things
+    Pair<String, byte[]> p6 = new Pair<String, byte[]>("Hello", new byte[] {
1,
+        0, 4 });
+    assertFalse(p2.equals(p6));
+
+    // will still fail for the other predicate being different
+    Pair<String, byte[]> p7 = new Pair<String, byte[]>("World", new byte[] {
1,
+        0, 5 });
+    assertFalse(p2.equals(p7));
+  }
+}
+/**
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+/**
+ * Testing Testing {@link Pair#equals(Object)} for deep checking of arrays
+ */
+public class TestPairEquals {
+
+  /**
+   * Testing {@link Pair#equals(Object)} for deep checking of arrays
+   */
+  @Test
+  public void testEquals() {
+    Pair<String, String> p1 = new Pair<String, String>("Hello", "World");
+    Pair<String, String> p1a = new Pair<String, String>("Hello", "World");
+    assertTrue(p1.equals(p1a));
+
+    Pair<String, byte[]> p2 = new Pair<String, byte[]>("Hello", new byte[] {
1,
+        0, 5 });
+    Pair<String, byte[]> p2a = new Pair<String, byte[]>("Hello", new byte[] {
+        1, 0, 5 });
+    // Previously this test would fail as they are two different pointers to
+    // arrays that inherently the same.
+    assertTrue(p2.equals(p2a));
+
+    Pair<char[], String> p3 = new Pair<char[], String>(new char[] { 'h', 'e'
},
+        "world");
+    assertTrue(p3.equals(p3));
+
+    // These kinds of tests will still fail as they have fundamentally different
+    // elements
+    Pair<Character[], String> p4 = new Pair<Character[], String>(
+        new Character[] { new Character('h'), new Character('e') }, "world");
+    // checking for autoboxing non-equality to the original class
+    assertFalse(p3.equals(p4));
+
+    // still fail for a different autoboxing situation (just to prove that it
+    // is not just chars)
+    Pair<String, Integer[]> p5 = new Pair<String, Integer[]>("hello",
+        new Integer[] { new Integer(1), new Integer(982) });
+    Pair<String, int[]> p5a = new Pair<String, int[]>("hello", new int[] { 1,
+        982 });
+    assertFalse(p5.equals(p5a));
+
+    // will still fail for that different things
+    Pair<String, byte[]> p6 = new Pair<String, byte[]>("Hello", new byte[] {
1,
+        0, 4 });
+    assertFalse(p2.equals(p6));
+
+    // will still fail for the other predicate being different
+    Pair<String, byte[]> p7 = new Pair<String, byte[]>("World", new byte[] {
1,
+        0, 5 });
+    assertFalse(p2.equals(p7));
+  }
+}



Mime
View raw message