From: Mathieu Baudier Date: Thu, 1 May 2008 10:12:31 +0000 (+0000) Subject: Simplify TreeSPath X-Git-Tag: argeo-slc-2.1.7~2952 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=d5d72139e7497923c2b8e2f4d25f366d01425498;p=gpl%2Fargeo-slc.git Simplify TreeSPath git-svn-id: https://svn.argeo.org/slc/trunk@1088 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/structure/tree/TreeSPath.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/structure/tree/TreeSPath.java index e38d0e3dd..9c9eb54c8 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/structure/tree/TreeSPath.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/structure/tree/TreeSPath.java @@ -13,10 +13,8 @@ import org.argeo.slc.core.structure.StructureRegistry; */ public class TreeSPath implements StructurePath, Comparable { /** Default character to use a separator: /. */ - public static Character DEFAULT_SEPARATOR = '/'; + private static Character DEFAULT_SEPARATOR = '/'; - private TreeSPath parent; - private String name; private Character separator = DEFAULT_SEPARATOR; private String asUniqueString; @@ -24,20 +22,26 @@ public class TreeSPath implements StructurePath, Comparable { /** For ORM */ private Long tid; + public TreeSPath() { + + } + + public TreeSPath(String asUniqueString) { + this.asUniqueString = checkAndFormatPath(asUniqueString); + } + public String getAsUniqueString() { - if (asUniqueString == null) { - String parentStr = parent != null ? parent.getAsUniqueString() : ""; - asUniqueString = parentStr + separator + name; - } return asUniqueString; } - /** Sets all the required data from a string. */ - public void setAsUniqueString(String str) { - TreeSPath twin = parseToCreatePath(str, getSeparator()); - name = twin.name; - parent = twin.parent; - asUniqueString = getAsUniqueString(); + /** + * Sets all the required data from a string. ATTENTION: the path is + * not checked for performance reason. This method should be used only by + * ORM/OXM frameworks. Use constructor to create immutable tree structure + * paths. + */ + void setAsUniqueString(String str) { + this.asUniqueString = str; } /** The separator actually used by this path. */ @@ -47,20 +51,26 @@ public class TreeSPath implements StructurePath, Comparable { /** Gets the parent path. */ public TreeSPath getParent() { - return parent; + int lastSep = getAsUniqueString().lastIndexOf(separator); + if (lastSep < 1) { + return null; + } + String parentUniqueString = getAsUniqueString().substring(0, lastSep); + return new TreeSPath(parentUniqueString); } /** Gets the name part of the path. */ public String getName() { - return name; + int lastSep = getAsUniqueString().lastIndexOf(separator); + return getAsUniqueString().substring(lastSep); } /** Create a path without parent. */ public static TreeSPath createRootPath(String name) { - TreeSPath path = new TreeSPath(); - path.parent = null; - path.name = name; - return path; + if (name.indexOf(DEFAULT_SEPARATOR) >= 0) { + throw new SlcException("Name cannot contain " + DEFAULT_SEPARATOR); + } + return new TreeSPath('/' + name); } /** Create a child . */ @@ -69,46 +79,56 @@ public class TreeSPath implements StructurePath, Comparable { throw new SlcException("Tree path name '" + name + "' contains separator character " + separator); } - TreeSPath path = new TreeSPath(); - path.parent = this; - path.name = name; - return path; + return new TreeSPath(getAsUniqueString() + '/' + name); } - /** Parses a string to a path. */ + /** + * Parses a string to a path. + * + * @deprecated use constructor instead + */ public static TreeSPath parseToCreatePath(String path) { return parseToCreatePath(path, DEFAULT_SEPARATOR); } - /** Parses a string to a path. */ - public static TreeSPath parseToCreatePath(String path, Character separator) { - StringTokenizer st = new StringTokenizer(path, Character - .toString(separator)); + protected String checkAndFormatPath(String str) { + if (str.length() < 2) { + throw new SlcException("Path " + str + " is not short"); + } + if (str.charAt(0) != separator) { + throw new SlcException("Path " + str + " have to start with " + + separator); + } - TreeSPath currPath = null; + StringBuffer buf = new StringBuffer(str.length() + 5); + StringTokenizer st = new StringTokenizer(str, separator.toString()); while (st.hasMoreTokens()) { - if (currPath == null) {// begin - currPath = createRootPath(st.nextToken()); - } else { - currPath = currPath.createChild(st.nextToken()); - } + buf.append(separator).append(st.nextToken()); } - return currPath; + return buf.toString(); + } + + /** + * Parses a string to a path. + * + * @deprecated use constructor instead + */ + public static TreeSPath parseToCreatePath(String path, Character separator) { + return new TreeSPath(path); } /** Lists the children from a registry. */ - public List listChildren(StructureRegistry registry) { + public List listChildren(StructureRegistry registry) { return listChildrenPaths(registry, this); } /** Lists the children from a given path from a registry. */ - public static List listChildrenPaths(StructureRegistry registry, - TreeSPath path) { + public static List listChildrenPaths( + StructureRegistry registry, TreeSPath path) { List paths = new Vector(); - List allPaths = registry.listPaths(); - for (StructurePath sPath : allPaths) { - TreeSPath pathT = (TreeSPath) sPath; - if (pathT.parent != null && pathT.parent.equals(path)) { + List allPaths = registry.listPaths(); + for (TreeSPath pathT : allPaths) { + if (pathT.getParent() != null && pathT.getParent().equals(path)) { paths.add(pathT); } } @@ -182,24 +202,4 @@ public class TreeSPath implements StructurePath, Comparable { void setTid(Long tid) { this.tid = tid; } - - /** - * Sets the separator character to use. - * - * @deprecated - */ - public void setSeparator(Character separator) { - this.separator = separator; - } - - /** Sets the parent (for ORM). */ - protected void setParent(TreeSPath parent) { - this.parent = parent; - } - - /** Sets the name (for ORM). */ - protected void setName(String name) { - this.name = name; - } - } diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java index 6d39d210e..8da3e7d5a 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java @@ -17,9 +17,7 @@ import org.argeo.slc.core.structure.StructureAware; import org.argeo.slc.core.structure.StructureElement; import org.argeo.slc.core.structure.StructureRegistry; import org.argeo.slc.core.structure.tree.TreeSPath; -import org.argeo.slc.core.test.NumericTRId; import org.argeo.slc.core.test.TestResult; -import org.argeo.slc.core.test.TestResultId; import org.argeo.slc.core.test.TestResultListener; import org.argeo.slc.core.test.TestResultPart; @@ -30,10 +28,7 @@ import org.argeo.slc.core.test.TestResultPart; public class TreeTestResult implements TestResult, StructureAware, SlcExecutionAware { private Log log = LogFactory.getLog(TreeTestResult.class); - /** For ORM */ - private Long tid; - //private NumericTRId testResultId; private List listeners = new Vector(); private TreeSPath currentPath; @@ -49,25 +44,6 @@ public class TreeTestResult implements TestResult, StructureAware, private SortedMap resultParts = new TreeMap(); private SortedMap elements = new TreeMap(); - private StructureRegistry registry; - -// public TestResultId getTestResultId() { -// return testResultId; -// } - - /** - * Use of a NumericTRId is required by Hibernate. It may - * change in the future. - */ -// public NumericTRId getNumericResultId() { -// return testResultId; -// } - - /** Sets the test result id as a numeric test result id. */ -// public void setNumericResultId(NumericTRId testResultId) { -// this.testResultId = testResultId; -// } - /** Sets the list of listeners. */ public void setListeners(List listeners) { this.listeners = listeners; @@ -101,18 +77,18 @@ public class TreeTestResult implements TestResult, StructureAware, if (!elements.containsKey(p)) { StructureElement elem = registry.getElement(p); if (elem != null) { - // elements.put(p, elem.getLabel()); elements.put(p, elem); - } else { - log.warn("An element is already registered for path " - + p); } + } else { + if (log.isTraceEnabled()) + log.trace("An element is already registered for path " + + p + " and was not updated"); } + } } currentPath = (TreeSPath) path; - this.registry = registry; } /** Gets the current path. */ @@ -148,23 +124,6 @@ public class TreeTestResult implements TestResult, StructureAware, log.info("Test Result #" + getUuid() + " closed."); } - Long getTid() { - return tid; - } - - void setTid(Long tid) { - this.tid = tid; - } - - /** Gets the related registry (can be null). */ - public StructureRegistry getRegistry() { - return registry; - } - - /** Sets the related registry. */ - // public void setRegistry(StructureRegistry registry) { - // this.registry = registry; - // } public Date getCloseDate() { return closeDate; } diff --git a/org.argeo.slc.core/src/main/resources/org/argeo/slc/hibernate/structure/tree/TreeSPath.hbm.xml b/org.argeo.slc.core/src/main/resources/org/argeo/slc/hibernate/structure/tree/TreeSPath.hbm.xml index d9934ee33..0e282505f 100644 --- a/org.argeo.slc.core/src/main/resources/org/argeo/slc/hibernate/structure/tree/TreeSPath.hbm.xml +++ b/org.argeo.slc.core/src/main/resources/org/argeo/slc/hibernate/structure/tree/TreeSPath.hbm.xml @@ -9,6 +9,5 @@ - \ No newline at end of file