1 package org
.argeo
.slc
.core
.structure
.tree
;
4 import java
.util
.StringTokenizer
;
5 import java
.util
.Vector
;
7 import org
.argeo
.slc
.SlcException
;
8 import org
.argeo
.slc
.structure
.StructurePath
;
9 import org
.argeo
.slc
.structure
.StructureRegistry
;
12 * Path for tree based <code>StructureRegistry</code> implementations.
14 public class TreeSPath
implements StructurePath
, Comparable
<StructurePath
> {
15 /** Default character to use a separator: /. */
16 private static Character DEFAULT_SEPARATOR
= '/';
18 private Character separator
= DEFAULT_SEPARATOR
;
20 private String asUniqueString
;
29 public TreeSPath(String asUniqueString
) {
30 this.asUniqueString
= checkAndFormatPath(asUniqueString
);
33 public String
getAsUniqueString() {
34 return asUniqueString
;
38 * Sets all the required data from a string. <b>ATTENTION</b>: the path is
39 * not checked for performance reason. This method should be used only by
40 * ORM/OXM frameworks. Use constructor to create immutable tree structure
43 public void setAsUniqueString(String str
) {
44 this.asUniqueString
= str
;
47 /** The separator actually used by this path. */
48 public Character
getSeparator() {
52 /** Gets the parent path. */
53 public TreeSPath
getParent() {
54 int lastSep
= getAsUniqueString().lastIndexOf(separator
);
58 String parentUniqueString
= getAsUniqueString().substring(0, lastSep
);
59 return new TreeSPath(parentUniqueString
);
62 /** Gets the name part of the path. */
63 public String
getName() {
64 int lastSep
= getAsUniqueString().lastIndexOf(separator
);
65 return getAsUniqueString().substring(lastSep
+ 1);
68 /** Create a path without parent. */
69 public static TreeSPath
createRootPath(String name
) {
70 if (name
.indexOf(DEFAULT_SEPARATOR
) >= 0) {
71 throw new SlcException("Name cannot contain " + DEFAULT_SEPARATOR
);
73 return new TreeSPath('/' + name
);
76 /** Create a child . */
77 public TreeSPath
createChild(String name
) {
78 if (name
.indexOf(separator
) > -1) {
79 throw new SlcException("Tree path name '" + name
80 + "' contains separator character " + separator
);
82 return new TreeSPath(getAsUniqueString() + '/' + name
);
86 * Parses a string to a path.
88 * @deprecated use constructor instead
90 public static TreeSPath
parseToCreatePath(String path
) {
91 return parseToCreatePath(path
, DEFAULT_SEPARATOR
);
94 protected String
checkAndFormatPath(String str
) {
95 if (str
.length() < 2) {
96 throw new SlcException("Path " + str
+ " is not short");
98 if (str
.charAt(0) != separator
) {
99 throw new SlcException("Path " + str
+ " have to start with "
103 StringBuffer buf
= new StringBuffer(str
.length() + 5);
104 StringTokenizer st
= new StringTokenizer(str
, separator
.toString());
105 while (st
.hasMoreTokens()) {
106 buf
.append(separator
).append(st
.nextToken());
108 return buf
.toString();
112 * Parses a string to a path.
114 * @deprecated use constructor instead
116 public static TreeSPath
parseToCreatePath(String path
, Character separator
) {
117 return new TreeSPath(path
);
120 /** Lists the children from a registry. */
121 public List
<TreeSPath
> listChildren(StructureRegistry
<TreeSPath
> registry
) {
122 return listChildrenPaths(registry
, this);
125 /** Lists the children from a given path from a registry. */
126 public static List
<TreeSPath
> listChildrenPaths(
127 StructureRegistry
<TreeSPath
> registry
, TreeSPath path
) {
128 List
<TreeSPath
> paths
= new Vector
<TreeSPath
>();
129 List
<TreeSPath
> allPaths
= registry
.listPaths();
130 for (TreeSPath pathT
: allPaths
) {
131 if (pathT
.getParent() != null && pathT
.getParent().equals(path
)) {
138 /** Gets the root tree path of this path. */
139 public TreeSPath
getRoot() {
140 TreeSPath root
= this;
141 while (root
.getParent() != null) {
142 root
= root
.getParent();
147 /** Depth of this path. */
148 public Integer
getDepth() {
149 return depthImpl(this);
152 protected int depthImpl(TreeSPath path
) {
153 if (path
.getParent() == null) {
156 return depthImpl(path
.getParent()) + 1;
160 public List
<TreeSPath
> getHierarchyAsList() {
161 List
<TreeSPath
> lst
= new Vector
<TreeSPath
>();
162 addParentToList(lst
, this);
167 protected void addParentToList(List
<TreeSPath
> lst
, TreeSPath current
) {
168 TreeSPath parent
= current
.getParent();
169 if (parent
!= null) {
170 addParentToList(lst
, parent
);
176 public String
toString() {
177 return getAsUniqueString();
181 public boolean equals(Object obj
) {
182 if (obj
instanceof StructurePath
) {
183 StructurePath path
= (StructurePath
) obj
;
184 return getAsUniqueString().equals(path
.getAsUniqueString());
190 public int hashCode() {
191 return getAsUniqueString().hashCode();
194 public int compareTo(StructurePath o
) {
195 return getAsUniqueString().compareTo(o
.getAsUniqueString());
198 public Long
getTid() {
202 void setTid(Long tid
) {