KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

82     protected Bag getBag() {
83         return (Bag) collection;
84     }
85     
86     //-----------------------------------------------------------------------
87
public boolean add(Object JavaDoc object, int count) {
88         synchronized (lock) {
89             return getBag().add(object, count);
90         }
91     }
92
93     public boolean remove(Object JavaDoc object, int count) {
94         synchronized (lock) {
95             return getBag().remove(object, count);
96         }
97     }
98
99     public Set JavaDoc uniqueSet() {
100         synchronized (lock) {
101             Set JavaDoc set = getBag().uniqueSet();
102             return new SynchronizedBagSet(set, lock);
103         }
104     }
105
106     public int getCount(Object JavaDoc object) {
107         synchronized (lock) {
108             return getBag().getCount(object);
109         }
110     }
111     
112     //-----------------------------------------------------------------------
113
/**
114      * Synchronized Set for the Bag class.
115      */

116     class SynchronizedBagSet extends SynchronizedSet {
117         /**
118          * Constructor.
119          * @param set the set to decorate
120          * @param lock the lock to use, shared with the bag
121          */

122         SynchronizedBagSet(Set JavaDoc set, Object JavaDoc lock) {
123             super(set, lock);
124         }
125     }
126
127 }
128
Popular Tags