KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > buffer > BlockingBuffer


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.buffer;
17
18 import java.util.Collection JavaDoc;
19
20 import org.apache.commons.collections.Buffer;
21 import org.apache.commons.collections.BufferUnderflowException;
22
23 /**
24  * Decorates another <code>Buffer</code> to make {@link #get()} and
25  * {@link #remove()} block when the <code>Buffer</code> is empty.
26  * <p>
27  * If either <code>get</code> or <code>remove</code> is called on an empty
28  * <code>Buffer</code>, the calling thread waits for notification that
29  * an <code>add</code> or <code>addAll</code> operation has completed.
30  * <p>
31  * When one or more entries are added to an empty <code>Buffer</code>,
32  * all threads blocked in <code>get</code> or <code>remove</code> are notified.
33  * There is no guarantee that concurrent blocked <code>get</code> or
34  * <code>remove</code> requests will be "unblocked" and receive data in the
35  * order that they arrive.
36  * <p>
37  * This class is Serializable from Commons Collections 3.1.
38  *
39  * @since Commons Collections 3.0
40  * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:13 $
41  *
42  * @author Stephen Colebourne
43  * @author Janek Bogucki
44  * @author Phil Steitz
45  */

46 public class BlockingBuffer extends SynchronizedBuffer {
47
48     /** Serialization version */
49     private static final long serialVersionUID = 1719328905017860541L;
50
51     /**
52      * Factory method to create a blocking buffer.
53      *
54      * @param buffer the buffer to decorate, must not be null
55      * @return a new blocking Buffer
56      * @throws IllegalArgumentException if buffer is null
57      */

58     public static Buffer decorate(Buffer buffer) {
59         return new BlockingBuffer(buffer);
60     }
61
62     //-----------------------------------------------------------------------
63
/**
64      * Constructor that wraps (not copies).
65      *
66      * @param buffer the buffer to decorate, must not be null
67      * @throws IllegalArgumentException if the buffer is null
68      */

69     protected BlockingBuffer(Buffer buffer) {
70         super(buffer);
71     }
72
73     //-----------------------------------------------------------------------
74
public boolean add(Object JavaDoc o) {
75         synchronized (lock) {
76             boolean result = collection.add(o);
77             notifyAll();
78             return result;
79         }
80     }
81     
82     public boolean addAll(Collection JavaDoc c) {
83         synchronized (lock) {
84             boolean result = collection.addAll(c);
85             notifyAll();
86             return result;
87         }
88     }
89     
90     public Object JavaDoc get() {
91         synchronized (lock) {
92             while (collection.isEmpty()) {
93                 try {
94                     wait();
95                 } catch (InterruptedException JavaDoc e) {
96                     throw new BufferUnderflowException();
97                 }
98             }
99             return getBuffer().get();
100         }
101     }
102     
103     public Object JavaDoc remove() {
104         synchronized (lock) {
105             while (collection.isEmpty()) {
106                 try {
107                     wait();
108                 } catch (InterruptedException JavaDoc e) {
109                     throw new BufferUnderflowException();
110                 }
111             }
112             return getBuffer().remove();
113         }
114     }
115     
116 }
117
Popular Tags