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.bag;
018
019 import java.util.Comparator;
020
021 import org.apache.commons.collections.Bag;
022 import org.apache.commons.collections.SortedBag;
023
024 /**
025 * Decorates another <code>SortedBag</code> to synchronize its behaviour
026 * for a multi-threaded environment.
027 * <p>
028 * Methods are synchronized, then forwarded to the decorated bag.
029 * Iterators must be separately synchronized around the loop.
030 * <p>
031 * This class is Serializable from Commons Collections 3.1.
032 *
033 * @since Commons Collections 3.0
034 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
035 *
036 * @author Stephen Colebourne
037 */
038 public class SynchronizedSortedBag
039 extends SynchronizedBag implements SortedBag {
040
041 /** Serialization version */
042 private static final long serialVersionUID = 722374056718497858L;
043
044 /**
045 * Factory method to create a synchronized sorted bag.
046 *
047 * @param bag the bag to decorate, must not be null
048 * @return a new synchronized SortedBag
049 * @throws IllegalArgumentException if bag is null
050 */
051 public static SortedBag decorate(SortedBag bag) {
052 return new SynchronizedSortedBag(bag);
053 }
054
055 //-----------------------------------------------------------------------
056 /**
057 * Constructor that wraps (not copies).
058 *
059 * @param bag the bag to decorate, must not be null
060 * @throws IllegalArgumentException if bag is null
061 */
062 protected SynchronizedSortedBag(SortedBag bag) {
063 super(bag);
064 }
065
066 /**
067 * Constructor that wraps (not copies).
068 *
069 * @param bag the bag to decorate, must not be null
070 * @param lock the lock to use, must not be null
071 * @throws IllegalArgumentException if bag is null
072 */
073 protected SynchronizedSortedBag(Bag bag, Object lock) {
074 super(bag, lock);
075 }
076
077 /**
078 * Gets the bag being decorated.
079 *
080 * @return the decorated bag
081 */
082 protected SortedBag getSortedBag() {
083 return (SortedBag) collection;
084 }
085
086 //-----------------------------------------------------------------------
087 public synchronized Object first() {
088 synchronized (lock) {
089 return getSortedBag().first();
090 }
091 }
092
093 public synchronized Object last() {
094 synchronized (lock) {
095 return getSortedBag().last();
096 }
097 }
098
099 public synchronized Comparator comparator() {
100 synchronized (lock) {
101 return getSortedBag().comparator();
102 }
103 }
104
105 }