01: /*
02: * Copyright 2003-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
31: *
32: * @author Stephen Colebourne
33: */
34: public class SynchronizedBuffer extends SynchronizedCollection
35: implements Buffer {
36:
37: /** Serialization version */
38: private static final long serialVersionUID = -6859936183953626253L;
39:
40: /**
41: * Factory method to create a synchronized buffer.
42: *
43: * @param buffer the buffer to decorate, must not be null
44: * @return a new synchronized Buffer
45: * @throws IllegalArgumentException if buffer is null
46: */
47: public static Buffer decorate(Buffer buffer) {
48: return new SynchronizedBuffer(buffer);
49: }
50:
51: //-----------------------------------------------------------------------
52: /**
53: * Constructor that wraps (not copies).
54: *
55: * @param buffer the buffer to decorate, must not be null
56: * @throws IllegalArgumentException if the buffer is null
57: */
58: protected SynchronizedBuffer(Buffer buffer) {
59: super (buffer);
60: }
61:
62: /**
63: * Constructor that wraps (not copies).
64: *
65: * @param buffer the buffer to decorate, must not be null
66: * @param lock the lock object to use, must not be null
67: * @throws IllegalArgumentException if the buffer is null
68: */
69: protected SynchronizedBuffer(Buffer buffer, Object lock) {
70: super (buffer, lock);
71: }
72:
73: /**
74: * Gets the buffer being decorated.
75: *
76: * @return the decorated buffer
77: */
78: protected Buffer getBuffer() {
79: return (Buffer) collection;
80: }
81:
82: //-----------------------------------------------------------------------
83: public Object get() {
84: synchronized (lock) {
85: return getBuffer().get();
86: }
87: }
88:
89: public Object remove() {
90: synchronized (lock) {
91: return getBuffer().remove();
92: }
93: }
94:
95: }
|