Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NodeLinkerManager implements Runnable {
- private Map<Node, Node[]> nodeSet = new HashMap<>();
- private Map<Class<? extends Node>, Node> allNodes = new HashMap<>();
- private Node[] currentNodeSet;
- public void initCurrentSet() {
- System.out.println(allNodes.size());
- for(Node node : allNodes.values()) {
- System.out.println(node + " " + node.canProcess());
- if(node.canProcess()) {
- currentNodeSet = nodeSet.get(node);
- return;
- }
- }
- throw new IllegalStateException("No nodes can be processed");
- }
- public void add(@SuppressWarnings("unchecked") Class<? extends Node>...nodeTypes) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
- for(Class<? extends Node> nodeType : nodeTypes)
- if(nodeType != null && !nodeSet.containsKey(nodeType)) {
- Class<? extends Node>[] linkedNodeTypes = nodeType.getAnnotation(Linked.class).nodes();
- Node node = createAndAddNode(nodeType);
- nodeSet.put(node, linkNodes(linkedNodeTypes));
- }
- }
- private Node[] linkNodes(Class<? extends Node>[] linkedNodeTypes) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
- if(linkedNodeTypes.length == 0)
- return new Node[0];
- int linkedLength = linkedNodeTypes.length + 1;
- Node[] linkedNodes = new Node[linkedLength];
- for(int i = 0; i < linkedLength; i++) {
- Class<? extends Node> currentNodeType = linkedNodeTypes[i];
- linkedNodes[i] = allNodes.containsKey(currentNodeType) ? allNodes.get(currentNodeType) : createAndAddNode(currentNodeType);
- }
- return linkedNodes;
- }
- private Node createAndAddNode(Class<? extends Node> nodeType) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
- Node node = nodeType.getConstructor().newInstance();
- allNodes.put(nodeType, node);
- return node;
- }
- public void run() {
- System.out.println(currentNodeSet);
- for(Node node : currentNodeSet) {
- if(node.canProcess()) {
- currentNodeSet = nodeSet.get(node.getClass());
- node.process();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement