1 package org
.argeo
.slc
.core
.structure
.tree
;
4 import java
.util
.StringTokenizer
;
5 import java
.util
.Vector
;
7 import org
.argeo
.slc
.core
.SlcException
;
8 import org
.argeo
.slc
.core
.structure
.StructurePath
;
9 import org
.argeo
.slc
.core
.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 public static Character DEFAULT_SEPARATOR
= '/';
18 private TreeSPath parent
;
20 private Character separator
= DEFAULT_SEPARATOR
;
22 private String asUniqueString
;
27 public String
getAsUniqueString() {
28 if (asUniqueString
== null) {
29 String parentStr
= parent
!= null ? parent
.getAsUniqueString() : "";
30 asUniqueString
= parentStr
+ separator
+ name
;
32 return asUniqueString
;
35 /** Sets all the required data from a string. */
36 public void setAsUniqueString(String str
) {
37 TreeSPath twin
= parseToCreatePath(str
, getSeparator());
40 asUniqueString
= getAsUniqueString();
43 /** The separator actually used by this path. */
44 public Character
getSeparator() {
48 /** Gets the parent path. */
49 public TreeSPath
getParent() {
53 /** Gets the name part of the path. */
54 public String
getName() {
58 /** Create a path without parent. */
59 public static TreeSPath
createRootPath(String name
) {
60 TreeSPath path
= new TreeSPath();
66 /** Create a child . */
67 public TreeSPath
createChild(String name
) {
68 if (name
.indexOf(separator
) > -1) {
69 throw new SlcException("Tree path name '" + name
70 + "' contains separator character " + separator
);
72 TreeSPath path
= new TreeSPath();
78 /** Parses a string to a path. */
79 public static TreeSPath
parseToCreatePath(String path
) {
80 return parseToCreatePath(path
, DEFAULT_SEPARATOR
);
83 /** Parses a string to a path. */
84 public static TreeSPath
parseToCreatePath(String path
, Character separator
) {
85 StringTokenizer st
= new StringTokenizer(path
, Character
86 .toString(separator
));
88 TreeSPath currPath
= null;
89 while (st
.hasMoreTokens()) {
90 if (currPath
== null) {// begin
91 currPath
= createRootPath(st
.nextToken());
93 currPath
= currPath
.createChild(st
.nextToken());
99 /** Lists the children from a registry. */
100 public List
<TreeSPath
> listChildren(StructureRegistry registry
) {
101 return listChildrenPaths(registry
, this);
104 /** Lists the children from a given path from a registry. */
105 public static List
<TreeSPath
> listChildrenPaths(StructureRegistry registry
,
107 List
<TreeSPath
> paths
= new Vector
<TreeSPath
>();
108 List
<StructurePath
> allPaths
= registry
.listPaths();
109 for (StructurePath sPath
: allPaths
) {
110 TreeSPath pathT
= (TreeSPath
) sPath
;
111 if (pathT
.parent
!= null && pathT
.parent
.equals(path
)) {
118 /** Gets the root tree path of this path. */
119 public TreeSPath
getRoot() {
120 TreeSPath root
= this;
121 while (root
.getParent() != null) {
122 root
= root
.getParent();
127 /** Depth of this path. */
128 public Integer
getDepth() {
129 return depthImpl(this);
132 protected int depthImpl(TreeSPath path
) {
133 if (path
.getParent() == null) {
136 return depthImpl(path
.getParent()) + 1;
140 public List
<TreeSPath
> getHierarchyAsList() {
141 List
<TreeSPath
> lst
= new Vector
<TreeSPath
>();
142 addParentToList(lst
, this);
147 protected void addParentToList(List
<TreeSPath
> lst
, TreeSPath current
) {
148 TreeSPath parent
= current
.getParent();
149 if (parent
!= null) {
150 addParentToList(lst
, parent
);
156 public String
toString() {
157 return getAsUniqueString();
161 public boolean equals(Object obj
) {
162 if (obj
instanceof StructurePath
) {
163 StructurePath path
= (StructurePath
) obj
;
164 return getAsUniqueString().equals(path
.getAsUniqueString());
170 public int hashCode() {
171 return getAsUniqueString().hashCode();
174 public int compareTo(StructurePath o
) {
175 return getAsUniqueString().compareTo(o
.getAsUniqueString());
178 public Long
getTid() {
182 void setTid(Long tid
) {
187 * Sets the separator character to use.
191 public void setSeparator(Character separator
) {
192 this.separator
= separator
;
195 /** Sets the parent (for ORM). */
196 protected void setParent(TreeSPath parent
) {
197 this.parent
= parent
;
200 /** Sets the name (for ORM). */
201 protected void setName(String name
) {