2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.core
.structure
.tree
;
18 import java
.io
.Serializable
;
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
>,
32 private static final long serialVersionUID
= -110121029180848183L;
34 /** Default character to use a separator: /. */
35 private static Character DEFAULT_SEPARATOR
= '/';
37 private Character separator
= DEFAULT_SEPARATOR
;
39 private String asUniqueString
;
48 public TreeSPath(String asUniqueString
) {
49 this.asUniqueString
= checkAndFormatPath(asUniqueString
);
52 public String
getAsUniqueString() {
53 return asUniqueString
;
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
62 public void setAsUniqueString(String str
) {
63 this.asUniqueString
= str
;
66 /** The separator actually used by this path. */
67 public Character
getSeparator() {
71 /** Gets the parent path. */
72 public TreeSPath
getParent() {
73 int lastSep
= getAsUniqueString().lastIndexOf(separator
);
77 String parentUniqueString
= getAsUniqueString().substring(0, lastSep
);
78 return new TreeSPath(parentUniqueString
);
81 /** Gets the name part of the path. */
82 public String
getName() {
83 int lastSep
= getAsUniqueString().lastIndexOf(separator
);
84 return getAsUniqueString().substring(lastSep
+ 1);
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
);
92 return new TreeSPath('/' + name
);
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
);
101 return new TreeSPath(getAsUniqueString() + '/' + name
);
105 * Parses a string to a path.
107 * @deprecated use constructor instead
109 public static TreeSPath
parseToCreatePath(String path
) {
110 return parseToCreatePath(path
, DEFAULT_SEPARATOR
);
113 protected String
checkAndFormatPath(String str
) {
114 if (str
.length() < 2) {
115 throw new SlcException("Path " + str
+ " is not short");
117 if (str
.charAt(0) != separator
) {
118 throw new SlcException("Path " + str
+ " have to start with "
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());
127 return buf
.toString();
131 * Parses a string to a path.
133 * @deprecated use constructor instead
135 public static TreeSPath
parseToCreatePath(String path
, Character separator
) {
136 return new TreeSPath(path
);
139 /** Lists the children from a registry. */
140 public List
<TreeSPath
> listChildren(StructureRegistry
<TreeSPath
> registry
) {
141 return listChildrenPaths(registry
, this);
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
)) {
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();
166 /** Depth of this path. */
167 public Integer
getDepth() {
168 return depthImpl(this);
171 protected int depthImpl(TreeSPath path
) {
172 if (path
.getParent() == null) {
175 return depthImpl(path
.getParent()) + 1;
179 public List
<TreeSPath
> getHierarchyAsList() {
180 List
<TreeSPath
> lst
= new Vector
<TreeSPath
>();
181 addParentToList(lst
, this);
186 protected void addParentToList(List
<TreeSPath
> lst
, TreeSPath current
) {
187 TreeSPath parent
= current
.getParent();
188 if (parent
!= null) {
189 addParentToList(lst
, parent
);
195 public String
toString() {
196 return getAsUniqueString();
200 public boolean equals(Object obj
) {
201 if (obj
instanceof StructurePath
) {
202 StructurePath path
= (StructurePath
) obj
;
203 return getAsUniqueString().equals(path
.getAsUniqueString());
209 public int hashCode() {
210 return getAsUniqueString().hashCode();
213 public int compareTo(StructurePath o
) {
214 return getAsUniqueString().compareTo(o
.getAsUniqueString());
217 public Long
getTid() {
221 void setTid(Long tid
) {