Skip to content

Commit

Permalink
Correctly implement DefaultByteBufHolder.equals(...) and hashCode()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
normanmaurer committed May 20, 2016
1 parent e59d0e9 commit 43e91c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions buffer/src/main/java/io/netty/buffer/DefaultByteBufHolder.java
Expand Up @@ -134,4 +134,20 @@ protected final String contentToString() {
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();
}
}
13 changes: 13 additions & 0 deletions buffer/src/test/java/io/netty/buffer/DefaultByteBufHolderTest.java
Expand Up @@ -29,4 +29,17 @@ public void testToString() {
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();
}
}
}

0 comments on commit 43e91c2

Please sign in to comment.