KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > archie > Name


1 package org.sapia.archie;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6
7
8 /**
9  * This interface provides an object reprentation of a "name". A <code>Name</code> is composed
10  * of potentially multiple <code>NamePart</code>s.
11  * <p>
12  * A <code>Name</code> instance is typically obtained from a string representation, using a
13  * <code>NameParser</code>.
14  *
15  * @see org.sapia.archie.NamePart
16  * @see org.sapia.archie.NameParser
17  *
18  * @author Yanick Duchesne
19  * <dl>
20  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
21  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
22  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
23  * </dl>
24  */

25 public class Name implements Serializable JavaDoc{
26   private List JavaDoc _parts = new ArrayList JavaDoc();
27   private int _currentIndex;
28
29   protected Name(List JavaDoc parts) {
30     _parts.addAll(parts);
31   }
32   
33   public Name() {
34   }
35
36   /**
37    * Returns the number of <code>NamePart</code>s in this name.
38    *
39    * @return a count.
40    */

41   public int count() {
42     return _parts.size();
43   }
44
45   /**
46    * Returns the internal iteration index.
47    *
48    * @return an index.
49    */

50   public int getCurrentIndex() {
51     return _currentIndex;
52   }
53
54   /**
55    * Sets the internal iteration index.
56    *
57    * @param i an index.
58    */

59   public void setCurrentIndex(int i) {
60     if ((i < 0) || (i > (_parts.size() - 1))) {
61       throw new IllegalArgumentException JavaDoc("Specified index exceeds bounds");
62     }
63
64     _currentIndex = i;
65   }
66
67   /**
68    * Resets the internal iteration index.
69    */

70   public void reset() {
71     _currentIndex = 0;
72   }
73
74   /**
75    * Returns <code>true</code> if this instance has another
76    * part to iterate on.
77    *
78    * @see #reset()
79    * @see #getCurrentIndex()
80    * @see #nextPart()
81    */

82   public boolean hasNextPart() {
83     boolean hasNext = _currentIndex < _parts.size();
84
85     if (!hasNext) {
86       reset();
87     }
88
89     return hasNext;
90   }
91
92   /**
93    * Iterates on the next <code>NamePart</code>.
94    *
95    * @see #hasNextPart()
96    */

97   public NamePart nextPart() {
98     return (NamePart) _parts.get(_currentIndex++);
99   }
100
101   /**
102    * Returns the <code>NamePart</code> at the given index.
103    *
104    * @return a <code>NamePart</code>.
105    */

106   public NamePart get(int i) {
107     return (NamePart) _parts.get(i);
108   }
109
110   /**
111    * Adds a <code>NamePart</code> to this instance.
112    *
113    * @param part a <code>NamePart</code>
114    * @return this instance (to allow chained invocations).
115    */

116   public Name add(NamePart part) {
117     _parts.add(part);
118
119     return this;
120   }
121   
122   public Name addAt(int pos, NamePart part){
123     assertPos(pos);
124     _parts.add(pos, part);
125     return this;
126   }
127   
128   public NamePart removeAt(int pos){
129     return (NamePart)_parts.remove(pos);
130   }
131   
132   public boolean endsWith(Name other){
133     if(count() < other.count()) return false;
134     for(int i = other.count() - 1; i > 0; i--){
135       if(!((NamePart)other.get(i)).asString().equals(get(i).asString())){
136         return false;
137       }
138     }
139     return true;
140   }
141   
142   public boolean startsWith(Name other){
143     if(count() < other.count()) return false;
144     for(int i = 0; i < other.count(); i++){
145       if(!((NamePart)other.get(i)).asString().equals(get(i).asString())){
146         return false;
147       }
148     }
149     return true;
150   }
151
152   /**
153    * @param n adds the given <code>Name</code> to this instance (actually
154    * appends the <code>NamePart</code>s of the given name to this instance).
155    *
156    * @return this instance.
157    */

158   public Name add(Name n) {
159     _parts.addAll(n._parts);
160
161     return this;
162   }
163
164   /**
165    * Returns all the <code>NamePart</code>s that this name holds, up to the
166    * given index (exclusively). Returns the <code>NamePart</code>s in a <code>Name</code>
167    * instance.
168    *
169    * @param to an upperbound index.
170    * @return a <code>Name</code>.
171    */

172   public Name getTo(int to) {
173     Name n = new Name();
174     NamePart current;
175
176     for (int i = 0; i < to; i++) {
177       current = (NamePart) _parts.get(i);
178       n._parts.add(current);
179     }
180
181     return n;
182   }
183   
184   /**
185    * Returns all the <code>NamePart</code>s that this name holds, starting from the
186    * given index (inclusively). Returns the <code>NamePart</code>s in a <code>Name</code>
187    * instance.
188    *
189    * @param from a lowerbound index.
190    * @return a <code>Name</code>.
191    */

192
193   public Name getFrom(int from) {
194     Name n = new Name();
195     NamePart current;
196
197     for (int i = from; i < _parts.size(); i++) {
198       current = (NamePart) _parts.get(i);
199       n._parts.add(current);
200     }
201
202     return n;
203   }
204
205   /**
206    * Returns the first part in this name.
207    *
208    * @return a <code>NamePart</code>.
209    */

210   public NamePart first() {
211     return (NamePart) _parts.get(0);
212   }
213
214   /**
215    * Returns the last part in this name.
216    *
217    * @return a <code>NamePart</code>.
218    */

219   public NamePart last() {
220     return (NamePart) _parts.get(_parts.size() - 1);
221   }
222
223   /**
224    * Chops the last part from this instance and returns it.
225    *
226    * @return a <code>NamePart</code>.
227    */

228   public NamePart chopLast() {
229     return (NamePart) _parts.remove(_parts.size() - 1);
230   }
231
232   /**
233    * Chops the first part from this instance and returns it.
234    *
235    * @return a <code>NamePart</code>.
236    */

237   public NamePart chopFirst() {
238     return (NamePart) _parts.remove(0);
239   }
240
241   public Object JavaDoc clone() {
242     return new Name(_parts);
243   }
244
245   /**
246    * @return the string representation of this instance.
247    */

248   public String JavaDoc toString() {
249     return _parts.toString();
250   }
251   
252   public boolean equals(Object JavaDoc o){
253     try{
254       Name other = (Name)o;
255       if(other.count() != count()){
256         return false;
257       }
258       for(int i = 0; i < other.count(); i++){
259         if(!other.get(i).asString().equals(get(i).asString())){
260           return false;
261         }
262       }
263       return true;
264     }catch(ClassCastException JavaDoc e){
265       return false;
266     }
267   }
268   
269   
270   
271   private void assertPos(int pos){
272     if(pos < 0){
273       throw new ArrayIndexOutOfBoundsException JavaDoc("Index too small: " + pos);
274     }
275     if(pos > _parts.size()){
276       throw new ArrayIndexOutOfBoundsException JavaDoc("Index too large: " + pos);
277     }
278   }
279 }
280
Popular Tags