From b0e04a9162d9c2455bd61ceee1297c11afacfe81 Mon Sep 17 00:00:00 2001 From: Charles Allen Date: Mon, 11 Jan 2016 14:36:20 -0800 Subject: [PATCH] Add hashCode and equals to DruidNode --- .../main/java/io/druid/server/DruidNode.java | 31 +++++++++++++++++++ .../java/io/druid/server/DruidNodeTest.java | 26 ++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/server/src/main/java/io/druid/server/DruidNode.java b/server/src/main/java/io/druid/server/DruidNode.java index a195adfa330..528a9939091 100644 --- a/server/src/main/java/io/druid/server/DruidNode.java +++ b/server/src/main/java/io/druid/server/DruidNode.java @@ -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; + } } diff --git a/server/src/test/java/io/druid/server/DruidNodeTest.java b/server/src/test/java/io/druid/server/DruidNodeTest.java index 32d3419a39e..10825d719f1 100644 --- a/server/src/test/java/io/druid/server/DruidNodeTest.java +++ b/server/src/test/java/io/druid/server/DruidNodeTest.java @@ -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()); + } }