1 |
|
/* |
2 |
< |
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 1997, 2019, 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 |
28 |
|
import java.util.function.Consumer; |
29 |
|
import java.util.function.Predicate; |
30 |
|
import java.util.function.UnaryOperator; |
31 |
< |
import jdk.internal.misc.SharedSecrets; |
31 |
> |
// OPENJDK import jdk.internal.access.SharedSecrets; |
32 |
> |
import jdk.internal.util.ArraysSupport; |
33 |
|
|
34 |
|
/** |
35 |
|
* Resizable-array implementation of the {@code List} interface. Implements |
93 |
|
* should be used only to detect bugs.</i> |
94 |
|
* |
95 |
|
* <p>This class is a member of the |
96 |
< |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
96 |
> |
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> |
97 |
|
* Java Collections Framework</a>. |
98 |
|
* |
99 |
|
* @param <E> the type of elements in this list |
220 |
|
} |
221 |
|
|
222 |
|
/** |
222 |
– |
* The maximum size of array to allocate (unless necessary). |
223 |
– |
* Some VMs reserve some header words in an array. |
224 |
– |
* Attempts to allocate larger arrays may result in |
225 |
– |
* OutOfMemoryError: Requested array size exceeds VM limit |
226 |
– |
*/ |
227 |
– |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
228 |
– |
|
229 |
– |
/** |
223 |
|
* Increases the capacity to ensure that it can hold at least the |
224 |
|
* number of elements specified by the minimum capacity argument. |
225 |
|
* |
227 |
|
* @throws OutOfMemoryError if minCapacity is less than zero |
228 |
|
*/ |
229 |
|
private Object[] grow(int minCapacity) { |
230 |
< |
return elementData = Arrays.copyOf(elementData, |
231 |
< |
newCapacity(minCapacity)); |
230 |
> |
int oldCapacity = elementData.length; |
231 |
> |
if (oldCapacity > 0 || elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { |
232 |
> |
int newCapacity = ArraysSupport.newLength(oldCapacity, |
233 |
> |
minCapacity - oldCapacity, /* minimum growth */ |
234 |
> |
oldCapacity >> 1 /* preferred growth */); |
235 |
> |
return elementData = Arrays.copyOf(elementData, newCapacity); |
236 |
> |
} else { |
237 |
> |
return elementData = new Object[Math.max(DEFAULT_CAPACITY, minCapacity)]; |
238 |
> |
} |
239 |
|
} |
240 |
|
|
241 |
|
private Object[] grow() { |
243 |
|
} |
244 |
|
|
245 |
|
/** |
246 |
– |
* Returns a capacity at least as large as the given minimum capacity. |
247 |
– |
* Returns the current capacity increased by 50% if that suffices. |
248 |
– |
* Will not return a capacity greater than MAX_ARRAY_SIZE unless |
249 |
– |
* the given minimum capacity is greater than MAX_ARRAY_SIZE. |
250 |
– |
* |
251 |
– |
* @param minCapacity the desired minimum capacity |
252 |
– |
* @throws OutOfMemoryError if minCapacity is less than zero |
253 |
– |
*/ |
254 |
– |
private int newCapacity(int minCapacity) { |
255 |
– |
// overflow-conscious code |
256 |
– |
int oldCapacity = elementData.length; |
257 |
– |
int newCapacity = oldCapacity + (oldCapacity >> 1); |
258 |
– |
if (newCapacity - minCapacity <= 0) { |
259 |
– |
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) |
260 |
– |
return Math.max(DEFAULT_CAPACITY, minCapacity); |
261 |
– |
if (minCapacity < 0) // overflow |
262 |
– |
throw new OutOfMemoryError(); |
263 |
– |
return minCapacity; |
264 |
– |
} |
265 |
– |
return (newCapacity - MAX_ARRAY_SIZE <= 0) |
266 |
– |
? newCapacity |
267 |
– |
: hugeCapacity(minCapacity); |
268 |
– |
} |
269 |
– |
|
270 |
– |
private static int hugeCapacity(int minCapacity) { |
271 |
– |
if (minCapacity < 0) // overflow |
272 |
– |
throw new OutOfMemoryError(); |
273 |
– |
return (minCapacity > MAX_ARRAY_SIZE) |
274 |
– |
? Integer.MAX_VALUE |
275 |
– |
: MAX_ARRAY_SIZE; |
276 |
– |
} |
277 |
– |
|
278 |
– |
/** |
246 |
|
* Returns the number of elements in this list. |
247 |
|
* |
248 |
|
* @return the number of elements in this list |
281 |
|
* or -1 if there is no such index. |
282 |
|
*/ |
283 |
|
public int indexOf(Object o) { |
284 |
+ |
return indexOfRange(o, 0, size); |
285 |
+ |
} |
286 |
+ |
|
287 |
+ |
int indexOfRange(Object o, int start, int end) { |
288 |
+ |
Object[] es = elementData; |
289 |
|
if (o == null) { |
290 |
< |
for (int i = 0; i < size; i++) |
291 |
< |
if (elementData[i]==null) |
290 |
> |
for (int i = start; i < end; i++) { |
291 |
> |
if (es[i] == null) { |
292 |
|
return i; |
293 |
+ |
} |
294 |
+ |
} |
295 |
|
} else { |
296 |
< |
for (int i = 0; i < size; i++) |
297 |
< |
if (o.equals(elementData[i])) |
296 |
> |
for (int i = start; i < end; i++) { |
297 |
> |
if (o.equals(es[i])) { |
298 |
|
return i; |
299 |
+ |
} |
300 |
+ |
} |
301 |
|
} |
302 |
|
return -1; |
303 |
|
} |
310 |
|
* or -1 if there is no such index. |
311 |
|
*/ |
312 |
|
public int lastIndexOf(Object o) { |
313 |
+ |
return lastIndexOfRange(o, 0, size); |
314 |
+ |
} |
315 |
+ |
|
316 |
+ |
int lastIndexOfRange(Object o, int start, int end) { |
317 |
+ |
Object[] es = elementData; |
318 |
|
if (o == null) { |
319 |
< |
for (int i = size-1; i >= 0; i--) |
320 |
< |
if (elementData[i]==null) |
319 |
> |
for (int i = end - 1; i >= start; i--) { |
320 |
> |
if (es[i] == null) { |
321 |
|
return i; |
322 |
+ |
} |
323 |
+ |
} |
324 |
|
} else { |
325 |
< |
for (int i = size-1; i >= 0; i--) |
326 |
< |
if (o.equals(elementData[i])) |
325 |
> |
for (int i = end - 1; i >= start; i--) { |
326 |
> |
if (o.equals(es[i])) { |
327 |
|
return i; |
328 |
+ |
} |
329 |
+ |
} |
330 |
|
} |
331 |
|
return -1; |
332 |
|
} |
511 |
|
} |
512 |
|
|
513 |
|
/** |
514 |
+ |
* {@inheritDoc} |
515 |
+ |
*/ |
516 |
+ |
public boolean equals(Object o) { |
517 |
+ |
if (o == this) { |
518 |
+ |
return true; |
519 |
+ |
} |
520 |
+ |
|
521 |
+ |
if (!(o instanceof List)) { |
522 |
+ |
return false; |
523 |
+ |
} |
524 |
+ |
|
525 |
+ |
final int expectedModCount = modCount; |
526 |
+ |
// ArrayList can be subclassed and given arbitrary behavior, but we can |
527 |
+ |
// still deal with the common case where o is ArrayList precisely |
528 |
+ |
boolean equal = (o.getClass() == ArrayList.class) |
529 |
+ |
? equalsArrayList((ArrayList<?>) o) |
530 |
+ |
: equalsRange((List<?>) o, 0, size); |
531 |
+ |
|
532 |
+ |
checkForComodification(expectedModCount); |
533 |
+ |
return equal; |
534 |
+ |
} |
535 |
+ |
|
536 |
+ |
boolean equalsRange(List<?> other, int from, int to) { |
537 |
+ |
final Object[] es = elementData; |
538 |
+ |
if (to > es.length) { |
539 |
+ |
throw new ConcurrentModificationException(); |
540 |
+ |
} |
541 |
+ |
var oit = other.iterator(); |
542 |
+ |
for (; from < to; from++) { |
543 |
+ |
if (!oit.hasNext() || !Objects.equals(es[from], oit.next())) { |
544 |
+ |
return false; |
545 |
+ |
} |
546 |
+ |
} |
547 |
+ |
return !oit.hasNext(); |
548 |
+ |
} |
549 |
+ |
|
550 |
+ |
private boolean equalsArrayList(ArrayList<?> other) { |
551 |
+ |
final int otherModCount = other.modCount; |
552 |
+ |
final int s = size; |
553 |
+ |
boolean equal; |
554 |
+ |
if (equal = (s == other.size)) { |
555 |
+ |
final Object[] otherEs = other.elementData; |
556 |
+ |
final Object[] es = elementData; |
557 |
+ |
if (s > es.length || s > otherEs.length) { |
558 |
+ |
throw new ConcurrentModificationException(); |
559 |
+ |
} |
560 |
+ |
for (int i = 0; i < s; i++) { |
561 |
+ |
if (!Objects.equals(es[i], otherEs[i])) { |
562 |
+ |
equal = false; |
563 |
+ |
break; |
564 |
+ |
} |
565 |
+ |
} |
566 |
+ |
} |
567 |
+ |
other.checkForComodification(otherModCount); |
568 |
+ |
return equal; |
569 |
+ |
} |
570 |
+ |
|
571 |
+ |
private void checkForComodification(final int expectedModCount) { |
572 |
+ |
if (modCount != expectedModCount) { |
573 |
+ |
throw new ConcurrentModificationException(); |
574 |
+ |
} |
575 |
+ |
} |
576 |
+ |
|
577 |
+ |
/** |
578 |
+ |
* {@inheritDoc} |
579 |
+ |
*/ |
580 |
+ |
public int hashCode() { |
581 |
+ |
int expectedModCount = modCount; |
582 |
+ |
int hash = hashCodeRange(0, size); |
583 |
+ |
checkForComodification(expectedModCount); |
584 |
+ |
return hash; |
585 |
+ |
} |
586 |
+ |
|
587 |
+ |
int hashCodeRange(int from, int to) { |
588 |
+ |
final Object[] es = elementData; |
589 |
+ |
if (to > es.length) { |
590 |
+ |
throw new ConcurrentModificationException(); |
591 |
+ |
} |
592 |
+ |
int hashCode = 1; |
593 |
+ |
for (int i = from; i < to; i++) { |
594 |
+ |
Object e = es[i]; |
595 |
+ |
hashCode = 31 * hashCode + (e == null ? 0 : e.hashCode()); |
596 |
+ |
} |
597 |
+ |
return hashCode; |
598 |
+ |
} |
599 |
+ |
|
600 |
+ |
/** |
601 |
|
* Removes the first occurrence of the specified element from this list, |
602 |
|
* if it is present. If the list does not contain the element, it is |
603 |
|
* unchanged. More formally, removes the element with the lowest index |
893 |
|
|
894 |
|
if (size > 0) { |
895 |
|
// like clone(), allocate array based upon size not capacity |
896 |
< |
SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size); |
896 |
> |
jsr166.Platform.checkArray(s, Object[].class, size); |
897 |
|
Object[] elements = new Object[size]; |
898 |
|
|
899 |
|
// Read in all elements in the proper order. |
1242 |
|
return a; |
1243 |
|
} |
1244 |
|
|
1245 |
+ |
public boolean equals(Object o) { |
1246 |
+ |
if (o == this) { |
1247 |
+ |
return true; |
1248 |
+ |
} |
1249 |
+ |
|
1250 |
+ |
if (!(o instanceof List)) { |
1251 |
+ |
return false; |
1252 |
+ |
} |
1253 |
+ |
|
1254 |
+ |
boolean equal = root.equalsRange((List<?>)o, offset, offset + size); |
1255 |
+ |
checkForComodification(); |
1256 |
+ |
return equal; |
1257 |
+ |
} |
1258 |
+ |
|
1259 |
+ |
public int hashCode() { |
1260 |
+ |
int hash = root.hashCodeRange(offset, offset + size); |
1261 |
+ |
checkForComodification(); |
1262 |
+ |
return hash; |
1263 |
+ |
} |
1264 |
+ |
|
1265 |
+ |
public int indexOf(Object o) { |
1266 |
+ |
int index = root.indexOfRange(o, offset, offset + size); |
1267 |
+ |
checkForComodification(); |
1268 |
+ |
return index >= 0 ? index - offset : -1; |
1269 |
+ |
} |
1270 |
+ |
|
1271 |
+ |
public int lastIndexOf(Object o) { |
1272 |
+ |
int index = root.lastIndexOfRange(o, offset, offset + size); |
1273 |
+ |
checkForComodification(); |
1274 |
+ |
return index >= 0 ? index - offset : -1; |
1275 |
+ |
} |
1276 |
+ |
|
1277 |
+ |
public boolean contains(Object o) { |
1278 |
+ |
return indexOf(o) >= 0; |
1279 |
+ |
} |
1280 |
+ |
|
1281 |
|
public Iterator<E> iterator() { |
1282 |
|
return listIterator(); |
1283 |
|
} |
1704 |
|
@Override |
1705 |
|
public void replaceAll(UnaryOperator<E> operator) { |
1706 |
|
replaceAllRange(operator, 0, size); |
1707 |
+ |
// TODO(8203662): remove increment of modCount from ... |
1708 |
+ |
modCount++; |
1709 |
|
} |
1710 |
|
|
1711 |
< |
private void replaceAllRange(UnaryOperator<E> operator, int from, int to) { |
1711 |
> |
private void replaceAllRange(UnaryOperator<E> operator, int i, int end) { |
1712 |
|
Objects.requireNonNull(operator); |
1713 |
|
final int expectedModCount = modCount; |
1714 |
|
final Object[] es = elementData; |
1715 |
< |
for (int i = from; modCount == expectedModCount && i < to; i++) |
1715 |
> |
for (; modCount == expectedModCount && i < end; i++) |
1716 |
|
es[i] = operator.apply(elementAt(es, i)); |
1717 |
|
if (modCount != expectedModCount) |
1718 |
|
throw new ConcurrentModificationException(); |