mirror of https://github.com/apache/druid.git
Merge pull request #2248 from metamx/druidNodeHashEquals
Add hashCode and equals to DruidNode
This commit is contained in:
commit
dc1a62c3d9
|
@ -156,4 +156,35 @@ public class DruidNode
|
||||||
", port=" + port +
|
", 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,4 +114,30 @@ public class DruidNodeTest
|
||||||
{
|
{
|
||||||
new DruidNode("test/service", "[2001:db8:85a3::8a2e:370:7334]:123", 456);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue