KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > project > ProjectPath


1 /*
2  * ====================================================================
3  *
4  * The ObjectStyle Group Software License, version 1.1
5  * ObjectStyle Group - http://objectstyle.org/
6  *
7  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
8  * of the software. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution, if any,
23  * must include the following acknowlegement:
24  * "This product includes software developed by independent contributors
25  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
26  * Alternately, this acknowlegement may appear in the software itself,
27  * if and wherever such third-party acknowlegements normally appear.
28  *
29  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
30  * or promote products derived from this software without prior written
31  * permission. For written permission, email
32  * "andrus at objectstyle dot org".
33  *
34  * 5. Products derived from this software may not be called "ObjectStyle"
35  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
36  * names without prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals and hosted on ObjectStyle Group web site. For more
54  * information on the ObjectStyle Group, please see
55  * <http://objectstyle.org/>.
56  */

57 package org.objectstyle.cayenne.project;
58
59 import java.util.Arrays JavaDoc;
60
61 /**
62  * Immutable holder of a selection path within a Cayenne project. Mostly used
63  * by various tools (CayenneModeler comes to mind) to navigate Cayenne
64  * projects. Contains a number of convenience methods to access path elements.
65  *
66  * <p>
67  * For instance, given a path <code>Project -> DataMap -> ObjEntity -> ObjAttribute</code>,
68  * <code>getObject</code> will return ObjAttribute, <code>getObjectParent</code>-
69  * ObjEntity, <code>getRoot</code>- Project.
70  * </p>
71  *
72  * @author Andrei Adamchik
73  */

74 public class ProjectPath {
75     public static final Object JavaDoc[] EMPTY_PATH = new Object JavaDoc[0];
76
77     protected Object JavaDoc[] path;
78
79     public ProjectPath() {
80         path = EMPTY_PATH;
81     }
82
83     /**
84      * Constructor for ProjectPath.
85      */

86     public ProjectPath(Object JavaDoc object) {
87         path = new Object JavaDoc[] { object };
88     }
89
90     /**
91      * Constructor for ProjectPath.
92      */

93     public ProjectPath(Object JavaDoc[] path) {
94         this.path = (path != null) ? path : EMPTY_PATH;
95     }
96
97     public Object JavaDoc[] getPath() {
98         return path;
99     }
100
101     public boolean isEmpty() {
102         return path == null || path.length == 0;
103     }
104
105     /**
106      * Scans path, looking for the first element that is an instanceof <code>aClass</code>.
107      */

108     public Object JavaDoc firstInstanceOf(Class JavaDoc aClass) {
109         for (int i = 0; i < path.length; i++) {
110             if (path[i] != null && aClass.isAssignableFrom(path[i].getClass())) {
111                 return path[i];
112             }
113         }
114
115         return null;
116     }
117
118     /**
119      * Returns an instance of the path, expanding this one by appending an
120      * object at the end.
121      */

122     public ProjectPath appendToPath(Object JavaDoc object) {
123         if (object != null) {
124             Object JavaDoc[] newPath = new Object JavaDoc[path.length + 1];
125
126             if (path.length > 0) {
127                 System.arraycopy(path, 0, newPath, 0, path.length);
128             }
129             newPath[path.length] = object;
130             return new ProjectPath(newPath);
131         }
132         else {
133             return this;
134         }
135     }
136
137     /**
138      *
139      * @since 1.1
140      */

141     public ProjectPath subpathWithSize(int subpathSize) {
142         if (subpathSize <= 0) {
143             return new ProjectPath();
144         }
145         else if(subpathSize == path.length) {
146             return this;
147         }
148
149         if (subpathSize > path.length) {
150             throw new ArrayIndexOutOfBoundsException JavaDoc(
151                 "Subpath is longer than this path "
152                     + subpathSize
153                     + " components. Path size: "
154                     + path.length);
155         }
156
157         Object JavaDoc[] newPath = new Object JavaDoc[subpathSize];
158         System.arraycopy(path, 0, newPath, 0, subpathSize);
159         return new ProjectPath(newPath);
160     }
161
162     /**
163      * Returns a subpath to the first occurance of an object.
164      *
165      * @since 1.1
166      */

167     public ProjectPath subpathOfObject(Object JavaDoc object) {
168         for (int i = 0; i < path.length; i++) {
169             if (path[i] == object) {
170                 // strip remaining objects
171
return subpathWithSize(i + 1);
172             }
173         }
174
175         return null;
176     }
177
178     /**
179      * Returns the root or starting object of the path.
180      */

181     public Object JavaDoc getRoot() {
182         if (path.length == 0) {
183             return null;
184         }
185
186         return path[0];
187     }
188
189     /**
190      * Returns the last object in the path.
191      */

192     public Object JavaDoc getObject() {
193         if (path.length == 0) {
194             return null;
195         }
196
197         // return last object
198
return path[path.length - 1];
199     }
200
201     /**
202      * Returns an object corresponding to the parent node of the node
203      * represented by the path. This is the object next to last object in the
204      * path.
205      */

206     public Object JavaDoc getObjectParent() {
207         if (path.length == 0) {
208             return null;
209         }
210
211         // return next to last object
212
return (path.length > 1) ? path[path.length - 2] : null;
213     }
214
215     public String JavaDoc toString() {
216         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
217         buf.append("[ProjectPath: ");
218         for (int i = 0; i < path.length; i++) {
219             if (i > 0) {
220                 buf.append(", ");
221             }
222
223             String JavaDoc token = (path[i] != null) ? path[i].getClass().getName() : "<null>";
224             buf.append(token);
225         }
226         buf.append("]");
227         return buf.toString();
228     }
229
230     public boolean equals(Object JavaDoc object) {
231         if (object == this) {
232             return true;
233         }
234
235         if (!(object instanceof ProjectPath)) {
236             return false;
237         }
238
239         ProjectPath otherPath = (ProjectPath) object;
240         return Arrays.equals(getPath(), otherPath.getPath());
241     }
242 }
243
Popular Tags