From 43e91c2476e19f58744edb1263a79e8d3e67bc47 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 13 May 2016 18:47:01 +0200 Subject: [PATCH] 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 --- .../io/netty/buffer/DefaultByteBufHolder.java | 16 ++++++++++++++++ .../netty/buffer/DefaultByteBufHolderTest.java | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/buffer/src/main/java/io/netty/buffer/DefaultByteBufHolder.java b/buffer/src/main/java/io/netty/buffer/DefaultByteBufHolder.java index 263538778f..1e19a5eacb 100644 --- a/buffer/src/main/java/io/netty/buffer/DefaultByteBufHolder.java +++ b/buffer/src/main/java/io/netty/buffer/DefaultByteBufHolder.java @@ -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(); + } } diff --git a/buffer/src/test/java/io/netty/buffer/DefaultByteBufHolderTest.java b/buffer/src/test/java/io/netty/buffer/DefaultByteBufHolderTest.java index 0b462c4f54..4c60d0e7b6 100644 --- a/buffer/src/test/java/io/netty/buffer/DefaultByteBufHolderTest.java +++ b/buffer/src/test/java/io/netty/buffer/DefaultByteBufHolderTest.java @@ -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(); + } + } }