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.1 by dl, Fri Nov 25 13:27:29 2005 UTC vs.
Revision 1.8 by jsr166, Tue Feb 7 20:54:24 2006 UTC

# Line 1 | Line 1
1   /*
2   * %W% %E%
3   *
4 < * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5   * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6   */
7  
8   package java.util;
9 import java.util.*; // for javadoc (till 6280605 is fixed)
9  
10   /**
11   * The <code>Vector</code> class implements a growable array of
# Line 147 | Line 146 | public class Vector<E>
146       * @since   1.2
147       */
148      public Vector(Collection<? extends E> c) {
149 <        Object[] a = c.toArray();
150 <        elementCount = a.length;
151 <        // If c.toArray incorrectly doesn't return Object[], copy it.
152 <        if (a.getClass() == Object[].class)
153 <            elementData = a;
155 <        else
156 <            elementData = Arrays.copyOf(a, a.length, Object[].class);
149 >        elementData = c.toArray();
150 >        elementCount = elementData.length;
151 >        // c.toArray might (incorrectly) not return Object[] (see 6260652)
152 >        if (elementData.getClass() != Object[].class)
153 >            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
154      }
155  
156      /**
# Line 1054 | Line 1051 | public class Vector<E>
1051              throw new IndexOutOfBoundsException("Index: "+index);
1052          return new VectorIterator(index);
1053      }
1054 <
1054 >
1055 >    /**
1056 >     * {@inheritDoc}
1057 >     */
1058 >    public synchronized ListIterator<E> listIterator() {
1059 >        return new VectorIterator(0);
1060 >    }
1061 >
1062      /**
1063       * Returns an iterator over the elements in this list in proper sequence.
1064       *
# Line 1065 | Line 1069 | public class Vector<E>
1069      }
1070  
1071      /**
1072 <     * A streamlined version of AbstractList.Itr.
1072 >     * A streamlined version of AbstractList.ListItr.
1073       */
1074 <    final class VectorIterator implements ListIterator<E> {
1075 <        int cursor;           // index of next element to return;
1076 <        int lastRet;          // index of last element, or -1 if no such
1077 <        int expectedModCount; // to check for CME
1074 >    private final class VectorIterator implements ListIterator<E> {
1075 >        int cursor;                // current position
1076 >        int lastRet;               // index of last returned element
1077 >        int expectedModCount;      // to check for CME
1078  
1079          VectorIterator(int index) {
1080              cursor = index;
1077            lastRet = -1;
1081              expectedModCount = modCount;
1082 +            lastRet = -1;
1083          }
1084  
1085          public boolean hasNext() {
1086 <            // Racy but within spec and backwards-compatible
1087 <            return cursor < elementCount;
1086 >            // Racy but within spec, since modifications are checked
1087 >            // within or after synchronization in next/previous
1088 >            return cursor != elementCount;
1089          }
1090  
1091          public boolean hasPrevious() {
1092 <            return cursor > 0;
1092 >            return cursor != 0;
1093          }
1094  
1095          public int nextIndex() {
# Line 1096 | Line 1101 | public class Vector<E>
1101          }
1102  
1103          public E next() {
1104 <            synchronized(Vector.this) {
1105 <                if (expectedModCount == modCount) {
1106 <                    int i = cursor;
1107 <                    if (i < elementCount) {
1108 <                        try {
1109 <                            E e = (E)elementData[i];
1110 <                            lastRet = i;
1111 <                            cursor = i + 1;
1112 <                            return e;
1113 <                        } catch (IndexOutOfBoundsException fallthrough) {
1114 <                        }
1110 <                    }
1111 <                }
1112 <                // Prefer reporting CME if applicable on failures
1113 <                if (expectedModCount == modCount)
1114 <                    throw new NoSuchElementException();
1115 <                throw new ConcurrentModificationException();
1104 >            try {
1105 >                int i = cursor;
1106 >                E next = get(i);
1107 >                lastRet = i;
1108 >                cursor = i + 1;
1109 >                return next;
1110 >            } catch (IndexOutOfBoundsException ex) {
1111 >                throw new NoSuchElementException();
1112 >            } finally {
1113 >                if (expectedModCount != modCount)
1114 >                    throw new ConcurrentModificationException();
1115              }
1116          }
1117  
1118 <        public E previous() {
1119 <            synchronized(Vector.this) {
1120 <                if (expectedModCount == modCount) {
1121 <                    int i = cursor - 1;
1122 <                    if (i < elementCount) {
1123 <                        try {
1124 <                            E e = (E)elementData[i];
1125 <                            lastRet = i;
1126 <                            cursor = i;
1127 <                            return e;
1128 <                        } catch (IndexOutOfBoundsException fallthrough) {
1129 <                        }
1131 <                    }
1132 <                }
1133 <                if (expectedModCount == modCount)
1134 <                    throw new NoSuchElementException();
1135 <                throw new ConcurrentModificationException();
1118 >        public E previous() {
1119 >            try {
1120 >                int i = cursor - 1;
1121 >                E prev = get(i);
1122 >                lastRet = i;
1123 >                cursor = i;
1124 >                return prev;
1125 >            } catch (IndexOutOfBoundsException ex) {
1126 >                throw new NoSuchElementException();
1127 >            } finally {
1128 >                if (expectedModCount != modCount)
1129 >                    throw new ConcurrentModificationException();
1130              }
1131          }
1132  
1133          public void remove() {
1134 <            if (lastRet < 0)
1134 >            if (lastRet == -1)
1135                  throw new IllegalStateException();
1136 <            synchronized(Vector.this) {
1137 <                if (modCount != expectedModCount)
1138 <                    throw new ConcurrentModificationException();
1139 <                Vector.this.remove(lastRet);
1140 <                if (lastRet < cursor)
1141 <                    cursor--;
1142 <                lastRet = -1;
1143 <                expectedModCount = modCount;
1144 <            }
1136 >            if (expectedModCount != modCount)
1137 >                throw new ConcurrentModificationException();
1138 >            try {
1139 >                Vector.this.remove(lastRet);
1140 >                if (lastRet < cursor)
1141 >                    cursor--;
1142 >                lastRet = -1;
1143 >                expectedModCount = modCount;
1144 >            } catch (IndexOutOfBoundsException ex) {
1145 >                throw new ConcurrentModificationException();
1146 >            }
1147          }
1148  
1149          public void set(E e) {
1150 <            if (lastRet < 0)
1150 >            if (lastRet == -1)
1151                  throw new IllegalStateException();
1152 <            synchronized(Vector.this) {
1153 <                if (modCount != expectedModCount)
1154 <                    throw new ConcurrentModificationException();
1155 <                Vector.this.set(lastRet, e);
1156 <                expectedModCount = modCount;
1157 <            }
1152 >            if (expectedModCount != modCount)
1153 >                throw new ConcurrentModificationException();
1154 >            try {
1155 >                Vector.this.set(lastRet, e);
1156 >                expectedModCount = modCount;
1157 >            } catch (IndexOutOfBoundsException ex) {
1158 >                throw new ConcurrentModificationException();
1159 >            }
1160          }
1161  
1162          public void add(E e) {
1163 <            synchronized(Vector.this) {
1164 <                if (modCount != expectedModCount)
1165 <                    throw new ConcurrentModificationException();
1166 <                Vector.this.add(cursor++, e);
1167 <                lastRet = -1;
1168 <                expectedModCount = modCount;
1169 <            }
1163 >            if (expectedModCount != modCount)
1164 >                throw new ConcurrentModificationException();
1165 >            try {
1166 >                int i = cursor;
1167 >                Vector.this.add(i, e);
1168 >                cursor = i + 1;
1169 >                lastRet = -1;
1170 >                expectedModCount = modCount;
1171 >            } catch (IndexOutOfBoundsException ex) {
1172 >                throw new ConcurrentModificationException();
1173 >            }
1174          }
1175      }
1174
1176   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines