1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
|
|
import java.util.*; |
9 |
|
|
|
10 |
|
|
/** |
11 |
|
|
* Static methods that operate on or return instances of collection |
12 |
|
|
* and synchronizer classes and interfaces defined in this package. |
13 |
|
|
* |
14 |
|
|
* @since 1.6 |
15 |
|
|
* @author Doug Lea |
16 |
|
|
*/ |
17 |
|
|
|
18 |
|
|
|
19 |
|
|
public class Concurrent { |
20 |
|
|
/** |
21 |
|
|
* Returns a view of a {@link BlockingDeque} as a stack-based |
22 |
|
|
* Last-in-first-out (Lifo) {@link BlockingQueue}. Method |
23 |
|
|
* <tt>put</tt> is mapped to <tt>putFirst</tt>, <tt>take</tt> is |
24 |
|
|
* mapped to <tt>takeFirst</tt> and so on. This view can be useful |
25 |
|
|
* when you would like to use a method requiring a |
26 |
|
|
* <tt>BlockingQueue</tt> but you need Lifo ordering. |
27 |
|
|
* @param deque the BlockingDeque |
28 |
|
|
* @return the queue |
29 |
|
|
*/ |
30 |
|
|
public static <T> BlockingQueue<T> asLifoBlockingQueue(BlockingDeque<T> deque){ |
31 |
|
|
return new AsLIFOBlockingQueue<T>(deque); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
static class AsLIFOBlockingQueue<E> extends AbstractQueue<E> |
35 |
|
|
implements BlockingQueue<E>, java.io.Serializable { |
36 |
|
|
private final BlockingDeque<E> q; |
37 |
|
|
AsLIFOBlockingQueue(BlockingDeque<E> q) { this.q = q; } |
38 |
|
|
public boolean offer(E o) { return q.offerFirst(o); } |
39 |
|
|
public E poll() { return q.pollFirst(); } |
40 |
|
|
public E remove() { return q.removeFirst(); } |
41 |
|
|
public E peek() { return q.peekFirst(); } |
42 |
|
|
public E element() { return q.getFirst(); } |
43 |
|
|
public int size() { return q.size(); } |
44 |
|
|
public boolean isEmpty() { return q.isEmpty(); } |
45 |
|
|
public boolean contains(Object o) { return q.contains(o); } |
46 |
|
|
public Iterator<E> iterator() { return q.iterator(); } |
47 |
|
|
public Object[] toArray() { return q.toArray(); } |
48 |
|
|
public <T> T[] toArray(T[] a) { return q.toArray(a); } |
49 |
|
|
public boolean add(E o) { return q.offerFirst(o); } |
50 |
|
|
public boolean remove(Object o) { return q.remove(o); } |
51 |
|
|
public void clear() { q.clear(); } |
52 |
|
|
public int remainingCapacity() { return q.remainingCapacity(); } |
53 |
|
|
public int drainTo(Collection<? super E> c) { return q.drainTo(c); } |
54 |
|
|
public int drainTo(Collection<? super E> c, int m) { |
55 |
|
|
return q.drainTo(c, m); |
56 |
|
|
} |
57 |
|
|
public void put(E o) throws InterruptedException { q.putFirst(o); } |
58 |
|
|
public E take() throws InterruptedException { return q.takeFirst(); } |
59 |
|
|
public boolean offer(E o, long timeout, TimeUnit unit) |
60 |
|
|
throws InterruptedException { |
61 |
|
|
return q.offerFirst(o, timeout, unit); |
62 |
|
|
} |
63 |
|
|
public E poll(long timeout, TimeUnit unit) |
64 |
|
|
throws InterruptedException { |
65 |
|
|
return q.pollFirst(timeout, unit); |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
} |