*/\r
public class TreeSPath implements StructurePath, Comparable<StructurePath> {\r
/** Default character to use a separator: /. */\r
- public static Character DEFAULT_SEPARATOR = '/';\r
+ private static Character DEFAULT_SEPARATOR = '/';\r
\r
- private TreeSPath parent;\r
- private String name;\r
private Character separator = DEFAULT_SEPARATOR;\r
\r
private String asUniqueString;\r
/** For ORM */\r
private Long tid;\r
\r
+ public TreeSPath() {\r
+\r
+ }\r
+\r
+ public TreeSPath(String asUniqueString) {\r
+ this.asUniqueString = checkAndFormatPath(asUniqueString);\r
+ }\r
+\r
public String getAsUniqueString() {\r
- if (asUniqueString == null) {\r
- String parentStr = parent != null ? parent.getAsUniqueString() : "";\r
- asUniqueString = parentStr + separator + name;\r
- }\r
return asUniqueString;\r
}\r
\r
- /** Sets all the required data from a string. */\r
+ /**\r
+ * Sets all the required data from a string. <b>ATTENTION</b>: the path is\r
+ * not checked for performance reason. This method should be used only by\r
+ * ORM/OXM frameworks. Use constructor to create immutable tree structure\r
+ * paths.\r
+ */\r
public void setAsUniqueString(String str) {\r
- TreeSPath twin = parseToCreatePath(str, getSeparator());\r
- name = twin.name;\r
- parent = twin.parent;\r
- asUniqueString = getAsUniqueString();\r
+ this.asUniqueString = str;\r
}\r
\r
/** The separator actually used by this path. */\r
\r
/** Gets the parent path. */\r
public TreeSPath getParent() {\r
- return parent;\r
+ int lastSep = getAsUniqueString().lastIndexOf(separator);\r
+ if (lastSep < 1) {\r
+ return null;\r
+ }\r
+ String parentUniqueString = getAsUniqueString().substring(0, lastSep);\r
+ return new TreeSPath(parentUniqueString);\r
}\r
\r
/** Gets the name part of the path. */\r
public String getName() {\r
- return name;\r
+ int lastSep = getAsUniqueString().lastIndexOf(separator);\r
+ return getAsUniqueString().substring(lastSep);\r
}\r
\r
/** Create a path without parent. */\r
public static TreeSPath createRootPath(String name) {\r
- TreeSPath path = new TreeSPath();\r
- path.parent = null;\r
- path.name = name;\r
- return path;\r
+ if (name.indexOf(DEFAULT_SEPARATOR) >= 0) {\r
+ throw new SlcException("Name cannot contain " + DEFAULT_SEPARATOR);\r
+ }\r
+ return new TreeSPath('/' + name);\r
}\r
\r
/** Create a child . */\r
throw new SlcException("Tree path name '" + name\r
+ "' contains separator character " + separator);\r
}\r
- TreeSPath path = new TreeSPath();\r
- path.parent = this;\r
- path.name = name;\r
- return path;\r
+ return new TreeSPath(getAsUniqueString() + '/' + name);\r
}\r
\r
- /** Parses a string to a path. */\r
+ /**\r
+ * Parses a string to a path.\r
+ * \r
+ * @deprecated use constructor instead\r
+ */\r
public static TreeSPath parseToCreatePath(String path) {\r
return parseToCreatePath(path, DEFAULT_SEPARATOR);\r
}\r
\r
- /** Parses a string to a path. */\r
- public static TreeSPath parseToCreatePath(String path, Character separator) {\r
- StringTokenizer st = new StringTokenizer(path, Character\r
- .toString(separator));\r
+ protected String checkAndFormatPath(String str) {\r
+ if (str.length() < 2) {\r
+ throw new SlcException("Path " + str + " is not short");\r
+ }\r
+ if (str.charAt(0) != separator) {\r
+ throw new SlcException("Path " + str + " have to start with "\r
+ + separator);\r
+ }\r
\r
- TreeSPath currPath = null;\r
+ StringBuffer buf = new StringBuffer(str.length() + 5);\r
+ StringTokenizer st = new StringTokenizer(str, separator.toString());\r
while (st.hasMoreTokens()) {\r
- if (currPath == null) {// begin\r
- currPath = createRootPath(st.nextToken());\r
- } else {\r
- currPath = currPath.createChild(st.nextToken());\r
- }\r
+ buf.append(separator).append(st.nextToken());\r
}\r
- return currPath;\r
+ return buf.toString();\r
+ }\r
+\r
+ /**\r
+ * Parses a string to a path.\r
+ * \r
+ * @deprecated use constructor instead\r
+ */\r
+ public static TreeSPath parseToCreatePath(String path, Character separator) {\r
+ return new TreeSPath(path);\r
}\r
\r
/** Lists the children from a registry. */\r
- public List<TreeSPath> listChildren(StructureRegistry registry) {\r
+ public List<TreeSPath> listChildren(StructureRegistry<TreeSPath> registry) {\r
return listChildrenPaths(registry, this);\r
}\r
\r
/** Lists the children from a given path from a registry. */\r
- public static List<TreeSPath> listChildrenPaths(StructureRegistry registry,\r
- TreeSPath path) {\r
+ public static List<TreeSPath> listChildrenPaths(\r
+ StructureRegistry<TreeSPath> registry, TreeSPath path) {\r
List<TreeSPath> paths = new Vector<TreeSPath>();\r
- List<StructurePath> allPaths = registry.listPaths();\r
- for (StructurePath sPath : allPaths) {\r
- TreeSPath pathT = (TreeSPath) sPath;\r
- if (pathT.parent != null && pathT.parent.equals(path)) {\r
+ List<TreeSPath> allPaths = registry.listPaths();\r
+ for (TreeSPath pathT : allPaths) {\r
+ if (pathT.getParent() != null && pathT.getParent().equals(path)) {\r
paths.add(pathT);\r
}\r
}\r
void setTid(Long tid) {\r
this.tid = tid;\r
}\r
-\r
- /**\r
- * Sets the separator character to use.\r
- * \r
- * @deprecated\r
- */\r
- public void setSeparator(Character separator) {\r
- this.separator = separator;\r
- }\r
-\r
- /** Sets the parent (for ORM). */\r
- protected void setParent(TreeSPath parent) {\r
- this.parent = parent;\r
- }\r
-\r
- /** Sets the name (for ORM). */\r
- protected void setName(String name) {\r
- this.name = name;\r
- }\r
-\r
}\r