KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.collections.Buffer;
19 import org.apache.commons.collections.collection.SynchronizedCollection;
20
21 /**
22  * Decorates another <code>Buffer</code> to synchronize its behaviour
23  * for a multi-threaded environment.
24  * <p>
25  * Methods are synchronized, then forwarded to the decorated buffer.
26  * <p>
27  * This class is Serializable from Commons Collections 3.1.
28  *
29  * @since Commons Collections 3.0
30  * @version $Revision: 1.6 $ $Date: 2004/06/03 22:02:13 $
31  *
32  * @author Stephen Colebourne
33  */

34 public class SynchronizedBuffer extends SynchronizedCollection implements Buffer {
35
36     /** Serialization version */
37     private static final long serialVersionUID = -6859936183953626253L;
38
39     /**
40      * Factory method to create a synchronized buffer.
41      *
42      * @param buffer the buffer to decorate, must not be null
43      * @return a new synchronized Buffer
44      * @throws IllegalArgumentException if buffer is null
45      */

46     public static Buffer decorate(Buffer buffer) {
47         return new SynchronizedBuffer(buffer);
48     }
49     
50     //-----------------------------------------------------------------------
51
/**
52      * Constructor that wraps (not copies).
53      *
54      * @param buffer the buffer to decorate, must not be null
55      * @throws IllegalArgumentException if the buffer is null
56      */

57     protected SynchronizedBuffer(Buffer buffer) {
58         super(buffer);
59     }
60
61     /**
62      * Constructor that wraps (not copies).
63      *
64      * @param buffer the buffer to decorate, must not be null
65      * @param lock the lock object to use, must not be null
66      * @throws IllegalArgumentException if the buffer is null
67      */

68     protected SynchronizedBuffer(Buffer buffer, Object JavaDoc lock) {
69         super(buffer, lock);
70     }
71
72     /**
73      * Gets the buffer being decorated.
74      *
75      * @return the decorated buffer
76      */

77     protected Buffer getBuffer() {
78         return (Buffer) collection;
79     }
80
81     //-----------------------------------------------------------------------
82
public Object JavaDoc get() {
83         synchronized (lock) {
84             return getBuffer().get();
85         }
86     }
87
88     public Object JavaDoc remove() {
89         synchronized (lock) {
90             return getBuffer().remove();
91         }
92     }
93     
94 }
95
Popular Tags