1169 |
|
} |
1170 |
|
|
1171 |
|
/** |
1172 |
< |
* Save the state of the {@code Vector} instance to a stream (that |
1173 |
< |
* is, serialize it). |
1172 |
> |
* Saves the state of the {@code Vector} instance to a stream |
1173 |
> |
* (that is, serializes it). |
1174 |
|
* This method performs synchronization to ensure the consistency |
1175 |
|
* of the serialized data. |
1176 |
+ |
* |
1177 |
+ |
* @param s the stream |
1178 |
+ |
* @throws java.io.IOException if an I/O error occurs |
1179 |
|
*/ |
1180 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1181 |
|
throws java.io.IOException { |
1401 |
|
*/ |
1402 |
|
@Override |
1403 |
|
public Spliterator<E> spliterator() { |
1404 |
< |
return new VectorSpliterator<>(this, null, 0, -1, 0); |
1404 |
> |
return new VectorSpliterator(null, 0, -1, 0); |
1405 |
|
} |
1406 |
|
|
1407 |
|
/** Similar to ArrayList Spliterator */ |
1408 |
< |
static final class VectorSpliterator<E> implements Spliterator<E> { |
1406 |
< |
private final Vector<E> list; |
1408 |
> |
final class VectorSpliterator implements Spliterator<E> { |
1409 |
|
private Object[] array; |
1410 |
|
private int index; // current index, modified on advance/split |
1411 |
|
private int fence; // -1 until used; then one past last index |
1412 |
|
private int expectedModCount; // initialized when fence set |
1413 |
|
|
1414 |
|
/** Create new spliterator covering the given range */ |
1415 |
< |
VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence, |
1415 |
> |
VectorSpliterator(Object[] array, int origin, int fence, |
1416 |
|
int expectedModCount) { |
1415 |
– |
this.list = list; |
1417 |
|
this.array = array; |
1418 |
|
this.index = origin; |
1419 |
|
this.fence = fence; |
1423 |
|
private int getFence() { // initialize on first use |
1424 |
|
int hi; |
1425 |
|
if ((hi = fence) < 0) { |
1426 |
< |
synchronized (list) { |
1427 |
< |
array = list.elementData; |
1428 |
< |
expectedModCount = list.modCount; |
1429 |
< |
hi = fence = list.elementCount; |
1426 |
> |
synchronized (Vector.this) { |
1427 |
> |
array = elementData; |
1428 |
> |
expectedModCount = modCount; |
1429 |
> |
hi = fence = elementCount; |
1430 |
|
} |
1431 |
|
} |
1432 |
|
return hi; |
1435 |
|
public Spliterator<E> trySplit() { |
1436 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1437 |
|
return (lo >= mid) ? null : |
1438 |
< |
new VectorSpliterator<>(list, array, lo, index = mid, |
1438 |
< |
expectedModCount); |
1438 |
> |
new VectorSpliterator(array, lo, index = mid, expectedModCount); |
1439 |
|
} |
1440 |
|
|
1441 |
|
@SuppressWarnings("unchecked") |
1446 |
|
if (getFence() > (i = index)) { |
1447 |
|
index = i + 1; |
1448 |
|
action.accept((E)array[i]); |
1449 |
< |
if (list.modCount != expectedModCount) |
1449 |
> |
if (modCount != expectedModCount) |
1450 |
|
throw new ConcurrentModificationException(); |
1451 |
|
return true; |
1452 |
|
} |
1455 |
|
|
1456 |
|
@SuppressWarnings("unchecked") |
1457 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1458 |
– |
int i, hi; // hoist accesses and checks from loop |
1459 |
– |
Vector<E> lst; Object[] a; |
1458 |
|
if (action == null) |
1459 |
|
throw new NullPointerException(); |
1460 |
< |
if ((lst = list) != null) { |
1461 |
< |
if ((hi = fence) < 0) { |
1462 |
< |
synchronized (lst) { |
1463 |
< |
expectedModCount = lst.modCount; |
1464 |
< |
a = array = lst.elementData; |
1465 |
< |
hi = fence = lst.elementCount; |
1466 |
< |
} |
1469 |
< |
} |
1470 |
< |
else |
1471 |
< |
a = array; |
1472 |
< |
if (a != null && (i = index) >= 0 && (index = hi) <= a.length) { |
1473 |
< |
while (i < hi) |
1474 |
< |
action.accept((E) a[i++]); |
1475 |
< |
if (lst.modCount == expectedModCount) |
1476 |
< |
return; |
1477 |
< |
} |
1478 |
< |
} |
1479 |
< |
throw new ConcurrentModificationException(); |
1460 |
> |
final int hi = getFence(); |
1461 |
> |
final Object[] a = array; |
1462 |
> |
int i; |
1463 |
> |
for (i = index, index = hi; i < hi; i++) |
1464 |
> |
action.accept((E) a[i]); |
1465 |
> |
if (modCount != expectedModCount) |
1466 |
> |
throw new ConcurrentModificationException(); |
1467 |
|
} |
1468 |
|
|
1469 |
|
public long estimateSize() { |