Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MockGraph implements Graph<String> {
- private final Map<String, MockNode> nodes = new ConcurrentHashMap<String, MockNode>();
- private final MockNode root = new MockNode();
- public Iterable<String> getAllNodes() {
- return Collections.unmodifiableCollection(nodes.keySet());
- }
- public Iterable<String> getRootNodes() {
- return Collections.unmodifiableCollection(root.edges);
- }
- public Iterable<String> getConnectedNodesOf(String node) {
- return Collections.unmodifiableCollection(getNode(node).edges);
- }
- public void createNode(String node) {
- nodes.put(node, new MockNode());
- }
- public void createRootNode(String node) {
- createNode(node);
- root.edges.add(node);
- }
- public void removeNode(String node) {
- nodes.remove(node);
- root.edges.remove(node);
- }
- public void createDirectedEdge(@Nullable String from, String to) {
- getNode(from).edges.add(to);
- }
- public void removeDirectedEdge(@Nullable String from, String to) {
- getNode(from).edges.remove(to);
- }
- public long getStatus(String node) {
- return getNode(node).status;
- }
- public void setStatus(String node, long status) {
- getNode(node).status = status;
- }
- private MockNode getNode(@Nullable String node) {
- if (node == null) {
- return root;
- }
- MockNode n = nodes.get(node);
- if (n == null) {
- n = new MockNode();
- }
- return n;
- }
- private class MockNode {
- public long status = NULL_STATUS;
- public final List<String> edges = new CopyOnWriteArrayList<String>();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement