187 |
|
Object[] elements = c.toArray(); |
188 |
|
// defend against c.toArray (incorrectly) not returning Object[] |
189 |
|
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
190 |
+ |
size = elements.length; |
191 |
|
if (elements.getClass() != Object[].class) |
192 |
|
elements = Arrays.copyOf(elements, size, Object[].class); |
193 |
|
for (Object obj : elements) |
194 |
|
Objects.requireNonNull(obj); |
194 |
– |
size = elements.length; |
195 |
|
this.elements = elements; |
196 |
|
} |
197 |
|
|
365 |
|
*/ |
366 |
|
public E removeFirst() { |
367 |
|
// checkInvariants(); |
368 |
< |
E x = pollFirst(); |
369 |
< |
if (x == null) |
368 |
> |
E e = pollFirst(); |
369 |
> |
if (e == null) |
370 |
|
throw new NoSuchElementException(); |
371 |
< |
return x; |
371 |
> |
return e; |
372 |
|
} |
373 |
|
|
374 |
|
/** |
376 |
|
*/ |
377 |
|
public E removeLast() { |
378 |
|
// checkInvariants(); |
379 |
< |
E x = pollLast(); |
380 |
< |
if (x == null) |
379 |
> |
E e = pollLast(); |
380 |
> |
if (e == null) |
381 |
|
throw new NoSuchElementException(); |
382 |
< |
return x; |
382 |
> |
return e; |
383 |
|
} |
384 |
|
|
385 |
|
public E pollFirst() { |
983 |
|
*/ |
984 |
|
public void clear() { |
985 |
|
final Object[] elements = this.elements; |
986 |
< |
final int capacity = elements.length; |
987 |
< |
final int h = this.head; |
988 |
< |
final int s = size; |
989 |
< |
if (capacity - h >= s) |
990 |
< |
Arrays.fill(elements, h, h + s, null); |
986 |
> |
final int capacity = elements.length, tail = head + size; |
987 |
> |
if (capacity - tail >= 0) |
988 |
> |
Arrays.fill(elements, head, tail, null); |
989 |
|
else { |
990 |
< |
Arrays.fill(elements, h, capacity, null); |
991 |
< |
Arrays.fill(elements, 0, s - capacity + h, null); |
990 |
> |
Arrays.fill(elements, head, capacity, null); |
991 |
> |
Arrays.fill(elements, 0, tail - capacity, null); |
992 |
|
} |
993 |
|
size = head = 0; |
994 |
|
// checkInvariants(); |
1008 |
|
* @return an array containing all of the elements in this deque |
1009 |
|
*/ |
1010 |
|
public Object[] toArray() { |
1011 |
< |
final int head = this.head; |
1012 |
< |
final int firstLeg; |
1013 |
< |
Object[] a = Arrays.copyOfRange(elements, head, head + size); |
1014 |
< |
if ((firstLeg = elements.length - head) < size) |
1015 |
< |
System.arraycopy(elements, 0, a, firstLeg, size - firstLeg); |
1011 |
> |
return toArray(Object[].class); |
1012 |
> |
} |
1013 |
> |
|
1014 |
> |
private <T> T[] toArray(Class<T[]> klazz) { |
1015 |
> |
final Object[] elements = this.elements; |
1016 |
> |
final int capacity = elements.length; |
1017 |
> |
final int head = this.head, tail = head + size; |
1018 |
> |
final T[] a; |
1019 |
> |
if (tail >= 0) { |
1020 |
> |
a = Arrays.copyOfRange(elements, head, tail, klazz); |
1021 |
> |
} else { |
1022 |
> |
// integer overflow! |
1023 |
> |
a = Arrays.copyOfRange(elements, 0, size, klazz); |
1024 |
> |
System.arraycopy(elements, head, a, 0, capacity - head); |
1025 |
> |
} |
1026 |
> |
if (tail - capacity > 0) |
1027 |
> |
System.arraycopy(elements, 0, a, capacity - head, tail - capacity); |
1028 |
|
return a; |
1029 |
|
} |
1030 |
|
|
1066 |
|
*/ |
1067 |
|
@SuppressWarnings("unchecked") |
1068 |
|
public <T> T[] toArray(T[] a) { |
1069 |
+ |
final int size = this.size; |
1070 |
+ |
if (size > a.length) |
1071 |
+ |
return toArray((Class<T[]>) a.getClass()); |
1072 |
|
final Object[] elements = this.elements; |
1073 |
< |
final int head = this.head; |
1074 |
< |
final int firstLeg; |
1075 |
< |
boolean wrap = (firstLeg = elements.length - head) < size; |
1076 |
< |
if (size > a.length) { |
1077 |
< |
a = (T[]) Arrays.copyOfRange(elements, head, head + size, |
1078 |
< |
a.getClass()); |
1079 |
< |
} else { |
1067 |
< |
System.arraycopy(elements, head, a, 0, wrap ? firstLeg : size); |
1068 |
< |
if (size < a.length) |
1069 |
< |
a[size] = null; |
1073 |
> |
final int capacity = elements.length; |
1074 |
> |
final int head = this.head, tail = head + size; |
1075 |
> |
if (capacity - tail >= 0) |
1076 |
> |
System.arraycopy(elements, head, a, 0, size); |
1077 |
> |
else { |
1078 |
> |
System.arraycopy(elements, head, a, 0, capacity - head); |
1079 |
> |
System.arraycopy(elements, 0, a, capacity - head, tail - capacity); |
1080 |
|
} |
1081 |
< |
if (wrap) |
1082 |
< |
System.arraycopy(elements, 0, a, firstLeg, size - firstLeg); |
1081 |
> |
if (size < a.length) |
1082 |
> |
a[size] = null; |
1083 |
|
return a; |
1084 |
|
} |
1085 |
|
|