ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Vector.java
(Generate patch)

Comparing jsr166/src/main/java/util/Vector.java (file contents):
Revision 1.22 by jsr166, Tue Sep 11 15:38:19 2007 UTC vs.
Revision 1.23 by jsr166, Sun May 18 23:47:56 2008 UTC

# Line 125 | Line 125 | public class Vector<E>
125       *         is negative
126       */
127      public Vector(int initialCapacity, int capacityIncrement) {
128 <        super();
128 >        super();
129          if (initialCapacity < 0)
130              throw new IllegalArgumentException("Illegal Capacity: "+
131                                                 initialCapacity);
132 <        this.elementData = new Object[initialCapacity];
133 <        this.capacityIncrement = capacityIncrement;
132 >        this.elementData = new Object[initialCapacity];
133 >        this.capacityIncrement = capacityIncrement;
134      }
135  
136      /**
# Line 142 | Line 142 | public class Vector<E>
142       *         is negative
143       */
144      public Vector(int initialCapacity) {
145 <        this(initialCapacity, 0);
145 >        this(initialCapacity, 0);
146      }
147  
148      /**
# Line 151 | Line 151 | public class Vector<E>
151       * zero.
152       */
153      public Vector() {
154 <        this(10);
154 >        this(10);
155      }
156  
157      /**
# Line 165 | Line 165 | public class Vector<E>
165       * @since   1.2
166       */
167      public Vector(Collection<? extends E> c) {
168 <        elementData = c.toArray();
169 <        elementCount = elementData.length;
170 <        // c.toArray might (incorrectly) not return Object[] (see 6260652)
171 <        if (elementData.getClass() != Object[].class)
172 <            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
168 >        elementData = c.toArray();
169 >        elementCount = elementData.length;
170 >        // c.toArray might (incorrectly) not return Object[] (see 6260652)
171 >        if (elementData.getClass() != Object[].class)
172 >            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
173      }
174  
175      /**
# Line 186 | Line 186 | public class Vector<E>
186       * @see #toArray(Object[])
187       */
188      public synchronized void copyInto(Object[] anArray) {
189 <        System.arraycopy(elementData, 0, anArray, 0, elementCount);
189 >        System.arraycopy(elementData, 0, anArray, 0, elementCount);
190      }
191  
192      /**
# Line 198 | Line 198 | public class Vector<E>
198       * minimize the storage of a vector.
199       */
200      public synchronized void trimToSize() {
201 <        modCount++;
202 <        int oldCapacity = elementData.length;
203 <        if (elementCount < oldCapacity) {
201 >        modCount++;
202 >        int oldCapacity = elementData.length;
203 >        if (elementCount < oldCapacity) {
204              elementData = Arrays.copyOf(elementData, elementCount);
205 <        }
205 >        }
206      }
207  
208      /**
# Line 223 | Line 223 | public class Vector<E>
223       * @param minCapacity the desired minimum capacity
224       */
225      public synchronized void ensureCapacity(int minCapacity) {
226 <        modCount++;
227 <        ensureCapacityHelper(minCapacity);
226 >        modCount++;
227 >        ensureCapacityHelper(minCapacity);
228      }
229  
230      /**
# Line 236 | Line 236 | public class Vector<E>
236       * @see #ensureCapacity(int)
237       */
238      private void ensureCapacityHelper(int minCapacity) {
239 <        int oldCapacity = elementData.length;
240 <        if (minCapacity > oldCapacity) {
241 <            Object[] oldData = elementData;
242 <            int newCapacity = (capacityIncrement > 0) ?
243 <                (oldCapacity + capacityIncrement) : (oldCapacity * 2);
244 <            if (newCapacity < minCapacity) {
245 <                newCapacity = minCapacity;
246 <            }
239 >        int oldCapacity = elementData.length;
240 >        if (minCapacity > oldCapacity) {
241 >            Object[] oldData = elementData;
242 >            int newCapacity = (capacityIncrement > 0) ?
243 >                (oldCapacity + capacityIncrement) : (oldCapacity * 2);
244 >            if (newCapacity < minCapacity) {
245 >                newCapacity = minCapacity;
246 >            }
247              elementData = Arrays.copyOf(elementData, newCapacity);
248 <        }
248 >        }
249      }
250  
251      /**
# Line 258 | Line 258 | public class Vector<E>
258       * @throws ArrayIndexOutOfBoundsException if the new size is negative
259       */
260      public synchronized void setSize(int newSize) {
261 <        modCount++;
262 <        if (newSize > elementCount) {
263 <            ensureCapacityHelper(newSize);
264 <        } else {
265 <            for (int i = newSize ; i < elementCount ; i++) {
266 <                elementData[i] = null;
267 <            }
268 <        }
269 <        elementCount = newSize;
261 >        modCount++;
262 >        if (newSize > elementCount) {
263 >            ensureCapacityHelper(newSize);
264 >        } else {
265 >            for (int i = newSize ; i < elementCount ; i++) {
266 >                elementData[i] = null;
267 >            }
268 >        }
269 >        elementCount = newSize;
270      }
271  
272      /**
# Line 277 | Line 277 | public class Vector<E>
277       *          of this vector)
278       */
279      public synchronized int capacity() {
280 <        return elementData.length;
280 >        return elementData.length;
281      }
282  
283      /**
# Line 286 | Line 286 | public class Vector<E>
286       * @return  the number of components in this vector
287       */
288      public synchronized int size() {
289 <        return elementCount;
289 >        return elementCount;
290      }
291  
292      /**
# Line 297 | Line 297 | public class Vector<E>
297       *          {@code false} otherwise.
298       */
299      public synchronized boolean isEmpty() {
300 <        return elementCount == 0;
300 >        return elementCount == 0;
301      }
302  
303      /**
# Line 310 | Line 310 | public class Vector<E>
310       * @see     Iterator
311       */
312      public Enumeration<E> elements() {
313 <        return new Enumeration<E>() {
314 <            int count = 0;
313 >        return new Enumeration<E>() {
314 >            int count = 0;
315  
316 <            public boolean hasMoreElements() {
317 <                return count < elementCount;
318 <            }
319 <
320 <            public E nextElement() {
321 <                synchronized (Vector.this) {
322 <                    if (count < elementCount) {
323 <                        return elementData(count++);
324 <                    }
325 <                }
326 <                throw new NoSuchElementException("Vector Enumeration");
327 <            }
328 <        };
316 >            public boolean hasMoreElements() {
317 >                return count < elementCount;
318 >            }
319 >
320 >            public E nextElement() {
321 >                synchronized (Vector.this) {
322 >                    if (count < elementCount) {
323 >                        return elementData(count++);
324 >                    }
325 >                }
326 >                throw new NoSuchElementException("Vector Enumeration");
327 >            }
328 >        };
329      }
330  
331      /**
# Line 338 | Line 338 | public class Vector<E>
338       * @return {@code true} if this vector contains the specified element
339       */
340      public boolean contains(Object o) {
341 <        return indexOf(o, 0) >= 0;
341 >        return indexOf(o, 0) >= 0;
342      }
343  
344      /**
# Line 353 | Line 353 | public class Vector<E>
353       *         this vector, or -1 if this vector does not contain the element
354       */
355      public int indexOf(Object o) {
356 <        return indexOf(o, 0);
356 >        return indexOf(o, 0);
357      }
358  
359      /**
# Line 373 | Line 373 | public class Vector<E>
373       * @see     Object#equals(Object)
374       */
375      public synchronized int indexOf(Object o, int index) {
376 <        if (o == null) {
377 <            for (int i = index ; i < elementCount ; i++)
378 <                if (elementData[i]==null)
379 <                    return i;
380 <        } else {
381 <            for (int i = index ; i < elementCount ; i++)
382 <                if (o.equals(elementData[i]))
383 <                    return i;
384 <        }
385 <        return -1;
376 >        if (o == null) {
377 >            for (int i = index ; i < elementCount ; i++)
378 >                if (elementData[i]==null)
379 >                    return i;
380 >        } else {
381 >            for (int i = index ; i < elementCount ; i++)
382 >                if (o.equals(elementData[i]))
383 >                    return i;
384 >        }
385 >        return -1;
386      }
387  
388      /**
# Line 397 | Line 397 | public class Vector<E>
397       *         this vector, or -1 if this vector does not contain the element
398       */
399      public synchronized int lastIndexOf(Object o) {
400 <        return lastIndexOf(o, elementCount-1);
400 >        return lastIndexOf(o, elementCount-1);
401      }
402  
403      /**
# Line 420 | Line 420 | public class Vector<E>
420          if (index >= elementCount)
421              throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
422  
423 <        if (o == null) {
424 <            for (int i = index; i >= 0; i--)
425 <                if (elementData[i]==null)
426 <                    return i;
427 <        } else {
428 <            for (int i = index; i >= 0; i--)
429 <                if (o.equals(elementData[i]))
430 <                    return i;
431 <        }
432 <        return -1;
423 >        if (o == null) {
424 >            for (int i = index; i >= 0; i--)
425 >                if (elementData[i]==null)
426 >                    return i;
427 >        } else {
428 >            for (int i = index; i >= 0; i--)
429 >                if (o.equals(elementData[i]))
430 >                    return i;
431 >        }
432 >        return -1;
433      }
434  
435      /**
# Line 441 | Line 441 | public class Vector<E>
441       * @param      index   an index into this vector
442       * @return     the component at the specified index
443       * @throws ArrayIndexOutOfBoundsException if the index is out of range
444 <     *         ({@code index < 0 || index >= size()})
444 >     *         ({@code index < 0 || index >= size()})
445       */
446      public synchronized E elementAt(int index) {
447 <        if (index >= elementCount) {
448 <            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
449 <        }
447 >        if (index >= elementCount) {
448 >            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
449 >        }
450  
451          return elementData(index);
452      }
# Line 459 | Line 459 | public class Vector<E>
459       * @throws NoSuchElementException if this vector has no components
460       */
461      public synchronized E firstElement() {
462 <        if (elementCount == 0) {
463 <            throw new NoSuchElementException();
464 <        }
465 <        return elementData(0);
462 >        if (elementCount == 0) {
463 >            throw new NoSuchElementException();
464 >        }
465 >        return elementData(0);
466      }
467  
468      /**
# Line 473 | Line 473 | public class Vector<E>
473       * @throws NoSuchElementException if this vector is empty
474       */
475      public synchronized E lastElement() {
476 <        if (elementCount == 0) {
477 <            throw new NoSuchElementException();
478 <        }
479 <        return elementData(elementCount - 1);
476 >        if (elementCount == 0) {
477 >            throw new NoSuchElementException();
478 >        }
479 >        return elementData(elementCount - 1);
480      }
481  
482      /**
# Line 497 | Line 497 | public class Vector<E>
497       * @param      obj     what the component is to be set to
498       * @param      index   the specified index
499       * @throws ArrayIndexOutOfBoundsException if the index is out of range
500 <     *         ({@code index < 0 || index >= size()})
500 >     *         ({@code index < 0 || index >= size()})
501       */
502      public synchronized void setElementAt(E obj, int index) {
503 <        if (index >= elementCount) {
504 <            throw new ArrayIndexOutOfBoundsException(index + " >= " +
505 <                                                     elementCount);
506 <        }
507 <        elementData[index] = obj;
503 >        if (index >= elementCount) {
504 >            throw new ArrayIndexOutOfBoundsException(index + " >= " +
505 >                                                     elementCount);
506 >        }
507 >        elementData[index] = obj;
508      }
509  
510      /**
# Line 524 | Line 524 | public class Vector<E>
524       *
525       * @param      index   the index of the object to remove
526       * @throws ArrayIndexOutOfBoundsException if the index is out of range
527 <     *         ({@code index < 0 || index >= size()})
527 >     *         ({@code index < 0 || index >= size()})
528       */
529      public synchronized void removeElementAt(int index) {
530 <        modCount++;
531 <        if (index >= elementCount) {
532 <            throw new ArrayIndexOutOfBoundsException(index + " >= " +
533 <                                                     elementCount);
534 <        }
535 <        else if (index < 0) {
536 <            throw new ArrayIndexOutOfBoundsException(index);
537 <        }
538 <        int j = elementCount - index - 1;
539 <        if (j > 0) {
540 <            System.arraycopy(elementData, index + 1, elementData, index, j);
541 <        }
542 <        elementCount--;
543 <        elementData[elementCount] = null; /* to let gc do its work */
530 >        modCount++;
531 >        if (index >= elementCount) {
532 >            throw new ArrayIndexOutOfBoundsException(index + " >= " +
533 >                                                     elementCount);
534 >        }
535 >        else if (index < 0) {
536 >            throw new ArrayIndexOutOfBoundsException(index);
537 >        }
538 >        int j = elementCount - index - 1;
539 >        if (j > 0) {
540 >            System.arraycopy(elementData, index + 1, elementData, index, j);
541 >        }
542 >        elementCount--;
543 >        elementData[elementCount] = null; /* to let gc do its work */
544      }
545  
546      /**
# Line 564 | Line 564 | public class Vector<E>
564       * @param      obj     the component to insert
565       * @param      index   where to insert the new component
566       * @throws ArrayIndexOutOfBoundsException if the index is out of range
567 <     *         ({@code index < 0 || index > size()})
567 >     *         ({@code index < 0 || index > size()})
568       */
569      public synchronized void insertElementAt(E obj, int index) {
570 <        modCount++;
571 <        if (index > elementCount) {
572 <            throw new ArrayIndexOutOfBoundsException(index
573 <                                                     + " > " + elementCount);
574 <        }
575 <        ensureCapacityHelper(elementCount + 1);
576 <        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
577 <        elementData[index] = obj;
578 <        elementCount++;
570 >        modCount++;
571 >        if (index > elementCount) {
572 >            throw new ArrayIndexOutOfBoundsException(index
573 >                                                     + " > " + elementCount);
574 >        }
575 >        ensureCapacityHelper(elementCount + 1);
576 >        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
577 >        elementData[index] = obj;
578 >        elementCount++;
579      }
580  
581      /**
# Line 590 | Line 590 | public class Vector<E>
590       * @param   obj   the component to be added
591       */
592      public synchronized void addElement(E obj) {
593 <        modCount++;
594 <        ensureCapacityHelper(elementCount + 1);
595 <        elementData[elementCount++] = obj;
593 >        modCount++;
594 >        ensureCapacityHelper(elementCount + 1);
595 >        elementData[elementCount++] = obj;
596      }
597  
598      /**
# Line 611 | Line 611 | public class Vector<E>
611       *          vector; {@code false} otherwise.
612       */
613      public synchronized boolean removeElement(Object obj) {
614 <        modCount++;
615 <        int i = indexOf(obj);
616 <        if (i >= 0) {
617 <            removeElementAt(i);
618 <            return true;
619 <        }
620 <        return false;
614 >        modCount++;
615 >        int i = indexOf(obj);
616 >        if (i >= 0) {
617 >            removeElementAt(i);
618 >            return true;
619 >        }
620 >        return false;
621      }
622  
623      /**
# Line 628 | Line 628 | public class Vector<E>
628       */
629      public synchronized void removeAllElements() {
630          modCount++;
631 <        // Let gc do its work
632 <        for (int i = 0; i < elementCount; i++)
633 <            elementData[i] = null;
631 >        // Let gc do its work
632 >        for (int i = 0; i < elementCount; i++)
633 >            elementData[i] = null;
634  
635 <        elementCount = 0;
635 >        elementCount = 0;
636      }
637  
638      /**
# Line 643 | Line 643 | public class Vector<E>
643       * @return  a clone of this vector
644       */
645      public synchronized Object clone() {
646 <        try {
647 <            @SuppressWarnings("unchecked")
648 <                Vector<E> v = (Vector<E>) super.clone();
649 <            v.elementData = Arrays.copyOf(elementData, elementCount);
650 <            v.modCount = 0;
651 <            return v;
652 <        } catch (CloneNotSupportedException e) {
653 <            // this shouldn't happen, since we are Cloneable
654 <            throw new InternalError();
655 <        }
646 >        try {
647 >            @SuppressWarnings("unchecked")
648 >                Vector<E> v = (Vector<E>) super.clone();
649 >            v.elementData = Arrays.copyOf(elementData, elementCount);
650 >            v.modCount = 0;
651 >            return v;
652 >        } catch (CloneNotSupportedException e) {
653 >            // this shouldn't happen, since we are Cloneable
654 >            throw new InternalError();
655 >        }
656      }
657  
658      /**
# Line 680 | Line 680 | public class Vector<E>
680       * does not contain any null elements.)
681       *
682       * @param a the array into which the elements of the Vector are to
683 <     *          be stored, if it is big enough; otherwise, a new array of the
684 <     *          same runtime type is allocated for this purpose.
683 >     *          be stored, if it is big enough; otherwise, a new array of the
684 >     *          same runtime type is allocated for this purpose.
685       * @return an array containing the elements of the Vector
686       * @throws ArrayStoreException if the runtime type of a is not a supertype
687       * of the runtime type of every element in this Vector
# Line 693 | Line 693 | public class Vector<E>
693          if (a.length < elementCount)
694              return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
695  
696 <        System.arraycopy(elementData, 0, a, 0, elementCount);
696 >        System.arraycopy(elementData, 0, a, 0, elementCount);
697  
698          if (a.length > elementCount)
699              a[elementCount] = null;
# Line 705 | Line 705 | public class Vector<E>
705  
706      @SuppressWarnings("unchecked")
707      E elementData(int index) {
708 <        return (E) elementData[index];
708 >        return (E) elementData[index];
709      }
710  
711      /**
# Line 718 | Line 718 | public class Vector<E>
718       * @since 1.2
719       */
720      public synchronized E get(int index) {
721 <        if (index >= elementCount)
722 <            throw new ArrayIndexOutOfBoundsException(index);
721 >        if (index >= elementCount)
722 >            throw new ArrayIndexOutOfBoundsException(index);
723  
724 <        return elementData(index);
724 >        return elementData(index);
725      }
726  
727      /**
# Line 732 | Line 732 | public class Vector<E>
732       * @param element element to be stored at the specified position
733       * @return the element previously at the specified position
734       * @throws ArrayIndexOutOfBoundsException if the index is out of range
735 <     *         ({@code index < 0 || index >= size()})
735 >     *         ({@code index < 0 || index >= size()})
736       * @since 1.2
737       */
738      public synchronized E set(int index, E element) {
739 <        if (index >= elementCount)
740 <            throw new ArrayIndexOutOfBoundsException(index);
739 >        if (index >= elementCount)
740 >            throw new ArrayIndexOutOfBoundsException(index);
741  
742 <        E oldValue = elementData(index);
743 <        elementData[index] = element;
744 <        return oldValue;
742 >        E oldValue = elementData(index);
743 >        elementData[index] = element;
744 >        return oldValue;
745      }
746  
747      /**
# Line 752 | Line 752 | public class Vector<E>
752       * @since 1.2
753       */
754      public synchronized boolean add(E e) {
755 <        modCount++;
756 <        ensureCapacityHelper(elementCount + 1);
757 <        elementData[elementCount++] = e;
755 >        modCount++;
756 >        ensureCapacityHelper(elementCount + 1);
757 >        elementData[elementCount++] = e;
758          return true;
759      }
760  
# Line 800 | Line 800 | public class Vector<E>
800       * @since 1.2
801       */
802      public synchronized E remove(int index) {
803 <        modCount++;
804 <        if (index >= elementCount)
805 <            throw new ArrayIndexOutOfBoundsException(index);
806 <        E oldValue = elementData(index);
807 <
808 <        int numMoved = elementCount - index - 1;
809 <        if (numMoved > 0)
810 <            System.arraycopy(elementData, index+1, elementData, index,
811 <                             numMoved);
812 <        elementData[--elementCount] = null; // Let gc do its work
803 >        modCount++;
804 >        if (index >= elementCount)
805 >            throw new ArrayIndexOutOfBoundsException(index);
806 >        E oldValue = elementData(index);
807 >
808 >        int numMoved = elementCount - index - 1;
809 >        if (numMoved > 0)
810 >            System.arraycopy(elementData, index+1, elementData, index,
811 >                             numMoved);
812 >        elementData[--elementCount] = null; // Let gc do its work
813  
814 <        return oldValue;
814 >        return oldValue;
815      }
816  
817      /**
# Line 833 | Line 833 | public class Vector<E>
833       * @param   c a collection whose elements will be tested for containment
834       *          in this Vector
835       * @return true if this Vector contains all of the elements in the
836 <     *         specified collection
836 >     *         specified collection
837       * @throws NullPointerException if the specified collection is null
838       */
839      public synchronized boolean containsAll(Collection<?> c) {
# Line 854 | Line 854 | public class Vector<E>
854       * @since 1.2
855       */
856      public synchronized boolean addAll(Collection<? extends E> c) {
857 <        modCount++;
857 >        modCount++;
858          Object[] a = c.toArray();
859          int numNew = a.length;
860 <        ensureCapacityHelper(elementCount + numNew);
860 >        ensureCapacityHelper(elementCount + numNew);
861          System.arraycopy(a, 0, elementData, elementCount, numNew);
862          elementCount += numNew;
863 <        return numNew != 0;
863 >        return numNew != 0;
864      }
865  
866      /**
# Line 919 | Line 919 | public class Vector<E>
919       * @since 1.2
920       */
921      public synchronized boolean addAll(int index, Collection<? extends E> c) {
922 <        modCount++;
923 <        if (index < 0 || index > elementCount)
924 <            throw new ArrayIndexOutOfBoundsException(index);
922 >        modCount++;
923 >        if (index < 0 || index > elementCount)
924 >            throw new ArrayIndexOutOfBoundsException(index);
925  
926          Object[] a = c.toArray();
927 <        int numNew = a.length;
928 <        ensureCapacityHelper(elementCount + numNew);
927 >        int numNew = a.length;
928 >        ensureCapacityHelper(elementCount + numNew);
929  
930 <        int numMoved = elementCount - index;
931 <        if (numMoved > 0)
932 <            System.arraycopy(elementData, index, elementData, index + numNew,
933 <                             numMoved);
930 >        int numMoved = elementCount - index;
931 >        if (numMoved > 0)
932 >            System.arraycopy(elementData, index, elementData, index + numNew,
933 >                             numMoved);
934  
935          System.arraycopy(a, 0, elementData, index, numNew);
936 <        elementCount += numNew;
937 <        return numNew != 0;
936 >        elementCount += numNew;
937 >        return numNew != 0;
938      }
939  
940      /**
# Line 982 | Line 982 | public class Vector<E>
982       * instead of a whole List.  For example, the following idiom
983       * removes a range of elements from a List:
984       * <pre>
985 <     *      list.subList(from, to).clear();
985 >     *      list.subList(from, to).clear();
986       * </pre>
987       * Similar idioms may be constructed for indexOf and lastIndexOf,
988       * and all of the algorithms in the Collections class can be applied to
# Line 1000 | Line 1000 | public class Vector<E>
1000       * @throws IndexOutOfBoundsException if an endpoint index value is out of range
1001       *         {@code (fromIndex < 0 || toIndex > size)}
1002       * @throws IllegalArgumentException if the endpoint indices are out of order
1003 <     *         {@code (fromIndex > toIndex)}
1003 >     *         {@code (fromIndex > toIndex)}
1004       */
1005      public synchronized List<E> subList(int fromIndex, int toIndex) {
1006          return Collections.synchronizedList(super.subList(fromIndex, toIndex),
# Line 1015 | Line 1015 | public class Vector<E>
1015       * (If {@code toIndex==fromIndex}, this operation has no effect.)
1016       */
1017      protected synchronized void removeRange(int fromIndex, int toIndex) {
1018 <        modCount++;
1019 <        int numMoved = elementCount - toIndex;
1018 >        modCount++;
1019 >        int numMoved = elementCount - toIndex;
1020          System.arraycopy(elementData, toIndex, elementData, fromIndex,
1021                           numMoved);
1022  
1023 <        // Let gc do its work
1024 <        int newElementCount = elementCount - (toIndex-fromIndex);
1025 <        while (elementCount != newElementCount)
1026 <            elementData[--elementCount] = null;
1023 >        // Let gc do its work
1024 >        int newElementCount = elementCount - (toIndex-fromIndex);
1025 >        while (elementCount != newElementCount)
1026 >            elementData[--elementCount] = null;
1027      }
1028  
1029      /**
# Line 1034 | Line 1034 | public class Vector<E>
1034      private synchronized void writeObject(java.io.ObjectOutputStream s)
1035          throws java.io.IOException
1036      {
1037 <        s.defaultWriteObject();
1037 >        s.defaultWriteObject();
1038      }
1039  
1040      /**
# Line 1050 | Line 1050 | public class Vector<E>
1050       * @throws IndexOutOfBoundsException {@inheritDoc}
1051       */
1052      public synchronized ListIterator<E> listIterator(int index) {
1053 <        if (index < 0 || index > elementCount)
1053 >        if (index < 0 || index > elementCount)
1054              throw new IndexOutOfBoundsException("Index: "+index);
1055 <        return new ListItr(index);
1055 >        return new ListItr(index);
1056      }
1057  
1058      /**
# Line 1064 | Line 1064 | public class Vector<E>
1064       * @see #listIterator(int)
1065       */
1066      public synchronized ListIterator<E> listIterator() {
1067 <        return new ListItr(0);
1067 >        return new ListItr(0);
1068      }
1069  
1070      /**
# Line 1075 | Line 1075 | public class Vector<E>
1075       * @return an iterator over the elements in this list in proper sequence
1076       */
1077      public synchronized Iterator<E> iterator() {
1078 <        return new Itr();
1078 >        return new Itr();
1079      }
1080  
1081      /**
1082       * An optimized version of AbstractList.Itr
1083       */
1084      private class Itr implements Iterator<E> {
1085 <        int cursor;       // index of next element to return
1086 <        int lastRet = -1; // index of last element returned; -1 if no such
1087 <        int expectedModCount = modCount;
1085 >        int cursor;       // index of next element to return
1086 >        int lastRet = -1; // index of last element returned; -1 if no such
1087 >        int expectedModCount = modCount;
1088  
1089 <        public boolean hasNext() {
1089 >        public boolean hasNext() {
1090              // Racy but within spec, since modifications are checked
1091              // within or after synchronization in next/previous
1092              return cursor != elementCount;
1093 <        }
1093 >        }
1094 >
1095 >        public E next() {
1096 >            synchronized (Vector.this) {
1097 >                checkForComodification();
1098 >                int i = cursor;
1099 >                if (i >= elementCount)
1100 >                    throw new NoSuchElementException();
1101 >                cursor = i + 1;
1102 >                return elementData(lastRet = i);
1103 >            }
1104 >        }
1105  
1106 <        public E next() {
1107 <            synchronized (Vector.this) {
1108 <                checkForComodification();
1109 <                int i = cursor;
1110 <                if (i >= elementCount)
1111 <                    throw new NoSuchElementException();
1112 <                cursor = i + 1;
1102 <                return elementData(lastRet = i);
1103 <            }
1104 <        }
1105 <
1106 <        public void remove() {
1107 <            if (lastRet == -1)
1108 <                throw new IllegalStateException();
1109 <            synchronized (Vector.this) {
1110 <                checkForComodification();
1111 <                Vector.this.remove(lastRet);
1112 <                expectedModCount = modCount;
1106 >        public void remove() {
1107 >            if (lastRet == -1)
1108 >                throw new IllegalStateException();
1109 >            synchronized (Vector.this) {
1110 >                checkForComodification();
1111 >                Vector.this.remove(lastRet);
1112 >                expectedModCount = modCount;
1113              }
1114 <            cursor = lastRet;
1115 <            lastRet = -1;
1116 <        }
1117 <
1118 <        final void checkForComodification() {
1119 <            if (modCount != expectedModCount)
1120 <                throw new ConcurrentModificationException();
1121 <        }
1114 >            cursor = lastRet;
1115 >            lastRet = -1;
1116 >        }
1117 >
1118 >        final void checkForComodification() {
1119 >            if (modCount != expectedModCount)
1120 >                throw new ConcurrentModificationException();
1121 >        }
1122      }
1123  
1124      /**
1125       * An optimized version of AbstractList.ListItr
1126       */
1127      final class ListItr extends Itr implements ListIterator<E> {
1128 <        ListItr(int index) {
1129 <            super();
1130 <            cursor = index;
1131 <        }
1132 <
1133 <        public boolean hasPrevious() {
1134 <            return cursor != 0;
1135 <        }
1136 <
1137 <        public int nextIndex() {
1138 <            return cursor;
1139 <        }
1140 <
1141 <        public int previousIndex() {
1142 <            return cursor - 1;
1143 <        }
1144 <
1145 <        public E previous() {
1146 <            synchronized (Vector.this) {
1147 <                checkForComodification();
1148 <                int i = cursor - 1;
1149 <                if (i < 0)
1150 <                    throw new NoSuchElementException();
1151 <                cursor = i;
1152 <                return elementData(lastRet = i);
1153 <            }
1154 <        }
1155 <
1156 <        public void set(E e) {
1157 <            if (lastRet == -1)
1158 <                throw new IllegalStateException();
1159 <            synchronized (Vector.this) {
1160 <                checkForComodification();
1161 <                Vector.this.set(lastRet, e);
1162 <            }
1163 <        }
1164 <
1165 <        public void add(E e) {
1166 <            int i = cursor;
1167 <            synchronized (Vector.this) {
1168 <                checkForComodification();
1169 <                Vector.this.add(i, e);
1170 <                expectedModCount = modCount;
1171 <            }
1172 <            cursor = i + 1;
1173 <            lastRet = -1;
1174 <        }
1128 >        ListItr(int index) {
1129 >            super();
1130 >            cursor = index;
1131 >        }
1132 >
1133 >        public boolean hasPrevious() {
1134 >            return cursor != 0;
1135 >        }
1136 >
1137 >        public int nextIndex() {
1138 >            return cursor;
1139 >        }
1140 >
1141 >        public int previousIndex() {
1142 >            return cursor - 1;
1143 >        }
1144 >
1145 >        public E previous() {
1146 >            synchronized (Vector.this) {
1147 >                checkForComodification();
1148 >                int i = cursor - 1;
1149 >                if (i < 0)
1150 >                    throw new NoSuchElementException();
1151 >                cursor = i;
1152 >                return elementData(lastRet = i);
1153 >            }
1154 >        }
1155 >
1156 >        public void set(E e) {
1157 >            if (lastRet == -1)
1158 >                throw new IllegalStateException();
1159 >            synchronized (Vector.this) {
1160 >                checkForComodification();
1161 >                Vector.this.set(lastRet, e);
1162 >            }
1163 >        }
1164 >
1165 >        public void add(E e) {
1166 >            int i = cursor;
1167 >            synchronized (Vector.this) {
1168 >                checkForComodification();
1169 >                Vector.this.add(i, e);
1170 >                expectedModCount = modCount;
1171 >            }
1172 >            cursor = i + 1;
1173 >            lastRet = -1;
1174 >        }
1175      }
1176   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines