src/share/classes/java/util/Vector.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 




  28 /**
  29  * The {@code Vector} class implements a growable array of
  30  * objects. Like an array, it contains components that can be
  31  * accessed using an integer index. However, the size of a
  32  * {@code Vector} can grow or shrink as needed to accommodate
  33  * adding and removing items after the {@code Vector} has been created.
  34  *
  35  * <p>Each vector tries to optimize storage management by maintaining a
  36  * {@code capacity} and a {@code capacityIncrement}. The
  37  * {@code capacity} is always at least as large as the vector
  38  * size; it is usually larger because as components are added to the
  39  * vector, the vector's storage increases in chunks the size of
  40  * {@code capacityIncrement}. An application can increase the
  41  * capacity of a vector before inserting a large number of
  42  * components; this reduces the amount of incremental reallocation.
  43  *
  44  * <p><a name="fail-fast"/>
  45  * The iterators returned by this class's {@link #iterator() iterator} and
  46  * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
  47  * if the vector is structurally modified at any time after the iterator is


1191 
1192         public void set(E e) {
1193             if (lastRet == -1)
1194                 throw new IllegalStateException();
1195             synchronized (Vector.this) {
1196                 checkForComodification();
1197                 Vector.this.set(lastRet, e);
1198             }
1199         }
1200 
1201         public void add(E e) {
1202             int i = cursor;
1203             synchronized (Vector.this) {
1204                 checkForComodification();
1205                 Vector.this.add(i, e);
1206                 expectedModCount = modCount;
1207             }
1208             cursor = i + 1;
1209             lastRet = -1;
1210         }























































































1211     }
1212 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.util.function.Block;
  29 import java.util.function.Predicate;
  30 import java.util.function.UnaryOperator;
  31 
  32 /**
  33  * The {@code Vector} class implements a growable array of
  34  * objects. Like an array, it contains components that can be
  35  * accessed using an integer index. However, the size of a
  36  * {@code Vector} can grow or shrink as needed to accommodate
  37  * adding and removing items after the {@code Vector} has been created.
  38  *
  39  * <p>Each vector tries to optimize storage management by maintaining a
  40  * {@code capacity} and a {@code capacityIncrement}. The
  41  * {@code capacity} is always at least as large as the vector
  42  * size; it is usually larger because as components are added to the
  43  * vector, the vector's storage increases in chunks the size of
  44  * {@code capacityIncrement}. An application can increase the
  45  * capacity of a vector before inserting a large number of
  46  * components; this reduces the amount of incremental reallocation.
  47  *
  48  * <p><a name="fail-fast"/>
  49  * The iterators returned by this class's {@link #iterator() iterator} and
  50  * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
  51  * if the vector is structurally modified at any time after the iterator is


1195 
1196         public void set(E e) {
1197             if (lastRet == -1)
1198                 throw new IllegalStateException();
1199             synchronized (Vector.this) {
1200                 checkForComodification();
1201                 Vector.this.set(lastRet, e);
1202             }
1203         }
1204 
1205         public void add(E e) {
1206             int i = cursor;
1207             synchronized (Vector.this) {
1208                 checkForComodification();
1209                 Vector.this.add(i, e);
1210                 expectedModCount = modCount;
1211             }
1212             cursor = i + 1;
1213             lastRet = -1;
1214         }
1215     }
1216 
1217     @Override
1218     public synchronized void forEach(Block<? super E> block) {
1219         Objects.requireNonNull(block);
1220         final E[] elementData = (E[]) this.elementData;
1221         final int expectedModCount = modCount;
1222         final int size = elementCount;
1223         for (int i=0; modCount == expectedModCount && i < size; i++) {
1224             block.accept(elementData[i]);
1225         }
1226         if (modCount != expectedModCount) {
1227             throw new ConcurrentModificationException();
1228         }
1229     }
1230 
1231     @Override
1232     @SuppressWarnings("unchecked")
1233     public synchronized boolean removeAll(Predicate<? super E> filter) {
1234         Objects.requireNonNull(filter);
1235         // figure out which elements are to be removed
1236         // any exception thrown from the filter predicate at this stage
1237         // will leave the collection unmodified
1238         int removeCount = 0;
1239         final int size = elementCount;
1240         final BitSet removeSet = new BitSet(size);
1241         final int expectedModCount = modCount;
1242         for (int i=0; modCount == expectedModCount && i < size; i++) {
1243             @SuppressWarnings("unchecked")
1244             final E element = (E) elementData[i];
1245             if (filter.test(element)) {
1246                 removeSet.set(i);
1247                 removeCount++;
1248             }
1249         }
1250 
1251         if (modCount != expectedModCount) {
1252             throw new ConcurrentModificationException();
1253         }
1254 
1255         // shift surviving elements left over the spaces left by removed elements
1256         final boolean anyToRemove = removeCount > 0;
1257         if (anyToRemove) {
1258             final int newSize = size - removeCount;
1259             for (int i=0, j=0; modCount == expectedModCount &&
1260                     (i < size) && (j < newSize); i++, j++) {
1261                 i = removeSet.nextClearBit(i);
1262                 elementData[j] = elementData[i];
1263             }
1264             for (int k=newSize; modCount == expectedModCount && k < size; k++) {
1265                 elementData[k] = null;  // Let gc do its work
1266             }
1267             elementCount = newSize;
1268             if (modCount != expectedModCount) {
1269                 throw new ConcurrentModificationException();
1270             }
1271             modCount++;
1272         }
1273 
1274         return anyToRemove;
1275     }
1276 
1277     @Override
1278     @SuppressWarnings("unchecked")
1279     public synchronized void replaceAll(UnaryOperator<E> operator) {
1280         Objects.requireNonNull(operator);
1281         final int expectedModCount = modCount;
1282         final int size = elementCount;
1283         for (int i=0; modCount == expectedModCount && i < size; i++) {
1284             elementData[i] = operator.operate((E) elementData[i]);
1285         }
1286         if (modCount != expectedModCount) {
1287             throw new ConcurrentModificationException();
1288         }
1289         modCount++;
1290     }
1291 
1292     @Override
1293     @SuppressWarnings("unchecked")
1294     public synchronized void sort(Comparator<? super E> c) {
1295         Objects.requireNonNull(c);
1296         final int expectedModCount = modCount;
1297         Arrays.sort((E[]) elementData, 0, elementCount, c);
1298         if (modCount != expectedModCount) {
1299             throw new ConcurrentModificationException();
1300         }
1301         modCount++;
1302     }
1303 }