Correctly implement DefaultByteBufHolder.equals(...) and hashCode()

Motivation:

DefaultByteBufHolder.equals(...) and hashCode() should be implemented so it works correctly with instances that share the same content.

Modifications:

Add implementations and a unit-test.

Result:

Have correctly working equals(...) and hashCode() method
This commit is contained in:
Norman Maurer 2016-05-13 18:47:01 +02:00
parent e59d0e9efb
commit 43e91c2476
2 changed files with 29 additions and 0 deletions

View File

@ -134,4 +134,20 @@ public class DefaultByteBufHolder implements ByteBufHolder {
public String toString() {
return StringUtil.simpleClassName(this) + '(' + contentToString() + ')';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof ByteBufHolder) {
return data.equals(((ByteBufHolder) o).content());
}
return false;
}
@Override
public int hashCode() {
return data.hashCode();
}
}

View File

@ -29,4 +29,17 @@ public class DefaultByteBufHolderTest {
assertTrue(holder.release());
assertNotNull(holder.toString());
}
@Test
public void testEqualsAndHashCode() {
ByteBufHolder holder = new DefaultByteBufHolder(Unpooled.EMPTY_BUFFER);
ByteBufHolder copy = holder.copy();
try {
assertEquals(holder, copy);
assertEquals(holder.hashCode(), copy.hashCode());
} finally {
holder.release();
copy.release();
}
}
}