Skip to content

Commit

Permalink
Merge pull request apache#2248 from metamx/druidNodeHashEquals
Browse files Browse the repository at this point in the history
Add hashCode and equals to DruidNode
  • Loading branch information
xvrl committed Jan 15, 2016
2 parents 4c014c1 + b0e04a9 commit dc1a62c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
31 changes: 31 additions & 0 deletions server/src/main/java/io/druid/server/DruidNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,35 @@ public String toString()
", port=" + port +
'}';
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

DruidNode node = (DruidNode) o;

if (port != node.port) {
return false;
}
if (!serviceName.equals(node.serviceName)) {
return false;
}
return host.equals(node.host);

}

@Override
public int hashCode()
{
int result = serviceName.hashCode();
result = 31 * result + host.hashCode();
result = 31 * result + port;
return result;
}
}
26 changes: 26 additions & 0 deletions server/src/test/java/io/druid/server/DruidNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,30 @@ public void testConflictingPortsNonsense() throws Exception
{
new DruidNode("test/service", "[2001:db8:85a3::8a2e:370:7334]:123", 456);
}

@Test
public void testEquals() throws Exception
{
final String serviceName = "serviceName";
final String host = "some.host";
final int port = 9898;
Assert.assertEquals(new DruidNode(serviceName, host, port), new DruidNode(serviceName, host, port));
Assert.assertNotEquals(new DruidNode(serviceName, host, port), new DruidNode(serviceName, host, -1));
Assert.assertNotEquals(new DruidNode(serviceName, host, port), new DruidNode(serviceName, "other.host", port));
Assert.assertNotEquals(new DruidNode(serviceName, host, port), new DruidNode("otherServiceName", host, port));
}

@Test
public void testHashCode() throws Exception
{

final String serviceName = "serviceName";
final String host = "some.host";
final int port = 9898;
Assert.assertEquals(new DruidNode(serviceName, host, port).hashCode(), new DruidNode(serviceName, host, port).hashCode());
// Potential hash collision if hashCode method ever changes
Assert.assertNotEquals(new DruidNode(serviceName, host, port).hashCode(), new DruidNode(serviceName, host, -1).hashCode());
Assert.assertNotEquals(new DruidNode(serviceName, host, port).hashCode(), new DruidNode(serviceName, "other.host", port).hashCode());
Assert.assertNotEquals(new DruidNode(serviceName, host, port).hashCode(), new DruidNode("otherServiceName", host, port).hashCode());
}
}

0 comments on commit dc1a62c

Please sign in to comment.