Refactor Node from interface to value type

The modeling of BootstrapNode as an enum implementing the Node interface
became awkward, requiring an ugly, package-private 'NodeImpl' class and
other problems.

This change eliminates NodeImpl, refactors Node from being an interface
to being a value type [1,2], and refactors BootstrapNode from being an
enum to being an interface (the #all method there takes the place of
what was the enum's inherited #values method). This is slightly more
verbose in the end than being modeled as an enum, but in the end, we
were never using BootstrapNode as an enum (e.g., never used in switch
statements or == equality comparisons, etc).

[1]: http://blog.joda.org/2014/03/valjos-value-java-objects.html
[2]: http://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html
This commit is contained in:
Chris Beams 2014-11-06 13:52:59 +01:00
parent 640a736ec3
commit 36b3b82e52
No known key found for this signature in database
GPG key ID: 3D214F8F5BC5ED73
4 changed files with 63 additions and 106 deletions

View file

@ -62,7 +62,7 @@ public class SeedNode {
}
final Set<PeerAddress> peerAddresses = new HashSet<>();
for (Node node : BootstrapNode.values()) {
for (Node node : BootstrapNode.all()) {
if (!node.getId().equals(seedID)) {
try {
peerAddresses.add(new PeerAddress(Number160.createHash(node.getId()), node.getIp(),

View file

@ -17,28 +17,16 @@
package io.bitsquare.network;
public enum BootstrapNode implements Node {
LOCALHOST(Node.at("localhost", "127.0.0.1")),
DIGITAL_OCEAN1(Node.at("digitalocean1.bitsquare.io", "188.226.179.109"));
import java.util.Arrays;
import java.util.List;
private final Node self;
public interface BootstrapNode {
Node LOCALHOST = Node.at("localhost", "127.0.0.1");
Node DIGITAL_OCEAN1 = Node.at("digitalocean1.bitsquare.io", "188.226.179.109");
BootstrapNode(Node self) {
this.self = self;
}
@Override
public String getId() {
return self.getId();
}
@Override
public String getIp() {
return self.getIp();
}
@Override
public int getPort() {
return self.getPort();
static List<Node> all() {
return Arrays.asList(
LOCALHOST, DIGITAL_OCEAN1
);
}
}

View file

@ -17,21 +17,66 @@
package io.bitsquare.network;
public interface Node {
import com.google.common.base.Objects;
public final class Node {
public static final int DEFAULT_PORT = 5000;
String getId();
private final String id;
private final String ip;
private final int port;
String getIp();
private Node(String id, String ip, int port) {
this.id = id;
this.ip = ip;
this.port = port;
}
int getPort();
static Node at(String id, String ip) {
public 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);
public static Node at(String id, String ip, int port) {
return new Node(id, ip, port);
}
public String getId() {
return id;
}
public String getIp() {
return ip;
}
public int getPort() {
return port;
}
@Override
public boolean equals(Object object) {
if (this == object)
return true;
if (object == null || getClass() != object.getClass())
return false;
Node that = (Node) 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();
}
}

View file

@ -1,76 +0,0 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
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();
}
}