public TreeNode<T> getRoot() { TreeNode<T> node = this; while (node.getParent() != null) { node = node.getParent(); } return node; }
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(); }
public void addChild(TreeNode<T> child) { Assert.notNull(child, "child"); if (children == null) { children = new LinkedList<TreeNode<T>>(); } children.add(child); child.setParent(this); }
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; }
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; }
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; }