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

This commit is contained in:
tedyu 2016-10-17 12:42:06 -07:00
parent c6e9dabe62
commit 73e9456702
4 changed files with 126 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}