1 /* GnuName.java -- implementation of the javax.naming.Name
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu
.javax
.naming
.ictxImpl
.trans
;
40 import gnu
.java
.lang
.CPStringBuilder
;
42 import java
.util
.Enumeration
;
43 import java
.util
.NoSuchElementException
;
45 import javax
.naming
.InvalidNameException
;
46 import javax
.naming
.Name
;
49 * The implementation of the {@link Name}.
51 * @author Audrius Meskauskas
57 * The enumeration to traverse over name components.
60 implements Enumeration
63 * Get the new enumeration that enumerates from the given position forward
65 * @param position the position of the first name component to enumerate (0
66 * means first element)
68 GnuNameEnum(int position
)
70 nxt
= from
+ position
;
74 * The position of the next enumeration component to be returned or -1 if
75 * the end has been reached.
80 * Check if there are more elements in this enumeration.
82 public boolean hasMoreElements()
88 * Return the next element or throw a NoSuchElementException if there is no
91 public Object
nextElement()
94 throw new NoSuchElementException();
95 Object r
= content
[nxt
++];
97 if (nxt
- from
== length
)
104 private static final long serialVersionUID
= - 3617482732056931635L;
112 * The content buffer of the name. This buffer may be shared, so the array
113 * member content should never be modified.
118 * The place, inclusive, where the name content starts in the content buffer.
123 * The length of the name.
128 * Creates the unitialised name.
136 * Creates the name, containing from the given chain of the atomic components.
138 * @param name the array, containing the name components.
140 public GnuName(String
[] name
)
142 this(name
, 0, name
.length
);
146 * Creates the name that uses the given portion of the array for its
149 public GnuName(String
[] buffer
, int useFrom
, int useLength
)
157 * Inserts the given <code>String</code> component to this <code>Name</code>
158 * at the given index. The method modifies the current <code>Name</code> and
161 * @exception ArrayIndexOutOfBoundsException if the given index is smaller
162 * then zero or greater then or equal to <code>size()</code>.
163 * @exception InvalidNameException if the given <code>String</code> is not a
164 * valid component for this <code>Name</code>.
166 public Name
add(int posn
, String comp
) throws InvalidNameException
168 String
[] nc
= new String
[content
.length
+ 1];
169 System
.arraycopy(content
, from
, nc
, 0, posn
);
171 System
.arraycopy(content
, from
+ posn
, nc
, posn
+ 1, length
- posn
);
175 length
= content
.length
;
181 * Adds the given <code>String</code> component to the end of this
182 * <code>Name</code>. The method modifies the current <code>Name</code>
183 * and then returns it.
185 * @exception InvalidNameException if the given <code>String</code> is not a
186 * valid component for this <code>Name</code>.
188 public Name
add(String comp
) throws InvalidNameException
190 String
[] nc
= new String
[content
.length
+ 1];
191 System
.arraycopy(content
, from
, nc
, 0, length
);
192 nc
[nc
.length
- 1] = comp
;
196 length
= content
.length
;
202 * Inserts all the components of the given <code>Name</code> to this
203 * <code>Name</code> at the given index. Components after this index (if
204 * any) are shifted up. The method modifies the current <code>Name</code>
205 * and then returns it.
207 * @exception ArrayIndexOutOfBoundsException if the given index is smaller
208 * then zero or greater then or equal to <code>size()</code>.
209 * @exception InvalidNameException if any of the given components is not a
210 * valid component for this <code>Name</code>.
212 public Name
addAll(int posn
, Name n
) throws InvalidNameException
214 String
[] nc
= new String
[length
+ n
.size()];
215 System
.arraycopy(content
, from
, nc
, 0, posn
);
218 for (int p
= 0; p
< n
.size(); i
++, p
++)
221 System
.arraycopy(content
, from
+ posn
, nc
, i
, length
- posn
);
223 length
= length
+ n
.size();
230 * Adds all the components of the given <code>Name</code> to the end of this
231 * <code>Name</code>. The method modifies the current <code>Name</code>
232 * and then returns it.
234 * @exception InvalidNameException if any of the given components is not a
235 * valid component for this <code>Name</code>.
237 public Name
addAll(Name suffix
) throws InvalidNameException
239 String
[] nc
= new String
[length
+ suffix
.size()];
240 System
.arraycopy(content
, from
, nc
, 0, length
);
242 for (int i
= length
, p
= 0; i
< nc
.length
; i
++, p
++)
243 nc
[i
] = suffix
.get(p
);
245 length
= length
+ suffix
.size();
252 * Compares the given object to this <code>Name</code>. Returns a negative
253 * value if the given <code>Object</code> is smaller then this
254 * <code>Name</code>, a positive value if the <code>Object</code> is
255 * bigger, and zero if the are equal. If the <code>Object</code> is not of a
256 * class that can be compared to the class of this <code>Name</code> then a
257 * <code>ClassCastException</code> is thrown. Note that it is not guaranteed
258 * that <code>Name</code>s implemented in different classes can be
259 * compared. The definition of smaller, bigger and equal is up to the actual
260 * implementing class.
262 public int compareTo(Object obj
)
266 int l
= Math
.min(length
, n
.size());
269 for (int i
= 0; i
< l
; i
++)
271 c
= content
[from
+ i
].compareTo(n
.get(i
));
275 return length
- n
.size();
279 * Returns <code>true</code> if this <code>Name</code> ends with the
280 * components of the given <code>Name</code>, <code>false</code>
283 public boolean endsWith(Name n
)
285 if (n
.size() > length
)
288 int ofs
= length
- n
.size() + from
;
290 for (int i
= 0; i
< n
.size(); i
++, ofs
++)
291 if (! content
[ofs
].equals(n
.get(i
)))
298 * Gets the component at the given index.
300 * @exception ArrayIndexOutOfBoundsException if the given index is smaller
301 * then zero or greater then or equal to <code>size()</code>.
303 public String
get(int posn
)
305 return content
[from
+ posn
];
309 * Returns a non-null (but possibly empty) <code>Enumeration</code> of the
310 * components of the <code>Name</code> as <code>String</code>s.
312 public Enumeration
getAll()
314 return new GnuNameEnum(0);
318 * Returns the components till the given index as a <code>Name</code>. The
319 * returned <code>Name</code> can be modified without changing the original.
321 * @param posn the ending position, exclusive
322 * @exception ArrayIndexOutOfBoundsException if the given index is smaller
323 * then zero or greater then or equal to <code>size()</code>.
325 public Name
getPrefix(int posn
)
327 return new GnuName(content
, from
, posn
);
331 * Returns the components from the given index till the end as a
332 * <code>Name</code>. The returned <code>Name</code> can be modified
333 * without changing the original.
335 * @param posn the starting position, inclusive. If it is equal to the size of
336 * the name, the empty name is returned.
337 * @exception ArrayIndexOutOfBoundsException if the given index is smaller
338 * then zero or greater then or equal to <code>size()</code>.
340 public Name
getSuffix(int posn
)
342 return new GnuName(content
, from
+ posn
, length
- posn
);
346 * Returns <code>true</code> if the number of components of this
347 * <code>Name</code> is zero, <code>false</code> otherwise.
349 public boolean isEmpty()
355 * Removes the component at the given index from this <code>Name</code>.
356 * The method modifies the current <code>Name</code> and then returns it.
358 * @exception InvalidNameException if the name size reduces below zero.
360 public Object
remove(int posn
) throws InvalidNameException
363 throw new InvalidNameException("negative size");
369 else if (posn
< length
)
371 String
[] nc
= new String
[length
];
372 System
.arraycopy(content
, from
, nc
, 0, posn
);
373 System
.arraycopy(content
, from
+ posn
+ 1, nc
, posn
, length
- posn
);
383 * Returns the number of components of this <code>Name</code>. The returned
384 * number can be zero.
392 * Returns <code>true</code> if this <code>Name</code> starts with the
393 * components of the given <code>Name</code>, <code>false</code>
396 public boolean startsWith(Name n
)
398 if (n
.size() > length
)
401 for (int i
= 0; i
< n
.size(); i
++)
402 if (! content
[from
+ i
].equals(n
.get(i
)))
409 * Returns a clone of this <code>Name</code>. It will be a deep copy of all
410 * the components of the <code>Name</code> so that changes to components of
411 * the components does not change the component in this <code>Name</code>.
413 public Object
clone()
415 return new GnuName(content
, from
, length
);
419 * The name is equal to other name if they contents are equal.
421 public boolean equals(Object arg0
)
425 else if (arg0
instanceof Name
)
427 Name n
= (Name
) arg0
;
428 if (length
!= n
.size())
431 for (int i
= 0; i
< length
; i
++)
432 if (! content
[from
+ i
].equals(n
.get(i
)))
441 * Overridden to make consistent with equals.
443 public int hashCode()
445 if (hash
== 0 && length
> 0)
448 for (int i
= from
; i
< from
+ length
; i
++)
449 s ^
= content
[i
].hashCode();
456 * Get the string representation, separating the name components by slashes
458 public String
toString()
460 CPStringBuilder b
= new CPStringBuilder();
461 for (int i
= 0; i
< length
; i
++)