001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.collections.buffer;
018
019 import org.apache.commons.collections.Buffer;
020 import org.apache.commons.collections.collection.SynchronizedCollection;
021
022 /**
023 * Decorates another <code>Buffer</code> to synchronize its behaviour
024 * for a multi-threaded environment.
025 * <p>
026 * Methods are synchronized, then forwarded to the decorated buffer.
027 * <p>
028 * This class is Serializable from Commons Collections 3.1.
029 *
030 * @since Commons Collections 3.0
031 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
032 *
033 * @author Stephen Colebourne
034 */
035 public class SynchronizedBuffer extends SynchronizedCollection implements Buffer {
036
037 /** Serialization version */
038 private static final long serialVersionUID = -6859936183953626253L;
039
040 /**
041 * Factory method to create a synchronized buffer.
042 *
043 * @param buffer the buffer to decorate, must not be null
044 * @return a new synchronized Buffer
045 * @throws IllegalArgumentException if buffer is null
046 */
047 public static Buffer decorate(Buffer buffer) {
048 return new SynchronizedBuffer(buffer);
049 }
050
051 //-----------------------------------------------------------------------
052 /**
053 * Constructor that wraps (not copies).
054 *
055 * @param buffer the buffer to decorate, must not be null
056 * @throws IllegalArgumentException if the buffer is null
057 */
058 protected SynchronizedBuffer(Buffer buffer) {
059 super(buffer);
060 }
061
062 /**
063 * Constructor that wraps (not copies).
064 *
065 * @param buffer the buffer to decorate, must not be null
066 * @param lock the lock object to use, must not be null
067 * @throws IllegalArgumentException if the buffer is null
068 */
069 protected SynchronizedBuffer(Buffer buffer, Object lock) {
070 super(buffer, lock);
071 }
072
073 /**
074 * Gets the buffer being decorated.
075 *
076 * @return the decorated buffer
077 */
078 protected Buffer getBuffer() {
079 return (Buffer) collection;
080 }
081
082 //-----------------------------------------------------------------------
083 public Object get() {
084 synchronized (lock) {
085 return getBuffer().get();
086 }
087 }
088
089 public Object remove() {
090 synchronized (lock) {
091 return getBuffer().remove();
092 }
093 }
094
095 }