]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.core/src/main/java/org/argeo/slc/core/structure/tree/TreeSPath.java
Improve formatting
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / structure / tree / TreeSPath.java
index 2efeb152709811306829e1113b2f5228e58072f0..c673faa65e063dcdf30cc0a8ca8499425347b315 100644 (file)
@@ -13,25 +13,35 @@ import org.argeo.slc.core.structure.StructureRegistry;
  */\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
+\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
-               String parentStr = parent != null ? parent.getAsUniqueString() : "";\r
-               return parentStr + separator + name;\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
+               this.asUniqueString = str;\r
        }\r
 \r
        /** The separator actually used by this path. */\r
@@ -41,20 +51,26 @@ public class TreeSPath implements StructurePath, Comparable<StructurePath> {
 \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 + 1);\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
@@ -63,46 +79,56 @@ public class TreeSPath implements StructurePath, Comparable<StructurePath> {
                        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
@@ -119,11 +145,11 @@ public class TreeSPath implements StructurePath, Comparable<StructurePath> {
        }\r
 \r
        /** Depth of this path. */\r
-       public Integer depth() {\r
+       public Integer getDepth() {\r
                return depthImpl(this);\r
        }\r
 \r
-       private static int depthImpl(TreeSPath path) {\r
+       protected int depthImpl(TreeSPath path) {\r
                if (path.getParent() == null) {\r
                        return 1;\r
                } else {\r
@@ -131,6 +157,21 @@ public class TreeSPath implements StructurePath, Comparable<StructurePath> {
                }\r
        }\r
 \r
+       public List<TreeSPath> getHierarchyAsList() {\r
+               List<TreeSPath> lst = new Vector<TreeSPath>();\r
+               addParentToList(lst, this);\r
+               lst.add(this);\r
+               return lst;\r
+       }\r
+\r
+       protected void addParentToList(List<TreeSPath> lst, TreeSPath current) {\r
+               TreeSPath parent = current.getParent();\r
+               if (parent != null) {\r
+                       addParentToList(lst, parent);\r
+                       lst.add(parent);\r
+               }\r
+       }\r
+\r
        @Override\r
        public String toString() {\r
                return getAsUniqueString();\r
@@ -145,31 +186,20 @@ public class TreeSPath implements StructurePath, Comparable<StructurePath> {
                return false;\r
        }\r
 \r
+       @Override\r
+       public int hashCode() {\r
+               return getAsUniqueString().hashCode();\r
+       }\r
+\r
        public int compareTo(StructurePath o) {\r
                return getAsUniqueString().compareTo(o.getAsUniqueString());\r
        }\r
 \r
-       Long getTid() {\r
+       public Long getTid() {\r
                return tid;\r
        }\r
 \r
        void setTid(Long tid) {\r
                this.tid = tid;\r
        }\r
-\r
-       /** Sets the separator character to use. */\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