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
;
25 public String
getAsUniqueString() {
26 String parentStr
= parent
!= null ? parent
.getAsUniqueString() : "";
27 return parentStr
+ separator
+ name
;
30 /** Sets all the required data from a string. */
31 public void setAsUniqueString(String str
) {
32 TreeSPath twin
= parseToCreatePath(str
, getSeparator());
37 /** The separator actually used by this path. */
38 public Character
getSeparator() {
42 /** Gets the parent path. */
43 public TreeSPath
getParent() {
47 /** Gets the name part of the path. */
48 public String
getName() {
52 /** Create a path without parent. */
53 public static TreeSPath
createRootPath(String name
) {
54 TreeSPath path
= new TreeSPath();
60 /** Create a child . */
61 public TreeSPath
createChild(String name
) {
62 if (name
.indexOf(separator
) > -1) {
63 throw new SlcException("Tree path name '" + name
64 + "' contains separator character " + separator
);
66 TreeSPath path
= new TreeSPath();
72 /** Parses a string to a path. */
73 public static TreeSPath
parseToCreatePath(String path
) {
74 return parseToCreatePath(path
, DEFAULT_SEPARATOR
);
77 /** Parses a string to a path. */
78 public static TreeSPath
parseToCreatePath(String path
, Character separator
) {
79 StringTokenizer st
= new StringTokenizer(path
, Character
80 .toString(separator
));
82 TreeSPath currPath
= null;
83 while (st
.hasMoreTokens()) {
84 if (currPath
== null) {// begin
85 currPath
= createRootPath(st
.nextToken());
87 currPath
= currPath
.createChild(st
.nextToken());
93 /** Lists the children from a registry. */
94 public List
<TreeSPath
> listChildren(StructureRegistry registry
) {
95 return listChildrenPaths(registry
, this);
98 /** Lists the children from a given path from a registry. */
99 public static List
<TreeSPath
> listChildrenPaths(StructureRegistry registry
,
101 List
<TreeSPath
> paths
= new Vector
<TreeSPath
>();
102 List
<StructurePath
> allPaths
= registry
.listPaths();
103 for (StructurePath sPath
: allPaths
) {
104 TreeSPath pathT
= (TreeSPath
) sPath
;
105 if (pathT
.parent
!= null && pathT
.parent
.equals(path
)) {
112 /** Gets the root tree path of this path. */
113 public TreeSPath
getRoot() {
114 TreeSPath root
= this;
115 while (root
.getParent() != null) {
116 root
= root
.getParent();
121 /** Depth of this path. */
122 public Integer
depth() {
123 return depthImpl(this);
126 private static int depthImpl(TreeSPath path
) {
127 if (path
.getParent() == null) {
130 return depthImpl(path
.getParent()) + 1;
135 public String
toString() {
136 return getAsUniqueString();
140 public boolean equals(Object obj
) {
141 if (obj
instanceof StructurePath
) {
142 StructurePath path
= (StructurePath
) obj
;
143 return getAsUniqueString().equals(path
.getAsUniqueString());
148 public int compareTo(StructurePath o
) {
149 return getAsUniqueString().compareTo(o
.getAsUniqueString());
156 void setTid(Long tid
) {
160 /** Sets the separator character to use. */
161 public void setSeparator(Character separator
) {
162 this.separator
= separator
;
165 /** Sets the parent (for ORM). */
166 protected void setParent(TreeSPath parent
) {
167 this.parent
= parent
;
170 /** Sets the name (for ORM). */
171 protected void setName(String name
) {