]> 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
Start working on serialized JMS
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / structure / tree / TreeSPath.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.core.structure.tree;
18
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 /** Default character to use a separator: /. */
32 private static Character DEFAULT_SEPARATOR = '/';
33
34 private Character separator = DEFAULT_SEPARATOR;
35
36 private String asUniqueString;
37
38 /** For ORM */
39 private Long tid;
40
41 public TreeSPath() {
42
43 }
44
45 public TreeSPath(String asUniqueString) {
46 this.asUniqueString = checkAndFormatPath(asUniqueString);
47 }
48
49 public String getAsUniqueString() {
50 return asUniqueString;
51 }
52
53 /**
54 * Sets all the required data from a string. <b>ATTENTION</b>: the path is
55 * not checked for performance reason. This method should be used only by
56 * ORM/OXM frameworks. Use constructor to create immutable tree structure
57 * paths.
58 */
59 public void setAsUniqueString(String str) {
60 this.asUniqueString = str;
61 }
62
63 /** The separator actually used by this path. */
64 public Character getSeparator() {
65 return separator;
66 }
67
68 /** Gets the parent path. */
69 public TreeSPath getParent() {
70 int lastSep = getAsUniqueString().lastIndexOf(separator);
71 if (lastSep < 1) {
72 return null;
73 }
74 String parentUniqueString = getAsUniqueString().substring(0, lastSep);
75 return new TreeSPath(parentUniqueString);
76 }
77
78 /** Gets the name part of the path. */
79 public String getName() {
80 int lastSep = getAsUniqueString().lastIndexOf(separator);
81 return getAsUniqueString().substring(lastSep + 1);
82 }
83
84 /** Create a path without parent. */
85 public static TreeSPath createRootPath(String name) {
86 if (name.indexOf(DEFAULT_SEPARATOR) >= 0) {
87 throw new SlcException("Name cannot contain " + DEFAULT_SEPARATOR);
88 }
89 return new TreeSPath('/' + name);
90 }
91
92 /** Create a child . */
93 public TreeSPath createChild(String name) {
94 if (name.indexOf(separator) > -1) {
95 throw new SlcException("Tree path name '" + name
96 + "' contains separator character " + separator);
97 }
98 return new TreeSPath(getAsUniqueString() + '/' + name);
99 }
100
101 /**
102 * Parses a string to a path.
103 *
104 * @deprecated use constructor instead
105 */
106 public static TreeSPath parseToCreatePath(String path) {
107 return parseToCreatePath(path, DEFAULT_SEPARATOR);
108 }
109
110 protected String checkAndFormatPath(String str) {
111 if (str.length() < 2) {
112 throw new SlcException("Path " + str + " is not short");
113 }
114 if (str.charAt(0) != separator) {
115 throw new SlcException("Path " + str + " have to start with "
116 + separator);
117 }
118
119 StringBuffer buf = new StringBuffer(str.length() + 5);
120 StringTokenizer st = new StringTokenizer(str, separator.toString());
121 while (st.hasMoreTokens()) {
122 buf.append(separator).append(st.nextToken());
123 }
124 return buf.toString();
125 }
126
127 /**
128 * Parses a string to a path.
129 *
130 * @deprecated use constructor instead
131 */
132 public static TreeSPath parseToCreatePath(String path, Character separator) {
133 return new TreeSPath(path);
134 }
135
136 /** Lists the children from a registry. */
137 public List<TreeSPath> listChildren(StructureRegistry<TreeSPath> registry) {
138 return listChildrenPaths(registry, this);
139 }
140
141 /** Lists the children from a given path from a registry. */
142 public static List<TreeSPath> listChildrenPaths(
143 StructureRegistry<TreeSPath> registry, TreeSPath path) {
144 List<TreeSPath> paths = new Vector<TreeSPath>();
145 List<TreeSPath> allPaths = registry.listPaths();
146 for (TreeSPath pathT : allPaths) {
147 if (pathT.getParent() != null && pathT.getParent().equals(path)) {
148 paths.add(pathT);
149 }
150 }
151 return paths;
152 }
153
154 /** Gets the root tree path of this path. */
155 public TreeSPath getRoot() {
156 TreeSPath root = this;
157 while (root.getParent() != null) {
158 root = root.getParent();
159 }
160 return root;
161 }
162
163 /** Depth of this path. */
164 public Integer getDepth() {
165 return depthImpl(this);
166 }
167
168 protected int depthImpl(TreeSPath path) {
169 if (path.getParent() == null) {
170 return 1;
171 } else {
172 return depthImpl(path.getParent()) + 1;
173 }
174 }
175
176 public List<TreeSPath> getHierarchyAsList() {
177 List<TreeSPath> lst = new Vector<TreeSPath>();
178 addParentToList(lst, this);
179 lst.add(this);
180 return lst;
181 }
182
183 protected void addParentToList(List<TreeSPath> lst, TreeSPath current) {
184 TreeSPath parent = current.getParent();
185 if (parent != null) {
186 addParentToList(lst, parent);
187 lst.add(parent);
188 }
189 }
190
191 @Override
192 public String toString() {
193 return getAsUniqueString();
194 }
195
196 @Override
197 public boolean equals(Object obj) {
198 if (obj instanceof StructurePath) {
199 StructurePath path = (StructurePath) obj;
200 return getAsUniqueString().equals(path.getAsUniqueString());
201 }
202 return false;
203 }
204
205 @Override
206 public int hashCode() {
207 return getAsUniqueString().hashCode();
208 }
209
210 public int compareTo(StructurePath o) {
211 return getAsUniqueString().compareTo(o.getAsUniqueString());
212 }
213
214 public Long getTid() {
215 return tid;
216 }
217
218 void setTid(Long tid) {
219 this.tid = tid;
220 }
221 }