From 73e945670270de187be337037f75577ccc1a770b Mon Sep 17 00:00:00 2001 From: tedyu Date: Mon, 17 Oct 2016 12:42:06 -0700 Subject: [PATCH] HBASE-16326 CellModel / RowModel should override 'equals', 'hashCode' and 'toString' (Minwoo Kang) --- .../hadoop/hbase/rest/model/CellModel.java | 40 +++++++++++++++++++ .../hadoop/hbase/rest/model/RowModel.java | 37 +++++++++++++++++ .../hbase/rest/model/TestCellModel.java | 24 +++++++++++ .../hadoop/hbase/rest/model/TestRowModel.java | 25 ++++++++++++ 4 files changed, 126 insertions(+) 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 919135fe414..0b98816e239 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(); + } } 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 596c7547af0..398d5e1ce70 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(); + } } 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 cdc6ee5b31c..d9e45df944a 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 { 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)); + } } 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 b5dcf2fcb26..5c2b90c41f5 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 { 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)); + } }