001: package org.drools.eclipse.editors.completion;
002:
003: import java.util.Collection;
004: import java.util.HashMap;
005:
006: public class Node {
007: private HashMap children = new HashMap();
008: private Node parent = null;
009: private String token;
010: private int depth = 0;
011:
012: public Node(String name) {
013: this .token = name;
014: }
015:
016: /**
017: * The method will create a new Node instance and try to add it as
018: * a child node. If an Node with the same string token exists, the
019: * method will return the existing node instead.
020: * @param token
021: * @return
022: */
023: public Node addToken(String token) {
024: Node newnode = new Node(token);
025: // set the depth first
026: newnode.setDepth(depth + 1);
027: // add the node as a child
028: newnode = addChild(newnode);
029: return newnode;
030: }
031:
032: /**
033: * if the string matches this node's token, the method will return
034: * true. Otherwise it returns false.
035: * @param input
036: * @return
037: */
038: public boolean isMatch(String input) {
039: return input.equals(token);
040: }
041:
042: public boolean isMatch(Node n) {
043: return this .token.equals(n.getToken());
044: }
045:
046: /**
047: * The method will check to see if a Node with the same string token
048: * already exists. If it doesn't, it will add the token as a child and
049: * return the same node.
050: *
051: * On the otherhand, if there is an existing Node for the same string
052: * token, the method returns the existing Node instance.
053: * @param n
054: * @return
055: */
056: public Node addChild(Node n) {
057: if (!this .children.containsKey(n.getToken())) {
058: this .children.put(n.getToken(), n);
059: n.setParent(this );
060: return n;
061: } else {
062: return (Node) this .children.get(n.getToken());
063: }
064: }
065:
066: public void removeChild(Node n) {
067: this .children.remove(n.getToken());
068: }
069:
070: public Collection getChildren() {
071: return this .children.values();
072: }
073:
074: /**
075: * The method will get the child matching the string token
076: * @param token
077: * @return
078: */
079: public Node getChild(String token) {
080: return (Node) this .children.get(token);
081: }
082:
083: public Node getParent() {
084: return parent;
085: }
086:
087: public void setParent(Node parent) {
088: this .parent = parent;
089: }
090:
091: public String getToken() {
092: return token;
093: }
094:
095: public void setToken(String token) {
096: this .token = token;
097: }
098:
099: public int getDepth() {
100: return depth;
101: }
102:
103: public void setDepth(int depth) {
104: this .depth = depth;
105: }
106:
107: public void clearChildren() {
108: this.children.clear();
109: }
110: }
|