1 | /* |
2 | * @(#) $Id: BlockingQueue.java 327113 2005-10-21 06:59:15Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */ |
19 | package org.apache.mina.util; |
20 | |
21 | import java.util.Collection; |
22 | import java.util.Iterator; |
23 | import java.util.List; |
24 | import java.util.ListIterator; |
25 | |
26 | /** |
27 | * A synchronized version of {@link Queue}. |
28 | * |
29 | * @author The Apache Directory Project (dev@directory.apache.org) |
30 | * @version $Rev: 327113 $, $Date: 2005-10-21 15:59:15 +0900 $ |
31 | */ |
32 | public class BlockingQueue extends Queue |
33 | { |
34 | private static final long serialVersionUID = 5516588196355725567L; |
35 | |
36 | private int waiters = 0; |
37 | |
38 | public BlockingQueue() |
39 | { |
40 | } |
41 | |
42 | /** |
43 | * Waits until any elements are in this queue. |
44 | * |
45 | * @throws InterruptedException |
46 | * if the current thread is interrupted |
47 | */ |
48 | public synchronized void waitForNewItem() throws InterruptedException |
49 | { |
50 | waiters++; |
51 | try |
52 | { |
53 | while( super.isEmpty() ) |
54 | { |
55 | wait(); |
56 | } |
57 | } |
58 | finally |
59 | { |
60 | waiters--; |
61 | } |
62 | } |
63 | |
64 | public synchronized void push( Object obj ) |
65 | { |
66 | super.push( obj ); |
67 | notifyAdded(); |
68 | } |
69 | |
70 | public synchronized void add( int idx, Object o ) |
71 | { |
72 | super.add( idx, o ); |
73 | notifyAdded(); |
74 | } |
75 | |
76 | public synchronized boolean add( Object o ) |
77 | { |
78 | if( super.add( o ) ) |
79 | { |
80 | notifyAdded(); |
81 | return true; |
82 | } |
83 | else |
84 | { |
85 | return false; |
86 | } |
87 | } |
88 | |
89 | public synchronized boolean addAll( int arg0, Collection arg1 ) |
90 | { |
91 | if( super.addAll( arg0, arg1 ) ) |
92 | { |
93 | notifyAdded(); |
94 | return true; |
95 | } |
96 | else |
97 | { |
98 | return false; |
99 | } |
100 | } |
101 | |
102 | public synchronized boolean addAll( Collection arg0 ) |
103 | { |
104 | if( super.addAll( arg0 ) ) |
105 | { |
106 | notifyAdded(); |
107 | return true; |
108 | } |
109 | else |
110 | { |
111 | return false; |
112 | } |
113 | } |
114 | |
115 | public synchronized boolean offer( Object o ) |
116 | { |
117 | if( super.offer( o ) ) |
118 | { |
119 | notifyAdded(); |
120 | return true; |
121 | } |
122 | else |
123 | { |
124 | return false; |
125 | } |
126 | } |
127 | |
128 | private void notifyAdded() |
129 | { |
130 | if( waiters > 0 ) |
131 | notify(); |
132 | } |
133 | |
134 | public synchronized int capacity() |
135 | { |
136 | return super.capacity(); |
137 | } |
138 | |
139 | public synchronized void clear() |
140 | { |
141 | super.clear(); |
142 | } |
143 | |
144 | public synchronized Object first() |
145 | { |
146 | return super.first(); |
147 | } |
148 | |
149 | public synchronized Object get( int idx ) |
150 | { |
151 | return super.get( idx ); |
152 | } |
153 | |
154 | public synchronized boolean isEmpty() |
155 | { |
156 | return super.isEmpty(); |
157 | } |
158 | |
159 | public synchronized Object last() |
160 | { |
161 | return super.last(); |
162 | } |
163 | |
164 | public synchronized Object pop() |
165 | { |
166 | return super.pop(); |
167 | } |
168 | |
169 | public synchronized int size() |
170 | { |
171 | return super.size(); |
172 | } |
173 | |
174 | public synchronized String toString() |
175 | { |
176 | return super.toString(); |
177 | } |
178 | |
179 | public synchronized Object remove( int idx ) |
180 | { |
181 | return super.remove( idx ); |
182 | } |
183 | |
184 | public synchronized Object set( int idx, Object o ) |
185 | { |
186 | return super.set( idx, o ); |
187 | } |
188 | |
189 | public synchronized boolean equals( Object o ) |
190 | { |
191 | return super.equals( o ); |
192 | } |
193 | |
194 | public synchronized int hashCode() |
195 | { |
196 | return super.hashCode(); |
197 | } |
198 | |
199 | public synchronized int indexOf( Object o ) |
200 | { |
201 | return super.indexOf( o ); |
202 | } |
203 | |
204 | public synchronized Iterator iterator() |
205 | { |
206 | return super.iterator(); |
207 | } |
208 | |
209 | public synchronized int lastIndexOf( Object o ) |
210 | { |
211 | return super.lastIndexOf( o ); |
212 | } |
213 | |
214 | public synchronized ListIterator listIterator() |
215 | { |
216 | return super.listIterator(); |
217 | } |
218 | |
219 | public synchronized ListIterator listIterator( int index ) |
220 | { |
221 | return super.listIterator( index ); |
222 | } |
223 | |
224 | public synchronized List subList( int fromIndex, int toIndex ) |
225 | { |
226 | return super.subList( fromIndex, toIndex ); |
227 | } |
228 | |
229 | public synchronized boolean contains( Object o ) |
230 | { |
231 | return super.contains( o ); |
232 | } |
233 | |
234 | public synchronized boolean containsAll( Collection arg0 ) |
235 | { |
236 | return super.containsAll( arg0 ); |
237 | } |
238 | |
239 | public synchronized boolean remove( Object o ) |
240 | { |
241 | return super.remove( o ); |
242 | } |
243 | |
244 | public synchronized boolean removeAll( Collection arg0 ) |
245 | { |
246 | return super.removeAll( arg0 ); |
247 | } |
248 | |
249 | public synchronized boolean retainAll( Collection arg0 ) |
250 | { |
251 | return super.retainAll( arg0 ); |
252 | } |
253 | |
254 | public synchronized Object[] toArray() |
255 | { |
256 | return super.toArray(); |
257 | } |
258 | |
259 | public synchronized Object[] toArray( Object[] arg0 ) |
260 | { |
261 | return super.toArray( arg0 ); |
262 | } |
263 | |
264 | public synchronized Object element() |
265 | { |
266 | return super.element(); |
267 | } |
268 | |
269 | public synchronized Object peek() |
270 | { |
271 | return super.peek(); |
272 | } |
273 | |
274 | public synchronized Object poll() |
275 | { |
276 | return super.poll(); |
277 | } |
278 | |
279 | public synchronized Object remove() |
280 | { |
281 | return super.remove(); |
282 | } |
283 | |
284 | |
285 | } |