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.Consumer;
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(Consumer<? super E> action) {
1219 Objects.requireNonNull(action);
1220 final int expectedModCount = modCount;
1221 @SuppressWarnings("unchecked")
1222 final E[] elementData = (E[]) this.elementData;
1223 final int elementCount = this.elementCount;
1224 for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
1225 action.accept(elementData[i]);
1226 }
1227 if (modCount != expectedModCount) {
1228 throw new ConcurrentModificationException();
1229 }
1230 }
1231
1232 @Override
1233 @SuppressWarnings("unchecked")
1234 public synchronized boolean removeIf(Predicate<? super E> filter) {
1235 Objects.requireNonNull(filter);
1236 // figure out which elements are to be removed
1237 // any exception thrown from the filter predicate at this stage
1238 // will leave the collection unmodified
1239 int removeCount = 0;
1240 final int size = elementCount;
1241 final BitSet removeSet = new BitSet(size);
1242 final int expectedModCount = modCount;
1243 for (int i=0; modCount == expectedModCount && i < size; i++) {
1244 @SuppressWarnings("unchecked")
1245 final E element = (E) elementData[i];
1246 if (filter.test(element)) {
1247 removeSet.set(i);
1248 removeCount++;
1249 }
1250 }
1251
1252 if (modCount != expectedModCount) {
1253 throw new ConcurrentModificationException();
1254 }
1255
1256 // shift surviving elements left over the spaces left by removed elements
1257 final boolean anyToRemove = removeCount > 0;
1258 if (anyToRemove) {
1259 final int newSize = size - removeCount;
1260 for (int i=0, j=0; modCount == expectedModCount &&
1261 (i < size) && (j < newSize); i++, j++) {
1262 i = removeSet.nextClearBit(i);
1263 elementData[j] = elementData[i];
1264 }
1265 for (int k=newSize; modCount == expectedModCount && k < size; k++) {
1266 elementData[k] = null; // Let gc do its work
1267 }
1268 elementCount = newSize;
1269 if (modCount != expectedModCount) {
1270 throw new ConcurrentModificationException();
1271 }
1272 modCount++;
1273 }
1274
1275 return anyToRemove;
1276 }
1277
1278 @Override
1279 @SuppressWarnings("unchecked")
1280 public synchronized void replaceAll(UnaryOperator<E> operator) {
1281 Objects.requireNonNull(operator);
1282 final int expectedModCount = modCount;
1283 final int size = elementCount;
1284 for (int i=0; modCount == expectedModCount && i < size; i++) {
1285 elementData[i] = operator.apply((E) elementData[i]);
1286 }
1287 if (modCount != expectedModCount) {
1288 throw new ConcurrentModificationException();
1289 }
1290 modCount++;
1291 }
1292
1293 @Override
1294 @SuppressWarnings("unchecked")
1295 public synchronized void sort(Comparator<? super E> 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 }
|