From b5e89c6ddbfad339f803c6a753fa659c7826d161 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Wed, 26 Feb 2014 11:57:59 +0530 Subject: [PATCH] Keeping with some good coding standards - Code to Interfaces not implementations Changed the use of ArrayList to List and HashMap to Map --- src/kademlia/core/KadServer.java | 5 +++-- src/kademlia/dht/StorageEntryManager.java | 6 ++++-- src/kademlia/message/NodeLookupReceiver.java | 4 ++-- src/kademlia/message/NodeReplyMessage.java | 9 ++++----- src/kademlia/operation/NodeLookupOperation.java | 14 +++++++------- src/kademlia/operation/StoreOperation.java | 4 ++-- src/kademlia/routing/KadBucket.java | 6 ++++-- src/kademlia/routing/RoutingTable.java | 11 ++++++----- 8 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/kademlia/core/KadServer.java b/src/kademlia/core/KadServer.java index 5c07fac..df54107 100644 --- a/src/kademlia/core/KadServer.java +++ b/src/kademlia/core/KadServer.java @@ -14,6 +14,7 @@ import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; import java.util.HashMap; +import java.util.Map; import java.util.Random; import java.util.Timer; import java.util.TimerTask; @@ -32,9 +33,9 @@ public class KadServer private final int udpPort; private final DatagramSocket socket; private boolean isRunning; - private final HashMap receivers; + private final Map receivers; private final Timer timer; // Schedule future tasks - private final HashMap tasks; // Keep track of scheduled tasks + private final Map tasks; // Keep track of scheduled tasks private final Node localNode; diff --git a/src/kademlia/dht/StorageEntryManager.java b/src/kademlia/dht/StorageEntryManager.java index e54d79c..674f74b 100644 --- a/src/kademlia/dht/StorageEntryManager.java +++ b/src/kademlia/dht/StorageEntryManager.java @@ -2,6 +2,8 @@ package kademlia.dht; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import java.util.Map; import kademlia.node.NodeId; /** @@ -15,7 +17,7 @@ import kademlia.node.NodeId; public class StorageEntryManager { - private final HashMap> entries; + private final Map> entries; { @@ -60,7 +62,7 @@ public class StorageEntryManager * * @return List of content for the specific search parameters */ - public ArrayList get(NodeId key) + public List get(NodeId key) { return this.entries.get(key); } diff --git a/src/kademlia/message/NodeLookupReceiver.java b/src/kademlia/message/NodeLookupReceiver.java index 5119ec8..f3a4901 100644 --- a/src/kademlia/message/NodeLookupReceiver.java +++ b/src/kademlia/message/NodeLookupReceiver.java @@ -6,7 +6,7 @@ package kademlia.message; import java.io.IOException; -import java.util.ArrayList; +import java.util.List; import kademlia.core.Configuration; import kademlia.core.KadServer; import kademlia.node.Node; @@ -45,7 +45,7 @@ public class NodeLookupReceiver implements Receiver this.localNode.getRoutingTable().insert(origin); /* Find nodes closest to the LookupId */ - ArrayList nodes = this.localNode.getRoutingTable().findClosest(msg.getLookupId(), Configuration.K); + List nodes = this.localNode.getRoutingTable().findClosest(msg.getLookupId(), Configuration.K); System.out.println("\nClosest Nodes: "); for (Node n : nodes) diff --git a/src/kademlia/message/NodeReplyMessage.java b/src/kademlia/message/NodeReplyMessage.java index 746867e..3312c85 100644 --- a/src/kademlia/message/NodeReplyMessage.java +++ b/src/kademlia/message/NodeReplyMessage.java @@ -5,12 +5,11 @@ */ package kademlia.message; -import java.io.DataInput; import java.io.DataInputStream; -import java.io.DataOutput; import java.io.DataOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.List; import kademlia.node.Node; public class NodeReplyMessage implements Message @@ -18,9 +17,9 @@ public class NodeReplyMessage implements Message private Node origin; public static final byte CODE = 0x04; - private ArrayList nodes; + private List nodes; - public NodeReplyMessage(Node origin, ArrayList nodes) + public NodeReplyMessage(Node origin, List nodes) { this.origin = origin; this.nodes = nodes; @@ -80,7 +79,7 @@ public class NodeReplyMessage implements Message return CODE; } - public ArrayList getNodes() + public List getNodes() { return this.nodes; } diff --git a/src/kademlia/operation/NodeLookupOperation.java b/src/kademlia/operation/NodeLookupOperation.java index d7c29a6..26bd59c 100644 --- a/src/kademlia/operation/NodeLookupOperation.java +++ b/src/kademlia/operation/NodeLookupOperation.java @@ -45,7 +45,7 @@ public class NodeLookupOperation implements Operation, Receiver private final SortedMap nodes; /* Tracks messages in transit and awaiting reply */ - private final HashMap messagesTransiting; + private final Map messagesTransiting; /* Used to sort nodes */ private final Comparator comparator; @@ -83,7 +83,7 @@ public class NodeLookupOperation implements Operation, Receiver * @throws kademlia.exceptions.RoutingException */ @Override - public synchronized ArrayList execute() throws IOException, RoutingException + public synchronized List execute() throws IOException, RoutingException { try { @@ -159,7 +159,7 @@ public class NodeLookupOperation implements Operation, Receiver } /* Get unqueried nodes among the K closest seen that have not FAILED */ - ArrayList unasked = this.closestNodesNotFailed(UNASKED); + List unasked = this.closestNodesNotFailed(UNASKED); for (Node nn : unasked) { System.out.println(nn.getNodeId()); @@ -198,9 +198,9 @@ public class NodeLookupOperation implements Operation, Receiver * * @return The K closest nodes to the target lookupId given that have the specified status */ - private ArrayList closestNodes(Byte status) + private List closestNodes(Byte status) { - ArrayList closestNodes = new ArrayList<>(Configuration.K); + List closestNodes = new ArrayList<>(Configuration.K); int remainingSpaces = Configuration.K; for (Map.Entry e : this.nodes.entrySet()) @@ -227,9 +227,9 @@ public class NodeLookupOperation implements Operation, Receiver * * @return A List of the closest nodes */ - private ArrayList closestNodesNotFailed(Byte status) + private List closestNodesNotFailed(Byte status) { - ArrayList closestNodes = new ArrayList<>(Configuration.K); + List closestNodes = new ArrayList<>(Configuration.K); int remainingSpaces = Configuration.K; for (Map.Entry e : this.nodes.entrySet()) diff --git a/src/kademlia/operation/StoreOperation.java b/src/kademlia/operation/StoreOperation.java index 1d8a963..07ce27c 100644 --- a/src/kademlia/operation/StoreOperation.java +++ b/src/kademlia/operation/StoreOperation.java @@ -1,7 +1,7 @@ package kademlia.operation; import java.io.IOException; -import java.util.ArrayList; +import java.util.List; import kademlia.core.KadServer; import kademlia.dht.KadContent; import kademlia.message.Message; @@ -37,7 +37,7 @@ public class StoreOperation implements Operation public synchronized Object execute() throws IOException { /* Get the nodes on which we need to store the content */ - ArrayList nodes = new NodeLookupOperation(this.server, this.localNode, this.content.getKey()).execute(); + List nodes = new NodeLookupOperation(this.server, this.localNode, this.content.getKey()).execute(); System.out.println("Nodes to put content on: " + nodes); /* Create the message */ diff --git a/src/kademlia/routing/KadBucket.java b/src/kademlia/routing/KadBucket.java index d846f46..925668a 100644 --- a/src/kademlia/routing/KadBucket.java +++ b/src/kademlia/routing/KadBucket.java @@ -7,6 +7,8 @@ package kademlia.routing; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import java.util.Map; import kademlia.node.Node; import kademlia.node.NodeId; @@ -14,7 +16,7 @@ public class KadBucket implements Bucket { private final int depth; - private final HashMap nodes; + private final Map nodes; { @@ -85,7 +87,7 @@ public class KadBucket implements Bucket this.nodes.remove(n.getNodeId()); } - public ArrayList getNodes() + public List getNodes() { return new ArrayList<>(this.nodes.values()); } diff --git a/src/kademlia/routing/RoutingTable.java b/src/kademlia/routing/RoutingTable.java index 3feb30c..fe5ad63 100644 --- a/src/kademlia/routing/RoutingTable.java +++ b/src/kademlia/routing/RoutingTable.java @@ -6,6 +6,7 @@ package kademlia.routing; import java.util.ArrayList; +import java.util.List; import kademlia.node.Node; import kademlia.node.NodeId; @@ -72,11 +73,11 @@ public class RoutingTable * @param target The NodeId to find contacts close to * @param num The number of contacts to find * - * @return ArrayList An ArrayList of num contacts closest to target + * @return List A List of num contacts closest to target */ - public ArrayList findClosest(NodeId target, int num) + public List findClosest(NodeId target, int num) { - ArrayList closest = new ArrayList<>(num); + List closest = new ArrayList<>(num); /* Get the bucket number to search for closest from */ int bucketNumber = this.localNode.getNodeId().xor(target).getFirstSetBitIndex() - 1; @@ -144,9 +145,9 @@ public class RoutingTable return closest; } - public ArrayList getAllNodes() + public List getAllNodes() { - ArrayList nodes = new ArrayList<>(); + List nodes = new ArrayList<>(); for (KadBucket b : this.buckets) {