Advertisement
Guest User

Untitled

a guest
Nov 30th, 2008
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. public class MockGraph implements Graph<String> {
  2.  
  3.     private final Map<String, MockNode> nodes = new ConcurrentHashMap<String, MockNode>();
  4.     private final MockNode root = new MockNode();
  5.  
  6.     public Iterable<String> getAllNodes() {
  7.         return Collections.unmodifiableCollection(nodes.keySet());
  8.     }
  9.  
  10.     public Iterable<String> getRootNodes() {
  11.         return Collections.unmodifiableCollection(root.edges);
  12.     }
  13.  
  14.     public Iterable<String> getConnectedNodesOf(String node) {
  15.         return Collections.unmodifiableCollection(getNode(node).edges);
  16.     }
  17.  
  18.     public void createNode(String node) {
  19.         nodes.put(node, new MockNode());
  20.     }
  21.  
  22.     public void createRootNode(String node) {
  23.         createNode(node);
  24.         root.edges.add(node);
  25.     }
  26.  
  27.     public void removeNode(String node) {
  28.         nodes.remove(node);
  29.         root.edges.remove(node);
  30.     }
  31.  
  32.     public void createDirectedEdge(@Nullable String from, String to) {
  33.         getNode(from).edges.add(to);
  34.     }
  35.  
  36.     public void removeDirectedEdge(@Nullable String from, String to) {
  37.         getNode(from).edges.remove(to);
  38.     }
  39.  
  40.     public long getStatus(String node) {
  41.         return getNode(node).status;
  42.     }
  43.  
  44.     public void setStatus(String node, long status) {
  45.         getNode(node).status = status;
  46.     }
  47.  
  48.     private MockNode getNode(@Nullable String node) {
  49.         if (node == null) {
  50.             return root;
  51.         }
  52.         MockNode n = nodes.get(node);
  53.         if (n == null) {
  54.             n = new MockNode();
  55.         }
  56.         return n;
  57.     }
  58.  
  59.     private class MockNode {
  60.         public long status = NULL_STATUS;
  61.         public final List<String> edges = new CopyOnWriteArrayList<String>();
  62.     }
  63. }
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement