KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bag > SynchronizedSortedBag


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.bag;
17
18 import java.util.Comparator JavaDoc;
19
20 import org.apache.commons.collections.Bag;
21 import org.apache.commons.collections.SortedBag;
22
23 /**
24  * Decorates another <code>SortedBag</code> to synchronize its behaviour
25  * for a multi-threaded environment.
26  * <p>
27  * Methods are synchronized, then forwarded to the decorated bag.
28  * Iterators must be separately synchronized around the loop.
29  * <p>
30  * This class is Serializable from Commons Collections 3.1.
31  *
32  * @since Commons Collections 3.0
33  * @version $Revision: 1.8 $ $Date: 2004/06/03 22:02:12 $
34  *
35  * @author Stephen Colebourne
36  */

37 public class SynchronizedSortedBag
38         extends SynchronizedBag implements SortedBag {
39
40     /** Serialization version */
41     private static final long serialVersionUID = 722374056718497858L;
42
43     /**
44      * Factory method to create a synchronized sorted bag.
45      *
46      * @param bag the bag to decorate, must not be null
47      * @return a new synchronized SortedBag
48      * @throws IllegalArgumentException if bag is null
49      */

50     public static SortedBag decorate(SortedBag bag) {
51         return new SynchronizedSortedBag(bag);
52     }
53     
54     //-----------------------------------------------------------------------
55
/**
56      * Constructor that wraps (not copies).
57      *
58      * @param bag the bag to decorate, must not be null
59      * @throws IllegalArgumentException if bag is null
60      */

61     protected SynchronizedSortedBag(SortedBag bag) {
62         super(bag);
63     }
64
65     /**
66      * Constructor that wraps (not copies).
67      *
68      * @param bag the bag to decorate, must not be null
69      * @param lock the lock to use, must not be null
70      * @throws IllegalArgumentException if bag is null
71      */

72     protected SynchronizedSortedBag(Bag bag, Object JavaDoc lock) {
73         super(bag, lock);
74     }
75
76     /**
77      * Gets the bag being decorated.
78      *
79      * @return the decorated bag
80      */

81     protected SortedBag getSortedBag() {
82         return (SortedBag) collection;
83     }
84     
85     //-----------------------------------------------------------------------
86
public synchronized Object JavaDoc first() {
87         synchronized (lock) {
88             return getSortedBag().first();
89         }
90     }
91
92     public synchronized Object JavaDoc last() {
93         synchronized (lock) {
94             return getSortedBag().last();
95         }
96     }
97
98     public synchronized Comparator JavaDoc comparator() {
99         synchronized (lock) {
100             return getSortedBag().comparator();
101         }
102     }
103
104 }
105
Popular Tags