]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/structure/tree/TreeSPath.java
Make default execution ressources temp dir dependent of the JVM OS user, in order...
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / structure / tree / TreeSPath.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.core.structure.tree;
17
18 import java.io.Serializable;
19 import java.util.List;
20 import java.util.StringTokenizer;
21 import java.util.Vector;
22
23 import org.argeo.slc.SlcException;
24 import org.argeo.slc.structure.StructurePath;
25 import org.argeo.slc.structure.StructureRegistry;
26
27 /**
28 * Path for tree based <code>StructureRegistry</code> implementations.
29 */
30 public class TreeSPath implements StructurePath, Comparable<StructurePath>,
31 Serializable {
32 private static final long serialVersionUID = -110121029180848183L;
33
34 /** Default character to use a separator: /. */
35 private static Character DEFAULT_SEPARATOR = '/';
36
37 private Character separator = DEFAULT_SEPARATOR;
38
39 private String asUniqueString;
40
41 /** For ORM */
42 private Long tid;
43
44 public TreeSPath() {
45
46 }
47
48 public TreeSPath(String asUniqueString) {
49 this.asUniqueString = checkAndFormatPath(asUniqueString);
50 }
51
52 public String getAsUniqueString() {
53 return asUniqueString;
54 }
55
56 /**
57 * Sets all the required data from a string. <b>ATTENTION</b>: the path is
58 * not checked for performance reason. This method should be used only by
59 * ORM/OXM frameworks. Use constructor to create immutable tree structure
60 * paths.
61 */
62 public void setAsUniqueString(String str) {
63 this.asUniqueString = str;
64 }
65
66 /** The separator actually used by this path. */
67 public Character getSeparator() {
68 return separator;
69 }
70
71 /** Gets the parent path. */
72 public TreeSPath getParent() {
73 int lastSep = getAsUniqueString().lastIndexOf(separator);
74 if (lastSep < 1) {
75 return null;
76 }
77 String parentUniqueString = getAsUniqueString().substring(0, lastSep);
78 return new TreeSPath(parentUniqueString);
79 }
80
81 /** Gets the name part of the path. */
82 public String getName() {
83 int lastSep = getAsUniqueString().lastIndexOf(separator);
84 return getAsUniqueString().substring(lastSep + 1);
85 }
86
87 /** Create a path without parent. */
88 public static TreeSPath createRootPath(String name) {
89 if (name.indexOf(DEFAULT_SEPARATOR) >= 0) {
90 throw new SlcException("Name cannot contain " + DEFAULT_SEPARATOR);
91 }
92 return new TreeSPath('/' + name);
93 }
94
95 /** Create a child . */
96 public TreeSPath createChild(String name) {
97 if (name.indexOf(separator) > -1) {
98 throw new SlcException("Tree path name '" + name
99 + "' contains separator character " + separator);
100 }
101 return new TreeSPath(getAsUniqueString() + '/' + name);
102 }
103
104 /**
105 * Parses a string to a path.
106 *
107 * @deprecated use constructor instead
108 */
109 public static TreeSPath parseToCreatePath(String path) {
110 return parseToCreatePath(path, DEFAULT_SEPARATOR);
111 }
112
113 protected String checkAndFormatPath(String str) {
114 if (str.length() < 2) {
115 throw new SlcException("Path " + str + " is not short");
116 }
117 if (str.charAt(0) != separator) {
118 throw new SlcException("Path " + str + " have to start with "
119 + separator);
120 }
121
122 StringBuffer buf = new StringBuffer(str.length() + 5);
123 StringTokenizer st = new StringTokenizer(str, separator.toString());
124 while (st.hasMoreTokens()) {
125 buf.append(separator).append(st.nextToken());
126 }
127 return buf.toString();
128 }
129
130 /**
131 * Parses a string to a path.
132 *
133 * @deprecated use constructor instead
134 */
135 public static TreeSPath parseToCreatePath(String path, Character separator) {
136 return new TreeSPath(path);
137 }
138
139 /** Lists the children from a registry. */
140 public List<TreeSPath> listChildren(StructureRegistry<TreeSPath> registry) {
141 return listChildrenPaths(registry, this);
142 }
143
144 /** Lists the children from a given path from a registry. */
145 public static List<TreeSPath> listChildrenPaths(
146 StructureRegistry<TreeSPath> registry, TreeSPath path) {
147 List<TreeSPath> paths = new Vector<TreeSPath>();
148 List<TreeSPath> allPaths = registry.listPaths();
149 for (TreeSPath pathT : allPaths) {
150 if (pathT.getParent() != null && pathT.getParent().equals(path)) {
151 paths.add(pathT);
152 }
153 }
154 return paths;
155 }
156
157 /** Gets the root tree path of this path. */
158 public TreeSPath getRoot() {
159 TreeSPath root = this;
160 while (root.getParent() != null) {
161 root = root.getParent();
162 }
163 return root;
164 }
165
166 /** Depth of this path. */
167 public Integer getDepth() {
168 return depthImpl(this);
169 }
170
171 protected int depthImpl(TreeSPath path) {
172 if (path.getParent() == null) {
173 return 1;
174 } else {
175 return depthImpl(path.getParent()) + 1;
176 }
177 }
178
179 public List<TreeSPath> getHierarchyAsList() {
180 List<TreeSPath> lst = new Vector<TreeSPath>();
181 addParentToList(lst, this);
182 lst.add(this);
183 return lst;
184 }
185
186 protected void addParentToList(List<TreeSPath> lst, TreeSPath current) {
187 TreeSPath parent = current.getParent();
188 if (parent != null) {
189 addParentToList(lst, parent);
190 lst.add(parent);
191 }
192 }
193
194 @Override
195 public String toString() {
196 return getAsUniqueString();
197 }
198
199 @Override
200 public boolean equals(Object obj) {
201 if (obj instanceof StructurePath) {
202 StructurePath path = (StructurePath) obj;
203 return getAsUniqueString().equals(path.getAsUniqueString());
204 }
205 return false;
206 }
207
208 @Override
209 public int hashCode() {
210 return getAsUniqueString().hashCode();
211 }
212
213 public int compareTo(StructurePath o) {
214 return getAsUniqueString().compareTo(o.getAsUniqueString());
215 }
216
217 public Long getTid() {
218 return tid;
219 }
220
221 void setTid(Long tid) {
222 this.tid = tid;
223 }
224 }