View Javadoc
1 package net.sf.flock.tree; 2 3 import java.util.Collection; 4 import java.util.Iterator; 5 import java.util.Set; 6 import java.util.StringTokenizer; 7 import java.util.TreeSet; 8 9 import org.apache.log4j.LogManager; 10 import org.apache.log4j.Logger; 11 12 class BaseTreeNode implements ITreeNode, Comparable { 13 14 private final static Logger LOGGER = LogManager.getLogger(BaseTreeNode.class); 15 16 private final String id; 17 private ITreeNode parent; 18 private Set children = new TreeSet(); 19 20 public BaseTreeNode(String id) { 21 this.id = id; 22 } 23 24 public ITreeNode getParent() { 25 return this.parent; 26 } 27 28 public void setParent(ITreeNode parent) { 29 if (this.parent!=null) { 30 throw new IllegalStateException("Parent node already set"); 31 } 32 this.parent = parent; 33 } 34 35 /*** 36 * @see net.sf.flock.tree.ITreeNode#getId() 37 */ 38 public String getId() { 39 return this.id; 40 } 41 42 /*** 43 * @see net.sf.flock.tree.ITreeNode#getFullPath() 44 */ 45 public String getFullPath() { 46 return (this.parent!=null ? this.parent.getFullPath()+"/" : "") + this.getId(); 47 } 48 49 50 /*** 51 * @see net.sf.flock.tree.ITreeNode#getChildren() 52 */ 53 public Collection getChildren() { 54 return this.children; 55 } 56 57 public BaseTreeNode getChild(String id) { 58 for (Iterator i=this.children.iterator(); i.hasNext(); ) { 59 BaseTreeNode node = (BaseTreeNode)i.next(); 60 if (id.equals(node.getId())) { 61 return node; 62 } 63 } 64 return null; 65 } 66 67 public void addChild(BaseTreeNode child) { 68 child.setParent(this); 69 this.children.add(child); 70 } 71 72 73 public boolean equals(Object o) { 74 if (o instanceof ITreeNode) { 75 return this.id.equals( ((ITreeNode)o).getId() ); 76 } 77 return false; 78 } 79 80 public int compareTo(Object o) { 81 return this.id.compareTo( ((ITreeNode)o).getId() ); 82 } 83 84 85 public BaseTreeNode getChild(String path, boolean create) { 86 LOGGER.debug("getCategoryByPath " + path + " create:" + create); 87 88 if (!path.startsWith("/")) { 89 throw new IllegalArgumentException("Illegal path specified:" + path); 90 } 91 92 path = path.substring(1); 93 BaseTreeNode currNode = this; 94 for (StringTokenizer pathTokens = new StringTokenizer(path, "/"); pathTokens.hasMoreTokens(); ) { 95 96 String id = pathTokens.nextToken(); 97 98 LOGGER.debug("-" + currNode.getId() + "->" + id); 99 100 BaseTreeNode childNode = currNode.getChild(id); 101 102 LOGGER.debug("- childNode : " + childNode); 103 104 if (childNode == null) { 105 if (create) { 106 LOGGER.debug("- create child: " + id); 107 // create 108 childNode = new BaseTreeNode(id); 109 currNode.addChild(childNode); 110 } else { 111 LOGGER.debug("- child not found[" + id + "] `return null`"); 112 return null; 113 } 114 } 115 currNode = childNode; 116 } 117 LOGGER.debug("- return node " + currNode); 118 119 return currNode; 120 } 121 122 }

This page was automatically generated by Maven