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.2 by jsr166, Sat Nov 26 03:12:10 2005 UTC vs.
Revision 1.3 by dl, Sun Nov 27 14:54:23 2005 UTC

# Line 1056 | Line 1056 | public class Vector<E>
1056      }
1057  
1058      /**
1059 +     * {@inheritDoc}
1060 +     */
1061 +    public synchronized ListIterator<E> listIterator() {
1062 +        return new VectorIterator(0);
1063 +    }
1064 +
1065 +    /**
1066       * Returns an iterator over the elements in this list in proper sequence.
1067       *
1068       * @return an iterator over the elements in this list in proper sequence
# Line 1065 | Line 1072 | public class Vector<E>
1072      }
1073  
1074      /**
1075 <     * A streamlined version of AbstractList.Itr.
1075 >     * A streamlined version of AbstractList.ListItr.
1076       */
1077 <    final class VectorIterator implements ListIterator<E> {
1078 <        int cursor;           // index of next element to return;
1079 <        int lastRet;          // index of last element, or -1 if no such
1080 <        int expectedModCount; // to check for CME
1077 >    private final class VectorIterator implements ListIterator<E> {
1078 >        int cursor;                // current position
1079 >        int lastRet;               // index of last returned element
1080 >        int expectedModCount;      // to check for CME
1081  
1082          VectorIterator(int index) {
1083              cursor = index;
1077            lastRet = -1;
1084              expectedModCount = modCount;
1085 +            lastRet = -1;
1086          }
1087  
1088          public boolean hasNext() {
1089 <            // Racy but within spec and backwards-compatible
1090 <            return cursor < elementCount;
1089 >            // Racy but within spec, since modifications are checked
1090 >            // within or after synchronization in next/previous
1091 >            return cursor < Vector.this.elementCount;
1092          }
1093  
1094          public boolean hasPrevious() {
# Line 1096 | Line 1104 | public class Vector<E>
1104          }
1105  
1106          public E next() {
1107 <            synchronized(Vector.this) {
1108 <                if (expectedModCount == modCount) {
1109 <                    int i = cursor;
1110 <                    if (i < elementCount) {
1111 <                        try {
1112 <                            E e = (E)elementData[i];
1113 <                            lastRet = i;
1114 <                            cursor = i + 1;
1115 <                            return e;
1116 <                        } catch (IndexOutOfBoundsException fallthrough) {
1117 <                        }
1110 <                    }
1111 <                }
1112 <                // Prefer reporting CME if applicable on failures
1113 <                if (expectedModCount == modCount)
1114 <                    throw new NoSuchElementException();
1115 <                throw new ConcurrentModificationException();
1107 >            try {
1108 >                int i = cursor;
1109 >                E next = get(i);
1110 >                lastRet = i;
1111 >                cursor = i + 1;
1112 >                return next;
1113 >            } catch (IndexOutOfBoundsException ex) {
1114 >                throw new NoSuchElementException();
1115 >            } finally {
1116 >                if (expectedModCount != modCount)
1117 >                    throw new ConcurrentModificationException();
1118              }
1119          }
1118
1120          public E previous() {
1121 <            synchronized(Vector.this) {
1122 <                if (expectedModCount == modCount) {
1123 <                    int i = cursor - 1;
1124 <                    if (i < elementCount) {
1125 <                        try {
1126 <                            E e = (E)elementData[i];
1127 <                            lastRet = i;
1128 <                            cursor = i;
1129 <                            return e;
1130 <                        } catch (IndexOutOfBoundsException fallthrough) {
1131 <                        }
1131 <                    }
1132 <                }
1133 <                if (expectedModCount == modCount)
1134 <                    throw new NoSuchElementException();
1135 <                throw new ConcurrentModificationException();
1121 >            try {
1122 >                int i = cursor - 1;
1123 >                E prev = get(i);
1124 >                lastRet = i;
1125 >                cursor = i;
1126 >                return prev;
1127 >            } catch (IndexOutOfBoundsException ex) {
1128 >                throw new NoSuchElementException();
1129 >            } finally {
1130 >                if (expectedModCount != modCount)
1131 >                    throw new ConcurrentModificationException();
1132              }
1133          }
1134  
1135          public void remove() {
1136 <            if (lastRet < 0)
1136 >            if (lastRet == -1)
1137                  throw new IllegalStateException();
1138 <            synchronized(Vector.this) {
1139 <                if (modCount != expectedModCount)
1140 <                    throw new ConcurrentModificationException();
1141 <                Vector.this.remove(lastRet);
1142 <                if (lastRet < cursor)
1143 <                    cursor--;
1144 <                lastRet = -1;
1145 <                expectedModCount = modCount;
1146 <            }
1138 >            if (expectedModCount != modCount)
1139 >                throw new ConcurrentModificationException();
1140 >            try {
1141 >                Vector.this.remove(lastRet);
1142 >                if (lastRet < cursor)
1143 >                    cursor--;
1144 >                lastRet = -1;
1145 >                expectedModCount = modCount;
1146 >            } catch (IndexOutOfBoundsException e) {
1147 >                throw new ConcurrentModificationException();
1148 >            }
1149          }
1150  
1151          public void set(E e) {
1152 <            if (lastRet < 0)
1152 >            if (lastRet == -1)
1153                  throw new IllegalStateException();
1154 <            synchronized(Vector.this) {
1155 <                if (modCount != expectedModCount)
1156 <                    throw new ConcurrentModificationException();
1157 <                Vector.this.set(lastRet, e);
1158 <                expectedModCount = modCount;
1159 <            }
1154 >            if (expectedModCount != modCount)
1155 >                throw new ConcurrentModificationException();
1156 >            try {
1157 >                Vector.this.set(lastRet, e);
1158 >                expectedModCount = modCount;
1159 >            } catch (IndexOutOfBoundsException ex) {
1160 >                throw new ConcurrentModificationException();
1161 >            }
1162          }
1163  
1164          public void add(E e) {
1165 <            synchronized(Vector.this) {
1166 <                if (modCount != expectedModCount)
1167 <                    throw new ConcurrentModificationException();
1168 <                Vector.this.add(cursor++, e);
1169 <                lastRet = -1;
1170 <                expectedModCount = modCount;
1171 <            }
1165 >            if (expectedModCount != modCount)
1166 >                throw new ConcurrentModificationException();
1167 >            try {
1168 >                int i = cursor;
1169 >                Vector.this.add(i, e);
1170 >                cursor = i + 1;
1171 >                lastRet = -1;
1172 >                expectedModCount = modCount;
1173 >            } catch (IndexOutOfBoundsException ex) {
1174 >                throw new ConcurrentModificationException();
1175 >            }
1176          }
1177      }
1174
1178   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines