4d17b61ff944d3d247c136dc72160e8d9e38ce77
[IRC.git] / Robust / src / ClassLibrary / MGC / gnu / ArrayList.java
1 /* ArrayList.java -- JDK1.2's answer to Vector; this is an array-backed
2    implementation of the List interface
3    Copyright (C) 1998, 1999, 2000, 2001, 2004, 2005  Free Software Foundation, Inc.
4
5 This file is part of GNU Classpath.
6
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING.  If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
21
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library.  Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
26
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module.  An independent module is a module which is not derived from
34 or based on this library.  If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so.  If you do not wish to do so, delete this
37 exception statement from your version. */
38
39
40 //package java.util;
41
42 /*import java.io.IOException;
43 import java.io.ObjectInputStream;
44 import java.io.ObjectOutputStream;
45 import java.io.Serializable;
46 import java.lang.reflect.Array;
47 */
48 /**
49  * An array-backed implementation of the List interface.  This implements
50  * all optional list operations, and permits null elements, so that it is
51  * better than Vector, which it replaces. Random access is roughly constant
52  * time, and iteration is roughly linear time, so it is nice and fast, with
53  * less overhead than a LinkedList.
54  * <p>
55  *
56  * Each list has a capacity, and as the array reaches that capacity it
57  * is automatically transferred to a larger array. You also have access to
58  * ensureCapacity and trimToSize to control the backing array's size, avoiding
59  * reallocation or wasted memory.
60  * <p>
61  *
62  * ArrayList is not synchronized, so if you need multi-threaded access,
63  * consider using:<br>
64  * <code>List l = Collections.synchronizedList(new ArrayList(...));</code>
65  * <p>
66  *
67  * The iterators are <i>fail-fast</i>, meaning that any structural
68  * modification, except for <code>remove()</code> called on the iterator
69  * itself, cause the iterator to throw a
70  * {@link ConcurrentModificationException} rather than exhibit
71  * non-deterministic behavior.
72  *
73  * @author Jon A. Zeppieri
74  * @author Bryce McKinlay
75  * @author Eric Blake (ebb9@email.byu.edu)
76  * @see Collection
77  * @see List
78  * @see LinkedList
79  * @see Vector
80  * @see Collections#synchronizedList(List)
81  * @see AbstractList
82  * @status updated to 1.4
83  */
84 //public class ArrayList<E> extends AbstractList<E>
85 //  implements List<E>, RandomAccess, Cloneable, Serializable
86 public class ArrayList
87 {
88   /**
89    * Compatible with JDK 1.2
90    */
91   private static final long serialVersionUID = 8683452581122892189L;
92
93   /**
94    * The default capacity for new ArrayLists.
95    */
96   private static final int DEFAULT_CAPACITY = 10;
97
98   /**
99    * The number of elements in this list.
100    * @serial the list size
101    */
102   private int size;
103
104   /**
105    * Where the data is stored.
106    */
107   //private transient E[] data;
108   private transient Object[] data;
109
110   /**
111    * Construct a new ArrayList with the supplied initial capacity.
112    *
113    * @param capacity initial capacity of this ArrayList
114    * @throws IllegalArgumentException if capacity is negative
115    */
116   public ArrayList(int capacity)
117   {
118     // Must explicitly check, to get correct exception.
119     if (capacity < 0)
120       throw new Error("Illegal Argument Exception")/*IllegalArgumentException()*/;
121     data = (Object/*E*/[]) new Object[capacity];
122   }
123
124   /**
125    * Construct a new ArrayList with the default capacity (16).
126    */
127   public ArrayList()
128   {
129     this(DEFAULT_CAPACITY);
130   }
131
132   /**
133    * Construct a new ArrayList, and initialize it with the elements
134    * in the supplied Collection. The initial capacity is 110% of the
135    * Collection's size.
136    *
137    * @param c the collection whose elements will initialize this list
138    * @throws NullPointerException if c is null
139    */
140   /*public ArrayList(Collection<? extends E> c)
141   {
142     this((int) (c.size() * 1.1f));
143     addAll(c);
144   }*/
145
146   /**
147    * Trims the capacity of this List to be equal to its size;
148    * a memory saver.
149    */
150   public void trimToSize()
151   {
152     // Not a structural change from the perspective of iterators on this list,
153     // so don't update modCount.
154     if (size != data.length)
155       {
156         Object/*E*/[] newData = /*(ObjectE[])*/ new Object[size];
157         System.arraycopy(data, 0, newData, 0, size);
158         data = newData;
159       }
160   }
161
162   /**
163    * Guarantees that this list will have at least enough capacity to
164    * hold minCapacity elements. This implementation will grow the list to
165    * max(current * 2, minCapacity) if (minCapacity &gt; current). The JCL says
166    * explictly that "this method increases its capacity to minCap", while
167    * the JDK 1.3 online docs specify that the list will grow to at least the
168    * size specified.
169    *
170    * @param minCapacity the minimum guaranteed capacity
171    */
172   public void ensureCapacity(int minCapacity)
173   {
174     int current = data.length;
175
176     if (minCapacity > current)
177       {
178         Object/*E*/[] newData = /*(E[])*/ new Object[Math.max(current * 2, minCapacity)];
179         System.arraycopy(data, 0, newData, 0, size);
180         data = newData;
181       }
182   }
183
184   /**
185    * Returns the number of elements in this list.
186    *
187    * @return the list size
188    */
189   public int size()
190   {
191     return size;
192   }
193
194   /**
195    * Checks if the list is empty.
196    *
197    * @return true if there are no elements
198    */
199   public boolean isEmpty()
200   {
201     return size == 0;
202   }
203
204   /**
205    * Returns true iff element is in this ArrayList.
206    *
207    * @param e the element whose inclusion in the List is being tested
208    * @return true if the list contains e
209    */
210   public boolean contains(Object e)
211   {
212     return indexOf(e) != -1;
213   }
214
215   /**
216    * Returns the lowest index at which element appears in this List, or
217    * -1 if it does not appear.
218    *
219    * @param e the element whose inclusion in the List is being tested
220    * @return the index where e was found
221    */
222   public int indexOf(Object e)
223   {
224     for (int i = 0; i < size; i++)
225       if (equals(e, data[i]))
226         return i;
227     return -1;
228   }
229
230   /**
231    * Returns the highest index at which element appears in this List, or
232    * -1 if it does not appear.
233    *
234    * @param e the element whose inclusion in the List is being tested
235    * @return the index where e was found
236    */
237   public int lastIndexOf(Object e)
238   {
239     for (int i = size - 1; i >= 0; i--)
240       if (equals(e, data[i]))
241         return i;
242     return -1;
243   }
244
245   /**
246    * Creates a shallow copy of this ArrayList (elements are not cloned).
247    *
248    * @return the cloned object
249    */
250   public Object clone()
251   {
252     ArrayList/*<E>*/ clone = null;
253     //try
254       {
255         //clone = (ArrayList<E>) super.clone();
256         clone = new ArrayList();
257         clone.data = /*(E[])*/ data.clone();
258       }
259     /*catch (CloneNotSupportedException e)
260       {
261         // Impossible to get here.
262       }*/
263     return clone;
264   }
265
266   /**
267    * Returns an Object array containing all of the elements in this ArrayList.
268    * The array is independent of this list.
269    *
270    * @return an array representation of this list
271    */
272   public Object[] toArray()
273   {
274     Object/*E*/[] array = /*(E[])*/ new Object[size];
275     System.arraycopy(data, 0, array, 0, size);
276     return array;
277   }
278
279   /**
280    * Returns an Array whose component type is the runtime component type of
281    * the passed-in Array.  The returned Array is populated with all of the
282    * elements in this ArrayList.  If the passed-in Array is not large enough
283    * to store all of the elements in this List, a new Array will be created
284    * and returned; if the passed-in Array is <i>larger</i> than the size
285    * of this List, then size() index will be set to null.
286    *
287    * @param a the passed-in Array
288    * @return an array representation of this list
289    * @throws ArrayStoreException if the runtime type of a does not allow
290    *         an element in this list
291    * @throws NullPointerException if a is null
292    */
293   /*public <T> T[] toArray(T[] a)
294   {
295     if (a.length < size)
296       a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
297     else if (a.length > size)
298       a[size] = null;
299     System.arraycopy(data, 0, a, 0, size);
300     return a;
301   }*/
302
303   /**
304    * Retrieves the element at the user-supplied index.
305    *
306    * @param index the index of the element we are fetching
307    * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
308    */
309   public Object/*E*/ get(int index)
310   {
311     checkBoundExclusive(index);
312     return data[index];
313   }
314
315   /**
316    * Sets the element at the specified index.  The new element, e,
317    * can be an object of any type or null.
318    *
319    * @param index the index at which the element is being set
320    * @param e the element to be set
321    * @return the element previously at the specified index
322    * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= 0
323    */
324   public Object/*E*/ set(int index, Object/*E*/ e)
325   {
326     checkBoundExclusive(index);
327     Object/*E*/ result = data[index];
328     data[index] = e;
329     return result;
330   }
331
332   /**
333    * Appends the supplied element to the end of this list.
334    * The element, e, can be an object of any type or null.
335    *
336    * @param e the element to be appended to this list
337    * @return true, the add will always succeed
338    */
339   public boolean add(Object/*E*/ e)
340   {
341     modCount++;
342     if (size == data.length)
343       ensureCapacity(size + 1);
344     data[size++] = e;
345     return true;
346   }
347
348   /**
349    * Adds the supplied element at the specified index, shifting all
350    * elements currently at that index or higher one to the right.
351    * The element, e, can be an object of any type or null.
352    *
353    * @param index the index at which the element is being added
354    * @param e the item being added
355    * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
356    */
357   public void add(int index, Object/*E*/ e)
358   {
359     checkBoundInclusive(index);
360     modCount++;
361     if (size == data.length)
362       ensureCapacity(size + 1);
363     if (index != size)
364       System.arraycopy(data, index, data, index + 1, size - index);
365     data[index] = e;
366     size++;
367   }
368
369   /**
370    * Removes the element at the user-supplied index.
371    *
372    * @param index the index of the element to be removed
373    * @return the removed Object
374    * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
375    */
376   public Object/*E*/ remove(int index)
377   {
378     checkBoundExclusive(index);
379     Object/*E*/ r = data[index];
380     modCount++;
381     if (index != --size)
382       System.arraycopy(data, index + 1, data, index, size - index);
383     // Aid for garbage collection by releasing this pointer.
384     data[size] = null;
385     return r;
386   }
387
388   /**
389    * Removes all elements from this List
390    */
391   public void clear()
392   {
393     if (size > 0)
394       {
395         modCount++;
396         // Allow for garbage collection.
397         Arrays.fill(data, 0, size, null);
398         size = 0;
399       }
400   }
401
402   /**
403    * Add each element in the supplied Collection to this List. It is undefined
404    * what happens if you modify the list while this is taking place; for
405    * example, if the collection contains this list.  c can contain objects
406    * of any type, as well as null values.
407    *
408    * @param c a Collection containing elements to be added to this List
409    * @return true if the list was modified, in other words c is not empty
410    * @throws NullPointerException if c is null
411    */
412   /*public boolean addAll(Collection<? extends E> c)
413   {
414     return addAll(size, c);
415   }*/
416
417   /**
418    * Add all elements in the supplied collection, inserting them beginning
419    * at the specified index.  c can contain objects of any type, as well
420    * as null values.
421    *
422    * @param index the index at which the elements will be inserted
423    * @param c the Collection containing the elements to be inserted
424    * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; 0
425    * @throws NullPointerException if c is null
426    */
427   /*public boolean addAll(int index, Collection<? extends E> c)
428   {
429     checkBoundInclusive(index);
430     Iterator<? extends E> itr = c.iterator();
431     int csize = c.size();
432
433     modCount++;
434     if (csize + size > data.length)
435       ensureCapacity(size + csize);
436     int end = index + csize;
437     if (size > 0 && index != size)
438       System.arraycopy(data, index, data, end, size - index);
439     size += csize;
440     for ( ; index < end; index++)
441       data[index] = itr.next();
442     return csize > 0;
443   }*/
444
445   /**
446    * Removes all elements in the half-open interval [fromIndex, toIndex).
447    * Does nothing when toIndex is equal to fromIndex.
448    *
449    * @param fromIndex the first index which will be removed
450    * @param toIndex one greater than the last index which will be removed
451    * @throws IndexOutOfBoundsException if fromIndex &gt; toIndex
452    */
453   protected void removeRange(int fromIndex, int toIndex)
454   {
455     int change = toIndex - fromIndex;
456     if (change > 0)
457       {
458         modCount++;
459         System.arraycopy(data, toIndex, data, fromIndex, size - toIndex);
460         size -= change;
461       }
462     else if (change < 0)
463       throw new Error("Index Out Of Bounds Exception")/*IndexOutOfBoundsException()*/;
464   }
465
466   /**
467    * Checks that the index is in the range of possible elements (inclusive).
468    *
469    * @param index the index to check
470    * @throws IndexOutOfBoundsException if index &gt; size
471    */
472   private void checkBoundInclusive(int index)
473   {
474     // Implementation note: we do not check for negative ranges here, since
475     // use of a negative index will cause an ArrayIndexOutOfBoundsException,
476     // a subclass of the required exception, with no effort on our part.
477     if (index > size)
478       raiseBoundsError(index);
479   }
480
481   /**
482    * Checks that the index is in the range of existing elements (exclusive).
483    *
484    * @param index the index to check
485    * @throws IndexOutOfBoundsException if index &gt;= size
486    */
487   private void checkBoundExclusive(int index)
488   {
489     // Implementation note: we do not check for negative ranges here, since
490     // use of a negative index will cause an ArrayIndexOutOfBoundsException,
491     // a subclass of the required exception, with no effort on our part.
492     if (index >= size)
493       raiseBoundsError(index);
494   }
495
496   /**
497    * Raise the ArrayIndexOfOutBoundsException.
498    *
499    * @param index the index of the access
500    * @throws IndexOutOfBoundsException unconditionally
501    */
502   private void raiseBoundsError(int index)
503   {
504     // Implementaion note: put in a separate method to make the JITs job easier
505     // (separate common from uncommon code at method boundaries when trivial to
506     // do so).
507     throw new Error/*IndexOutOfBoundsException*/("IndexOutOfBoundsException Index: " + index + ", Size: " + size);
508   }
509   
510   
511   /**
512    * Remove from this list all elements contained in the given collection.
513    * This is not public, due to Sun's API, but this performs in linear
514    * time while the default behavior of AbstractList would be quadratic.
515    *
516    * @param c the collection to filter out
517    * @return true if this list changed
518    * @throws NullPointerException if c is null
519    */
520   /*boolean removeAllInternal(Collection<?> c)
521   {
522     int i;
523     int j;
524     for (i = 0; i < size; i++)
525       if (c.contains(data[i]))
526         break;
527     if (i == size)
528       return false;
529
530     modCount++;
531     for (j = i++; i < size; i++)
532       if (! c.contains(data[i]))
533         data[j++] = data[i];
534     size -= i - j;
535     return true;
536   }*/
537
538   /**
539    * Retain in this vector only the elements contained in the given collection.
540    * This is not public, due to Sun's API, but this performs in linear
541    * time while the default behavior of AbstractList would be quadratic.
542    *
543    * @param c the collection to filter by
544    * @return true if this vector changed
545    * @throws NullPointerException if c is null
546    * @since 1.2
547    */
548   /*boolean retainAllInternal(Collection<?> c)
549   {
550     int i;
551     int j;
552     for (i = 0; i < size; i++)
553       if (! c.contains(data[i]))
554         break;
555     if (i == size)
556       return false;
557
558     modCount++;
559     for (j = i++; i < size; i++)
560       if (c.contains(data[i]))
561         data[j++] = data[i];
562     size -= i - j;
563     return true;
564   }*/
565
566   /**
567    * Serializes this object to the given stream.
568    *
569    * @param s the stream to write to
570    * @throws IOException if the underlying stream fails
571    * @serialData the size field (int), the length of the backing array
572    *             (int), followed by its elements (Objects) in proper order.
573    */
574   /*private void writeObject(ObjectOutputStream s) throws IOException
575   {
576     // The 'size' field.
577     s.defaultWriteObject();
578     // We serialize unused list entries to preserve capacity.
579     int len = data.length;
580     s.writeInt(len);
581     // it would be more efficient to just write "size" items,
582     // this need readObject read "size" items too.
583     for (int i = 0; i < size; i++)
584       s.writeObject(data[i]);
585   }*/
586
587   /**
588    * Deserializes this object from the given stream.
589    *
590    * @param s the stream to read from
591    * @throws ClassNotFoundException if the underlying stream fails
592    * @throws IOException if the underlying stream fails
593    * @serialData the size field (int), the length of the backing array
594    *             (int), followed by its elements (Objects) in proper order.
595    */
596   /*private void readObject(ObjectInputStream s)
597     throws IOException, ClassNotFoundException
598   {
599     // the `size' field.
600     s.defaultReadObject();
601     int capacity = s.readInt();
602     data = (E[]) new Object[capacity];
603     for (int i = 0; i < size; i++)
604       data[i] = (E) s.readObject();
605   }*/
606 }