Add hashCode and equals to DruidNode

This commit is contained in:
Charles Allen 2016-01-11 14:36:20 -08:00
parent 8d1686039a
commit b0e04a9162
2 changed files with 57 additions and 0 deletions

View File

@ -156,4 +156,35 @@ public class DruidNode
", 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;
}
}

View File

@ -114,4 +114,30 @@ public class DruidNodeTest
{
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());
}
}