97 |
|
*/ |
98 |
|
Node<E> next; |
99 |
|
|
100 |
< |
Node(E x, Node<E> p, Node<E> n) { |
100 |
> |
Node(E x) { |
101 |
|
item = x; |
102 |
– |
prev = p; |
103 |
– |
next = n; |
102 |
|
} |
103 |
|
} |
104 |
|
|
168 |
|
for (E e : c) { |
169 |
|
if (e == null) |
170 |
|
throw new NullPointerException(); |
171 |
< |
if (!linkLast(e)) |
171 |
> |
if (!linkLast(new Node<E>(e))) |
172 |
|
throw new IllegalStateException("Deque full"); |
173 |
|
} |
174 |
|
} finally { |
180 |
|
// Basic linking and unlinking operations, called only while holding lock |
181 |
|
|
182 |
|
/** |
183 |
< |
* Links e as first element, or returns false if full. |
183 |
> |
* Links node as first element, or returns false if full. |
184 |
|
*/ |
185 |
< |
private boolean linkFirst(E e) { |
185 |
> |
private boolean linkFirst(Node<E> node) { |
186 |
|
// assert lock.isHeldByCurrentThread(); |
187 |
|
if (count >= capacity) |
188 |
|
return false; |
189 |
|
Node<E> f = first; |
190 |
< |
Node<E> x = new Node<E>(e, null, f); |
191 |
< |
first = x; |
190 |
> |
node.next = f; |
191 |
> |
first = node; |
192 |
|
if (last == null) |
193 |
< |
last = x; |
193 |
> |
last = node; |
194 |
|
else |
195 |
< |
f.prev = x; |
195 |
> |
f.prev = node; |
196 |
|
++count; |
197 |
|
notEmpty.signal(); |
198 |
|
return true; |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
< |
* Links e as last element, or returns false if full. |
202 |
> |
* Links node as last element, or returns false if full. |
203 |
|
*/ |
204 |
< |
private boolean linkLast(E e) { |
204 |
> |
private boolean linkLast(Node<E> node) { |
205 |
|
// assert lock.isHeldByCurrentThread(); |
206 |
|
if (count >= capacity) |
207 |
|
return false; |
208 |
|
Node<E> l = last; |
209 |
< |
Node<E> x = new Node<E>(e, l, null); |
210 |
< |
last = x; |
209 |
> |
node.prev = l; |
210 |
> |
last = node; |
211 |
|
if (first == null) |
212 |
< |
first = x; |
212 |
> |
first = node; |
213 |
|
else |
214 |
< |
l.next = x; |
214 |
> |
l.next = node; |
215 |
|
++count; |
216 |
|
notEmpty.signal(); |
217 |
|
return true; |
308 |
|
*/ |
309 |
|
public boolean offerFirst(E e) { |
310 |
|
if (e == null) throw new NullPointerException(); |
311 |
+ |
Node<E> node = new Node<E>(e); |
312 |
|
final ReentrantLock lock = this.lock; |
313 |
|
lock.lock(); |
314 |
|
try { |
315 |
< |
return linkFirst(e); |
315 |
> |
return linkFirst(node); |
316 |
|
} finally { |
317 |
|
lock.unlock(); |
318 |
|
} |
323 |
|
*/ |
324 |
|
public boolean offerLast(E e) { |
325 |
|
if (e == null) throw new NullPointerException(); |
326 |
+ |
Node<E> node = new Node<E>(e); |
327 |
|
final ReentrantLock lock = this.lock; |
328 |
|
lock.lock(); |
329 |
|
try { |
330 |
< |
return linkLast(e); |
330 |
> |
return linkLast(node); |
331 |
|
} finally { |
332 |
|
lock.unlock(); |
333 |
|
} |
339 |
|
*/ |
340 |
|
public void putFirst(E e) throws InterruptedException { |
341 |
|
if (e == null) throw new NullPointerException(); |
342 |
+ |
Node<E> node = new Node<E>(e); |
343 |
|
final ReentrantLock lock = this.lock; |
344 |
|
lock.lock(); |
345 |
|
try { |
346 |
< |
while (!linkFirst(e)) |
346 |
> |
while (!linkFirst(node)) |
347 |
|
notFull.await(); |
348 |
|
} finally { |
349 |
|
lock.unlock(); |
356 |
|
*/ |
357 |
|
public void putLast(E e) throws InterruptedException { |
358 |
|
if (e == null) throw new NullPointerException(); |
359 |
+ |
Node<E> node = new Node<E>(e); |
360 |
|
final ReentrantLock lock = this.lock; |
361 |
|
lock.lock(); |
362 |
|
try { |
363 |
< |
while (!linkLast(e)) |
363 |
> |
while (!linkLast(node)) |
364 |
|
notFull.await(); |
365 |
|
} finally { |
366 |
|
lock.unlock(); |
374 |
|
public boolean offerFirst(E e, long timeout, TimeUnit unit) |
375 |
|
throws InterruptedException { |
376 |
|
if (e == null) throw new NullPointerException(); |
377 |
+ |
Node<E> node = new Node<E>(e); |
378 |
|
long nanos = unit.toNanos(timeout); |
379 |
|
final ReentrantLock lock = this.lock; |
380 |
|
lock.lockInterruptibly(); |
381 |
|
try { |
382 |
< |
while (!linkFirst(e)) { |
382 |
> |
while (!linkFirst(node)) { |
383 |
|
if (nanos <= 0) |
384 |
|
return false; |
385 |
|
nanos = notFull.awaitNanos(nanos); |
397 |
|
public boolean offerLast(E e, long timeout, TimeUnit unit) |
398 |
|
throws InterruptedException { |
399 |
|
if (e == null) throw new NullPointerException(); |
400 |
+ |
Node<E> node = new Node<E>(e); |
401 |
|
long nanos = unit.toNanos(timeout); |
402 |
|
final ReentrantLock lock = this.lock; |
403 |
|
lock.lockInterruptibly(); |
404 |
|
try { |
405 |
< |
while (!linkLast(e)) { |
405 |
> |
while (!linkLast(node)) { |
406 |
|
if (nanos <= 0) |
407 |
|
return false; |
408 |
|
nanos = notFull.awaitNanos(nanos); |