1 |
|
/* |
2 |
|
* Written by Josh Bloch of Google Inc. and released to the public domain, |
3 |
< |
* as explained at http://creativecommons.org/licenses/publicdomain. |
3 |
> |
* as explained at http://creativecommons.org/publicdomain/zero/1.0/. |
4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
< |
import java.io.*; |
7 |
> |
|
8 |
> |
import java.io.Serializable; |
9 |
> |
import java.util.function.Consumer; |
10 |
> |
import java.util.function.Predicate; |
11 |
> |
import java.util.function.UnaryOperator; |
12 |
|
|
13 |
|
/** |
14 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
19 |
|
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
20 |
|
* when used as a queue. |
21 |
|
* |
22 |
< |
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
23 |
< |
* Exceptions include {@link #remove(Object) remove}, {@link |
24 |
< |
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
25 |
< |
* removeLastOccurrence}, {@link #contains contains }, {@link #iterator |
26 |
< |
* iterator.remove()}, and the bulk operations, all of which run in linear |
27 |
< |
* time. |
22 |
> |
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
23 |
> |
* Exceptions include |
24 |
> |
* {@link #remove(Object) remove}, |
25 |
> |
* {@link #removeFirstOccurrence removeFirstOccurrence}, |
26 |
> |
* {@link #removeLastOccurrence removeLastOccurrence}, |
27 |
> |
* {@link #contains contains}, |
28 |
> |
* {@link #iterator iterator.remove()}, |
29 |
> |
* and the bulk operations, all of which run in linear time. |
30 |
|
* |
31 |
< |
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
32 |
< |
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
33 |
< |
* is created, in any way except through the iterator's own remove method, the |
34 |
< |
* iterator will generally throw a {@link ConcurrentModificationException}. |
35 |
< |
* Thus, in the face of concurrent modification, the iterator fails quickly |
36 |
< |
* and cleanly, rather than risking arbitrary, non-deterministic behavior at |
37 |
< |
* an undetermined time in the future. |
31 |
> |
* <p>The iterators returned by this class's {@link #iterator() iterator} |
32 |
> |
* method are <em>fail-fast</em>: If the deque is modified at any time after |
33 |
> |
* the iterator is created, in any way except through the iterator's own |
34 |
> |
* {@code remove} method, the iterator will generally throw a {@link |
35 |
> |
* ConcurrentModificationException}. Thus, in the face of concurrent |
36 |
> |
* modification, the iterator fails quickly and cleanly, rather than risking |
37 |
> |
* arbitrary, non-deterministic behavior at an undetermined time in the |
38 |
> |
* future. |
39 |
|
* |
40 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
41 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
42 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
43 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
43 |
> |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
44 |
|
* Therefore, it would be wrong to write a program that depended on this |
45 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
46 |
|
* should be used only to detect bugs.</i> |
47 |
|
* |
48 |
|
* <p>This class and its iterator implement all of the |
49 |
< |
* optional methods of the {@link Collection} and {@link |
50 |
< |
* Iterator} interfaces. This class is a member of the <a |
51 |
< |
* href="{@docRoot}/../guide/collections/index.html"> Java Collections |
52 |
< |
* Framework</a>. |
49 |
> |
* <em>optional</em> methods of the {@link Collection} and {@link |
50 |
> |
* Iterator} interfaces. |
51 |
> |
* |
52 |
> |
* <p>This class is a member of the |
53 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
54 |
> |
* Java Collections Framework</a>. |
55 |
|
* |
56 |
|
* @author Josh Bloch and Doug Lea |
57 |
+ |
* @param <E> the type of elements held in this deque |
58 |
|
* @since 1.6 |
49 |
– |
* @param <E> the type of elements held in this collection |
59 |
|
*/ |
60 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
61 |
|
implements Deque<E>, Cloneable, Serializable |
62 |
|
{ |
63 |
|
/** |
64 |
< |
* The array in which the elements of in the deque are stored. |
65 |
< |
* The capacity of the deque is the length of this array, which is |
66 |
< |
* always a power of two. The array is never allowed to become |
58 |
< |
* full, except transiently within an addX method where it is |
59 |
< |
* resized (see doubleCapacity) immediately upon becoming full, |
60 |
< |
* thus avoiding head and tail wrapping around to equal each |
61 |
< |
* other. We also guarantee that all array cells not holding |
62 |
< |
* deque elements are always null. |
64 |
> |
* The array in which the elements of the deque are stored. |
65 |
> |
* We guarantee that all array cells not holding deque elements |
66 |
> |
* are always null. |
67 |
|
*/ |
68 |
< |
private transient E[] elements; |
68 |
> |
transient Object[] elements; |
69 |
|
|
70 |
|
/** |
71 |
|
* The index of the element at the head of the deque (which is the |
72 |
|
* element that would be removed by remove() or pop()); or an |
73 |
< |
* arbitrary number equal to tail if the deque is empty. |
70 |
< |
*/ |
71 |
< |
private transient int head; |
72 |
< |
|
73 |
< |
/** |
74 |
< |
* The index at which the next element would be added to the tail |
75 |
< |
* of the deque (via addLast(E), add(E), or push(E)). |
76 |
< |
*/ |
77 |
< |
private transient int tail; |
78 |
< |
|
79 |
< |
/** |
80 |
< |
* The minimum capacity that we'll use for a newly created deque. |
81 |
< |
* Must be a power of 2. |
73 |
> |
* arbitrary number 0 <= head < elements.length if the deque is empty. |
74 |
|
*/ |
75 |
< |
private static final int MIN_INITIAL_CAPACITY = 8; |
75 |
> |
transient int head; |
76 |
|
|
77 |
< |
// ****** Array allocation and resizing utilities ****** |
77 |
> |
/** Number of elements in this collection. */ |
78 |
> |
transient int size; |
79 |
|
|
80 |
|
/** |
81 |
< |
* Allocate empty array to hold the given number of elements. |
82 |
< |
* |
83 |
< |
* @param numElements the number of elements to hold. |
84 |
< |
*/ |
85 |
< |
private void allocateElements(int numElements) { |
86 |
< |
int initialCapacity = MIN_INITIAL_CAPACITY; |
87 |
< |
// Find the best power of two to hold elements. |
88 |
< |
// Tests "<=" because arrays aren't kept full. |
89 |
< |
if (numElements >= initialCapacity) { |
90 |
< |
initialCapacity = numElements; |
91 |
< |
initialCapacity |= (initialCapacity >>> 1); |
92 |
< |
initialCapacity |= (initialCapacity >>> 2); |
93 |
< |
initialCapacity |= (initialCapacity >>> 4); |
94 |
< |
initialCapacity |= (initialCapacity >>> 8); |
95 |
< |
initialCapacity |= (initialCapacity >>> 16); |
96 |
< |
initialCapacity++; |
81 |
> |
* The maximum size of array to allocate. |
82 |
> |
* Some VMs reserve some header words in an array. |
83 |
> |
* Attempts to allocate larger arrays may result in |
84 |
> |
* OutOfMemoryError: Requested array size exceeds VM limit |
85 |
> |
*/ |
86 |
> |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
87 |
> |
|
88 |
> |
/** |
89 |
> |
* Increases the capacity of this deque by at least the given amount. |
90 |
> |
* |
91 |
> |
* @param needed the required minimum extra capacity; must be positive |
92 |
> |
*/ |
93 |
> |
private void grow(int needed) { |
94 |
> |
// overflow-conscious code |
95 |
> |
// checkInvariants(); |
96 |
> |
int oldCapacity = elements.length; |
97 |
> |
int newCapacity; |
98 |
> |
// Double size if small; else grow by 50% |
99 |
> |
int jump = (oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1); |
100 |
> |
if (jump < needed |
101 |
> |
|| (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0) |
102 |
> |
newCapacity = newCapacity(needed, jump); |
103 |
> |
elements = Arrays.copyOf(elements, newCapacity); |
104 |
> |
if (oldCapacity - head < size) { |
105 |
> |
// wrap around; slide first leg forward to end of array |
106 |
> |
int newSpace = newCapacity - oldCapacity; |
107 |
> |
System.arraycopy(elements, head, |
108 |
> |
elements, head + newSpace, |
109 |
> |
oldCapacity - head); |
110 |
> |
Arrays.fill(elements, head, head + newSpace, null); |
111 |
> |
head += newSpace; |
112 |
> |
} |
113 |
> |
// checkInvariants(); |
114 |
> |
} |
115 |
|
|
116 |
< |
if (initialCapacity < 0) // Too many elements, must back off |
117 |
< |
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements |
116 |
> |
/** Capacity calculation for edge conditions, especially overflow. */ |
117 |
> |
private int newCapacity(int needed, int jump) { |
118 |
> |
int oldCapacity = elements.length; |
119 |
> |
int minCapacity; |
120 |
> |
if ((minCapacity = oldCapacity + needed) - MAX_ARRAY_SIZE > 0) { |
121 |
> |
if (minCapacity < 0) |
122 |
> |
throw new IllegalStateException("Sorry, deque too big"); |
123 |
> |
return Integer.MAX_VALUE; |
124 |
|
} |
125 |
< |
elements = (E[]) new Object[initialCapacity]; |
125 |
> |
if (needed > jump) |
126 |
> |
return minCapacity; |
127 |
> |
return (oldCapacity + jump - MAX_ARRAY_SIZE < 0) |
128 |
> |
? oldCapacity + jump |
129 |
> |
: MAX_ARRAY_SIZE; |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* Double the capacity of this deque. Call only when full, i.e., |
134 |
< |
* when head and tail have wrapped around to become equal. |
133 |
> |
* Increases the internal storage of this collection, if necessary, |
134 |
> |
* to ensure that it can hold at least the given number of elements. |
135 |
> |
* |
136 |
> |
* @param minCapacity the desired minimum capacity |
137 |
> |
* @since TBD |
138 |
|
*/ |
139 |
< |
private void doubleCapacity() { |
140 |
< |
assert head == tail; |
141 |
< |
int p = head; |
142 |
< |
int n = elements.length; |
119 |
< |
int r = n - p; // number of elements to the right of p |
120 |
< |
int newCapacity = n << 1; |
121 |
< |
if (newCapacity < 0) |
122 |
< |
throw new IllegalStateException("Sorry, deque too big"); |
123 |
< |
Object[] a = new Object[newCapacity]; |
124 |
< |
System.arraycopy(elements, p, a, 0, r); |
125 |
< |
System.arraycopy(elements, 0, a, r, p); |
126 |
< |
elements = (E[])a; |
127 |
< |
head = 0; |
128 |
< |
tail = n; |
139 |
> |
/* public */ void ensureCapacity(int minCapacity) { |
140 |
> |
if (minCapacity > elements.length) |
141 |
> |
grow(minCapacity - elements.length); |
142 |
> |
// checkInvariants(); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
< |
* Copy the elements from our element array into the specified array, |
133 |
< |
* in order (from first to last element in the deque). It is assumed |
134 |
< |
* that the array is large enough to hold all elements in the deque. |
146 |
> |
* Minimizes the internal storage of this collection. |
147 |
|
* |
148 |
< |
* @return its argument |
148 |
> |
* @since TBD |
149 |
|
*/ |
150 |
< |
private <T> T[] copyElements(T[] a) { |
151 |
< |
if (head < tail) { |
152 |
< |
System.arraycopy(elements, head, a, 0, size()); |
153 |
< |
} else if (head > tail) { |
142 |
< |
int headPortionLen = elements.length - head; |
143 |
< |
System.arraycopy(elements, head, a, 0, headPortionLen); |
144 |
< |
System.arraycopy(elements, 0, a, headPortionLen, tail); |
150 |
> |
/* public */ void trimToSize() { |
151 |
> |
if (size < elements.length) { |
152 |
> |
elements = toArray(); |
153 |
> |
head = 0; |
154 |
|
} |
155 |
< |
return a; |
155 |
> |
// checkInvariants(); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
< |
* Constructs an empty array deque with the an initial capacity |
159 |
> |
* Constructs an empty array deque with an initial capacity |
160 |
|
* sufficient to hold 16 elements. |
161 |
|
*/ |
162 |
|
public ArrayDeque() { |
163 |
< |
elements = (E[]) new Object[16]; |
163 |
> |
elements = new Object[16]; |
164 |
|
} |
165 |
|
|
166 |
|
/** |
167 |
|
* Constructs an empty array deque with an initial capacity |
168 |
|
* sufficient to hold the specified number of elements. |
169 |
|
* |
170 |
< |
* @param numElements lower bound on initial capacity of the deque |
170 |
> |
* @param numElements lower bound on initial capacity of the deque |
171 |
|
*/ |
172 |
|
public ArrayDeque(int numElements) { |
173 |
< |
allocateElements(numElements); |
173 |
> |
elements = new Object[numElements]; |
174 |
|
} |
175 |
|
|
176 |
|
/** |
184 |
|
* @throws NullPointerException if the specified collection is null |
185 |
|
*/ |
186 |
|
public ArrayDeque(Collection<? extends E> c) { |
187 |
< |
allocateElements(c.size()); |
188 |
< |
addAll(c); |
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 |
> |
if (elements.getClass() != Object[].class) |
191 |
> |
elements = Arrays.copyOf(elements, size, Object[].class); |
192 |
> |
for (Object obj : elements) |
193 |
> |
Objects.requireNonNull(obj); |
194 |
> |
size = elements.length; |
195 |
> |
this.elements = elements; |
196 |
|
} |
197 |
|
|
198 |
< |
// The main insertion and extraction methods are addFirst, |
199 |
< |
// addLast, pollFirst, pollLast. The other methods are defined in |
200 |
< |
// terms of these. |
198 |
> |
/** |
199 |
> |
* Increments i, mod modulus. |
200 |
> |
* Precondition and postcondition: 0 <= i < modulus. |
201 |
> |
*/ |
202 |
> |
static final int inc(int i, int modulus) { |
203 |
> |
if (++i == modulus) i = 0; |
204 |
> |
return i; |
205 |
> |
} |
206 |
|
|
207 |
|
/** |
208 |
< |
* Inserts the specified element to the front this deque. |
209 |
< |
* |
189 |
< |
* @param e the element to insert |
190 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
208 |
> |
* Decrements i, mod modulus. |
209 |
> |
* Precondition and postcondition: 0 <= i < modulus. |
210 |
|
*/ |
211 |
< |
public void addFirst(E e) { |
212 |
< |
if (e == null) |
213 |
< |
throw new NullPointerException(); |
195 |
< |
elements[head = (head - 1) & (elements.length - 1)] = e; |
196 |
< |
if (head == tail) |
197 |
< |
doubleCapacity(); |
211 |
> |
static final int dec(int i, int modulus) { |
212 |
> |
if (--i < 0) i += modulus; |
213 |
> |
return i; |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
< |
* Inserts the specified element to the end this deque. |
218 |
< |
* This method is equivalent to {@link Collection#add} and |
203 |
< |
* {@link #push}. |
204 |
< |
* |
205 |
< |
* @param e the element to insert |
206 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
217 |
> |
* Adds i and j, mod modulus. |
218 |
> |
* Precondition and postcondition: 0 <= i < modulus, 0 <= j <= modulus. |
219 |
|
*/ |
220 |
< |
public void addLast(E e) { |
220 |
> |
static final int add(int i, int j, int modulus) { |
221 |
> |
if ((i += j) - modulus >= 0) i -= modulus; |
222 |
> |
return i; |
223 |
> |
} |
224 |
> |
|
225 |
> |
/** |
226 |
> |
* Returns the array index of the last element. |
227 |
> |
* May return invalid index -1 if there are no elements. |
228 |
> |
*/ |
229 |
> |
final int tail() { |
230 |
> |
return add(head, size - 1, elements.length); |
231 |
> |
} |
232 |
> |
|
233 |
> |
/** |
234 |
> |
* Returns element at array index i. |
235 |
> |
*/ |
236 |
> |
@SuppressWarnings("unchecked") |
237 |
> |
final E elementAt(int i) { |
238 |
> |
return (E) elements[i]; |
239 |
> |
} |
240 |
> |
|
241 |
> |
/** |
242 |
> |
* A version of elementAt that checks for null elements. |
243 |
> |
* This check doesn't catch all possible comodifications, |
244 |
> |
* but does catch ones that corrupt traversal. |
245 |
> |
*/ |
246 |
> |
E checkedElementAt(Object[] elements, int i) { |
247 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
248 |
|
if (e == null) |
249 |
< |
throw new NullPointerException(); |
250 |
< |
elements[tail] = e; |
212 |
< |
if ( (tail = (tail + 1) & (elements.length - 1)) == head) |
213 |
< |
doubleCapacity(); |
249 |
> |
throw new ConcurrentModificationException(); |
250 |
> |
return e; |
251 |
|
} |
252 |
|
|
253 |
+ |
// The main insertion and extraction methods are addFirst, |
254 |
+ |
// addLast, pollFirst, pollLast. The other methods are defined in |
255 |
+ |
// terms of these. |
256 |
+ |
|
257 |
|
/** |
258 |
< |
* Retrieves and removes the first element of this deque, or |
218 |
< |
* <tt>null</tt> if this deque is empty. |
258 |
> |
* Inserts the specified element at the front of this deque. |
259 |
|
* |
260 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
261 |
< |
* this deque is empty |
260 |
> |
* @param e the element to add |
261 |
> |
* @throws NullPointerException if the specified element is null |
262 |
|
*/ |
263 |
< |
public E pollFirst() { |
264 |
< |
int h = head; |
265 |
< |
E result = elements[h]; // Element is null if deque empty |
266 |
< |
if (result == null) |
267 |
< |
return null; |
268 |
< |
elements[h] = null; // Must null out slot |
269 |
< |
head = (h + 1) & (elements.length - 1); |
270 |
< |
return result; |
263 |
> |
public void addFirst(E e) { |
264 |
> |
// checkInvariants(); |
265 |
> |
Objects.requireNonNull(e); |
266 |
> |
final Object[] elements; |
267 |
> |
final int capacity, s; |
268 |
> |
if ((s = size) == (capacity = (elements = this.elements).length)) |
269 |
> |
addFirstSlowPath(e); |
270 |
> |
else |
271 |
> |
elements[head = dec(head, capacity)] = e; |
272 |
> |
size = s + 1; |
273 |
> |
} |
274 |
> |
|
275 |
> |
private void addFirstSlowPath(E e) { |
276 |
> |
grow(1); |
277 |
> |
final Object[] elements = this.elements; |
278 |
> |
elements[head = dec(head, elements.length)] = e; |
279 |
> |
// checkInvariants(); |
280 |
|
} |
281 |
|
|
282 |
|
/** |
283 |
< |
* Retrieves and removes the last element of this deque, or |
284 |
< |
* <tt>null</tt> if this deque is empty. |
283 |
> |
* Inserts the specified element at the end of this deque. |
284 |
> |
* |
285 |
> |
* <p>This method is equivalent to {@link #add}. |
286 |
|
* |
287 |
< |
* @return the last element of this deque, or <tt>null</tt> if |
288 |
< |
* this deque is empty |
287 |
> |
* @param e the element to add |
288 |
> |
* @throws NullPointerException if the specified element is null |
289 |
|
*/ |
290 |
< |
public E pollLast() { |
291 |
< |
int t = (tail - 1) & (elements.length - 1); |
292 |
< |
E result = elements[t]; |
293 |
< |
if (result == null) |
294 |
< |
return null; |
295 |
< |
elements[t] = null; |
296 |
< |
tail = t; |
297 |
< |
return result; |
290 |
> |
public void addLast(E e) { |
291 |
> |
// checkInvariants(); |
292 |
> |
Objects.requireNonNull(e); |
293 |
> |
final Object[] elements; |
294 |
> |
final int capacity, s; |
295 |
> |
if ((s = size) == (capacity = (elements = this.elements).length)) |
296 |
> |
addLastSlowPath(e); |
297 |
> |
else |
298 |
> |
elements[add(head, s, capacity)] = e; |
299 |
> |
size = s + 1; |
300 |
> |
} |
301 |
> |
|
302 |
> |
private void addLastSlowPath(E e) { |
303 |
> |
grow(1); |
304 |
> |
final Object[] elements = this.elements; |
305 |
> |
elements[add(head, size, elements.length)] = e; |
306 |
> |
// checkInvariants(); |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
< |
* Inserts the specified element to the front this deque. |
310 |
> |
* Adds all of the elements in the specified collection at the end |
311 |
> |
* of this deque, as if by calling {@link #addLast} on each one, |
312 |
> |
* in the order that they are returned by the collection's |
313 |
> |
* iterator. |
314 |
> |
* |
315 |
> |
* @param c the elements to be inserted into this deque |
316 |
> |
* @return {@code true} if this deque changed as a result of the call |
317 |
> |
* @throws NullPointerException if the specified collection or any |
318 |
> |
* of its elements are null |
319 |
> |
*/ |
320 |
> |
@Override |
321 |
> |
public boolean addAll(Collection<? extends E> c) { |
322 |
> |
// checkInvariants(); |
323 |
> |
Object[] a, elements; |
324 |
> |
int newcomers, capacity, s = size; |
325 |
> |
if ((newcomers = (a = c.toArray()).length) == 0) |
326 |
> |
return false; |
327 |
> |
while ((capacity = (elements = this.elements).length) - s < newcomers) |
328 |
> |
grow(newcomers - (capacity - s)); |
329 |
> |
int i = add(head, s, capacity); |
330 |
> |
for (Object x : a) { |
331 |
> |
Objects.requireNonNull(x); |
332 |
> |
elements[i] = x; |
333 |
> |
i = inc(i, capacity); |
334 |
> |
size++; |
335 |
> |
} |
336 |
> |
return true; |
337 |
> |
} |
338 |
> |
|
339 |
> |
/** |
340 |
> |
* Inserts the specified element at the front of this deque. |
341 |
|
* |
342 |
< |
* @param e the element to insert |
343 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
344 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
342 |
> |
* @param e the element to add |
343 |
> |
* @return {@code true} (as specified by {@link Deque#offerFirst}) |
344 |
> |
* @throws NullPointerException if the specified element is null |
345 |
|
*/ |
346 |
|
public boolean offerFirst(E e) { |
347 |
|
addFirst(e); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
< |
* Inserts the specified element to the end this deque. |
352 |
> |
* Inserts the specified element at the end of this deque. |
353 |
|
* |
354 |
< |
* @param e the element to insert |
355 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
356 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
354 |
> |
* @param e the element to add |
355 |
> |
* @return {@code true} (as specified by {@link Deque#offerLast}) |
356 |
> |
* @throws NullPointerException if the specified element is null |
357 |
|
*/ |
358 |
|
public boolean offerLast(E e) { |
359 |
|
addLast(e); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
364 |
< |
* Retrieves and removes the first element of this deque. This method |
276 |
< |
* differs from the <tt>pollFirst</tt> method in that it throws an |
277 |
< |
* exception if this deque is empty. |
278 |
< |
* |
279 |
< |
* @return the first element of this deque |
280 |
< |
* @throws NoSuchElementException if this deque is empty |
364 |
> |
* @throws NoSuchElementException {@inheritDoc} |
365 |
|
*/ |
366 |
|
public E removeFirst() { |
367 |
+ |
// checkInvariants(); |
368 |
|
E x = pollFirst(); |
369 |
|
if (x == null) |
370 |
|
throw new NoSuchElementException(); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
< |
* Retrieves and removes the last element of this deque. This method |
291 |
< |
* differs from the <tt>pollLast</tt> method in that it throws an |
292 |
< |
* exception if this deque is empty. |
293 |
< |
* |
294 |
< |
* @return the last element of this deque |
295 |
< |
* @throws NoSuchElementException if this deque is empty |
375 |
> |
* @throws NoSuchElementException {@inheritDoc} |
376 |
|
*/ |
377 |
|
public E removeLast() { |
378 |
+ |
// checkInvariants(); |
379 |
|
E x = pollLast(); |
380 |
|
if (x == null) |
381 |
|
throw new NoSuchElementException(); |
382 |
|
return x; |
383 |
|
} |
384 |
|
|
385 |
< |
/** |
386 |
< |
* Retrieves, but does not remove, the first element of this deque, |
387 |
< |
* returning <tt>null</tt> if this deque is empty. |
388 |
< |
* |
389 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
390 |
< |
* this deque is empty |
391 |
< |
*/ |
392 |
< |
public E peekFirst() { |
393 |
< |
return elements[head]; // elements[head] is null if deque empty |
385 |
> |
public E pollFirst() { |
386 |
> |
// checkInvariants(); |
387 |
> |
final int s, h; |
388 |
> |
if ((s = size) == 0) |
389 |
> |
return null; |
390 |
> |
final Object[] elements = this.elements; |
391 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[h = head]; |
392 |
> |
elements[h] = null; |
393 |
> |
head = inc(h, elements.length); |
394 |
> |
size = s - 1; |
395 |
> |
return e; |
396 |
|
} |
397 |
|
|
398 |
< |
/** |
399 |
< |
* Retrieves, but does not remove, the last element of this deque, |
400 |
< |
* returning <tt>null</tt> if this deque is empty. |
401 |
< |
* |
402 |
< |
* @return the last element of this deque, or <tt>null</tt> if this deque |
403 |
< |
* is empty |
404 |
< |
*/ |
405 |
< |
public E peekLast() { |
406 |
< |
return elements[(tail - 1) & (elements.length - 1)]; |
398 |
> |
public E pollLast() { |
399 |
> |
// checkInvariants(); |
400 |
> |
final int s, tail; |
401 |
> |
if ((s = size) == 0) |
402 |
> |
return null; |
403 |
> |
final Object[] elements = this.elements; |
404 |
> |
@SuppressWarnings("unchecked") |
405 |
> |
E e = (E) elements[tail = add(head, s - 1, elements.length)]; |
406 |
> |
elements[tail] = null; |
407 |
> |
size = s - 1; |
408 |
> |
return e; |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
< |
* Retrieves, but does not remove, the first element of this |
328 |
< |
* deque. This method differs from the <tt>peek</tt> method only |
329 |
< |
* in that it throws an exception if this deque is empty. |
330 |
< |
* |
331 |
< |
* @return the first element of this deque |
332 |
< |
* @throws NoSuchElementException if this deque is empty |
412 |
> |
* @throws NoSuchElementException {@inheritDoc} |
413 |
|
*/ |
414 |
|
public E getFirst() { |
415 |
< |
E x = elements[head]; |
416 |
< |
if (x == null) |
417 |
< |
throw new NoSuchElementException(); |
338 |
< |
return x; |
415 |
> |
// checkInvariants(); |
416 |
> |
if (size == 0) throw new NoSuchElementException(); |
417 |
> |
return elementAt(head); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* Retrieves, but does not remove, the last element of this |
343 |
< |
* deque. This method differs from the <tt>peek</tt> method only |
344 |
< |
* in that it throws an exception if this deque is empty. |
345 |
< |
* |
346 |
< |
* @return the last element of this deque |
347 |
< |
* @throws NoSuchElementException if this deque is empty |
421 |
> |
* @throws NoSuchElementException {@inheritDoc} |
422 |
|
*/ |
423 |
|
public E getLast() { |
424 |
< |
E x = elements[(tail - 1) & (elements.length - 1)]; |
425 |
< |
if (x == null) |
426 |
< |
throw new NoSuchElementException(); |
427 |
< |
return x; |
424 |
> |
// checkInvariants(); |
425 |
> |
if (size == 0) throw new NoSuchElementException(); |
426 |
> |
return elementAt(tail()); |
427 |
> |
} |
428 |
> |
|
429 |
> |
public E peekFirst() { |
430 |
> |
// checkInvariants(); |
431 |
> |
return (size == 0) ? null : elementAt(head); |
432 |
> |
} |
433 |
> |
|
434 |
> |
public E peekLast() { |
435 |
> |
// checkInvariants(); |
436 |
> |
return (size == 0) ? null : elementAt(tail()); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
|
* Removes the first occurrence of the specified element in this |
441 |
< |
* deque (when traversing the deque from head to tail). If the deque |
442 |
< |
* does not contain the element, it is unchanged. |
443 |
< |
* |
444 |
< |
* @param e element to be removed from this deque, if present |
445 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
446 |
< |
*/ |
447 |
< |
public boolean removeFirstOccurrence(Object e) { |
448 |
< |
if (e == null) |
449 |
< |
return false; |
450 |
< |
int mask = elements.length - 1; |
451 |
< |
int i = head; |
452 |
< |
E x; |
453 |
< |
while ( (x = elements[i]) != null) { |
454 |
< |
if (e.equals(x)) { |
455 |
< |
delete(i); |
456 |
< |
return true; |
441 |
> |
* deque (when traversing the deque from head to tail). |
442 |
> |
* If the deque does not contain the element, it is unchanged. |
443 |
> |
* More formally, removes the first element {@code e} such that |
444 |
> |
* {@code o.equals(e)} (if such an element exists). |
445 |
> |
* Returns {@code true} if this deque contained the specified element |
446 |
> |
* (or equivalently, if this deque changed as a result of the call). |
447 |
> |
* |
448 |
> |
* @param o element to be removed from this deque, if present |
449 |
> |
* @return {@code true} if the deque contained the specified element |
450 |
> |
*/ |
451 |
> |
public boolean removeFirstOccurrence(Object o) { |
452 |
> |
// checkInvariants(); |
453 |
> |
if (o != null) { |
454 |
> |
final Object[] elements = this.elements; |
455 |
> |
final int capacity = elements.length; |
456 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) { |
457 |
> |
if (o.equals(elements[i])) { |
458 |
> |
delete(i); |
459 |
> |
return true; |
460 |
> |
} |
461 |
|
} |
375 |
– |
i = (i + 1) & mask; |
462 |
|
} |
463 |
|
return false; |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
|
* Removes the last occurrence of the specified element in this |
468 |
< |
* deque (when traversing the deque from head to tail). If the deque |
469 |
< |
* does not contain the element, it is unchanged. |
470 |
< |
* |
471 |
< |
* @param e element to be removed from this deque, if present |
472 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
473 |
< |
*/ |
474 |
< |
public boolean removeLastOccurrence(Object e) { |
475 |
< |
if (e == null) |
476 |
< |
return false; |
477 |
< |
int mask = elements.length - 1; |
478 |
< |
int i = (tail - 1) & mask; |
479 |
< |
E x; |
480 |
< |
while ( (x = elements[i]) != null) { |
481 |
< |
if (e.equals(x)) { |
482 |
< |
delete(i); |
483 |
< |
return true; |
468 |
> |
* deque (when traversing the deque from head to tail). |
469 |
> |
* If the deque does not contain the element, it is unchanged. |
470 |
> |
* More formally, removes the last element {@code e} such that |
471 |
> |
* {@code o.equals(e)} (if such an element exists). |
472 |
> |
* Returns {@code true} if this deque contained the specified element |
473 |
> |
* (or equivalently, if this deque changed as a result of the call). |
474 |
> |
* |
475 |
> |
* @param o element to be removed from this deque, if present |
476 |
> |
* @return {@code true} if the deque contained the specified element |
477 |
> |
*/ |
478 |
> |
public boolean removeLastOccurrence(Object o) { |
479 |
> |
if (o != null) { |
480 |
> |
final Object[] elements = this.elements; |
481 |
> |
final int capacity = elements.length; |
482 |
> |
for (int k = size, i = add(head, k - 1, capacity); |
483 |
> |
--k >= 0; i = dec(i, capacity)) { |
484 |
> |
if (o.equals(elements[i])) { |
485 |
> |
delete(i); |
486 |
> |
return true; |
487 |
> |
} |
488 |
|
} |
399 |
– |
i = (i - 1) & mask; |
489 |
|
} |
490 |
|
return false; |
491 |
|
} |
493 |
|
// *** Queue methods *** |
494 |
|
|
495 |
|
/** |
496 |
< |
* Inserts the specified element to the end of this deque. |
408 |
< |
* |
409 |
< |
* <p>This method is equivalent to {@link #offerLast}. |
410 |
< |
* |
411 |
< |
* @param e the element to insert |
412 |
< |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
413 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
414 |
< |
*/ |
415 |
< |
public boolean offer(E e) { |
416 |
< |
return offerLast(e); |
417 |
< |
} |
418 |
< |
|
419 |
< |
/** |
420 |
< |
* Inserts the specified element to the end of this deque. |
496 |
> |
* Inserts the specified element at the end of this deque. |
497 |
|
* |
498 |
|
* <p>This method is equivalent to {@link #addLast}. |
499 |
|
* |
500 |
< |
* @param e the element to insert |
501 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
502 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
500 |
> |
* @param e the element to add |
501 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
502 |
> |
* @throws NullPointerException if the specified element is null |
503 |
|
*/ |
504 |
|
public boolean add(E e) { |
505 |
|
addLast(e); |
507 |
|
} |
508 |
|
|
509 |
|
/** |
510 |
< |
* Retrieves and removes the head of the queue represented by |
435 |
< |
* this deque, or <tt>null</tt> if this deque is empty. In other words, |
436 |
< |
* retrieves and removes the first element of this deque, or <tt>null</tt> |
437 |
< |
* if this deque is empty. |
510 |
> |
* Inserts the specified element at the end of this deque. |
511 |
|
* |
512 |
< |
* <p>This method is equivalent to {@link #pollFirst}. |
512 |
> |
* <p>This method is equivalent to {@link #offerLast}. |
513 |
|
* |
514 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
515 |
< |
* this deque is empty |
514 |
> |
* @param e the element to add |
515 |
> |
* @return {@code true} (as specified by {@link Queue#offer}) |
516 |
> |
* @throws NullPointerException if the specified element is null |
517 |
|
*/ |
518 |
< |
public E poll() { |
519 |
< |
return pollFirst(); |
518 |
> |
public boolean offer(E e) { |
519 |
> |
return offerLast(e); |
520 |
|
} |
521 |
|
|
522 |
|
/** |
523 |
|
* Retrieves and removes the head of the queue represented by this deque. |
524 |
< |
* This method differs from the <tt>poll</tt> method in that it throws an |
524 |
> |
* |
525 |
> |
* This method differs from {@link #poll poll} only in that it throws an |
526 |
|
* exception if this deque is empty. |
527 |
|
* |
528 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
529 |
|
* |
530 |
|
* @return the head of the queue represented by this deque |
531 |
< |
* @throws NoSuchElementException if this deque is empty |
531 |
> |
* @throws NoSuchElementException {@inheritDoc} |
532 |
|
*/ |
533 |
|
public E remove() { |
534 |
|
return removeFirst(); |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
< |
* Retrieves, but does not remove, the head of the queue represented by |
539 |
< |
* this deque, returning <tt>null</tt> if this deque is empty. |
538 |
> |
* Retrieves and removes the head of the queue represented by this deque |
539 |
> |
* (in other words, the first element of this deque), or returns |
540 |
> |
* {@code null} if this deque is empty. |
541 |
|
* |
542 |
< |
* <p>This method is equivalent to {@link #peekFirst} |
542 |
> |
* <p>This method is equivalent to {@link #pollFirst}. |
543 |
|
* |
544 |
|
* @return the head of the queue represented by this deque, or |
545 |
< |
* <tt>null</tt> if this deque is empty |
545 |
> |
* {@code null} if this deque is empty |
546 |
|
*/ |
547 |
< |
public E peek() { |
548 |
< |
return peekFirst(); |
547 |
> |
public E poll() { |
548 |
> |
return pollFirst(); |
549 |
|
} |
550 |
|
|
551 |
|
/** |
552 |
|
* Retrieves, but does not remove, the head of the queue represented by |
553 |
< |
* this deque. This method differs from the <tt>peek</tt> method only in |
553 |
> |
* this deque. This method differs from {@link #peek peek} only in |
554 |
|
* that it throws an exception if this deque is empty. |
555 |
|
* |
556 |
< |
* <p>This method is equivalent to {@link #getFirst} |
556 |
> |
* <p>This method is equivalent to {@link #getFirst}. |
557 |
|
* |
558 |
|
* @return the head of the queue represented by this deque |
559 |
< |
* @throws NoSuchElementException if this deque is empty |
559 |
> |
* @throws NoSuchElementException {@inheritDoc} |
560 |
|
*/ |
561 |
|
public E element() { |
562 |
|
return getFirst(); |
563 |
|
} |
564 |
|
|
565 |
+ |
/** |
566 |
+ |
* Retrieves, but does not remove, the head of the queue represented by |
567 |
+ |
* this deque, or returns {@code null} if this deque is empty. |
568 |
+ |
* |
569 |
+ |
* <p>This method is equivalent to {@link #peekFirst}. |
570 |
+ |
* |
571 |
+ |
* @return the head of the queue represented by this deque, or |
572 |
+ |
* {@code null} if this deque is empty |
573 |
+ |
*/ |
574 |
+ |
public E peek() { |
575 |
+ |
return peekFirst(); |
576 |
+ |
} |
577 |
+ |
|
578 |
|
// *** Stack methods *** |
579 |
|
|
580 |
|
/** |
581 |
|
* Pushes an element onto the stack represented by this deque. In other |
582 |
< |
* words, inserts the element to the front this deque. |
582 |
> |
* words, inserts the element at the front of this deque. |
583 |
|
* |
584 |
|
* <p>This method is equivalent to {@link #addFirst}. |
585 |
|
* |
586 |
|
* @param e the element to push |
587 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
587 |
> |
* @throws NullPointerException if the specified element is null |
588 |
|
*/ |
589 |
|
public void push(E e) { |
590 |
|
addFirst(e); |
597 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
598 |
|
* |
599 |
|
* @return the element at the front of this deque (which is the top |
600 |
< |
* of the stack represented by this deque) |
601 |
< |
* @throws NoSuchElementException if this deque is empty |
600 |
> |
* of the stack represented by this deque) |
601 |
> |
* @throws NoSuchElementException {@inheritDoc} |
602 |
|
*/ |
603 |
|
public E pop() { |
604 |
|
return removeFirst(); |
605 |
|
} |
606 |
|
|
607 |
|
/** |
608 |
< |
* Remove the element at the specified position in the elements array, |
609 |
< |
* adjusting head, tail, and size as necessary. This can result in |
610 |
< |
* motion of elements backwards or forwards in the array. |
611 |
< |
* |
612 |
< |
* <p>This method is called delete rather than remove to emphasize |
613 |
< |
* that its semantics differ from those of List.remove(int). |
614 |
< |
* |
608 |
> |
* Removes the element at the specified position in the elements array. |
609 |
> |
* This can result in forward or backwards motion of array elements. |
610 |
> |
* We optimize for least element motion. |
611 |
> |
* |
612 |
> |
* <p>This method is called delete rather than remove to emphasize |
613 |
> |
* that its semantics differ from those of {@link List#remove(int)}. |
614 |
> |
* |
615 |
|
* @return true if elements moved backwards |
616 |
|
*/ |
617 |
< |
private boolean delete(int i) { |
618 |
< |
// Case 1: Deque doesn't wrap |
619 |
< |
// Case 2: Deque does wrap and removed element is in the head portion |
620 |
< |
if ((head < tail || tail == 0) || i >= head) { |
621 |
< |
System.arraycopy(elements, head, elements, head + 1, i - head); |
622 |
< |
elements[head] = null; |
623 |
< |
head = (head + 1) & (elements.length - 1); |
617 |
> |
boolean delete(int i) { |
618 |
> |
// checkInvariants(); |
619 |
> |
final Object[] elements = this.elements; |
620 |
> |
final int capacity = elements.length; |
621 |
> |
final int h = head; |
622 |
> |
int front; // number of elements before to-be-deleted elt |
623 |
> |
if ((front = i - h) < 0) front += capacity; |
624 |
> |
final int back = size - front - 1; // number of elements after |
625 |
> |
if (front < back) { |
626 |
> |
// move front elements forwards |
627 |
> |
if (h <= i) { |
628 |
> |
System.arraycopy(elements, h, elements, h + 1, front); |
629 |
> |
} else { // Wrap around |
630 |
> |
System.arraycopy(elements, 0, elements, 1, i); |
631 |
> |
elements[0] = elements[capacity - 1]; |
632 |
> |
System.arraycopy(elements, h, elements, h + 1, front - (i + 1)); |
633 |
> |
} |
634 |
> |
elements[h] = null; |
635 |
> |
head = inc(h, capacity); |
636 |
> |
size--; |
637 |
> |
// checkInvariants(); |
638 |
|
return false; |
639 |
+ |
} else { |
640 |
+ |
// move back elements backwards |
641 |
+ |
int tail = tail(); |
642 |
+ |
if (i <= tail) { |
643 |
+ |
System.arraycopy(elements, i + 1, elements, i, back); |
644 |
+ |
} else { // Wrap around |
645 |
+ |
int firstLeg = capacity - (i + 1); |
646 |
+ |
System.arraycopy(elements, i + 1, elements, i, firstLeg); |
647 |
+ |
elements[capacity - 1] = elements[0]; |
648 |
+ |
System.arraycopy(elements, 1, elements, 0, back - firstLeg - 1); |
649 |
+ |
} |
650 |
+ |
elements[tail] = null; |
651 |
+ |
size--; |
652 |
+ |
// checkInvariants(); |
653 |
+ |
return true; |
654 |
|
} |
537 |
– |
|
538 |
– |
// Case 3: Deque wraps and removed element is in the tail portion |
539 |
– |
tail--; |
540 |
– |
System.arraycopy(elements, i + 1, elements, i, tail - i); |
541 |
– |
elements[tail] = null; |
542 |
– |
return true; |
655 |
|
} |
656 |
|
|
657 |
|
// *** Collection Methods *** |
662 |
|
* @return the number of elements in this deque |
663 |
|
*/ |
664 |
|
public int size() { |
665 |
< |
return (tail - head) & (elements.length - 1); |
665 |
> |
return size; |
666 |
|
} |
667 |
|
|
668 |
|
/** |
669 |
< |
* Returns <tt>true</tt> if this collection contains no elements.<p> |
669 |
> |
* Returns {@code true} if this deque contains no elements. |
670 |
|
* |
671 |
< |
* @return <tt>true</tt> if this collection contains no elements. |
671 |
> |
* @return {@code true} if this deque contains no elements |
672 |
|
*/ |
673 |
|
public boolean isEmpty() { |
674 |
< |
return head == tail; |
674 |
> |
return size == 0; |
675 |
|
} |
676 |
|
|
677 |
|
/** |
679 |
|
* will be ordered from first (head) to last (tail). This is the same |
680 |
|
* order that elements would be dequeued (via successive calls to |
681 |
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
682 |
< |
* |
683 |
< |
* @return an <tt>Iterator</tt> over the elements in this deque |
682 |
> |
* |
683 |
> |
* @return an iterator over the elements in this deque |
684 |
|
*/ |
685 |
|
public Iterator<E> iterator() { |
686 |
|
return new DeqIterator(); |
687 |
|
} |
688 |
|
|
689 |
+ |
public Iterator<E> descendingIterator() { |
690 |
+ |
return new DescendingIterator(); |
691 |
+ |
} |
692 |
+ |
|
693 |
|
private class DeqIterator implements Iterator<E> { |
694 |
< |
/** |
695 |
< |
* Index of element to be returned by subsequent call to next. |
580 |
< |
*/ |
581 |
< |
private int cursor = head; |
694 |
> |
/** Index of element to be returned by subsequent call to next. */ |
695 |
> |
int cursor; |
696 |
|
|
697 |
< |
/** |
698 |
< |
* Tail recorded at construction (also in remove), to stop |
585 |
< |
* iterator and also to check for comodification. |
586 |
< |
*/ |
587 |
< |
private int fence = tail; |
697 |
> |
/** Number of elements yet to be returned. */ |
698 |
> |
int remaining = size; |
699 |
|
|
700 |
|
/** |
701 |
|
* Index of element returned by most recent call to next. |
702 |
|
* Reset to -1 if element is deleted by a call to remove. |
703 |
|
*/ |
704 |
< |
private int lastRet = -1; |
704 |
> |
int lastRet = -1; |
705 |
> |
|
706 |
> |
DeqIterator() { cursor = head; } |
707 |
|
|
708 |
< |
public boolean hasNext() { |
709 |
< |
return cursor != fence; |
708 |
> |
public final boolean hasNext() { |
709 |
> |
return remaining > 0; |
710 |
|
} |
711 |
|
|
712 |
|
public E next() { |
713 |
< |
E result; |
601 |
< |
if (cursor == fence) |
713 |
> |
if (remaining == 0) |
714 |
|
throw new NoSuchElementException(); |
715 |
< |
// This check doesn't catch all possible comodifications, |
604 |
< |
// but does catch the ones that corrupt traversal |
605 |
< |
if (tail != fence || (result = elements[cursor]) == null) |
606 |
< |
throw new ConcurrentModificationException(); |
715 |
> |
E e = checkedElementAt(elements, cursor); |
716 |
|
lastRet = cursor; |
717 |
< |
cursor = (cursor + 1) & (elements.length - 1); |
718 |
< |
return result; |
717 |
> |
cursor = inc(cursor, elements.length); |
718 |
> |
remaining--; |
719 |
> |
return e; |
720 |
> |
} |
721 |
> |
|
722 |
> |
void postDelete(boolean leftShifted) { |
723 |
> |
if (leftShifted) |
724 |
> |
cursor = dec(cursor, elements.length); // undo inc in next |
725 |
|
} |
726 |
|
|
727 |
< |
public void remove() { |
727 |
> |
public final void remove() { |
728 |
|
if (lastRet < 0) |
729 |
|
throw new IllegalStateException(); |
730 |
< |
if (delete(lastRet)) |
616 |
< |
cursor--; |
730 |
> |
postDelete(delete(lastRet)); |
731 |
|
lastRet = -1; |
732 |
< |
fence = tail; |
732 |
> |
} |
733 |
> |
|
734 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
735 |
> |
Objects.requireNonNull(action); |
736 |
> |
final Object[] elements = ArrayDeque.this.elements; |
737 |
> |
final int capacity = elements.length; |
738 |
> |
int k = remaining; |
739 |
> |
remaining = 0; |
740 |
> |
for (int i = cursor; --k >= 0; i = inc(i, capacity)) |
741 |
> |
action.accept(checkedElementAt(elements, i)); |
742 |
> |
} |
743 |
> |
} |
744 |
> |
|
745 |
> |
private class DescendingIterator extends DeqIterator { |
746 |
> |
DescendingIterator() { cursor = tail(); } |
747 |
> |
|
748 |
> |
public final E next() { |
749 |
> |
if (remaining == 0) |
750 |
> |
throw new NoSuchElementException(); |
751 |
> |
E e = checkedElementAt(elements, cursor); |
752 |
> |
lastRet = cursor; |
753 |
> |
cursor = dec(cursor, elements.length); |
754 |
> |
remaining--; |
755 |
> |
return e; |
756 |
> |
} |
757 |
> |
|
758 |
> |
void postDelete(boolean leftShifted) { |
759 |
> |
if (!leftShifted) |
760 |
> |
cursor = inc(cursor, elements.length); // undo dec in next |
761 |
> |
} |
762 |
> |
|
763 |
> |
public final void forEachRemaining(Consumer<? super E> action) { |
764 |
> |
Objects.requireNonNull(action); |
765 |
> |
final Object[] elements = ArrayDeque.this.elements; |
766 |
> |
final int capacity = elements.length; |
767 |
> |
int k = remaining; |
768 |
> |
remaining = 0; |
769 |
> |
for (int i = cursor; --k >= 0; i = dec(i, capacity)) |
770 |
> |
action.accept(checkedElementAt(elements, i)); |
771 |
|
} |
772 |
|
} |
773 |
|
|
774 |
|
/** |
775 |
< |
* Returns <tt>true</tt> if this deque contains the specified |
776 |
< |
* element. More formally, returns <tt>true</tt> if and only if this |
777 |
< |
* deque contains at least one element <tt>e</tt> such that |
778 |
< |
* <tt>e.equals(o)</tt>. |
775 |
> |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
776 |
> |
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
777 |
> |
* deque. |
778 |
> |
* |
779 |
> |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
780 |
> |
* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and |
781 |
> |
* {@link Spliterator#NONNULL}. Overriding implementations should document |
782 |
> |
* the reporting of additional characteristic values. |
783 |
> |
* |
784 |
> |
* @return a {@code Spliterator} over the elements in this deque |
785 |
> |
* @since 1.8 |
786 |
> |
*/ |
787 |
> |
public Spliterator<E> spliterator() { |
788 |
> |
return new ArrayDequeSpliterator(); |
789 |
> |
} |
790 |
> |
|
791 |
> |
final class ArrayDequeSpliterator implements Spliterator<E> { |
792 |
> |
private int cursor; |
793 |
> |
private int remaining; // -1 until late-binding first use |
794 |
> |
|
795 |
> |
/** Constructs late-binding spliterator over all elements. */ |
796 |
> |
ArrayDequeSpliterator() { |
797 |
> |
this.remaining = -1; |
798 |
> |
} |
799 |
> |
|
800 |
> |
/** Constructs spliterator over the given slice. */ |
801 |
> |
ArrayDequeSpliterator(int cursor, int count) { |
802 |
> |
this.cursor = cursor; |
803 |
> |
this.remaining = count; |
804 |
> |
} |
805 |
> |
|
806 |
> |
/** Ensures late-binding initialization; then returns remaining. */ |
807 |
> |
private int remaining() { |
808 |
> |
if (remaining < 0) { |
809 |
> |
cursor = head; |
810 |
> |
remaining = size; |
811 |
> |
} |
812 |
> |
return remaining; |
813 |
> |
} |
814 |
> |
|
815 |
> |
public ArrayDequeSpliterator trySplit() { |
816 |
> |
final int mid; |
817 |
> |
if ((mid = remaining() >> 1) > 0) { |
818 |
> |
int oldCursor = cursor; |
819 |
> |
cursor = add(cursor, mid, elements.length); |
820 |
> |
remaining -= mid; |
821 |
> |
return new ArrayDequeSpliterator(oldCursor, mid); |
822 |
> |
} |
823 |
> |
return null; |
824 |
> |
} |
825 |
> |
|
826 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
827 |
> |
Objects.requireNonNull(action); |
828 |
> |
final Object[] elements = ArrayDeque.this.elements; |
829 |
> |
final int capacity = elements.length; |
830 |
> |
int k = remaining(); |
831 |
> |
remaining = 0; |
832 |
> |
for (int i = cursor; --k >= 0; i = inc(i, capacity)) |
833 |
> |
action.accept(checkedElementAt(elements, i)); |
834 |
> |
} |
835 |
> |
|
836 |
> |
public boolean tryAdvance(Consumer<? super E> action) { |
837 |
> |
Objects.requireNonNull(action); |
838 |
> |
if (remaining() == 0) |
839 |
> |
return false; |
840 |
> |
action.accept(checkedElementAt(elements, cursor)); |
841 |
> |
cursor = inc(cursor, elements.length); |
842 |
> |
remaining--; |
843 |
> |
return true; |
844 |
> |
} |
845 |
> |
|
846 |
> |
public long estimateSize() { |
847 |
> |
return remaining(); |
848 |
> |
} |
849 |
> |
|
850 |
> |
public int characteristics() { |
851 |
> |
return Spliterator.NONNULL |
852 |
> |
| Spliterator.ORDERED |
853 |
> |
| Spliterator.SIZED |
854 |
> |
| Spliterator.SUBSIZED; |
855 |
> |
} |
856 |
> |
} |
857 |
> |
|
858 |
> |
@Override |
859 |
> |
public void forEach(Consumer<? super E> action) { |
860 |
> |
// checkInvariants(); |
861 |
> |
Objects.requireNonNull(action); |
862 |
> |
final Object[] elements = this.elements; |
863 |
> |
final int capacity = elements.length; |
864 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
865 |
> |
action.accept(elementAt(i)); |
866 |
> |
// checkInvariants(); |
867 |
> |
} |
868 |
> |
|
869 |
> |
/** |
870 |
> |
* Replaces each element of this deque with the result of applying the |
871 |
> |
* operator to that element, as specified by {@link List#replaceAll}. |
872 |
> |
* |
873 |
> |
* @param operator the operator to apply to each element |
874 |
> |
* @since TBD |
875 |
> |
*/ |
876 |
> |
/* public */ void replaceAll(UnaryOperator<E> operator) { |
877 |
> |
Objects.requireNonNull(operator); |
878 |
> |
final Object[] elements = this.elements; |
879 |
> |
final int capacity = elements.length; |
880 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
881 |
> |
elements[i] = operator.apply(elementAt(i)); |
882 |
> |
// checkInvariants(); |
883 |
> |
} |
884 |
> |
|
885 |
> |
/** |
886 |
> |
* @throws NullPointerException {@inheritDoc} |
887 |
> |
*/ |
888 |
> |
@Override |
889 |
> |
public boolean removeIf(Predicate<? super E> filter) { |
890 |
> |
Objects.requireNonNull(filter); |
891 |
> |
return bulkRemove(filter); |
892 |
> |
} |
893 |
> |
|
894 |
> |
/** |
895 |
> |
* @throws NullPointerException {@inheritDoc} |
896 |
> |
*/ |
897 |
> |
@Override |
898 |
> |
public boolean removeAll(Collection<?> c) { |
899 |
> |
Objects.requireNonNull(c); |
900 |
> |
return bulkRemove(e -> c.contains(e)); |
901 |
> |
} |
902 |
> |
|
903 |
> |
/** |
904 |
> |
* @throws NullPointerException {@inheritDoc} |
905 |
> |
*/ |
906 |
> |
@Override |
907 |
> |
public boolean retainAll(Collection<?> c) { |
908 |
> |
Objects.requireNonNull(c); |
909 |
> |
return bulkRemove(e -> !c.contains(e)); |
910 |
> |
} |
911 |
> |
|
912 |
> |
/** Implementation of bulk remove methods. */ |
913 |
> |
private boolean bulkRemove(Predicate<? super E> filter) { |
914 |
> |
// checkInvariants(); |
915 |
> |
final Object[] elements = this.elements; |
916 |
> |
final int capacity = elements.length; |
917 |
> |
int i = head, j = i, remaining = size, deleted = 0; |
918 |
> |
try { |
919 |
> |
for (; remaining > 0; remaining--, i = inc(i, capacity)) { |
920 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
921 |
> |
if (filter.test(e)) |
922 |
> |
deleted++; |
923 |
> |
else { |
924 |
> |
if (j != i) |
925 |
> |
elements[j] = e; |
926 |
> |
j = inc(j, capacity); |
927 |
> |
} |
928 |
> |
} |
929 |
> |
return deleted > 0; |
930 |
> |
} catch (Throwable ex) { |
931 |
> |
if (deleted > 0) |
932 |
> |
for (; remaining > 0; |
933 |
> |
remaining--, i = inc(i, capacity), j = inc(j, capacity)) |
934 |
> |
elements[j] = elements[i]; |
935 |
> |
throw ex; |
936 |
> |
} finally { |
937 |
> |
size -= deleted; |
938 |
> |
for (; --deleted >= 0; j = inc(j, capacity)) |
939 |
> |
elements[j] = null; |
940 |
> |
// checkInvariants(); |
941 |
> |
} |
942 |
> |
} |
943 |
> |
|
944 |
> |
/** |
945 |
> |
* Returns {@code true} if this deque contains the specified element. |
946 |
> |
* More formally, returns {@code true} if and only if this deque contains |
947 |
> |
* at least one element {@code e} such that {@code o.equals(e)}. |
948 |
|
* |
949 |
|
* @param o object to be checked for containment in this deque |
950 |
< |
* @return <tt>true</tt> if this deque contains the specified element |
950 |
> |
* @return {@code true} if this deque contains the specified element |
951 |
|
*/ |
952 |
|
public boolean contains(Object o) { |
953 |
< |
if (o == null) |
954 |
< |
return false; |
955 |
< |
int mask = elements.length - 1; |
956 |
< |
int i = head; |
957 |
< |
E x; |
958 |
< |
while ( (x = elements[i]) != null) { |
638 |
< |
if (o.equals(x)) |
639 |
< |
return true; |
640 |
< |
i = (i + 1) & mask; |
953 |
> |
if (o != null) { |
954 |
> |
final Object[] elements = this.elements; |
955 |
> |
final int capacity = elements.length; |
956 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
957 |
> |
if (o.equals(elements[i])) |
958 |
> |
return true; |
959 |
|
} |
960 |
|
return false; |
961 |
|
} |
962 |
|
|
963 |
|
/** |
964 |
|
* Removes a single instance of the specified element from this deque. |
965 |
< |
* This method is equivalent to {@link #removeFirstOccurrence}. |
965 |
> |
* If the deque does not contain the element, it is unchanged. |
966 |
> |
* More formally, removes the first element {@code e} such that |
967 |
> |
* {@code o.equals(e)} (if such an element exists). |
968 |
> |
* Returns {@code true} if this deque contained the specified element |
969 |
> |
* (or equivalently, if this deque changed as a result of the call). |
970 |
|
* |
971 |
< |
* @param e element to be removed from this deque, if present |
972 |
< |
* @return <tt>true</tt> if this deque contained the specified element |
971 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. |
972 |
> |
* |
973 |
> |
* @param o element to be removed from this deque, if present |
974 |
> |
* @return {@code true} if this deque contained the specified element |
975 |
|
*/ |
976 |
< |
public boolean remove(Object e) { |
977 |
< |
return removeFirstOccurrence(e); |
976 |
> |
public boolean remove(Object o) { |
977 |
> |
return removeFirstOccurrence(o); |
978 |
|
} |
979 |
|
|
980 |
|
/** |
981 |
|
* Removes all of the elements from this deque. |
982 |
+ |
* The deque will be empty after this call returns. |
983 |
|
*/ |
984 |
|
public void clear() { |
985 |
< |
int h = head; |
986 |
< |
int t = tail; |
987 |
< |
if (h != t) { // clear all cells |
988 |
< |
head = tail = 0; |
989 |
< |
int i = h; |
990 |
< |
int mask = elements.length - 1; |
991 |
< |
do { |
992 |
< |
elements[i] = null; |
993 |
< |
i = (i + 1) & mask; |
669 |
< |
} while(i != t); |
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); |
991 |
> |
else { |
992 |
> |
Arrays.fill(elements, h, capacity, null); |
993 |
> |
Arrays.fill(elements, 0, s - capacity + h, null); |
994 |
|
} |
995 |
+ |
size = head = 0; |
996 |
+ |
// checkInvariants(); |
997 |
|
} |
998 |
|
|
999 |
|
/** |
1000 |
< |
* Returns an array containing all of the elements in this list |
1001 |
< |
* in the correct order. |
1000 |
> |
* Returns an array containing all of the elements in this deque |
1001 |
> |
* in proper sequence (from first to last element). |
1002 |
> |
* |
1003 |
> |
* <p>The returned array will be "safe" in that no references to it are |
1004 |
> |
* maintained by this deque. (In other words, this method must allocate |
1005 |
> |
* a new array). The caller is thus free to modify the returned array. |
1006 |
|
* |
1007 |
< |
* @return an array containing all of the elements in this list |
1008 |
< |
* in the correct order |
1007 |
> |
* <p>This method acts as bridge between array-based and collection-based |
1008 |
> |
* APIs. |
1009 |
> |
* |
1010 |
> |
* @return an array containing all of the elements in this deque |
1011 |
|
*/ |
1012 |
|
public Object[] toArray() { |
1013 |
< |
return copyElements(new Object[size()]); |
1013 |
> |
final int head = this.head; |
1014 |
> |
final int firstLeg; |
1015 |
> |
Object[] a = Arrays.copyOfRange(elements, head, head + size); |
1016 |
> |
if ((firstLeg = elements.length - head) < size) |
1017 |
> |
System.arraycopy(elements, 0, a, firstLeg, size - firstLeg); |
1018 |
> |
return a; |
1019 |
|
} |
1020 |
|
|
1021 |
|
/** |
1022 |
< |
* Returns an array containing all of the elements in this deque in the |
1023 |
< |
* correct order; the runtime type of the returned array is that of the |
1024 |
< |
* specified array. If the deque fits in the specified array, it is |
1025 |
< |
* returned therein. Otherwise, a new array is allocated with the runtime |
1026 |
< |
* type of the specified array and the size of this deque. |
1022 |
> |
* Returns an array containing all of the elements in this deque in |
1023 |
> |
* proper sequence (from first to last element); the runtime type of the |
1024 |
> |
* returned array is that of the specified array. If the deque fits in |
1025 |
> |
* the specified array, it is returned therein. Otherwise, a new array |
1026 |
> |
* is allocated with the runtime type of the specified array and the |
1027 |
> |
* size of this deque. |
1028 |
> |
* |
1029 |
> |
* <p>If this deque fits in the specified array with room to spare |
1030 |
> |
* (i.e., the array has more elements than this deque), the element in |
1031 |
> |
* the array immediately following the end of the deque is set to |
1032 |
> |
* {@code null}. |
1033 |
> |
* |
1034 |
> |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
1035 |
> |
* array-based and collection-based APIs. Further, this method allows |
1036 |
> |
* precise control over the runtime type of the output array, and may, |
1037 |
> |
* under certain circumstances, be used to save allocation costs. |
1038 |
> |
* |
1039 |
> |
* <p>Suppose {@code x} is a deque known to contain only strings. |
1040 |
> |
* The following code can be used to dump the deque into a newly |
1041 |
> |
* allocated array of {@code String}: |
1042 |
|
* |
1043 |
< |
* <p>If the deque fits in the specified array with room to spare (i.e., |
1044 |
< |
* the array has more elements than the deque), the element in the array |
1045 |
< |
* immediately following the end of the collection is set to <tt>null</tt>. |
1043 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
1044 |
> |
* |
1045 |
> |
* Note that {@code toArray(new Object[0])} is identical in function to |
1046 |
> |
* {@code toArray()}. |
1047 |
|
* |
1048 |
|
* @param a the array into which the elements of the deque are to |
1049 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
1050 |
< |
* same runtime type is allocated for this purpose |
1051 |
< |
* @return an array containing the elements of the deque |
1052 |
< |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
1053 |
< |
* of the runtime type of every element in this deque |
1049 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
1050 |
> |
* same runtime type is allocated for this purpose |
1051 |
> |
* @return an array containing all of the elements in this deque |
1052 |
> |
* @throws ArrayStoreException if the runtime type of the specified array |
1053 |
> |
* is not a supertype of the runtime type of every element in |
1054 |
> |
* this deque |
1055 |
> |
* @throws NullPointerException if the specified array is null |
1056 |
|
*/ |
1057 |
+ |
@SuppressWarnings("unchecked") |
1058 |
|
public <T> T[] toArray(T[] a) { |
1059 |
< |
int size = size(); |
1060 |
< |
if (a.length < size) |
1061 |
< |
a = (T[])java.lang.reflect.Array.newInstance( |
1062 |
< |
a.getClass().getComponentType(), size); |
1063 |
< |
copyElements(a); |
1064 |
< |
if (a.length > size) |
1065 |
< |
a[size] = null; |
1059 |
> |
final Object[] elements = this.elements; |
1060 |
> |
final int head = this.head; |
1061 |
> |
final int firstLeg; |
1062 |
> |
boolean wrap = (firstLeg = elements.length - head) < size; |
1063 |
> |
if (size > a.length) { |
1064 |
> |
a = (T[]) Arrays.copyOfRange(elements, head, head + size, |
1065 |
> |
a.getClass()); |
1066 |
> |
} else { |
1067 |
> |
System.arraycopy(elements, head, a, 0, wrap ? firstLeg : size); |
1068 |
> |
if (size < a.length) |
1069 |
> |
a[size] = null; |
1070 |
> |
} |
1071 |
> |
if (wrap) |
1072 |
> |
System.arraycopy(elements, 0, a, firstLeg, size - firstLeg); |
1073 |
|
return a; |
1074 |
|
} |
1075 |
|
|
1081 |
|
* @return a copy of this deque |
1082 |
|
*/ |
1083 |
|
public ArrayDeque<E> clone() { |
1084 |
< |
try { |
1084 |
> |
try { |
1085 |
> |
@SuppressWarnings("unchecked") |
1086 |
|
ArrayDeque<E> result = (ArrayDeque<E>) super.clone(); |
1087 |
< |
// These two lines are currently faster than cloning the array: |
724 |
< |
result.elements = (E[]) new Object[elements.length]; |
725 |
< |
System.arraycopy(elements, 0, result.elements, 0, elements.length); |
1087 |
> |
result.elements = Arrays.copyOf(elements, elements.length); |
1088 |
|
return result; |
1089 |
< |
|
728 |
< |
} catch (CloneNotSupportedException e) { |
1089 |
> |
} catch (CloneNotSupportedException e) { |
1090 |
|
throw new AssertionError(); |
1091 |
|
} |
1092 |
|
} |
1093 |
|
|
733 |
– |
/** |
734 |
– |
* Appease the serialization gods. |
735 |
– |
*/ |
1094 |
|
private static final long serialVersionUID = 2340985798034038923L; |
1095 |
|
|
1096 |
|
/** |
1097 |
< |
* Serialize this deque. |
1097 |
> |
* Saves this deque to a stream (that is, serializes it). |
1098 |
|
* |
1099 |
< |
* @serialData The current size (<tt>int</tt>) of the deque, |
1099 |
> |
* @param s the stream |
1100 |
> |
* @throws java.io.IOException if an I/O error occurs |
1101 |
> |
* @serialData The current size ({@code int}) of the deque, |
1102 |
|
* followed by all of its elements (each an object reference) in |
1103 |
|
* first-to-last order. |
1104 |
|
*/ |
1105 |
< |
private void writeObject(ObjectOutputStream s) throws IOException { |
1105 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
1106 |
> |
throws java.io.IOException { |
1107 |
|
s.defaultWriteObject(); |
1108 |
|
|
1109 |
|
// Write out size |
749 |
– |
int size = size(); |
1110 |
|
s.writeInt(size); |
1111 |
|
|
1112 |
|
// Write out elements in order. |
1113 |
< |
int i = head; |
1114 |
< |
int mask = elements.length - 1; |
1115 |
< |
for (int j = 0; j < size; j++) { |
1113 |
> |
final Object[] elements = this.elements; |
1114 |
> |
final int capacity = elements.length; |
1115 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
1116 |
|
s.writeObject(elements[i]); |
757 |
– |
i = (i + 1) & mask; |
758 |
– |
} |
1117 |
|
} |
1118 |
|
|
1119 |
|
/** |
1120 |
< |
* Deserialize this deque. |
1120 |
> |
* Reconstitutes this deque from a stream (that is, deserializes it). |
1121 |
> |
* @param s the stream |
1122 |
> |
* @throws ClassNotFoundException if the class of a serialized object |
1123 |
> |
* could not be found |
1124 |
> |
* @throws java.io.IOException if an I/O error occurs |
1125 |
|
*/ |
1126 |
< |
private void readObject(ObjectInputStream s) |
1127 |
< |
throws IOException, ClassNotFoundException { |
1126 |
> |
private void readObject(java.io.ObjectInputStream s) |
1127 |
> |
throws java.io.IOException, ClassNotFoundException { |
1128 |
|
s.defaultReadObject(); |
1129 |
|
|
1130 |
|
// Read in size and allocate array |
1131 |
< |
int size = s.readInt(); |
770 |
< |
allocateElements(size); |
771 |
< |
head = 0; |
772 |
< |
tail = size; |
1131 |
> |
elements = new Object[size = s.readInt()]; |
1132 |
|
|
1133 |
|
// Read in all elements in the proper order. |
1134 |
|
for (int i = 0; i < size; i++) |
1135 |
< |
elements[i] = (E)s.readObject(); |
1135 |
> |
elements[i] = s.readObject(); |
1136 |
> |
} |
1137 |
|
|
1138 |
+ |
/** debugging */ |
1139 |
+ |
private void checkInvariants() { |
1140 |
+ |
try { |
1141 |
+ |
int capacity = elements.length; |
1142 |
+ |
assert size >= 0 && size <= capacity; |
1143 |
+ |
assert head >= 0 && ((capacity == 0 && head == 0 && size == 0) |
1144 |
+ |
|| head < capacity); |
1145 |
+ |
assert size == 0 |
1146 |
+ |
|| (elements[head] != null && elements[tail()] != null); |
1147 |
+ |
assert size == capacity |
1148 |
+ |
|| (elements[dec(head, capacity)] == null |
1149 |
+ |
&& elements[inc(tail(), capacity)] == null); |
1150 |
+ |
} catch (Throwable t) { |
1151 |
+ |
System.err.printf("head=%d size=%d capacity=%d%n", |
1152 |
+ |
head, size, elements.length); |
1153 |
+ |
System.err.printf("elements=%s%n", |
1154 |
+ |
Arrays.toString(elements)); |
1155 |
+ |
throw t; |
1156 |
+ |
} |
1157 |
|
} |
1158 |
+ |
|
1159 |
|
} |