48 |
|
|
49 |
|
/** Doubly-linked list node class */ |
50 |
|
static final class Node<E> { |
51 |
< |
E item; |
51 |
> |
E item; |
52 |
|
Node<E> prev; |
53 |
|
Node<E> next; |
54 |
|
Node(E x, Node<E> p, Node<E> n) { |
99 |
|
* added in traversal order of the collection's iterator. |
100 |
|
* @param c the collection of elements to initially contain |
101 |
|
* @throws NullPointerException if <tt>c</tt> or any element within it |
102 |
< |
* is <tt>null</tt> |
102 |
> |
* is <tt>null</tt>. |
103 |
|
*/ |
104 |
|
public LinkedBlockingDeque(Collection<? extends E> c) { |
105 |
|
this(Integer.MAX_VALUE); |
111 |
|
// Basic linking and unlinking operations, called only while holding lock |
112 |
|
|
113 |
|
/** |
114 |
< |
* Link e as first element, or return false if full |
114 |
> |
* Links e as first element, or returns false if full. |
115 |
|
*/ |
116 |
|
private boolean linkFirst(E e) { |
117 |
|
if (count >= capacity) |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
< |
* Link e as last element, or return false if full |
132 |
> |
* Links e as last element, or returns false if full. |
133 |
|
*/ |
134 |
|
private boolean linkLast(E e) { |
135 |
|
if (count >= capacity) |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
< |
* Remove and return first element, or null if empty |
150 |
> |
* Removes and returns first element, or null if empty. |
151 |
|
*/ |
152 |
|
private E unlinkFirst() { |
153 |
|
Node<E> f = first; |
155 |
|
return null; |
156 |
|
Node<E> n = f.next; |
157 |
|
first = n; |
158 |
< |
if (n == null) |
158 |
> |
if (n == null) |
159 |
|
last = null; |
160 |
< |
else |
160 |
> |
else |
161 |
|
n.prev = null; |
162 |
|
--count; |
163 |
|
notFull.signal(); |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
< |
* Remove and return last element, or null if empty |
168 |
> |
* Removes and returns last element, or null if empty. |
169 |
|
*/ |
170 |
|
private E unlinkLast() { |
171 |
|
Node<E> l = last; |
173 |
|
return null; |
174 |
|
Node<E> p = l.prev; |
175 |
|
last = p; |
176 |
< |
if (p == null) |
176 |
> |
if (p == null) |
177 |
|
first = null; |
178 |
< |
else |
178 |
> |
else |
179 |
|
p.next = null; |
180 |
|
--count; |
181 |
|
notFull.signal(); |
189 |
|
Node<E> p = x.prev; |
190 |
|
Node<E> n = x.next; |
191 |
|
if (p == null) { |
192 |
< |
if (n == null) |
192 |
> |
if (n == null) |
193 |
|
first = last = null; |
194 |
|
else { |
195 |
|
n.prev = null; |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
< |
public void addFirst(E e) { |
231 |
> |
public void addFirst(E e) { |
232 |
|
if (!offerFirst(e)) |
233 |
|
throw new IllegalStateException("Deque full"); |
234 |
|
} |
235 |
|
|
236 |
< |
public void addLast(E e) { |
236 |
> |
public void addLast(E e) { |
237 |
|
if (!offerLast(e)) |
238 |
|
throw new IllegalStateException("Deque full"); |
239 |
|
} |
363 |
|
lock.unlock(); |
364 |
|
} |
365 |
|
} |
366 |
< |
|
366 |
> |
|
367 |
|
public boolean offerLast(E o, long timeout, TimeUnit unit) |
368 |
|
throws InterruptedException { |
369 |
|
if (o == null) throw new NullPointerException(); |
382 |
|
} |
383 |
|
} |
384 |
|
|
385 |
< |
public E pollFirst(long timeout, TimeUnit unit) |
385 |
> |
public E pollFirst(long timeout, TimeUnit unit) |
386 |
|
throws InterruptedException { |
387 |
|
lock.lockInterruptibly(); |
388 |
|
try { |
400 |
|
} |
401 |
|
} |
402 |
|
|
403 |
< |
public E pollLast(long timeout, TimeUnit unit) |
403 |
> |
public E pollLast(long timeout, TimeUnit unit) |
404 |
|
throws InterruptedException { |
405 |
|
lock.lockInterruptibly(); |
406 |
|
try { |
477 |
|
if (o == null) return false; |
478 |
|
lock.lock(); |
479 |
|
try { |
480 |
< |
for (Node<E> p = first; p != null; p = p.next) |
480 |
> |
for (Node<E> p = first; p != null; p = p.next) |
481 |
|
if (o.equals(p.item)) |
482 |
|
return true; |
483 |
|
return false; |
542 |
|
try { |
543 |
|
Object[] a = new Object[count]; |
544 |
|
int k = 0; |
545 |
< |
for (Node<E> p = first; p != null; p = p.next) |
545 |
> |
for (Node<E> p = first; p != null; p = p.next) |
546 |
|
a[k++] = p.item; |
547 |
|
return a; |
548 |
|
} finally { |
560 |
|
); |
561 |
|
|
562 |
|
int k = 0; |
563 |
< |
for (Node<E> p = first; p != null; p = p.next) |
563 |
> |
for (Node<E> p = first; p != null; p = p.next) |
564 |
|
a[k++] = (T)p.item; |
565 |
|
if (a.length > k) |
566 |
|
a[k] = null; |
601 |
|
throw new IllegalArgumentException(); |
602 |
|
lock.lock(); |
603 |
|
try { |
604 |
< |
for (Node<E> p = first; p != null; p = p.next) |
604 |
> |
for (Node<E> p = first; p != null; p = p.next) |
605 |
|
c.add(p.item); |
606 |
|
int n = count; |
607 |
|
count = 0; |
640 |
|
/** |
641 |
|
* Returns an iterator over the elements in this deque in proper sequence. |
642 |
|
* The returned <tt>Iterator</tt> is a "weakly consistent" iterator that |
643 |
< |
* will never throw {@link java.util.ConcurrentModificationException}, |
643 |
> |
* will never throw {@link ConcurrentModificationException}, |
644 |
|
* and guarantees to traverse elements as they existed upon |
645 |
|
* construction of the iterator, and may (but is not guaranteed to) |
646 |
|
* reflect any modifications subsequent to construction. |
662 |
|
* an element exists in hasNext(), we must return item read |
663 |
|
* under lock (in advance()) even if it was in the process of |
664 |
|
* being removed when hasNext() was called. |
665 |
< |
**/ |
665 |
> |
*/ |
666 |
|
private E nextItem; |
667 |
|
|
668 |
|
/** |
678 |
|
/** |
679 |
|
* Advance next, or if not yet initialized, set to first node. |
680 |
|
*/ |
681 |
< |
private void advance() { |
681 |
> |
private void advance() { |
682 |
|
final ReentrantLock lock = LinkedBlockingDeque.this.lock; |
683 |
|
lock.lock(); |
684 |
|
try { |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
< |
* Save the state to a stream (that is, serialize it). |
718 |
> |
* Save the state of this deque to a stream (that is, serialize it). |
719 |
|
* |
720 |
|
* @serialData The capacity (int), followed by elements (each an |
721 |
|
* <tt>Object</tt>) in the proper order, followed by a null |
738 |
|
} |
739 |
|
|
740 |
|
/** |
741 |
< |
* Reconstitute this deque instance from a stream (that is, |
741 |
> |
* Reconstitute this deque from a stream (that is, |
742 |
|
* deserialize it). |
743 |
|
* @param s the stream |
744 |
|
*/ |
756 |
|
add(item); |
757 |
|
} |
758 |
|
} |
759 |
< |
|
759 |
> |
|
760 |
|
} |