772 |
|
} |
773 |
|
|
774 |
|
/** |
775 |
< |
* Save the state of the {@code ArrayList} instance to a stream (that |
776 |
< |
* is, serialize it). |
775 |
> |
* Saves the state of the {@code ArrayList} instance to a stream |
776 |
> |
* (that is, serializes it). |
777 |
|
* |
778 |
+ |
* @param s the stream |
779 |
+ |
* @throws java.io.IOException if an I/O error occurs |
780 |
|
* @serialData The length of the array backing the {@code ArrayList} |
781 |
|
* instance is emitted (int), followed by all of its elements |
782 |
|
* (each an {@code Object}) in the proper order. |
783 |
|
*/ |
784 |
|
private void writeObject(java.io.ObjectOutputStream s) |
785 |
< |
throws java.io.IOException{ |
785 |
> |
throws java.io.IOException { |
786 |
|
// Write out element count, and any hidden stuff |
787 |
|
int expectedModCount = modCount; |
788 |
|
s.defaultWriteObject(); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
< |
* Reconstitute the {@code ArrayList} instance from a stream (that is, |
805 |
< |
* deserialize it). |
804 |
> |
* Reconstitutes the {@code ArrayList} instance from a stream (that is, |
805 |
> |
* deserializes it). |
806 |
> |
* @param s the stream |
807 |
> |
* @throws ClassNotFoundException if the class of a serialized object |
808 |
> |
* could not be found |
809 |
> |
* @throws java.io.IOException if an I/O error occurs |
810 |
|
*/ |
811 |
|
private void readObject(java.io.ObjectInputStream s) |
812 |
|
throws java.io.IOException, ClassNotFoundException { |
1297 |
|
public Spliterator<E> spliterator() { |
1298 |
|
checkForComodification(); |
1299 |
|
|
1300 |
< |
// ArrayListSpliterator is not used because late-binding logic |
1301 |
< |
// is different here |
1296 |
< |
return new Spliterator<>() { |
1300 |
> |
// ArrayListSpliterator not used here due to late-binding |
1301 |
> |
return new Spliterator<E>() { |
1302 |
|
private int index = offset; // current index, modified on advance/split |
1303 |
|
private int fence = -1; // -1 until used; then one past last index |
1304 |
|
private int expectedModCount; // initialized when fence set |
1312 |
|
return hi; |
1313 |
|
} |
1314 |
|
|
1315 |
< |
public ArrayListSpliterator<E> trySplit() { |
1315 |
> |
public ArrayList<E>.ArrayListSpliterator trySplit() { |
1316 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1317 |
< |
// ArrayListSpliterator could be used here as the source is already bound |
1317 |
> |
// ArrayListSpliterator can be used here as the source is already bound |
1318 |
|
return (lo >= mid) ? null : // divide range in half unless too small |
1319 |
< |
new ArrayListSpliterator<>(root, lo, index = mid, |
1315 |
< |
expectedModCount); |
1319 |
> |
root.new ArrayListSpliterator(lo, index = mid, expectedModCount); |
1320 |
|
} |
1321 |
|
|
1322 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1358 |
|
} |
1359 |
|
|
1360 |
|
public long estimateSize() { |
1361 |
< |
return (long) (getFence() - index); |
1361 |
> |
return getFence() - index; |
1362 |
|
} |
1363 |
|
|
1364 |
|
public int characteristics() { |
1395 |
|
*/ |
1396 |
|
@Override |
1397 |
|
public Spliterator<E> spliterator() { |
1398 |
< |
return new ArrayListSpliterator<>(this, 0, -1, 0); |
1398 |
> |
return new ArrayListSpliterator(0, -1, 0); |
1399 |
|
} |
1400 |
|
|
1401 |
|
/** Index-based split-by-two, lazily initialized Spliterator */ |
1402 |
< |
static final class ArrayListSpliterator<E> implements Spliterator<E> { |
1402 |
> |
final class ArrayListSpliterator implements Spliterator<E> { |
1403 |
|
|
1404 |
|
/* |
1405 |
|
* If ArrayLists were immutable, or structurally immutable (no |
1433 |
|
* these streamlinings. |
1434 |
|
*/ |
1435 |
|
|
1432 |
– |
private final ArrayList<E> list; |
1436 |
|
private int index; // current index, modified on advance/split |
1437 |
|
private int fence; // -1 until used; then one past last index |
1438 |
|
private int expectedModCount; // initialized when fence set |
1439 |
|
|
1440 |
|
/** Create new spliterator covering the given range */ |
1441 |
< |
ArrayListSpliterator(ArrayList<E> list, int origin, int fence, |
1439 |
< |
int expectedModCount) { |
1440 |
< |
this.list = list; // OK if null unless traversed |
1441 |
> |
ArrayListSpliterator(int origin, int fence, int expectedModCount) { |
1442 |
|
this.index = origin; |
1443 |
|
this.fence = fence; |
1444 |
|
this.expectedModCount = expectedModCount; |
1446 |
|
|
1447 |
|
private int getFence() { // initialize fence to size on first use |
1448 |
|
int hi; // (a specialized variant appears in method forEach) |
1448 |
– |
ArrayList<E> lst; |
1449 |
|
if ((hi = fence) < 0) { |
1450 |
< |
if ((lst = list) == null) |
1451 |
< |
hi = fence = 0; |
1452 |
< |
else { |
1453 |
< |
expectedModCount = lst.modCount; |
1454 |
< |
hi = fence = lst.size; |
1455 |
< |
} |
1450 |
> |
expectedModCount = modCount; |
1451 |
> |
hi = fence = size; |
1452 |
|
} |
1453 |
|
return hi; |
1454 |
|
} |
1455 |
|
|
1456 |
< |
public ArrayListSpliterator<E> trySplit() { |
1456 |
> |
public ArrayListSpliterator trySplit() { |
1457 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1458 |
|
return (lo >= mid) ? null : // divide range in half unless too small |
1459 |
< |
new ArrayListSpliterator<>(list, lo, index = mid, |
1464 |
< |
expectedModCount); |
1459 |
> |
new ArrayListSpliterator(lo, index = mid, expectedModCount); |
1460 |
|
} |
1461 |
|
|
1462 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1465 |
|
int hi = getFence(), i = index; |
1466 |
|
if (i < hi) { |
1467 |
|
index = i + 1; |
1468 |
< |
@SuppressWarnings("unchecked") E e = (E)list.elementData[i]; |
1468 |
> |
@SuppressWarnings("unchecked") E e = (E)elementData[i]; |
1469 |
|
action.accept(e); |
1470 |
< |
if (list.modCount != expectedModCount) |
1470 |
> |
if (modCount != expectedModCount) |
1471 |
|
throw new ConcurrentModificationException(); |
1472 |
|
return true; |
1473 |
|
} |
1476 |
|
|
1477 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1478 |
|
int i, hi, mc; // hoist accesses and checks from loop |
1479 |
< |
ArrayList<E> lst; Object[] a; |
1479 |
> |
Object[] a; |
1480 |
|
if (action == null) |
1481 |
|
throw new NullPointerException(); |
1482 |
< |
if ((lst = list) != null && (a = lst.elementData) != null) { |
1482 |
> |
if ((a = elementData) != null) { |
1483 |
|
if ((hi = fence) < 0) { |
1484 |
< |
mc = lst.modCount; |
1485 |
< |
hi = lst.size; |
1484 |
> |
mc = modCount; |
1485 |
> |
hi = size; |
1486 |
|
} |
1487 |
|
else |
1488 |
|
mc = expectedModCount; |
1491 |
|
@SuppressWarnings("unchecked") E e = (E) a[i]; |
1492 |
|
action.accept(e); |
1493 |
|
} |
1494 |
< |
if (lst.modCount == mc) |
1494 |
> |
if (modCount == mc) |
1495 |
|
return; |
1496 |
|
} |
1497 |
|
} |
1499 |
|
} |
1500 |
|
|
1501 |
|
public long estimateSize() { |
1502 |
< |
return (long) (getFence() - index); |
1502 |
> |
return getFence() - index; |
1503 |
|
} |
1504 |
|
|
1505 |
|
public int characteristics() { |