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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines