Simplify TreeSPath
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 1 May 2008 10:12:31 +0000 (10:12 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 1 May 2008 10:12:31 +0000 (10:12 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@1088 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.slc.core/src/main/java/org/argeo/slc/core/structure/tree/TreeSPath.java
org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java
org.argeo.slc.core/src/main/resources/org/argeo/slc/hibernate/structure/tree/TreeSPath.hbm.xml

index e38d0e3ddff6103f9771f02651a6a0a3a002b4ec..9c9eb54c8f0f37de8e5a69de5f09d9e693be229c 100644 (file)
@@ -13,10 +13,8 @@ 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
@@ -24,20 +22,26 @@ public class TreeSPath implements StructurePath, Comparable<StructurePath> {
        /** 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
-       public void setAsUniqueString(String str) {\r
-               TreeSPath twin = parseToCreatePath(str, getSeparator());\r
-               name = twin.name;\r
-               parent = twin.parent;\r
-               asUniqueString = getAsUniqueString();\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
+       void setAsUniqueString(String str) {\r
+               this.asUniqueString = str;\r
        }\r
 \r
        /** The separator actually used by this path. */\r
@@ -47,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);\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
@@ -69,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
@@ -182,24 +202,4 @@ public class TreeSPath implements StructurePath, Comparable<StructurePath> {
        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
index 6d39d210e9c27f17e00c22cd7f8ce76d5532be2d..8da3e7d5a2dde3bd932a5175b592cbac6d3c77a8 100644 (file)
@@ -17,9 +17,7 @@ import org.argeo.slc.core.structure.StructureAware;
 import org.argeo.slc.core.structure.StructureElement;\r
 import org.argeo.slc.core.structure.StructureRegistry;\r
 import org.argeo.slc.core.structure.tree.TreeSPath;\r
-import org.argeo.slc.core.test.NumericTRId;\r
 import org.argeo.slc.core.test.TestResult;\r
-import org.argeo.slc.core.test.TestResultId;\r
 import org.argeo.slc.core.test.TestResultListener;\r
 import org.argeo.slc.core.test.TestResultPart;\r
 \r
@@ -30,10 +28,7 @@ import org.argeo.slc.core.test.TestResultPart;
 public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,\r
                SlcExecutionAware {\r
        private Log log = LogFactory.getLog(TreeTestResult.class);\r
-       /** For ORM */\r
-       private Long tid;\r
 \r
-       //private NumericTRId testResultId;\r
        private List<TestResultListener> listeners = new Vector<TestResultListener>();\r
 \r
        private TreeSPath currentPath;\r
@@ -49,25 +44,6 @@ public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
        private SortedMap<TreeSPath, PartSubList> resultParts = new TreeMap<TreeSPath, PartSubList>();\r
        private SortedMap<TreeSPath, StructureElement> elements = new TreeMap<TreeSPath, StructureElement>();\r
 \r
-       private StructureRegistry<TreeSPath> registry;\r
-\r
-//     public TestResultId getTestResultId() {\r
-//             return testResultId;\r
-//     }\r
-\r
-       /**\r
-        * Use of a <code>NumericTRId</code> is required by Hibernate. <b>It may\r
-        * change in the future.</b>\r
-        */\r
-//     public NumericTRId getNumericResultId() {\r
-//             return testResultId;\r
-//     }\r
-\r
-       /** Sets the test result id as a numeric test result id. */\r
-//     public void setNumericResultId(NumericTRId testResultId) {\r
-//             this.testResultId = testResultId;\r
-//     }\r
-\r
        /** Sets the list of listeners. */\r
        public void setListeners(List<TestResultListener> listeners) {\r
                this.listeners = listeners;\r
@@ -101,18 +77,18 @@ public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
                                if (!elements.containsKey(p)) {\r
                                        StructureElement elem = registry.getElement(p);\r
                                        if (elem != null) {\r
-                                               // elements.put(p, elem.getLabel());\r
                                                elements.put(p, elem);\r
-                                       } else {\r
-                                               log.warn("An element is already registered for path "\r
-                                                               + p);\r
                                        }\r
+                               } else {\r
+                                       if (log.isTraceEnabled())\r
+                                               log.trace("An element is already registered for path "\r
+                                                               + p + " and was not updated");\r
                                }\r
+\r
                        }\r
                }\r
 \r
                currentPath = (TreeSPath) path;\r
-               this.registry = registry;\r
        }\r
 \r
        /** Gets the current path. */\r
@@ -148,23 +124,6 @@ public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
                log.info("Test Result #" + getUuid() + " closed.");\r
        }\r
 \r
-       Long getTid() {\r
-               return tid;\r
-       }\r
-\r
-       void setTid(Long tid) {\r
-               this.tid = tid;\r
-       }\r
-\r
-       /** Gets the related registry (can be null). */\r
-       public StructureRegistry<TreeSPath> getRegistry() {\r
-               return registry;\r
-       }\r
-\r
-       /** Sets the related registry. */\r
-       // public void setRegistry(StructureRegistry<TreeSPath> registry) {\r
-       // this.registry = registry;\r
-       // }\r
        public Date getCloseDate() {\r
                return closeDate;\r
        }\r
index d9934ee33309d593a46423ee7afeabee6f52d69b..0e282505f196e9c91e7f8112843df1a10b8c6c9e 100644 (file)
@@ -9,6 +9,5 @@
                </id>\r
                <property name="asUniqueString" column="PATH" not-null="true"\r
                        unique="true" />\r
-               <property name="separator" column="SEPARATOR" not-null="true" />\r
        </class>\r
 </hibernate-mapping>
\ No newline at end of file