Advertisement
Guest User

Untitled

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