Keeping with some good coding standards - Code to Interfaces not implementations

Changed the use of ArrayList to List and HashMap to Map
This commit is contained in:
Joshua Kissoon 2014-02-26 11:57:59 +05:30
parent 9f14c66a31
commit b5e89c6ddb
8 changed files with 32 additions and 27 deletions

View File

@ -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<Integer, Receiver> receivers;
private final Map<Integer, Receiver> receivers;
private final Timer timer; // Schedule future tasks
private final HashMap<Integer, TimerTask> tasks; // Keep track of scheduled tasks
private final Map<Integer, TimerTask> tasks; // Keep track of scheduled tasks
private final Node localNode;

View File

@ -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<NodeId, ArrayList<StorageEntry>> entries;
private final Map<NodeId, List<StorageEntry>> entries;
{
@ -60,7 +62,7 @@ public class StorageEntryManager
*
* @return List of content for the specific search parameters
*/
public ArrayList<StorageEntry> get(NodeId key)
public List<StorageEntry> get(NodeId key)
{
return this.entries.get(key);
}

View File

@ -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<Node> nodes = this.localNode.getRoutingTable().findClosest(msg.getLookupId(), Configuration.K);
List<Node> nodes = this.localNode.getRoutingTable().findClosest(msg.getLookupId(), Configuration.K);
System.out.println("\nClosest Nodes: ");
for (Node n : nodes)

View File

@ -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<Node> nodes;
private List<Node> nodes;
public NodeReplyMessage(Node origin, ArrayList<Node> nodes)
public NodeReplyMessage(Node origin, List<Node> nodes)
{
this.origin = origin;
this.nodes = nodes;
@ -80,7 +79,7 @@ public class NodeReplyMessage implements Message
return CODE;
}
public ArrayList<Node> getNodes()
public List<Node> getNodes()
{
return this.nodes;
}

View File

@ -45,7 +45,7 @@ public class NodeLookupOperation implements Operation, Receiver
private final SortedMap<Node, Byte> nodes;
/* Tracks messages in transit and awaiting reply */
private final HashMap<Integer, Node> messagesTransiting;
private final Map<Integer, Node> 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<Node> execute() throws IOException, RoutingException
public synchronized List<Node> 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<Node> unasked = this.closestNodesNotFailed(UNASKED);
List<Node> 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<Node> closestNodes(Byte status)
private List<Node> closestNodes(Byte status)
{
ArrayList<Node> closestNodes = new ArrayList<>(Configuration.K);
List<Node> 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<Node> closestNodesNotFailed(Byte status)
private List<Node> closestNodesNotFailed(Byte status)
{
ArrayList<Node> closestNodes = new ArrayList<>(Configuration.K);
List<Node> closestNodes = new ArrayList<>(Configuration.K);
int remainingSpaces = Configuration.K;
for (Map.Entry e : this.nodes.entrySet())

View File

@ -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<Node> nodes = new NodeLookupOperation(this.server, this.localNode, this.content.getKey()).execute();
List<Node> nodes = new NodeLookupOperation(this.server, this.localNode, this.content.getKey()).execute();
System.out.println("Nodes to put content on: " + nodes);
/* Create the message */

View File

@ -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<NodeId, Node> nodes;
private final Map<NodeId, Node> nodes;
{
@ -85,7 +87,7 @@ public class KadBucket implements Bucket
this.nodes.remove(n.getNodeId());
}
public ArrayList<Node> getNodes()
public List<Node> getNodes()
{
return new ArrayList<>(this.nodes.values());
}

View File

@ -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<Contact> An ArrayList of num contacts closest to target
* @return List<Contact> A List of num contacts closest to target
*/
public ArrayList<Node> findClosest(NodeId target, int num)
public List<Node> findClosest(NodeId target, int num)
{
ArrayList<Node> closest = new ArrayList<>(num);
List<Node> 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<Node> getAllNodes()
public List<Node> getAllNodes()
{
ArrayList<Node> nodes = new ArrayList<>();
List<Node> nodes = new ArrayList<>();
for (KadBucket b : this.buckets)
{