]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/structure/tree/TreeSPath.java
Add elements and tags in tree test result
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / structure / tree / TreeSPath.java
1 package org.argeo.slc.core.structure.tree;
2
3 import java.util.List;
4 import java.util.StringTokenizer;
5 import java.util.Vector;
6
7 import org.argeo.slc.core.SlcException;
8 import org.argeo.slc.core.structure.StructurePath;
9 import org.argeo.slc.core.structure.StructureRegistry;
10
11 /**
12 * Path for tree based <code>StructureRegistry</code> implementations.
13 */
14 public class TreeSPath implements StructurePath, Comparable<StructurePath> {
15 /** Default character to use a separator: /. */
16 public static Character DEFAULT_SEPARATOR = '/';
17
18 private TreeSPath parent;
19 private String name;
20 private Character separator = DEFAULT_SEPARATOR;
21
22 private String asUniqueString;
23
24 /** For ORM */
25 private Long tid;
26
27 public String getAsUniqueString() {
28 if (asUniqueString == null) {
29 String parentStr = parent != null ? parent.getAsUniqueString() : "";
30 asUniqueString = parentStr + separator + name;
31 }
32 return asUniqueString;
33 }
34
35 /** Sets all the required data from a string. */
36 public void setAsUniqueString(String str) {
37 TreeSPath twin = parseToCreatePath(str, getSeparator());
38 name = twin.name;
39 parent = twin.parent;
40 asUniqueString = getAsUniqueString();
41 }
42
43 /** The separator actually used by this path. */
44 public Character getSeparator() {
45 return separator;
46 }
47
48 /** Gets the parent path. */
49 public TreeSPath getParent() {
50 return parent;
51 }
52
53 /** Gets the name part of the path. */
54 public String getName() {
55 return name;
56 }
57
58 /** Create a path without parent. */
59 public static TreeSPath createRootPath(String name) {
60 TreeSPath path = new TreeSPath();
61 path.parent = null;
62 path.name = name;
63 return path;
64 }
65
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);
71 }
72 TreeSPath path = new TreeSPath();
73 path.parent = this;
74 path.name = name;
75 return path;
76 }
77
78 /** Parses a string to a path. */
79 public static TreeSPath parseToCreatePath(String path) {
80 return parseToCreatePath(path, DEFAULT_SEPARATOR);
81 }
82
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));
87
88 TreeSPath currPath = null;
89 while (st.hasMoreTokens()) {
90 if (currPath == null) {// begin
91 currPath = createRootPath(st.nextToken());
92 } else {
93 currPath = currPath.createChild(st.nextToken());
94 }
95 }
96 return currPath;
97 }
98
99 /** Lists the children from a registry. */
100 public List<TreeSPath> listChildren(StructureRegistry registry) {
101 return listChildrenPaths(registry, this);
102 }
103
104 /** Lists the children from a given path from a registry. */
105 public static List<TreeSPath> listChildrenPaths(StructureRegistry registry,
106 TreeSPath path) {
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)) {
112 paths.add(pathT);
113 }
114 }
115 return paths;
116 }
117
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();
123 }
124 return root;
125 }
126
127 /** Depth of this path. */
128 public Integer getDepth() {
129 return depthImpl(this);
130 }
131
132 protected int depthImpl(TreeSPath path) {
133 if (path.getParent() == null) {
134 return 1;
135 } else {
136 return depthImpl(path.getParent()) + 1;
137 }
138 }
139
140 public List<TreeSPath> getHierarchyAsList() {
141 List<TreeSPath> lst = new Vector<TreeSPath>();
142 addParentToList(lst, this);
143 lst.add(this);
144 return lst;
145 }
146
147 protected void addParentToList(List<TreeSPath> lst, TreeSPath current) {
148 TreeSPath parent = current.getParent();
149 if (parent != null) {
150 addParentToList(lst, parent);
151 lst.add(parent);
152 }
153 }
154
155 @Override
156 public String toString() {
157 return getAsUniqueString();
158 }
159
160 @Override
161 public boolean equals(Object obj) {
162 if (obj instanceof StructurePath) {
163 StructurePath path = (StructurePath) obj;
164 return getAsUniqueString().equals(path.getAsUniqueString());
165 }
166 return false;
167 }
168
169 @Override
170 public int hashCode() {
171 return getAsUniqueString().hashCode();
172 }
173
174 public int compareTo(StructurePath o) {
175 return getAsUniqueString().compareTo(o.getAsUniqueString());
176 }
177
178 public Long getTid() {
179 return tid;
180 }
181
182 void setTid(Long tid) {
183 this.tid = tid;
184 }
185
186 /**
187 * Sets the separator character to use.
188 *
189 * @deprecated
190 */
191 public void setSeparator(Character separator) {
192 this.separator = separator;
193 }
194
195 /** Sets the parent (for ORM). */
196 protected void setParent(TreeSPath parent) {
197 this.parent = parent;
198 }
199
200 /** Sets the name (for ORM). */
201 protected void setName(String name) {
202 this.name = name;
203 }
204
205 }