Пример #1
0
 public TreeNode<T> getRoot() {
   TreeNode<T> node = this;
   while (node.getParent() != null) {
     node = node.getParent();
   }
   return node;
 }
Пример #2
0
 public String getAbsolutePath() {
   StringBuilder path = new StringBuilder(32);
   TreeNode<T> node = this;
   while (node.getParent() != null) {
     path.insert(0, "/");
     path.insert(0, node.getId());
     node = node.getParent();
   }
   return path.toString();
 }
Пример #3
0
 public void addChild(TreeNode<T> child) {
   Assert.notNull(child, "child");
   if (children == null) {
     children = new LinkedList<TreeNode<T>>();
   }
   children.add(child);
   child.setParent(this);
 }
Пример #4
0
 private TreeNode<T> getChildForPath(String path, boolean create) {
   int separatorPos = path.indexOf('/');
   if (separatorPos == -1) {
     return getChildForId(path, create);
   } else if (separatorPos == 0) {
     return getRoot().getChildForPath(path.substring(1), create);
   }
   String id = path.substring(0, separatorPos);
   TreeNode<T> child = getChildForId(id, create);
   if (child != null) {
     String remainingPath = path.substring(separatorPos + 1);
     if (remainingPath.equals("")) {
       return child;
     } else {
       return child.getChildForPath(remainingPath, create);
     }
   }
   return null;
 }
Пример #5
0
 public boolean removeChild(TreeNode<T> child) {
   Assert.notNull(child, "child");
   boolean suceess = false;
   if (children != null) {
     suceess = children.remove(child);
     if (suceess) {
       child.setParent(null);
     }
   }
   return suceess;
 }
Пример #6
0
 private TreeNode<T> getChildForId(String id, boolean create) {
   if (id.equals("") || id.equals(".")) {
     return this;
   }
   if (id.equals("..")) {
     return getParent();
   }
   if (children != null) {
     for (TreeNode<T> child : children) {
       if (child.getId().equals(id)) {
         return child;
       }
     }
   }
   if (create) {
     TreeNode<T> proxyChild = new TreeNode<T>(id);
     addChild(proxyChild);
     return proxyChild;
   }
   return null;
 }