View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.util;
21  
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.ListIterator;
26  
27  /**
28   * A synchronized version of {@link Queue}.
29   * 
30   * @author The Apache Directory Project (mina-dev@directory.apache.org)
31   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $
32   */
33  public class BlockingQueue extends Queue {
34      private static final long serialVersionUID = 5516588196355725567L;
35  
36      private int waiters = 0;
37  
38      public BlockingQueue() {
39      }
40  
41      /**
42       * Waits until any elements are in this queue.
43       * 
44       * @throws InterruptedException
45       *             if the current thread is interrupted
46       */
47      public synchronized void waitForNewItem() throws InterruptedException {
48          waiters++;
49          try {
50              while (super.isEmpty()) {
51                  wait();
52              }
53          } finally {
54              waiters--;
55          }
56      }
57  
58      public synchronized void push(Object obj) {
59          super.push(obj);
60          notifyAdded();
61      }
62  
63      public synchronized void add(int idx, Object o) {
64          super.add(idx, o);
65          notifyAdded();
66      }
67  
68      public synchronized boolean add(Object o) {
69          if (super.add(o)) {
70              notifyAdded();
71              return true;
72          } else {
73              return false;
74          }
75      }
76  
77      public synchronized boolean addAll(int arg0, Collection arg1) {
78          if (super.addAll(arg0, arg1)) {
79              notifyAdded();
80              return true;
81          } else {
82              return false;
83          }
84      }
85  
86      public synchronized boolean addAll(Collection arg0) {
87          if (super.addAll(arg0)) {
88              notifyAdded();
89              return true;
90          } else {
91              return false;
92          }
93      }
94  
95      public synchronized boolean offer(Object o) {
96          if (super.offer(o)) {
97              notifyAdded();
98              return true;
99          } else {
100             return false;
101         }
102     }
103 
104     private void notifyAdded() {
105         if (waiters > 0)
106             notify();
107     }
108 
109     public synchronized int capacity() {
110         return super.capacity();
111     }
112 
113     public synchronized void clear() {
114         super.clear();
115     }
116 
117     public synchronized Object first() {
118         return super.first();
119     }
120 
121     public synchronized Object get(int idx) {
122         return super.get(idx);
123     }
124 
125     public synchronized boolean isEmpty() {
126         return super.isEmpty();
127     }
128 
129     public synchronized Object last() {
130         return super.last();
131     }
132 
133     public synchronized Object pop() {
134         return super.pop();
135     }
136 
137     public synchronized int size() {
138         return super.size();
139     }
140 
141     public synchronized String toString() {
142         return super.toString();
143     }
144 
145     public synchronized Object remove(int idx) {
146         return super.remove(idx);
147     }
148 
149     public synchronized Object set(int idx, Object o) {
150         return super.set(idx, o);
151     }
152 
153     public synchronized boolean equals(Object o) {
154         return super.equals(o);
155     }
156 
157     public synchronized int hashCode() {
158         return super.hashCode();
159     }
160 
161     public synchronized int indexOf(Object o) {
162         return super.indexOf(o);
163     }
164 
165     public synchronized Iterator iterator() {
166         return super.iterator();
167     }
168 
169     public synchronized int lastIndexOf(Object o) {
170         return super.lastIndexOf(o);
171     }
172 
173     public synchronized ListIterator listIterator() {
174         return super.listIterator();
175     }
176 
177     public synchronized ListIterator listIterator(int index) {
178         return super.listIterator(index);
179     }
180 
181     public synchronized List subList(int fromIndex, int toIndex) {
182         return super.subList(fromIndex, toIndex);
183     }
184 
185     public synchronized boolean contains(Object o) {
186         return super.contains(o);
187     }
188 
189     public synchronized boolean containsAll(Collection arg0) {
190         return super.containsAll(arg0);
191     }
192 
193     public synchronized boolean remove(Object o) {
194         return super.remove(o);
195     }
196 
197     public synchronized boolean removeAll(Collection arg0) {
198         return super.removeAll(arg0);
199     }
200 
201     public synchronized boolean retainAll(Collection arg0) {
202         return super.retainAll(arg0);
203     }
204 
205     public synchronized Object[] toArray() {
206         return super.toArray();
207     }
208 
209     public synchronized Object[] toArray(Object[] arg0) {
210         return super.toArray(arg0);
211     }
212 
213     public synchronized Object element() {
214         return super.element();
215     }
216 
217     public synchronized Object peek() {
218         return super.peek();
219     }
220 
221     public synchronized Object poll() {
222         return super.poll();
223     }
224 
225     public synchronized Object remove() {
226         return super.remove();
227     }
228 
229 }