KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project;
21
22 import java.util.Arrays JavaDoc;
23
24 /**
25  * Immutable holder of a selection path within a Cayenne project. Mostly used
26  * by various tools (CayenneModeler comes to mind) to navigate Cayenne
27  * projects. Contains a number of convenience methods to access path elements.
28  *
29  * <p>
30  * For instance, given a path <code>Project -> DataMap -> ObjEntity -> ObjAttribute</code>,
31  * <code>getObject</code> will return ObjAttribute, <code>getObjectParent</code>-
32  * ObjEntity, <code>getRoot</code>- Project.
33  * </p>
34  *
35  * @author Andrus Adamchik
36  */

37 public class ProjectPath {
38     public static final Object JavaDoc[] EMPTY_PATH = new Object JavaDoc[0];
39
40     protected Object JavaDoc[] path;
41
42     public ProjectPath() {
43         path = EMPTY_PATH;
44     }
45
46     /**
47      * Constructor for ProjectPath.
48      */

49     public ProjectPath(Object JavaDoc object) {
50         path = new Object JavaDoc[] { object };
51     }
52
53     /**
54      * Constructor for ProjectPath.
55      */

56     public ProjectPath(Object JavaDoc[] path) {
57         this.path = (path != null) ? path : EMPTY_PATH;
58     }
59
60     public Object JavaDoc[] getPath() {
61         return path;
62     }
63
64     public boolean isEmpty() {
65         return path == null || path.length == 0;
66     }
67
68     /**
69      * Scans path, looking for the first element that is an instanceof <code>aClass</code>.
70      */

71     public Object JavaDoc firstInstanceOf(Class JavaDoc aClass) {
72         for (int i = 0; i < path.length; i++) {
73             if (path[i] != null && aClass.isAssignableFrom(path[i].getClass())) {
74                 return path[i];
75             }
76         }
77
78         return null;
79     }
80
81     /**
82      * Returns an instance of the path, expanding this one by appending an
83      * object at the end.
84      */

85     public ProjectPath appendToPath(Object JavaDoc object) {
86         if (object != null) {
87             Object JavaDoc[] newPath = new Object JavaDoc[path.length + 1];
88
89             if (path.length > 0) {
90                 System.arraycopy(path, 0, newPath, 0, path.length);
91             }
92             newPath[path.length] = object;
93             return new ProjectPath(newPath);
94         }
95         else {
96             return this;
97         }
98     }
99
100     /**
101      *
102      * @since 1.1
103      */

104     public ProjectPath subpathWithSize(int subpathSize) {
105         if (subpathSize <= 0) {
106             return new ProjectPath();
107         }
108         else if(subpathSize == path.length) {
109             return this;
110         }
111
112         if (subpathSize > path.length) {
113             throw new ArrayIndexOutOfBoundsException JavaDoc(
114                 "Subpath is longer than this path "
115                     + subpathSize
116                     + " components. Path size: "
117                     + path.length);
118         }
119
120         Object JavaDoc[] newPath = new Object JavaDoc[subpathSize];
121         System.arraycopy(path, 0, newPath, 0, subpathSize);
122         return new ProjectPath(newPath);
123     }
124
125     /**
126      * Returns a subpath to the first occurance of an object.
127      *
128      * @since 1.1
129      */

130     public ProjectPath subpathOfObject(Object JavaDoc object) {
131         for (int i = 0; i < path.length; i++) {
132             if (path[i] == object) {
133                 // strip remaining objects
134
return subpathWithSize(i + 1);
135             }
136         }
137
138         return null;
139     }
140
141     /**
142      * Returns the root or starting object of the path.
143      */

144     public Object JavaDoc getRoot() {
145         if (path.length == 0) {
146             return null;
147         }
148
149         return path[0];
150     }
151
152     /**
153      * Returns the last object in the path.
154      */

155     public Object JavaDoc getObject() {
156         if (path.length == 0) {
157             return null;
158         }
159
160         // return last object
161
return path[path.length - 1];
162     }
163
164     /**
165      * Returns an object corresponding to the parent node of the node
166      * represented by the path. This is the object next to last object in the
167      * path.
168      */

169     public Object JavaDoc getObjectParent() {
170         if (path.length == 0) {
171             return null;
172         }
173
174         // return next to last object
175
return (path.length > 1) ? path[path.length - 2] : null;
176     }
177
178     public String JavaDoc toString() {
179         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
180         buf.append("[ProjectPath: ");
181         for (int i = 0; i < path.length; i++) {
182             if (i > 0) {
183                 buf.append(", ");
184             }
185
186             String JavaDoc token = (path[i] != null) ? path[i].getClass().getName() : "<null>";
187             buf.append(token);
188         }
189         buf.append("]");
190         return buf.toString();
191     }
192
193     public boolean equals(Object JavaDoc object) {
194         if (object == this) {
195             return true;
196         }
197
198         if (!(object instanceof ProjectPath)) {
199             return false;
200         }
201
202         ProjectPath otherPath = (ProjectPath) object;
203         return Arrays.equals(getPath(), otherPath.getPath());
204     }
205 }
206
Popular Tags