KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > helper > NonSynchronizedVector


1 package oracle.toplink.essentials.internal.helper;
2
3 import java.util.*;
4
5 /**
6  * Vector subclass that removes the synchronization.
7  */

8 public class NonSynchronizedVector extends Vector {
9     public static NonSynchronizedVector newInstance(int initialCapacity, int capacityIncrement) {
10         return new NonSynchronizedVector(initialCapacity, capacityIncrement);
11     }
12
13     public static NonSynchronizedVector newInstance(int initialCapacity) {
14         return new NonSynchronizedVector(initialCapacity);
15     }
16
17     public static NonSynchronizedVector newInstance() {
18         return new NonSynchronizedVector();
19     }
20
21     public static NonSynchronizedVector newInstance(Collection c) {
22         return new NonSynchronizedVector(c);
23     }
24     
25     public NonSynchronizedVector(int initialCapacity, int capacityIncrement) {
26         super(initialCapacity, capacityIncrement);
27     }
28
29     public NonSynchronizedVector(int initialCapacity) {
30         super(initialCapacity);
31     }
32
33     public NonSynchronizedVector() {
34         super();
35     }
36
37     public NonSynchronizedVector(Collection c) {
38         super(c);
39     }
40
41     public void copyInto(Object JavaDoc anArray[]) {
42         System.arraycopy(elementData, 0, anArray, 0, elementCount);
43     }
44
45     public void trimToSize() {
46         modCount++;
47         int oldCapacity = elementData.length;
48         if (elementCount < oldCapacity) {
49             Object JavaDoc oldData[] = elementData;
50             elementData = new Object JavaDoc[elementCount];
51             System.arraycopy(oldData, 0, elementData, 0, elementCount);
52         }
53     }
54
55     public void ensureCapacity(int minCapacity) {
56         modCount++;
57         ensureCapacityHelper(minCapacity);
58     }
59
60     private void ensureCapacityHelper(int minCapacity) {
61         int oldCapacity = elementData.length;
62         if (minCapacity > oldCapacity) {
63             Object JavaDoc oldData[] = elementData;
64             int newCapacity = (capacityIncrement > 0) ?
65             (oldCapacity + capacityIncrement) : (oldCapacity * 2);
66             if (newCapacity < minCapacity) {
67                 newCapacity = minCapacity;
68             }
69             elementData = new Object JavaDoc[newCapacity];
70             System.arraycopy(oldData, 0, elementData, 0, elementCount);
71         }
72     }
73
74     public void setSize(int newSize) {
75         modCount++;
76         if (newSize > elementCount) {
77             ensureCapacityHelper(newSize);
78         } else {
79             for (int i = newSize ; i < elementCount ; i++) {
80             elementData[i] = null;
81             }
82         }
83         elementCount = newSize;
84     }
85
86     public int capacity() {
87         return elementData.length;
88     }
89
90     public int size() {
91         return elementCount;
92     }
93
94     public boolean isEmpty() {
95         return elementCount == 0;
96     }
97
98     public Enumeration elements() {
99         return new Enumeration() {
100             int count = 0;
101     
102             public boolean hasMoreElements() {
103                 return count < elementCount;
104             }
105     
106             public Object JavaDoc nextElement() {
107                 if (count < elementCount) {
108                     return elementData[count++];
109                 }
110                 throw new NoSuchElementException("Vector Enumeration");
111             }
112         };
113     }
114
115     public int indexOf(Object JavaDoc elem, int index) {
116         if (elem == null) {
117             for (int i = index ; i < elementCount ; i++)
118             if (elementData[i]==null)
119                 return i;
120         } else {
121             for (int i = index ; i < elementCount ; i++)
122             if (elem.equals(elementData[i]))
123                 return i;
124         }
125         return -1;
126     }
127
128     public int lastIndexOf(Object JavaDoc elem) {
129         return lastIndexOf(elem, elementCount-1);
130     }
131
132     public int lastIndexOf(Object JavaDoc elem, int index) {
133         if (index >= elementCount)
134             throw new IndexOutOfBoundsException JavaDoc(index + " >= "+ elementCount);
135         if (elem == null) {
136             for (int i = index; i >= 0; i--)
137             if (elementData[i]==null)
138                 return i;
139         } else {
140             for (int i = index; i >= 0; i--)
141             if (elem.equals(elementData[i]))
142                 return i;
143         }
144         return -1;
145     }
146
147     public Object JavaDoc elementAt(int index) {
148         if (index >= elementCount) {
149             throw new ArrayIndexOutOfBoundsException JavaDoc(index + " >= " + elementCount);
150         }
151         return elementData[index];
152     }
153
154     public Object JavaDoc firstElement() {
155         if (elementCount == 0) {
156             throw new NoSuchElementException();
157         }
158         return elementData[0];
159     }
160
161     public Object JavaDoc lastElement() {
162         if (elementCount == 0) {
163             throw new NoSuchElementException();
164         }
165         return elementData[elementCount - 1];
166     }
167
168     public void setElementAt(Object JavaDoc obj, int index) {
169         if (index >= elementCount) {
170             throw new ArrayIndexOutOfBoundsException JavaDoc(index + " >= " +
171                                  elementCount);
172         }
173         elementData[index] = obj;
174     }
175
176     public void removeElementAt(int index) {
177         modCount++;
178         if (index >= elementCount) {
179             throw new ArrayIndexOutOfBoundsException JavaDoc(index + " >= " +
180                                  elementCount);
181         } else if (index < 0) {
182             throw new ArrayIndexOutOfBoundsException JavaDoc(index);
183         }
184         int j = elementCount - index - 1;
185         if (j > 0) {
186             System.arraycopy(elementData, index + 1, elementData, index, j);
187         }
188         elementCount--;
189         elementData[elementCount] = null; /* to let gc do its work */
190     }
191
192     public void insertElementAt(Object JavaDoc obj, int index) {
193         modCount++;
194         if (index > elementCount) {
195             throw new ArrayIndexOutOfBoundsException JavaDoc(index
196                                  + " > " + elementCount);
197         }
198         ensureCapacityHelper(elementCount + 1);
199         System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
200         elementData[index] = obj;
201         elementCount++;
202     }
203
204     public void addElement(Object JavaDoc obj) {
205         modCount++;
206         ensureCapacityHelper(elementCount + 1);
207         elementData[elementCount++] = obj;
208     }
209
210     public boolean removeElement(Object JavaDoc obj) {
211         modCount++;
212         int i = indexOf(obj);
213         if (i >= 0) {
214             removeElementAt(i);
215             return true;
216         }
217         return false;
218     }
219
220     public void removeAllElements() {
221         modCount++;
222         // Let gc do its work
223
for (int i = 0; i < elementCount; i++)
224             elementData[i] = null;
225     
226         elementCount = 0;
227     }
228
229     public Object JavaDoc[] toArray() {
230         Object JavaDoc[] result = new Object JavaDoc[elementCount];
231         System.arraycopy(elementData, 0, result, 0, elementCount);
232         return result;
233     }
234
235     public Object JavaDoc[] toArray(Object JavaDoc a[]) {
236         if (a.length < elementCount)
237             a = (Object JavaDoc[])java.lang.reflect.Array.newInstance(
238                                 a.getClass().getComponentType(), elementCount);
239
240         System.arraycopy(elementData, 0, a, 0, elementCount);
241
242         if (a.length > elementCount)
243             a[elementCount] = null;
244
245         return a;
246     }
247
248     public Object JavaDoc get(int index) {
249         if (index >= elementCount)
250             throw new ArrayIndexOutOfBoundsException JavaDoc(index);
251
252         return elementData[index];
253     }
254
255     public Object JavaDoc set(int index, Object JavaDoc element) {
256         if (index >= elementCount)
257             throw new ArrayIndexOutOfBoundsException JavaDoc(index);
258     
259         Object JavaDoc oldValue = elementData[index];
260         elementData[index] = element;
261         return oldValue;
262     }
263
264     public boolean add(Object JavaDoc o) {
265         modCount++;
266         ensureCapacityHelper(elementCount + 1);
267         elementData[elementCount++] = o;
268         return true;
269     }
270
271     public Object JavaDoc remove(int index) {
272         modCount++;
273         if (index >= elementCount)
274             throw new ArrayIndexOutOfBoundsException JavaDoc(index);
275         Object JavaDoc oldValue = elementData[index];
276     
277         int numMoved = elementCount - index - 1;
278         if (numMoved > 0)
279             System.arraycopy(elementData, index+1, elementData, index,
280                      numMoved);
281         elementData[--elementCount] = null; // Let gc do its work
282

283         return oldValue;
284     }
285
286     public boolean containsAll(Collection c) {
287         Iterator e = c.iterator();
288         while (e.hasNext())
289             if(!contains(e.next()))
290                 return false;
291     
292         return true;
293     }
294
295     public boolean addAll(Collection c) {
296         modCount++;
297             Object JavaDoc[] a = c.toArray();
298             int numNew = a.length;
299         ensureCapacityHelper(elementCount + numNew);
300             System.arraycopy(a, 0, elementData, elementCount, numNew);
301             elementCount += numNew;
302         return numNew != 0;
303     }
304
305     public boolean removeAll(Collection c) {
306         boolean modified = false;
307         Iterator e = iterator();
308         while (e.hasNext()) {
309             if(c.contains(e.next())) {
310             e.remove();
311             modified = true;
312             }
313         }
314         return modified;
315     }
316
317     public boolean retainAll(Collection c) {
318         boolean modified = false;
319         Iterator e = iterator();
320         while (e.hasNext()) {
321             if(!c.contains(e.next())) {
322             e.remove();
323             modified = true;
324             }
325         }
326         return modified;
327     }
328
329     public boolean addAll(int index, Collection c) {
330         modCount++;
331         if (index < 0 || index > elementCount)
332             throw new ArrayIndexOutOfBoundsException JavaDoc(index);
333     
334             Object JavaDoc[] a = c.toArray();
335         int numNew = a.length;
336         ensureCapacityHelper(elementCount + numNew);
337     
338         int numMoved = elementCount - index;
339         if (numMoved > 0)
340             System.arraycopy(elementData, index, elementData, index + numNew,
341                      numMoved);
342     
343             System.arraycopy(a, 0, elementData, index, numNew);
344         elementCount += numNew;
345         return numNew != 0;
346     }
347
348     public boolean equals(Object JavaDoc o) {
349         if (o == this)
350             return true;
351         if (!(o instanceof List))
352             return false;
353     
354         ListIterator e1 = listIterator();
355         ListIterator e2 = ((List) o).listIterator();
356         while(e1.hasNext() && e2.hasNext()) {
357             Object JavaDoc o1 = e1.next();
358             Object JavaDoc o2 = e2.next();
359             if (!(o1==null ? o2==null : o1.equals(o2)))
360             return false;
361         }
362         return !(e1.hasNext() || e2.hasNext());
363     }
364
365     public int hashCode() {
366         int hashCode = 1;
367         Iterator i = iterator();
368             while (i.hasNext()) {
369             Object JavaDoc obj = i.next();
370             hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
371         }
372         return hashCode;
373     }
374
375     public String JavaDoc toString() {
376         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
377         Iterator e = iterator();
378         buf.append("[");
379         int maxIndex = size() - 1;
380         for (int i = 0; i <= maxIndex; i++) {
381             buf.append(String.valueOf(e.next()));
382             if (i < maxIndex)
383             buf.append(", ");
384         }
385         buf.append("]");
386         return buf.toString();
387     }
388
389     protected void removeRange(int fromIndex, int toIndex) {
390         modCount++;
391         int numMoved = elementCount - toIndex;
392             System.arraycopy(elementData, toIndex, elementData, fromIndex,
393                              numMoved);
394     
395         // Let gc do its work
396
int newElementCount = elementCount - (toIndex-fromIndex);
397         while (elementCount != newElementCount)
398             elementData[--elementCount] = null;
399     }
400
401     private void writeObject(java.io.ObjectOutputStream JavaDoc s)
402         throws java.io.IOException JavaDoc
403     {
404         s.defaultWriteObject();
405     }
406 }
407
408
Popular Tags