KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > concurrent > TCBlockingLinkedQueue


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util.concurrent;
5
6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
7
8 import java.util.NoSuchElementException JavaDoc;
9
10 /**
11  * An implementation of the TC style blocking queue interface. Uses a <code>LinkedQueue</code> instance from Doug
12  * Lea's util.concurrent package to do the heavy lfting
13  *
14  * @author teck
15  */

16 public class TCBlockingLinkedQueue implements BlockingQueue {
17
18   private static final long NO_WAIT = 0;
19   private final LinkedQueue queue;
20
21   /**
22    * Factory method for creating instances of this class
23    *
24    * @return a new blocking queue instance
25    */

26   public TCBlockingLinkedQueue() {
27     queue = new LinkedQueue();
28   }
29
30   public boolean offer(Object JavaDoc o, long timeout) throws InterruptedException JavaDoc {
31     if (null == o) { throw new NullPointerException JavaDoc("Cannot add null item to queue"); }
32
33     return queue.offer(o, timeout);
34   }
35
36   public Object JavaDoc poll(long timeout) throws InterruptedException JavaDoc {
37     return queue.poll(timeout);
38   }
39
40   public Object JavaDoc take() throws InterruptedException JavaDoc {
41     return queue.take();
42   }
43
44   public boolean isEmpty() {
45     return queue.isEmpty();
46   }
47
48   public Object JavaDoc element() {
49     throw new UnsupportedOperationException JavaDoc();
50   }
51
52   public boolean offer(Object JavaDoc o) {
53     try {
54       return queue.offer(o, 0);
55     } catch (InterruptedException JavaDoc e) {
56       return false;
57     }
58   }
59
60   public Object JavaDoc peek() {
61     return queue.peek();
62   }
63
64   public Object JavaDoc poll() {
65     try {
66       return queue.poll(NO_WAIT);
67     } catch (InterruptedException JavaDoc e) {
68       return null;
69     }
70   }
71
72   public Object JavaDoc remove() {
73     Object JavaDoc rv = poll();
74
75     if (rv == null) { throw new NoSuchElementException JavaDoc(); }
76
77     return rv;
78   }
79
80 }
Popular Tags