63 |
|
* other. We also guarantee that all array cells not holding |
64 |
|
* deque elements are always null. |
65 |
|
*/ |
66 |
< |
private transient E[] elements; |
66 |
> |
private transient Object[] elements; |
67 |
|
|
68 |
|
/** |
69 |
|
* The index of the element at the head of the deque (which is the |
107 |
|
if (initialCapacity < 0) // Too many elements, must back off |
108 |
|
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements |
109 |
|
} |
110 |
< |
elements = (E[]) new Object[initialCapacity]; |
110 |
> |
elements = new Object[initialCapacity]; |
111 |
|
} |
112 |
|
|
113 |
|
/** |
125 |
|
Object[] a = new Object[newCapacity]; |
126 |
|
System.arraycopy(elements, p, a, 0, r); |
127 |
|
System.arraycopy(elements, 0, a, r, p); |
128 |
< |
elements = (E[])a; |
128 |
> |
elements = a; |
129 |
|
head = 0; |
130 |
|
tail = n; |
131 |
|
} |
153 |
|
* sufficient to hold 16 elements. |
154 |
|
*/ |
155 |
|
public ArrayDeque() { |
156 |
< |
elements = (E[]) new Object[16]; |
156 |
> |
elements = new Object[16]; |
157 |
|
} |
158 |
|
|
159 |
|
/** |
261 |
|
|
262 |
|
public E pollFirst() { |
263 |
|
int h = head; |
264 |
< |
E result = elements[h]; // Element is null if deque empty |
264 |
> |
@SuppressWarnings("unchecked") E result = (E) elements[h]; |
265 |
> |
// Element is null if deque empty |
266 |
|
if (result == null) |
267 |
|
return null; |
268 |
|
elements[h] = null; // Must null out slot |
272 |
|
|
273 |
|
public E pollLast() { |
274 |
|
int t = (tail - 1) & (elements.length - 1); |
275 |
< |
E result = elements[t]; |
275 |
> |
@SuppressWarnings("unchecked") E result = (E) elements[t]; |
276 |
|
if (result == null) |
277 |
|
return null; |
278 |
|
elements[t] = null; |
284 |
|
* @throws NoSuchElementException {@inheritDoc} |
285 |
|
*/ |
286 |
|
public E getFirst() { |
287 |
< |
E x = elements[head]; |
288 |
< |
if (x == null) |
287 |
> |
@SuppressWarnings("unchecked") E result = (E) elements[head]; |
288 |
> |
if (result == null) |
289 |
|
throw new NoSuchElementException(); |
290 |
< |
return x; |
290 |
> |
return result; |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
|
* @throws NoSuchElementException {@inheritDoc} |
295 |
|
*/ |
296 |
|
public E getLast() { |
297 |
< |
E x = elements[(tail - 1) & (elements.length - 1)]; |
298 |
< |
if (x == null) |
297 |
> |
@SuppressWarnings("unchecked") |
298 |
> |
E result = (E) elements[(tail - 1) & (elements.length - 1)]; |
299 |
> |
if (result == null) |
300 |
|
throw new NoSuchElementException(); |
301 |
< |
return x; |
301 |
> |
return result; |
302 |
|
} |
303 |
|
|
304 |
|
public E peekFirst() { |
305 |
< |
return elements[head]; // elements[head] is null if deque empty |
305 |
> |
@SuppressWarnings("unchecked") E result = (E) elements[head]; |
306 |
> |
// elements[head] is null if deque empty |
307 |
> |
return result; |
308 |
|
} |
309 |
|
|
310 |
|
public E peekLast() { |
311 |
< |
return elements[(tail - 1) & (elements.length - 1)]; |
311 |
> |
@SuppressWarnings("unchecked") |
312 |
> |
E result = (E) elements[(tail - 1) & (elements.length - 1)]; |
313 |
> |
return result; |
314 |
|
} |
315 |
|
|
316 |
|
/** |
330 |
|
return false; |
331 |
|
int mask = elements.length - 1; |
332 |
|
int i = head; |
333 |
< |
E x; |
333 |
> |
Object x; |
334 |
|
while ( (x = elements[i]) != null) { |
335 |
|
if (o.equals(x)) { |
336 |
|
delete(i); |
358 |
|
return false; |
359 |
|
int mask = elements.length - 1; |
360 |
|
int i = (tail - 1) & mask; |
361 |
< |
E x; |
361 |
> |
Object x; |
362 |
|
while ( (x = elements[i]) != null) { |
363 |
|
if (o.equals(x)) { |
364 |
|
delete(i); |
503 |
|
*/ |
504 |
|
private boolean delete(int i) { |
505 |
|
checkInvariants(); |
506 |
< |
final E[] elements = this.elements; |
506 |
> |
final Object[] elements = this.elements; |
507 |
|
final int mask = elements.length - 1; |
508 |
|
final int h = head; |
509 |
|
final int t = tail; |
601 |
|
public E next() { |
602 |
|
if (cursor == fence) |
603 |
|
throw new NoSuchElementException(); |
604 |
< |
E result = elements[cursor]; |
604 |
> |
@SuppressWarnings("unchecked") E result = (E) elements[cursor]; |
605 |
|
// This check doesn't catch all possible comodifications, |
606 |
|
// but does catch the ones that corrupt traversal |
607 |
|
if (tail != fence || result == null) |
640 |
|
if (cursor == fence) |
641 |
|
throw new NoSuchElementException(); |
642 |
|
cursor = (cursor - 1) & (elements.length - 1); |
643 |
< |
E result = elements[cursor]; |
643 |
> |
@SuppressWarnings("unchecked") E result = (E) elements[cursor]; |
644 |
|
if (head != fence || result == null) |
645 |
|
throw new ConcurrentModificationException(); |
646 |
|
lastRet = cursor; |
671 |
|
return false; |
672 |
|
int mask = elements.length - 1; |
673 |
|
int i = head; |
674 |
< |
E x; |
674 |
> |
Object x; |
675 |
|
while ( (x = elements[i]) != null) { |
676 |
|
if (o.equals(x)) |
677 |
|
return true; |
768 |
|
* this deque |
769 |
|
* @throws NullPointerException if the specified array is null |
770 |
|
*/ |
771 |
+ |
@SuppressWarnings("unchecked") |
772 |
|
public <T> T[] toArray(T[] a) { |
773 |
|
int size = size(); |
774 |
|
if (a.length < size) |
789 |
|
*/ |
790 |
|
public ArrayDeque<E> clone() { |
791 |
|
try { |
792 |
+ |
@SuppressWarnings("unchecked") |
793 |
|
ArrayDeque<E> result = (ArrayDeque<E>) super.clone(); |
794 |
|
result.elements = Arrays.copyOf(elements, elements.length); |
795 |
|
return result; |
839 |
|
|
840 |
|
// Read in all elements in the proper order. |
841 |
|
for (int i = 0; i < size; i++) |
842 |
< |
elements[i] = (E)s.readObject(); |
842 |
> |
elements[i] = s.readObject(); |
843 |
|
} |
844 |
|
} |