Repository: hbase
Updated Branches:
  refs/heads/master c6e9dabe6 -> 73e945670


HBASE-16326 CellModel / RowModel should override 'equals', 'hashCode' and 
'toString' (Minwoo Kang)


Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/73e94567
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/73e94567
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/73e94567

Branch: refs/heads/master
Commit: 73e945670270de187be337037f75577ccc1a770b
Parents: c6e9dab
Author: tedyu <yuzhih...@gmail.com>
Authored: Mon Oct 17 12:42:06 2016 -0700
Committer: tedyu <yuzhih...@gmail.com>
Committed: Mon Oct 17 12:42:06 2016 -0700

----------------------------------------------------------------------
 .../hadoop/hbase/rest/model/CellModel.java      | 40 ++++++++++++++++++++
 .../hadoop/hbase/rest/model/RowModel.java       | 37 ++++++++++++++++++
 .../hadoop/hbase/rest/model/TestCellModel.java  | 24 ++++++++++++
 .../hadoop/hbase/rest/model/TestRowModel.java   | 25 ++++++++++++
 4 files changed, 126 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/73e94567/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/model/CellModel.java
----------------------------------------------------------------------
diff --git 
a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/model/CellModel.java 
b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/model/CellModel.java
index 919135f..0b98816 100644
--- a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/model/CellModel.java
+++ b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/model/CellModel.java
@@ -28,6 +28,9 @@ import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlValue;
 
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.hadoop.hbase.util.ByteStringer;
 import org.apache.hadoop.hbase.classification.InterfaceAudience;
 import org.apache.hadoop.hbase.CellUtil;
@@ -207,4 +210,41 @@ public class CellModel implements ProtobufMessageHandler, 
Serializable {
     }
     return this;
   }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (obj == null) {
+      return false;
+    }
+    if (obj == this) {
+      return true;
+    }
+    if (obj.getClass() != getClass()) {
+      return false;
+    }
+    CellModel cellModel = (CellModel) obj;
+    return new EqualsBuilder().
+        append(column, cellModel.column).
+        append(timestamp, cellModel.timestamp).
+        append(value, cellModel.value).
+        isEquals();
+  }
+
+  @Override
+  public int hashCode() {
+    return new HashCodeBuilder().
+        append(column).
+        append(timestamp).
+        append(value).
+        toHashCode();
+  }
+
+  @Override
+  public String toString() {
+    return new ToStringBuilder(this).
+        append("column", column).
+        append("timestamp", timestamp).
+        append("value", value).
+        toString();
+  }
 }

http://git-wip-us.apache.org/repos/asf/hbase/blob/73e94567/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/model/RowModel.java
----------------------------------------------------------------------
diff --git 
a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/model/RowModel.java 
b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/model/RowModel.java
index 596c754..398d5e1 100644
--- a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/model/RowModel.java
+++ b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/model/RowModel.java
@@ -30,6 +30,9 @@ import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.hadoop.hbase.classification.InterfaceAudience;
 import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
 import org.codehaus.jackson.annotate.JsonProperty;
@@ -148,4 +151,38 @@ public class RowModel implements ProtobufMessageHandler, 
Serializable {
     throw new UnsupportedOperationException(
         "no protobuf equivalent to RowModel");
   }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (obj == null) {
+      return false;
+    }
+    if (obj == this) {
+      return true;
+    }
+    if (obj.getClass() != getClass()) {
+      return false;
+    }
+    RowModel rowModel = (RowModel) obj;
+    return new EqualsBuilder().
+        append(key, rowModel.key).
+        append(cells, rowModel.cells).
+        isEquals();
+  }
+
+  @Override
+  public int hashCode() {
+    return new HashCodeBuilder().
+        append(key).
+        append(cells).
+        toHashCode();
+  }
+
+  @Override
+  public String toString() {
+    return new ToStringBuilder(this).
+        append("key", key).
+        append("cells", cells).
+        toString();
+  }
 }

http://git-wip-us.apache.org/repos/asf/hbase/blob/73e94567/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellModel.java
----------------------------------------------------------------------
diff --git 
a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellModel.java
 
b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellModel.java
index cdc6ee5..d9e45df 100644
--- 
a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellModel.java
+++ 
b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellModel.java
@@ -19,10 +19,14 @@
 
 package org.apache.hadoop.hbase.rest.model;
 
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
 import org.apache.hadoop.hbase.testclassification.RestTests;
 import org.apache.hadoop.hbase.testclassification.SmallTests;
 import org.apache.hadoop.hbase.util.Bytes;
 
+import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
 @Category({RestTests.class, SmallTests.class})
@@ -71,5 +75,25 @@ public class TestCellModel extends TestModelBase<CellModel> {
     checkModel(fromPB(AS_PB));
   }
 
+  @Test
+  public void testEquals() throws Exception {
+    CellModel cellModel1 = buildTestModel();
+    CellModel cellModel2 = buildTestModel();
+
+    assertEquals(cellModel1, cellModel2);
+
+    CellModel cellModel3 = new CellModel();
+    assertFalse(cellModel1.equals(cellModel3));
+  }
+
+  @Test
+  public void testToString() throws Exception {
+    String expectedColumn = ToStringBuilder.reflectionToString(COLUMN, 
ToStringStyle.SIMPLE_STYLE);
+
+    CellModel cellModel = buildTestModel();
+    System.out.println(cellModel);
+
+    assertTrue(StringUtils.contains(cellModel.toString(), expectedColumn));
+  }
 }
 

http://git-wip-us.apache.org/repos/asf/hbase/blob/73e94567/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/model/TestRowModel.java
----------------------------------------------------------------------
diff --git 
a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/model/TestRowModel.java 
b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/model/TestRowModel.java
index b5dcf2f..5c2b90c 100644
--- 
a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/model/TestRowModel.java
+++ 
b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/model/TestRowModel.java
@@ -23,10 +23,14 @@ import java.util.Iterator;
 
 import javax.xml.bind.JAXBContext;
 
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
 import org.apache.hadoop.hbase.testclassification.RestTests;
 import org.apache.hadoop.hbase.testclassification.SmallTests;
 import org.apache.hadoop.hbase.util.Bytes;
 
+import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
 @Category({RestTests.class, SmallTests.class})
@@ -72,5 +76,26 @@ public class TestRowModel extends TestModelBase<RowModel> {
   public void testFromPB() throws Exception {
     //do nothing row model has no PB
   }
+
+  @Test
+  public void testEquals() throws Exception {
+    RowModel rowModel1 = buildTestModel();
+    RowModel rowModel2 = buildTestModel();
+
+    assertEquals(rowModel1, rowModel2);
+
+    RowModel rowModel3 = new RowModel();
+    assertFalse(rowModel1.equals(rowModel3));
+  }
+
+  @Test
+  public void testToString() throws Exception {
+    String expectedRowKey = ToStringBuilder.reflectionToString(ROW1, 
ToStringStyle.SIMPLE_STYLE);
+
+    RowModel rowModel = buildTestModel();
+    System.out.println(rowModel);
+
+    assertTrue(StringUtils.contains(rowModel.toString(), expectedRowKey));
+  }
 }
 

Reply via email to