Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. public class NodeLinkerManager implements Runnable {
  2.     private Map<Node, Node[]> nodeSet = new HashMap<>();
  3.     private Map<Class<? extends Node>, Node> allNodes = new HashMap<>();
  4.     private Node[] currentNodeSet;
  5.    
  6.     public void initCurrentSet() {
  7.         System.out.println(allNodes.size());
  8.         for(Node node : allNodes.values()) {
  9.             System.out.println(node + " " + node.canProcess());
  10.             if(node.canProcess()) {
  11.                 currentNodeSet = nodeSet.get(node);
  12.                 return;
  13.             }
  14.         }
  15.        
  16.         throw new IllegalStateException("No nodes can be processed");
  17.     }
  18.  
  19.     public void add(@SuppressWarnings("unchecked") Class<? extends Node>...nodeTypes) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  20.         for(Class<? extends Node> nodeType : nodeTypes)
  21.             if(nodeType != null && !nodeSet.containsKey(nodeType)) {
  22.                 Class<? extends Node>[] linkedNodeTypes = nodeType.getAnnotation(Linked.class).nodes();
  23.                 Node node = createAndAddNode(nodeType);
  24.                 nodeSet.put(node, linkNodes(linkedNodeTypes));
  25.             }
  26.     }
  27.    
  28.     private Node[] linkNodes(Class<? extends Node>[] linkedNodeTypes) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  29.         if(linkedNodeTypes.length == 0)
  30.             return new Node[0];
  31.            
  32.         int linkedLength = linkedNodeTypes.length + 1;
  33.         Node[] linkedNodes = new Node[linkedLength];
  34.        
  35.         for(int i = 0; i < linkedLength; i++) {
  36.             Class<? extends Node> currentNodeType = linkedNodeTypes[i];
  37.             linkedNodes[i] = allNodes.containsKey(currentNodeType) ? allNodes.get(currentNodeType) : createAndAddNode(currentNodeType);
  38.         }
  39.        
  40.         return linkedNodes;
  41.     }
  42.    
  43.     private Node createAndAddNode(Class<? extends Node> nodeType) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  44.         Node node = nodeType.getConstructor().newInstance();
  45.         allNodes.put(nodeType, node);
  46.         return node;
  47.     }
  48.    
  49.     public void run() {
  50.         System.out.println(currentNodeSet);
  51.         for(Node node : currentNodeSet) {
  52.             if(node.canProcess()) {
  53.                 currentNodeSet = nodeSet.get(node.getClass());
  54.                 node.process();
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement