2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.core
.structure
.tree
;
19 import java
.util
.List
;
20 import java
.util
.StringTokenizer
;
21 import java
.util
.Vector
;
23 import org
.argeo
.slc
.SlcException
;
24 import org
.argeo
.slc
.structure
.StructurePath
;
25 import org
.argeo
.slc
.structure
.StructureRegistry
;
28 * Path for tree based <code>StructureRegistry</code> implementations.
30 public class TreeSPath
implements StructurePath
, Comparable
<StructurePath
> {
31 /** Default character to use a separator: /. */
32 private static Character DEFAULT_SEPARATOR
= '/';
34 private Character separator
= DEFAULT_SEPARATOR
;
36 private String asUniqueString
;
45 public TreeSPath(String asUniqueString
) {
46 this.asUniqueString
= checkAndFormatPath(asUniqueString
);
49 public String
getAsUniqueString() {
50 return asUniqueString
;
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
59 public void setAsUniqueString(String str
) {
60 this.asUniqueString
= str
;
63 /** The separator actually used by this path. */
64 public Character
getSeparator() {
68 /** Gets the parent path. */
69 public TreeSPath
getParent() {
70 int lastSep
= getAsUniqueString().lastIndexOf(separator
);
74 String parentUniqueString
= getAsUniqueString().substring(0, lastSep
);
75 return new TreeSPath(parentUniqueString
);
78 /** Gets the name part of the path. */
79 public String
getName() {
80 int lastSep
= getAsUniqueString().lastIndexOf(separator
);
81 return getAsUniqueString().substring(lastSep
+ 1);
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
);
89 return new TreeSPath('/' + name
);
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
);
98 return new TreeSPath(getAsUniqueString() + '/' + name
);
102 * Parses a string to a path.
104 * @deprecated use constructor instead
106 public static TreeSPath
parseToCreatePath(String path
) {
107 return parseToCreatePath(path
, DEFAULT_SEPARATOR
);
110 protected String
checkAndFormatPath(String str
) {
111 if (str
.length() < 2) {
112 throw new SlcException("Path " + str
+ " is not short");
114 if (str
.charAt(0) != separator
) {
115 throw new SlcException("Path " + str
+ " have to start with "
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());
124 return buf
.toString();
128 * Parses a string to a path.
130 * @deprecated use constructor instead
132 public static TreeSPath
parseToCreatePath(String path
, Character separator
) {
133 return new TreeSPath(path
);
136 /** Lists the children from a registry. */
137 public List
<TreeSPath
> listChildren(StructureRegistry
<TreeSPath
> registry
) {
138 return listChildrenPaths(registry
, this);
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
)) {
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();
163 /** Depth of this path. */
164 public Integer
getDepth() {
165 return depthImpl(this);
168 protected int depthImpl(TreeSPath path
) {
169 if (path
.getParent() == null) {
172 return depthImpl(path
.getParent()) + 1;
176 public List
<TreeSPath
> getHierarchyAsList() {
177 List
<TreeSPath
> lst
= new Vector
<TreeSPath
>();
178 addParentToList(lst
, this);
183 protected void addParentToList(List
<TreeSPath
> lst
, TreeSPath current
) {
184 TreeSPath parent
= current
.getParent();
185 if (parent
!= null) {
186 addParentToList(lst
, parent
);
192 public String
toString() {
193 return getAsUniqueString();
197 public boolean equals(Object obj
) {
198 if (obj
instanceof StructurePath
) {
199 StructurePath path
= (StructurePath
) obj
;
200 return getAsUniqueString().equals(path
.getAsUniqueString());
206 public int hashCode() {
207 return getAsUniqueString().hashCode();
210 public int compareTo(StructurePath o
) {
211 return getAsUniqueString().compareTo(o
.getAsUniqueString());
214 public Long
getTid() {
218 void setTid(Long tid
) {