diff --git a/src/main/java/io/bitsquare/network/BootstrapNode.java b/src/main/java/io/bitsquare/network/BootstrapNode.java index a997072fd5..c4d37427b7 100644 --- a/src/main/java/io/bitsquare/network/BootstrapNode.java +++ b/src/main/java/io/bitsquare/network/BootstrapNode.java @@ -18,35 +18,27 @@ package io.bitsquare.network; public enum BootstrapNode implements Node { - LOCALHOST("localhost", "127.0.0.1"), - DIGITAL_OCEAN1("digitalocean1.bitsquare.io", "188.226.179.109"); + LOCALHOST(Node.at("localhost", "127.0.0.1")), + DIGITAL_OCEAN1(Node.at("digitalocean1.bitsquare.io", "188.226.179.109")); - private final String id; - private final String ip; - private final int port; + private final Node self; - BootstrapNode(String id, String ip) { - this(id, ip, DEFAULT_PORT); - } - - BootstrapNode(String id, String ip, int port) { - this.id = id; - this.ip = ip; - this.port = port; + BootstrapNode(Node self) { + this.self = self; } @Override public String getId() { - return id; + return self.getId(); } @Override public String getIp() { - return ip; + return self.getIp(); } @Override public int getPort() { - return port; + return self.getPort(); } } diff --git a/src/main/java/io/bitsquare/network/Node.java b/src/main/java/io/bitsquare/network/Node.java index 1018e84234..97e1953d9c 100644 --- a/src/main/java/io/bitsquare/network/Node.java +++ b/src/main/java/io/bitsquare/network/Node.java @@ -27,5 +27,11 @@ public interface Node { int getPort(); -} + static Node at(String id, String ip) { + return Node.at(id, ip, DEFAULT_PORT); + } + static Node at(String id, String ip, int port) { + return new NodeImpl(id, ip, port); + } +} diff --git a/src/main/java/io/bitsquare/network/NodeImpl.java b/src/main/java/io/bitsquare/network/NodeImpl.java new file mode 100644 index 0000000000..b98824d271 --- /dev/null +++ b/src/main/java/io/bitsquare/network/NodeImpl.java @@ -0,0 +1,76 @@ +/* + * This file is part of Bitsquare. + * + * Bitsquare is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bitsquare is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bitsquare. If not, see . + */ + +package io.bitsquare.network; + +import com.google.common.base.Objects; + +final class NodeImpl implements Node { + + private final String id; + private final String ip; + private final int port; + + NodeImpl(String id, String ip, int port) { + this.id = id; + this.ip = ip; + this.port = port; + } + + @Override + public String getId() { + return id; + } + + @Override + public String getIp() { + return ip; + } + + @Override + public int getPort() { + return port; + } + + @Override + public boolean equals(Object object) { + if (this == object) + return true; + + if (object == null || getClass() != object.getClass()) + return false; + + NodeImpl that = (NodeImpl) object; + return Objects.equal(this.id, that.id) && + Objects.equal(this.ip, that.ip) && + Objects.equal(this.port, that.port); + } + + @Override + public int hashCode() { + return Objects.hashCode(id, ip, port); + } + + @Override + public String toString() { + return Objects.toStringHelper(Node.class.getSimpleName()) + .add("id", id) + .add("ip", ip) + .add("port", port) + .toString(); + } +} diff --git a/src/test/java/io/bitsquare/network/NodeTests.java b/src/test/java/io/bitsquare/network/NodeTests.java new file mode 100644 index 0000000000..5c4d5433cd --- /dev/null +++ b/src/test/java/io/bitsquare/network/NodeTests.java @@ -0,0 +1,54 @@ +/* + * This file is part of Bitsquare. + * + * Bitsquare is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bitsquare is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bitsquare. If not, see . + */ + +package io.bitsquare.network; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +public class NodeTests { + + @Test + public void testEqualsAndHashCode() { + Node node1a = Node.at("bitsquare1.example.com", "203.0.113.1"); + Node node1b = Node.at("bitsquare1.example.com", "203.0.113.1"); + + assertThat(node1a, equalTo(node1a)); + + assertThat(node1a, equalTo(node1b)); + assertThat(node1b, equalTo(node1a)); + + assertThat(node1a, not((Object) equalTo(null))); + assertThat(node1a, not((Object) equalTo("not a node"))); + + assertThat(node1a, not(equalTo(Node.at("bitsquare2.example.com", node1a.getIp())))); + assertThat(node1a, not(equalTo(Node.at(node1a.getId(), "203.0.113.2")))); + assertThat(node1a, not(equalTo(Node.at(node1a.getId(), node1a.getIp(), Node.DEFAULT_PORT + 1)))); + + Node node2 = Node.at("bitsquare2.example.com", "203.0.113.2"); + assertThat(node1a.hashCode(), equalTo(node1b.hashCode())); + assertThat(node1a.hashCode(), not(equalTo(node2.hashCode()))); + } + + @Test + public void testToString() { + Node node = Node.at("bitsquare1.example.com", "203.0.113.1", 5001); + assertThat(node.toString(), equalTo("Node{id=bitsquare1.example.com, ip=203.0.113.1, port=5001}")); + } +} \ No newline at end of file