KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > collection > SynchronizedCollection


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.collection;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 /**
23  * Decorates another <code>Collection</code> to synchronize its behaviour
24  * for a multi-threaded environment.
25  * <p>
26  * Iterators must be manually synchronized:
27  * <pre>
28  * synchronized (coll) {
29  * Iterator it = coll.iterator();
30  * // do stuff with iterator
31  * }
32  * </pre>
33  * <p>
34  * This class is Serializable from Commons Collections 3.1.
35  *
36  * @since Commons Collections 3.0
37  * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:13 $
38  *
39  * @author Stephen Colebourne
40  */

41 public class SynchronizedCollection implements Collection JavaDoc, Serializable JavaDoc {
42
43     /** Serialization version */
44     private static final long serialVersionUID = 2412805092710877986L;
45
46     /** The collection to decorate */
47     protected final Collection JavaDoc collection;
48     /** The object to lock on, needed for List/SortedSet views */
49     protected final Object JavaDoc lock;
50
51     /**
52      * Factory method to create a synchronized collection.
53      *
54      * @param coll the collection to decorate, must not be null
55      * @return a new synchronized collection
56      * @throws IllegalArgumentException if collection is null
57      */

58     public static Collection JavaDoc decorate(Collection JavaDoc coll) {
59         return new SynchronizedCollection(coll);
60     }
61     
62     //-----------------------------------------------------------------------
63
/**
64      * Constructor that wraps (not copies).
65      *
66      * @param collection the collection to decorate, must not be null
67      * @throws IllegalArgumentException if the collection is null
68      */

69     protected SynchronizedCollection(Collection JavaDoc collection) {
70         if (collection == null) {
71             throw new IllegalArgumentException JavaDoc("Collection must not be null");
72         }
73         this.collection = collection;
74         this.lock = this;
75     }
76
77     /**
78      * Constructor that wraps (not copies).
79      *
80      * @param collection the collection to decorate, must not be null
81      * @param lock the lock object to use, must not be null
82      * @throws IllegalArgumentException if the collection is null
83      */

84     protected SynchronizedCollection(Collection JavaDoc collection, Object JavaDoc lock) {
85         if (collection == null) {
86             throw new IllegalArgumentException JavaDoc("Collection must not be null");
87         }
88         this.collection = collection;
89         this.lock = lock;
90     }
91
92     //-----------------------------------------------------------------------
93
public boolean add(Object JavaDoc object) {
94         synchronized (lock) {
95             return collection.add(object);
96         }
97     }
98
99     public boolean addAll(Collection JavaDoc coll) {
100         synchronized (lock) {
101             return collection.addAll(coll);
102         }
103     }
104
105     public void clear() {
106         synchronized (lock) {
107             collection.clear();
108         }
109     }
110
111     public boolean contains(Object JavaDoc object) {
112         synchronized (lock) {
113             return collection.contains(object);
114         }
115     }
116
117     public boolean containsAll(Collection JavaDoc coll) {
118         synchronized (lock) {
119             return collection.containsAll(coll);
120         }
121     }
122
123     public boolean isEmpty() {
124         synchronized (lock) {
125             return collection.isEmpty();
126         }
127     }
128
129     /**
130      * Iterators must be manually synchronized.
131      * <pre>
132      * synchronized (coll) {
133      * Iterator it = coll.iterator();
134      * // do stuff with iterator
135      * }
136      *
137      * @return an iterator that must be manually synchronized on the collection
138      */

139     public Iterator JavaDoc iterator() {
140         return collection.iterator();
141     }
142
143     public Object JavaDoc[] toArray() {
144         synchronized (lock) {
145             return collection.toArray();
146         }
147     }
148
149     public Object JavaDoc[] toArray(Object JavaDoc[] object) {
150         synchronized (lock) {
151             return collection.toArray(object);
152         }
153     }
154
155     public boolean remove(Object JavaDoc object) {
156         synchronized (lock) {
157             return collection.remove(object);
158         }
159     }
160
161     public boolean removeAll(Collection JavaDoc coll) {
162         synchronized (lock) {
163             return collection.removeAll(coll);
164         }
165     }
166
167     public boolean retainAll(Collection JavaDoc coll) {
168         synchronized (lock) {
169             return collection.retainAll(coll);
170         }
171     }
172
173     public int size() {
174         synchronized (lock) {
175             return collection.size();
176         }
177     }
178
179     public boolean equals(Object JavaDoc object) {
180         synchronized (lock) {
181             if (object == this) {
182                 return true;
183             }
184             return collection.equals(object);
185         }
186     }
187
188     public int hashCode() {
189         synchronized (lock) {
190             return collection.hashCode();
191         }
192     }
193
194     public String JavaDoc toString() {
195         synchronized (lock) {
196             return collection.toString();
197         }
198     }
199
200 }
201
Popular Tags