📄 objecttreestatemachine.java
字号:
/* nodes are full, therefore must change to different node type */ Node replacementNode = new WideNode(); Enumeration enumer = nodes.keys(); while(enumer.hasMoreElements()) { Integer i = (Integer) enumer.nextElement(); replacementNode = replacementNode.setNode((Node) nodes.get(i), i.intValue()); } replacementNode = replacementNode.setNode(n, index); return replacementNode; } public Node getNode(int index) { return (Node) nodes.get(new Integer(index)); } public boolean visitNodes(ObjectTreeStateMachineVisitor visitor) { if (!super.visitNodes(visitor)) return false; Enumeration enumer = nodes.keys(); while (enumer.hasMoreElements()) { Integer i = (Integer) enumer.nextElement(); Node child = (Node) nodes.get(i); if (!((Node) nodes.get(i)).visitNodes(visitor)) return false; } return true; } public void print(String indent) { super.print(indent); Enumeration enumer = nodes.keys(); System.out.print("["); while (enumer.hasMoreElements()) { Integer i = (Integer) enumer.nextElement(); ((Node) nodes.get(i)).print(indent+" "); System.out.print(i + ", "); } } } public static class BinaryNode extends Node { private Node leftNode, rightNode; private int leftIndex, rightIndex; /** Create a new binary node */ BinaryNode() { super(); leftNode = rightNode = null; } public int size() { if ((leftNode != null) && (rightNode != null)) return 2; else if ((leftNode != null) || (rightNode != null)) return 1; else return 0; } public Node setNode(Node n, int index) { boolean inserted = false; if ((leftNode == null) || (leftIndex == index)) { leftIndex = index; leftNode = n; inserted = true; } else if ((rightNode == null) || (rightIndex == index)) { rightIndex = index; rightNode = n; inserted = true; } if (inserted) return this; /* nodes are full, therefore must change to different node type */ Node replacementNode = new NarrowNode(); replacementNode = replacementNode.setNode(leftNode, leftIndex); replacementNode = replacementNode.setNode(rightNode, rightIndex); replacementNode = replacementNode.setNode(n, index); return replacementNode; } public Node getNode(int index) { if (leftIndex == index) return leftNode; else if (rightIndex == index) return rightNode; else return null; } public boolean visitNodes(ObjectTreeStateMachineVisitor visitor) { if (!super.visitNodes(visitor)) return false; if (rightNode != null) { if (!rightNode.visitNodes(visitor)) return false; } if (leftNode != null) { if (!leftNode.visitNodes(visitor)) return false; } return true; } public void print(String indent) { super.print(indent); System.out.println(indent+"["+leftIndex+" "+rightIndex+"]"); if (leftNode != null) leftNode.print(indent+" "); if (rightNode != null) rightNode.print(indent+" "); } } public static class SingularNode extends Node { private Node childNode; private int childIndex; /** Create a new single node */ SingularNode() { super(); childNode = null; } public int size() { if (childNode != null) return 1; return 0; } public Node setNode(Node n, int index) { if ((childNode == null) || (childIndex == index)) { childIndex = index; childNode = n; return this; } /* nodes are full, therefore must change to different node type */ Node replacementNode = new BinaryNode(); replacementNode = replacementNode.setNode(childNode, childIndex); replacementNode = replacementNode.setNode(n, index); return replacementNode; } public Node getNode(int index) { if (childIndex == index) return childNode; else return null; } public boolean visitNodes(ObjectTreeStateMachineVisitor visitor) { if (!super.visitNodes(visitor)) return false; if (childNode != null) { if (!childNode.visitNodes(visitor)) return false; } return true; } public void print(String indent) { super.print(indent); System.out.println(indent+"["+childIndex+"]"); if (childNode != null) childNode.print(indent+" "); } } /** * Clear the entire tree. */ public void clearAllObjects() { root = new WideNode(); } /** * Reset tree state to the top of the tree */ public void resetTreeState() { currentNode = root; currentParent = null; } /** * Step the tree to the next node in the tree along the path defined by index * Create a path if one doesn't exist * * @param index route to take to the next node in the tree. * @return true if the step is to an existing node, false if the step adds a node to the tree */ public boolean stepTree(int index) { if (currentNode == null) throw new IllegalStateException("Null Current Node?!"); Node child = currentNode.getNode(index); boolean childCreated = false; if (child == null) { // must create a new node childCreated = true; child = new SingularNode(); Node replacementNode = currentNode.setNode(child, index); if (replacementNode == null) // check if all hell broke loose throw new IllegalStateException("Null Current Node?!"); if ((currentParent != null) && (replacementNode != currentNode)) { currentParent.setNode(replacementNode, parentIndex); currentNode = replacementNode; } } currentParent = currentNode; parentIndex = index; currentNode = child; return (!(childCreated)); } /** * Get the Object to be found from current state of tree * * @return a pointer to the obj that represents the index sequnce or null if it doesn't exist */ public Object getObjectAtState() { if (currentNode == null) return null; return currentNode.getObject(); } /** * Sets the Object at the current state of the tree. * (overwrites if already exists) * * @param obj code obj representing the sequence of x86 bytes */ public void setObjectAtState(Object obj) { if (currentNode == null) throw new IllegalStateException("Cannot Add Object To null Node"); currentNode.setObject(obj); } /** * Visits all nodes in the tree with a visitor * * @param visitor visitor for the nodes * @see ObjectTreeStateMachineVisitor */ public void visitNodes(ObjectTreeStateMachineVisitor visitor) { if (root != null) root.visitNodes(visitor); } public void printTree() { root.print(""); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -