01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */package com.tc.util.concurrent;
04:
05: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
06:
07: import java.util.NoSuchElementException;
08:
09: /**
10: * An implementation of the TC style blocking queue interface. Uses a <code>LinkedQueue</code> instance from Doug
11: * Lea's util.concurrent package to do the heavy lfting
12: *
13: * @author teck
14: */
15: public class TCBlockingLinkedQueue implements BlockingQueue {
16:
17: private static final long NO_WAIT = 0;
18: private final LinkedQueue queue;
19:
20: /**
21: * Factory method for creating instances of this class
22: *
23: * @return a new blocking queue instance
24: */
25: public TCBlockingLinkedQueue() {
26: queue = new LinkedQueue();
27: }
28:
29: public boolean offer(Object o, long timeout)
30: throws InterruptedException {
31: if (null == o) {
32: throw new NullPointerException(
33: "Cannot add null item to queue");
34: }
35:
36: return queue.offer(o, timeout);
37: }
38:
39: public Object poll(long timeout) throws InterruptedException {
40: return queue.poll(timeout);
41: }
42:
43: public Object take() throws InterruptedException {
44: return queue.take();
45: }
46:
47: public boolean isEmpty() {
48: return queue.isEmpty();
49: }
50:
51: public Object element() {
52: throw new UnsupportedOperationException();
53: }
54:
55: public boolean offer(Object o) {
56: try {
57: return queue.offer(o, 0);
58: } catch (InterruptedException e) {
59: return false;
60: }
61: }
62:
63: public Object peek() {
64: return queue.peek();
65: }
66:
67: public Object poll() {
68: try {
69: return queue.poll(NO_WAIT);
70: } catch (InterruptedException e) {
71: return null;
72: }
73: }
74:
75: public Object remove() {
76: Object rv = poll();
77:
78: if (rv == null) {
79: throw new NoSuchElementException();
80: }
81:
82: return rv;
83: }
84:
85: }
|