1 |
|
/* |
2 |
< |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved. |
3 |
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
|
* This code is free software; you can redistribute it and/or modify it |
25 |
|
|
26 |
|
package java.util; |
27 |
|
|
28 |
+ |
import java.io.IOException; |
29 |
+ |
import java.io.ObjectInputStream; |
30 |
+ |
import java.io.StreamCorruptedException; |
31 |
|
import java.util.function.Consumer; |
32 |
|
import java.util.function.Predicate; |
33 |
|
import java.util.function.UnaryOperator; |
73 |
|
* |
74 |
|
* <p>As of the Java 2 platform v1.2, this class was retrofitted to |
75 |
|
* implement the {@link List} interface, making it a member of the |
76 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
76 |
> |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
77 |
|
* Java Collections Framework</a>. Unlike the new collection |
78 |
|
* implementations, {@code Vector} is synchronized. If a thread-safe |
79 |
|
* implementation is not needed, it is recommended to use {@link |
309 |
|
modCount++; |
310 |
|
if (newSize > elementData.length) |
311 |
|
grow(newSize); |
312 |
< |
for (int i = newSize; i < elementCount; i++) |
313 |
< |
elementData[i] = null; |
312 |
> |
final Object[] es = elementData; |
313 |
> |
for (int to = elementCount, i = newSize; i < to; i++) |
314 |
> |
es[i] = null; |
315 |
|
elementCount = newSize; |
316 |
|
} |
317 |
|
|
680 |
|
* method (which is part of the {@link List} interface). |
681 |
|
*/ |
682 |
|
public synchronized void removeAllElements() { |
683 |
< |
Arrays.fill(elementData, 0, elementCount, null); |
683 |
> |
final Object[] es = elementData; |
684 |
> |
for (int to = elementCount, i = elementCount = 0; i < to; i++) |
685 |
> |
es[i] = null; |
686 |
|
modCount++; |
681 |
– |
elementCount = 0; |
687 |
|
} |
688 |
|
|
689 |
|
/** |
989 |
|
return bulkRemove(e -> !c.contains(e)); |
990 |
|
} |
991 |
|
|
992 |
+ |
/** |
993 |
+ |
* @throws NullPointerException {@inheritDoc} |
994 |
+ |
*/ |
995 |
|
@Override |
996 |
|
public boolean removeIf(Predicate<? super E> filter) { |
997 |
|
Objects.requireNonNull(filter); |
1030 |
|
setBit(deathRow, i - beg); |
1031 |
|
if (modCount != expectedModCount) |
1032 |
|
throw new ConcurrentModificationException(); |
1025 |
– |
expectedModCount++; |
1033 |
|
modCount++; |
1034 |
|
int w = beg; |
1035 |
|
for (i = beg; i < end; i++) |
1036 |
|
if (isClear(deathRow, i - beg)) |
1037 |
|
es[w++] = es[i]; |
1038 |
< |
Arrays.fill(es, elementCount = w, end, null); |
1038 |
> |
for (i = elementCount = w; i < end; i++) |
1039 |
> |
es[i] = null; |
1040 |
|
// checkInvariants(); |
1041 |
|
return true; |
1042 |
|
} else { |
1167 |
|
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
1168 |
|
*/ |
1169 |
|
protected synchronized void removeRange(int fromIndex, int toIndex) { |
1162 |
– |
final Object[] es = elementData; |
1163 |
– |
final int oldSize = elementCount; |
1164 |
– |
System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex); |
1165 |
– |
|
1170 |
|
modCount++; |
1171 |
< |
Arrays.fill(es, elementCount -= (toIndex - fromIndex), oldSize, null); |
1171 |
> |
shiftTailOverGap(elementData, fromIndex, toIndex); |
1172 |
|
// checkInvariants(); |
1173 |
|
} |
1174 |
|
|
1175 |
+ |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
1176 |
+ |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
1177 |
+ |
System.arraycopy(es, hi, es, lo, elementCount - hi); |
1178 |
+ |
for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++) |
1179 |
+ |
es[i] = null; |
1180 |
+ |
} |
1181 |
+ |
|
1182 |
|
/** |
1183 |
< |
* Save the state of the {@code Vector} instance to a stream (that |
1184 |
< |
* is, serialize it). |
1183 |
> |
* Loads a {@code Vector} instance from a stream |
1184 |
> |
* (that is, deserializes it). |
1185 |
> |
* This method performs checks to ensure the consistency |
1186 |
> |
* of the fields. |
1187 |
> |
* |
1188 |
> |
* @param in the stream |
1189 |
> |
* @throws java.io.IOException if an I/O error occurs |
1190 |
> |
* @throws ClassNotFoundException if the stream contains data |
1191 |
> |
* of a non-existing class |
1192 |
> |
*/ |
1193 |
> |
private void readObject(ObjectInputStream in) |
1194 |
> |
throws IOException, ClassNotFoundException { |
1195 |
> |
ObjectInputStream.GetField gfields = in.readFields(); |
1196 |
> |
int count = gfields.get("elementCount", 0); |
1197 |
> |
Object[] data = (Object[])gfields.get("elementData", null); |
1198 |
> |
if (count < 0 || data == null || count > data.length) { |
1199 |
> |
throw new StreamCorruptedException("Inconsistent vector internals"); |
1200 |
> |
} |
1201 |
> |
elementCount = count; |
1202 |
> |
elementData = data.clone(); |
1203 |
> |
} |
1204 |
> |
|
1205 |
> |
/** |
1206 |
> |
* Saves the state of the {@code Vector} instance to a stream |
1207 |
> |
* (that is, serializes it). |
1208 |
|
* This method performs synchronization to ensure the consistency |
1209 |
|
* of the serialized data. |
1210 |
+ |
* |
1211 |
+ |
* @param s the stream |
1212 |
+ |
* @throws java.io.IOException if an I/O error occurs |
1213 |
|
*/ |
1214 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1215 |
|
throws java.io.IOException { |
1382 |
|
} |
1383 |
|
} |
1384 |
|
|
1385 |
+ |
/** |
1386 |
+ |
* @throws NullPointerException {@inheritDoc} |
1387 |
+ |
*/ |
1388 |
|
@Override |
1389 |
|
public synchronized void forEach(Consumer<? super E> action) { |
1390 |
|
Objects.requireNonNull(action); |
1398 |
|
// checkInvariants(); |
1399 |
|
} |
1400 |
|
|
1401 |
+ |
/** |
1402 |
+ |
* @throws NullPointerException {@inheritDoc} |
1403 |
+ |
*/ |
1404 |
|
@Override |
1405 |
|
public synchronized void replaceAll(UnaryOperator<E> operator) { |
1406 |
|
Objects.requireNonNull(operator); |
1411 |
|
es[i] = operator.apply(elementAt(es, i)); |
1412 |
|
if (modCount != expectedModCount) |
1413 |
|
throw new ConcurrentModificationException(); |
1371 |
– |
modCount++; |
1414 |
|
// checkInvariants(); |
1415 |
|
} |
1416 |
|
|
1450 |
|
private int fence; // -1 until used; then one past last index |
1451 |
|
private int expectedModCount; // initialized when fence set |
1452 |
|
|
1453 |
< |
/** Create new spliterator covering the given range */ |
1453 |
> |
/** Creates new spliterator covering the given range. */ |
1454 |
|
VectorSpliterator(Object[] array, int origin, int fence, |
1455 |
|
int expectedModCount) { |
1456 |
|
this.array = array; |
1479 |
|
|
1480 |
|
@SuppressWarnings("unchecked") |
1481 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1482 |
+ |
Objects.requireNonNull(action); |
1483 |
|
int i; |
1441 |
– |
if (action == null) |
1442 |
– |
throw new NullPointerException(); |
1484 |
|
if (getFence() > (i = index)) { |
1485 |
|
index = i + 1; |
1486 |
|
action.accept((E)array[i]); |
1493 |
|
|
1494 |
|
@SuppressWarnings("unchecked") |
1495 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1496 |
< |
int i, hi; // hoist accesses and checks from loop |
1497 |
< |
Object[] a; |
1498 |
< |
if (action == null) |
1499 |
< |
throw new NullPointerException(); |
1500 |
< |
if ((hi = fence) < 0) { |
1501 |
< |
synchronized (Vector.this) { |
1502 |
< |
expectedModCount = modCount; |
1503 |
< |
a = array = elementData; |
1463 |
< |
hi = fence = elementCount; |
1464 |
< |
} |
1465 |
< |
} |
1466 |
< |
else |
1467 |
< |
a = array; |
1468 |
< |
if (a != null && (i = index) >= 0 && (index = hi) <= a.length) { |
1469 |
< |
while (i < hi) |
1470 |
< |
action.accept((E) a[i++]); |
1471 |
< |
if (modCount == expectedModCount) |
1472 |
< |
return; |
1473 |
< |
} |
1474 |
< |
throw new ConcurrentModificationException(); |
1496 |
> |
Objects.requireNonNull(action); |
1497 |
> |
final int hi = getFence(); |
1498 |
> |
final Object[] a = array; |
1499 |
> |
int i; |
1500 |
> |
for (i = index, index = hi; i < hi; i++) |
1501 |
> |
action.accept((E) a[i]); |
1502 |
> |
if (modCount != expectedModCount) |
1503 |
> |
throw new ConcurrentModificationException(); |
1504 |
|
} |
1505 |
|
|
1506 |
|
public long estimateSize() { |