KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > registry > Path


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.arooa.registry;
5
6 import java.io.Serializable JavaDoc;
7
8 /**
9  * Represent the path to a component.
10  *
11  * @author Rob Gordon.
12  */

13 public class Path implements Serializable JavaDoc {
14     private static final long serialVersionUID = 20051117;
15     
16     /** The path separator. */
17     public static final char PATH_SEPARATOR = '/';
18
19     /** Private interface to for an element in a path. */
20     private interface PathElement extends Serializable JavaDoc {
21         public String JavaDoc idOf();
22     }
23     
24     /** A path element for a compoennt which has an id. */
25     private class IdPathElement implements PathElement {
26         private static final long serialVersionUID = 20051122;
27         private final String JavaDoc id;
28         IdPathElement(String JavaDoc id) {
29             if (id == null) {
30                 throw new NullPointerException JavaDoc("Id must not be null!");
31             }
32             this.id = id;
33         }
34         public String JavaDoc idOf() {
35             return id;
36         }
37         public boolean equals(Object JavaDoc o) {
38             if (!(o instanceof IdPathElement)) {
39                 return false;
40             }
41             IdPathElement other = (IdPathElement) o;
42             return id.equals(other.id);
43         }
44         public int hashCode() {
45             return id.hashCode();
46         }
47     }
48
49     /** Array of elements which make up this path */
50     private final PathElement[] pathElements;
51
52     /** Path element for a component which doesn't have an id. */
53     private final static PathElement nullElement = new PathElement() {
54         private static final long serialVersionUID = 20051122;
55         public String JavaDoc idOf() {
56             return null;
57         }
58     };
59     
60     /**
61      * Constructor for an empty path.
62      */

63     public Path() {
64         this.pathElements = new PathElement[0];
65     }
66     
67     /**
68      * Constructor for a path which takes a path in the form a/b/c.
69      *
70      * @param path The path as a String.
71      */

72     public Path(String JavaDoc path) {
73         this.pathElements = parsePath(path);
74     }
75
76     /**
77      * Constructor for a path as an array of path elements.
78      *
79      * @param pathElements The path elements.
80      */

81     private Path(PathElement[] pathElements) {
82         this.pathElements = pathElements;
83     }
84     
85     /**
86      * Parse a string path into it's path elements.
87      *
88      * @param path The String path.
89      * @return The path elements.
90      */

91     private PathElement[] parsePath(String JavaDoc path) {
92         String JavaDoc[] ids = path.split(new String JavaDoc(new char[] {PATH_SEPARATOR}));
93         PathElement[] elements = new PathElement[ids.length];
94         for (int i = 0; i < ids.length; ++i ) {
95             elements[i] = new IdPathElement(ids[i]);
96         }
97         return elements;
98     }
99     
100     /**
101      * Return the size or depth of this path. a/b/c has a size of 3.
102      *
103      * @return The size of this path.
104      */

105     public int size() {
106         return pathElements.length;
107     }
108     
109     /**
110      * Get the id of the top element in the path. This can be used to lookup
111      * the component which contains the component which is the next part
112      * of the path.
113      *
114      * @return The id of the topmost element.
115      */

116     public String JavaDoc getId() {
117         if (pathElements.length == 0) {
118             return null;
119         }
120         return pathElements[0].idOf();
121         
122     }
123
124     /**
125      * Get the path below the topmost element. This can be used to traverse
126      * down
127      * to lookup a component
128      *
129      * @return The id o
130      */

131     public Path getChildPath() {
132         if (pathElements.length == 0) {
133             return null;
134         }
135         if (pathElements[0] == nullElement) {
136             return null;
137         }
138         PathElement[] childElements = new PathElement[pathElements.length -1];
139         System.arraycopy(pathElements, 1, childElements, 0, childElements.length);
140         return new Path(childElements);
141     }
142     
143     /**
144      * Create a new path by adding a new path element identified by they id to
145      * this path. If the id is null a new null path element will be added.
146      *
147      * @param id The id of the new path element. May be null.
148      * @return The new path.
149      */

150     public Path addId(String JavaDoc id) {
151         if (id == null) {
152             return addNull();
153         }
154         PathElement[] newElements = new PathElement[pathElements.length + 1];
155         System.arraycopy(pathElements, 0, newElements, 0, pathElements.length);
156         newElements[pathElements.length] = new IdPathElement(id);
157         return new Path(newElements);
158     }
159     
160     /**
161      * Create a new path by adding a null path element to this path.
162      *
163      * @return The new path.
164      */

165     public Path addNull() {
166         PathElement[] newElements = new PathElement[pathElements.length + 1];
167         System.arraycopy(pathElements, 0, newElements, 0, pathElements.length);
168         newElements[pathElements.length] = nullElement;
169         return new Path(newElements);
170     }
171     
172     /**
173      * Does this path contain a null element.
174      *
175      * @return true if it does, false otherwise.
176      */

177     public boolean containsNull() {
178         for (int i = 0; i < pathElements.length; ++i) {
179             if (pathElements[i] == nullElement) {
180                 return true;
181             }
182         }
183         return false;
184     }
185     
186     /**
187      * Create a new path by adding a path to this path.
188      *
189      * @param extra The extra path.
190      * @return The new path.
191      */

192     public Path addPath(Path extra) {
193         PathElement[] extraElements = extra.pathElements;
194         PathElement[] newElements
195             = new PathElement[pathElements.length + extraElements.length];
196         System.arraycopy(pathElements, 0, newElements, 0, pathElements.length);
197         System.arraycopy(extraElements, 0, newElements,
198                 pathElements.length, extraElements.length);
199         return new Path(newElements);
200     }
201     
202     /**
203      * Resolve the other path relative to this path. if this path is a/b and
204      * other is a/b/c/d the resultant relative path is c/d.
205      *
206      * @param other The other path.
207      * @return The relative path, null if the other path is not in this heierarchy.
208      */

209     public Path relativeTo(Path other) {
210         PathElement[] otherElements = other.pathElements;
211             
212         // the other path is higher up or adjacent to our path.
213
if (otherElements.length <= pathElements.length) {
214             return null;
215         }
216         
217         for (int i = 0; i < pathElements.length; ++i) {
218             if (!otherElements[i].equals(pathElements[i])) {
219                 return null;
220             }
221         }
222         
223         PathElement[] result = new PathElement[otherElements.length -
224                                                pathElements.length];
225         System.arraycopy(otherElements, pathElements.length,
226                 result, 0, result.length);
227     
228         return new Path(result);
229         
230     }
231
232     /*
233      * (non-Javadoc)
234      * @see java.lang.Object#toString()
235      */

236     public String JavaDoc toString() {
237         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
238         for (int i = 0; i < pathElements.length; ++i) {
239             if (i > 0) {
240                 result.append(PATH_SEPARATOR);
241             }
242             result.append(pathElements[i].idOf());
243         }
244         return result.toString();
245     }
246 }
247
Popular Tags