001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.bag;
017:
018: import java.util.Comparator;
019:
020: import org.apache.commons.collections.Bag;
021: import org.apache.commons.collections.SortedBag;
022:
023: /**
024: * Decorates another <code>SortedBag</code> to synchronize its behaviour
025: * for a multi-threaded environment.
026: * <p>
027: * Methods are synchronized, then forwarded to the decorated bag.
028: * Iterators must be separately synchronized around the loop.
029: * <p>
030: * This class is Serializable from Commons Collections 3.1.
031: *
032: * @since Commons Collections 3.0
033: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
034: *
035: * @author Stephen Colebourne
036: */
037: public class SynchronizedSortedBag extends SynchronizedBag implements
038: SortedBag {
039:
040: /** Serialization version */
041: private static final long serialVersionUID = 722374056718497858L;
042:
043: /**
044: * Factory method to create a synchronized sorted bag.
045: *
046: * @param bag the bag to decorate, must not be null
047: * @return a new synchronized SortedBag
048: * @throws IllegalArgumentException if bag is null
049: */
050: public static SortedBag decorate(SortedBag bag) {
051: return new SynchronizedSortedBag(bag);
052: }
053:
054: //-----------------------------------------------------------------------
055: /**
056: * Constructor that wraps (not copies).
057: *
058: * @param bag the bag to decorate, must not be null
059: * @throws IllegalArgumentException if bag is null
060: */
061: protected SynchronizedSortedBag(SortedBag bag) {
062: super (bag);
063: }
064:
065: /**
066: * Constructor that wraps (not copies).
067: *
068: * @param bag the bag to decorate, must not be null
069: * @param lock the lock to use, must not be null
070: * @throws IllegalArgumentException if bag is null
071: */
072: protected SynchronizedSortedBag(Bag bag, Object lock) {
073: super (bag, lock);
074: }
075:
076: /**
077: * Gets the bag being decorated.
078: *
079: * @return the decorated bag
080: */
081: protected SortedBag getSortedBag() {
082: return (SortedBag) collection;
083: }
084:
085: //-----------------------------------------------------------------------
086: public synchronized Object first() {
087: synchronized (lock) {
088: return getSortedBag().first();
089: }
090: }
091:
092: public synchronized Object last() {
093: synchronized (lock) {
094: return getSortedBag().last();
095: }
096: }
097:
098: public synchronized Comparator comparator() {
099: synchronized (lock) {
100: return getSortedBag().comparator();
101: }
102: }
103:
104: }
|