Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
* See the License for the specific language governing permissions and |
15 |
* limitations under the License. |
16 |
*/ |
17 |
package org.apache.naming; |
18 |
|
19 |
import javax.naming.InvalidNameException; |
20 |
import javax.naming.Name; |
21 |
import java.util.Collections; |
22 |
import java.util.Enumeration; |
23 |
import java.util.List; |
24 |
|
25 |
public class SimpleCompositeName implements Name { |
26 |
|
27 |
private transient SimpleName impl; |
28 |
/** |
29 |
* Constructs a new composite name instance using the components |
30 |
* specified by 'comps'. This protected method is intended to be |
31 |
* to be used by subclasses of CompositeName when they override |
32 |
* methods such as clone(), getPrefix(), getSuffix(). |
33 |
* |
34 |
* @param comps A non-null enumeration containing the components for the new |
35 |
* composite name. Each element is of class String. |
36 |
* The enumeration will be consumed to extract its |
37 |
* elements. |
38 |
*/ |
39 |
protected SimpleCompositeName(List<String> comps) { |
40 |
impl = new SimpleName(comps); // null means use default syntax |
41 |
} |
42 |
|
43 |
/** |
44 |
* Constructs a new composite name instance by parsing the string n |
45 |
* using the composite name syntax (left-to-right, slash separated). |
46 |
* The composite name syntax is described in detail in the class |
47 |
* description. |
48 |
* |
49 |
* @param n The non-null string to parse. |
50 |
* @exception javax.naming.InvalidNameException If n has invalid composite name syntax. |
51 |
*/ |
52 |
public SimpleCompositeName(String n) throws InvalidNameException { |
53 |
impl = new SimpleName(n); // null means use default syntax |
54 |
} |
55 |
|
56 |
/** |
57 |
* Constructs a new empty composite name. Such a name returns true |
58 |
* when <code>isEmpty()</code> is invoked on it. |
59 |
*/ |
60 |
public SimpleCompositeName() { |
61 |
impl = new SimpleName(); // null means use default syntax |
62 |
} |
63 |
|
64 |
/** |
65 |
* Generates the string representation of this composite name. |
66 |
* The string representation consists of enumerating in order |
67 |
* each component of the composite name and separating |
68 |
* each component by a forward slash character. Quoting and |
69 |
* escape characters are applied where necessary according to |
70 |
* the JNDI syntax, which is described in the class description. |
71 |
* An empty component is represented by an empty string. |
72 |
* |
73 |
* The string representation thus generated can be passed to |
74 |
* the CompositeName constructor to create a new equivalent |
75 |
* composite name. |
76 |
* |
77 |
* @return A non-null string representation of this composite name. |
78 |
*/ |
79 |
public String toString() { |
80 |
return impl.toString(); |
81 |
} |
82 |
|
83 |
/** |
84 |
* Determines whether two composite names are equal. |
85 |
* If obj is null or not a composite name, false is returned. |
86 |
* Two composite names are equal if each component in one is equal |
87 |
* to the corresponding component in the other. This implies |
88 |
* both have the same number of components, and each component's |
89 |
* equals() test against the corresponding component in the other name |
90 |
* returns true. |
91 |
* |
92 |
* @param obj The possibly null object to compare against. |
93 |
* @return true if obj is equal to this composite name, false otherwise. |
94 |
* @see #hashCode |
95 |
*/ |
96 |
public boolean equals(Object obj) { |
97 |
return (obj != null && |
98 |
obj instanceof SimpleCompositeName && |
99 |
impl.equals(((SimpleCompositeName)obj).impl)); |
100 |
} |
101 |
|
102 |
/** |
103 |
* Computes the hash code of this composite name. |
104 |
* The hash code is the sum of the hash codes of individual components |
105 |
* of this composite name. |
106 |
* |
107 |
* @return An int representing the hash code of this name. |
108 |
* @see #equals |
109 |
*/ |
110 |
public int hashCode() { |
111 |
return impl.hashCode(); |
112 |
} |
113 |
|
114 |
|
115 |
/** |
116 |
* Compares this CompositeName with the specified Object for order. |
117 |
* Returns a |
118 |
* negative integer, zero, or a positive integer as this Name is less |
119 |
* than, equal to, or greater than the given Object. |
120 |
* <p> |
121 |
* If obj is null or not an instance of CompositeName, ClassCastException |
122 |
* is thrown. |
123 |
* <p> |
124 |
* See equals() for what it means for two composite names to be equal. |
125 |
* If two composite names are equal, 0 is returned. |
126 |
* <p> |
127 |
* Ordering of composite names follows the lexicographical rules for |
128 |
* string comparison, with the extension that this applies to all |
129 |
* the components in the composite name. The effect is as if all the |
130 |
* components were lined up in their specified ordered and the |
131 |
* lexicographical rules applied over the two line-ups. |
132 |
* If this composite name is "lexicographically" lesser than obj, |
133 |
* a negative number is returned. |
134 |
* If this composite name is "lexicographically" greater than obj, |
135 |
* a positive number is returned. |
136 |
* @param obj The non-null object to compare against. |
137 |
* |
138 |
* @return a negative integer, zero, or a positive integer as this Name |
139 |
* is less than, equal to, or greater than the given Object. |
140 |
* @exception ClassCastException if obj is not a CompositeName. |
141 |
*/ |
142 |
public int compareTo(Object obj) { |
143 |
if (!(obj instanceof SimpleCompositeName)) { |
144 |
throw new ClassCastException("Not a SimpleCompositeName"); |
145 |
} |
146 |
return impl.compareTo(((SimpleCompositeName)obj).impl); |
147 |
} |
148 |
|
149 |
/** |
150 |
* Generates a copy of this composite name. |
151 |
* Changes to the components of this composite name won't |
152 |
* affect the new copy and vice versa. |
153 |
* |
154 |
* @return A non-null copy of this composite name. |
155 |
*/ |
156 |
public Object clone() { |
157 |
return (new SimpleCompositeName(impl.getComponents())); |
158 |
} |
159 |
|
160 |
/** |
161 |
* Retrieves the number of components in this composite name. |
162 |
* |
163 |
* @return The nonnegative number of components in this composite name. |
164 |
*/ |
165 |
public int size() { |
166 |
return (impl.size()); |
167 |
} |
168 |
|
169 |
/** |
170 |
* Determines whether this composite name is empty. A composite name |
171 |
* is empty if it has zero components. |
172 |
* |
173 |
* @return true if this composite name is empty, false otherwise. |
174 |
*/ |
175 |
public boolean isEmpty() { |
176 |
return (impl.isEmpty()); |
177 |
} |
178 |
|
179 |
/** |
180 |
* Retrieves the components of this composite name as an enumeration |
181 |
* of strings. |
182 |
* The effects of updates to this composite name on this enumeration |
183 |
* is undefined. |
184 |
* |
185 |
* @return A non-null enumeration of the components of |
186 |
* this composite name. Each element of the enumeration is of |
187 |
* class String. |
188 |
*/ |
189 |
public Enumeration<String> getAll() { |
190 |
return Collections.enumeration(impl.getComponents()); |
191 |
} |
192 |
|
193 |
/** |
194 |
* Retrieves a component of this composite name. |
195 |
* |
196 |
* @param posn The 0-based index of the component to retrieve. |
197 |
* Must be in the range [0,size()). |
198 |
* @return The non-null component at index posn. |
199 |
* @exception ArrayIndexOutOfBoundsException if posn is outside the |
200 |
* specified range. |
201 |
*/ |
202 |
public String get(int posn) { |
203 |
return (impl.get(posn)); |
204 |
} |
205 |
|
206 |
/** |
207 |
* Creates a composite name whose components consist of a prefix of the |
208 |
* components in this composite name. Subsequent changes to |
209 |
* this composite name does not affect the name that is returned. |
210 |
* |
211 |
* @param pos The 0-based index of the component at which to stop. |
212 |
* Must be in the range [0,size()]. |
213 |
* @return A composite name consisting of the components at indexes in |
214 |
* the range [0,posn). |
215 |
* @exception ArrayIndexOutOfBoundsException |
216 |
* If posn is outside the specified range. |
217 |
*/ |
218 |
public Name getPrefix(int pos) { |
219 |
List<String> comps = impl.getPrefix(pos); |
220 |
return (new SimpleCompositeName(comps)); |
221 |
} |
222 |
|
223 |
/** |
224 |
* Creates a composite name whose components consist of a suffix of the |
225 |
* components in this composite name. Subsequent changes to |
226 |
* this composite name does not affect the name that is returned. |
227 |
* |
228 |
* @param pos The 0-based index of the component at which to start. |
229 |
* Must be in the range [0,size()]. |
230 |
* @return A composite name consisting of the components at indexes in |
231 |
* the range [posn,size()). If posn is equal to |
232 |
* size(), an empty composite name is returned. |
233 |
* @exception ArrayIndexOutOfBoundsException |
234 |
* If posn is outside the specified range. |
235 |
*/ |
236 |
public Name getSuffix(int pos) { |
237 |
List<String> comps = impl.getSuffix(pos); |
238 |
return (new SimpleCompositeName(comps)); |
239 |
} |
240 |
|
241 |
/** |
242 |
* Determines whether a composite name is a prefix of this composite name. |
243 |
* A composite name 'n' is a prefix if it is equal to |
244 |
* getPrefix(n.size())--in other words, this composite name |
245 |
* starts with 'n'. If 'n' is null or not a composite name, false is returned. |
246 |
* |
247 |
* @param n The possibly null name to check. |
248 |
* @return true if n is a CompositeName and |
249 |
* is a prefix of this composite name, false otherwise. |
250 |
*/ |
251 |
public boolean startsWith(Name n) { |
252 |
if (n instanceof SimpleCompositeName) { |
253 |
return (impl.startsWith(n.size(), n.getAll())); |
254 |
} else { |
255 |
return false; |
256 |
} |
257 |
} |
258 |
|
259 |
/** |
260 |
* Determines whether a composite name is a suffix of this composite name. |
261 |
* A composite name 'n' is a suffix if it it is equal to |
262 |
* getSuffix(size()-n.size())--in other words, this |
263 |
* composite name ends with 'n'. |
264 |
* If n is null or not a composite name, false is returned. |
265 |
* |
266 |
* @param n The possibly null name to check. |
267 |
* @return true if n is a CompositeName and |
268 |
* is a suffix of this composite name, false otherwise. |
269 |
*/ |
270 |
public boolean endsWith(Name n) { |
271 |
if (n instanceof SimpleCompositeName) { |
272 |
return (impl.endsWith(n.size(), n.getAll())); |
273 |
} else { |
274 |
return false; |
275 |
} |
276 |
} |
277 |
|
278 |
/** |
279 |
* Adds the components of a composite name -- in order -- to the end of |
280 |
* this composite name. |
281 |
* |
282 |
* @param suffix The non-null components to add. |
283 |
* @return The updated CompositeName, not a new one. Cannot be null. |
284 |
* @exception InvalidNameException If suffix is not a composite name. |
285 |
*/ |
286 |
public Name addAll(Name suffix) |
287 |
throws InvalidNameException |
288 |
{ |
289 |
if (suffix instanceof SimpleCompositeName) { |
290 |
impl.addAll(suffix.getAll()); |
291 |
return this; |
292 |
} else { |
293 |
throw new InvalidNameException("Not a composite name: " + suffix.toString()); |
294 |
} |
295 |
} |
296 |
|
297 |
/** |
298 |
* Adds the components of a composite name -- in order -- at a specified |
299 |
* position within this composite name. |
300 |
* Components of this composite name at or after the index of the first |
301 |
* new component are shifted up (away from index 0) |
302 |
* to accommodate the new components. |
303 |
* |
304 |
* @param n The non-null components to add. |
305 |
* @param posn The index in this name at which to add the new |
306 |
* components. Must be in the range [0,size()]. |
307 |
* @return The updated CompositeName, not a new one. Cannot be null. |
308 |
* @exception InvalidNameException If n is not a composite name. |
309 |
* @exception ArrayIndexOutOfBoundsException |
310 |
* If posn is outside the specified range. |
311 |
*/ |
312 |
public Name addAll(int posn, Name n) |
313 |
throws InvalidNameException |
314 |
{ |
315 |
if (n instanceof SimpleCompositeName) { |
316 |
impl.addAll(posn, n.getAll()); |
317 |
return this; |
318 |
} else { |
319 |
throw new InvalidNameException("Not a composite name: " + |
320 |
n.toString()); |
321 |
} |
322 |
} |
323 |
|
324 |
/** |
325 |
* Adds a single component to the end of this composite name. |
326 |
* |
327 |
* @param comp The non-null component to add. |
328 |
* @return The updated CompositeName, not a new one. Cannot be null. |
329 |
* @exception InvalidNameException If adding comp at end of the name |
330 |
* would violate the name's syntax. |
331 |
*/ |
332 |
public Name add(String comp) throws InvalidNameException { |
333 |
impl.add(comp); |
334 |
return this; |
335 |
} |
336 |
|
337 |
/** |
338 |
* Adds a single component at a specified position within this |
339 |
* composite name. |
340 |
* Components of this composite name at or after the index of the new |
341 |
* component are shifted up by one (away from index 0) to accommodate |
342 |
* the new component. |
343 |
* |
344 |
* @param comp The non-null component to add. |
345 |
* @param posn The index at which to add the new component. |
346 |
* Must be in the range [0,size()]. |
347 |
* @return The updated CompositeName, not a new one. Cannot be null. |
348 |
* @exception ArrayIndexOutOfBoundsException |
349 |
* If posn is outside the specified range. |
350 |
* @exception InvalidNameException If adding comp at the specified position |
351 |
* would violate the name's syntax. |
352 |
*/ |
353 |
public Name add(int posn, String comp) |
354 |
throws InvalidNameException |
355 |
{ |
356 |
impl.add(posn, comp); |
357 |
return this; |
358 |
} |
359 |
|
360 |
/** |
361 |
* Deletes a component from this composite name. |
362 |
* The component of this composite name at position 'posn' is removed, |
363 |
* and components at indices greater than 'posn' |
364 |
* are shifted down (towards index 0) by one. |
365 |
* |
366 |
* @param posn The index of the component to delete. |
367 |
* Must be in the range [0,size()). |
368 |
* @return The component removed (a String). |
369 |
* @exception ArrayIndexOutOfBoundsException |
370 |
* If posn is outside the specified range (includes case where |
371 |
* composite name is empty). |
372 |
* @exception InvalidNameException If deleting the component |
373 |
* would violate the name's syntax. |
374 |
*/ |
375 |
public Object remove(int posn) throws InvalidNameException{ |
376 |
return impl.remove(posn); |
377 |
} |
378 |
|
379 |
/** |
380 |
* Overridden to avoid implementation dependency. |
381 |
* @serialData The number of components (an <tt>int</tt>) followed by |
382 |
* the individual components (each a <tt>String</tt>). |
383 |
*/ |
384 |
private void writeObject(java.io.ObjectOutputStream s) |
385 |
throws java.io.IOException { |
386 |
s.writeInt(size()); |
387 |
Enumeration<String> comps = getAll(); |
388 |
while (comps.hasMoreElements()) { |
389 |
s.writeObject(comps.nextElement()); |
390 |
} |
391 |
} |
392 |
|
393 |
/** |
394 |
* Overridden to avoid implementation dependency. |
395 |
*/ |
396 |
private void readObject(java.io.ObjectInputStream s) |
397 |
throws java.io.IOException, ClassNotFoundException { |
398 |
impl = new SimpleName(); // null means use default syntax |
399 |
int n = s.readInt(); // number of components |
400 |
try { |
401 |
while (--n >= 0) { |
402 |
add((String)s.readObject()); |
403 |
} |
404 |
} catch (InvalidNameException e) { |
405 |
throw (new java.io.StreamCorruptedException("Invalid name")); |
406 |
} |
407 |
} |
408 |
} |
409 |
native |