View Javadoc

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 net.sf.collections15.collection;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  import java.util.Iterator;
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<String> 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.1 $ $Date: 2005/05/03 22:45:38 $
38   * 
39   * @author Stephen Colebourne
40   */
41  public class SynchronizedCollection<E> implements Collection<E>, Serializable {
42  
43      /*** Serialization version */
44  	private static final long serialVersionUID = 3256438097226182708L;
45  
46  	/*** The collection to decorate */
47      protected final Collection<E> collection;
48  
49  	/*** The object to lock on, needed for List/SortedSet views */
50      protected final Object lock;
51  
52      /***
53       * Factory method to create a synchronized collection.
54       * 
55       * @param coll  the collection to decorate, must not be null
56       * @return a new synchronized collection
57       * @throws IllegalArgumentException if collection is null
58       */
59      public static <T> Collection<T> decorate(Collection<T> coll) {
60          return new SynchronizedCollection<T>(coll);
61      }
62      
63      //-----------------------------------------------------------------------
64      /***
65       * Constructor that wraps (not copies).
66       * 
67       * @param collection  the collection to decorate, must not be null
68       * @throws IllegalArgumentException if the collection is null
69       */
70      protected SynchronizedCollection(Collection<E> collection) {
71          if (collection == null) {
72              throw new IllegalArgumentException("Collection must not be null");
73          }
74          this.collection = collection;
75          this.lock = this;
76      }
77  
78      /***
79       * Constructor that wraps (not copies).
80       * 
81       * @param collection  the collection to decorate, must not be null
82       * @param lock  the lock object to use, must not be null
83       * @throws IllegalArgumentException if the collection is null
84       */
85      protected SynchronizedCollection(Collection<E> collection, Object lock) {
86          if (collection == null) {
87              throw new IllegalArgumentException("Collection must not be null");
88          }
89          this.collection = collection;
90          this.lock = lock;
91      }
92  
93      //-----------------------------------------------------------------------
94      public boolean add(E object) {
95          synchronized (lock) {
96              return collection.add(object);
97          }
98      }
99  
100     public boolean addAll(Collection<? extends E> coll) {
101         synchronized (lock) {
102             return collection.addAll(coll);
103         }
104     }
105 
106     public void clear() {
107         synchronized (lock) {
108             collection.clear();
109         }
110     }
111 
112     public boolean contains(Object object) {
113         synchronized (lock) {
114             return collection.contains(object);
115         }
116     }
117 
118     public boolean containsAll(Collection<?> coll) {
119         synchronized (lock) {
120             return collection.containsAll(coll);
121         }
122     }
123 
124     public boolean isEmpty() {
125         synchronized (lock) {
126             return collection.isEmpty();
127         }
128     }
129 
130     /***
131      * Iterators must be manually synchronized.
132      * <pre>
133      * synchronized (coll) {
134      *   Iterator it = coll.iterator();
135      *   // do stuff with iterator
136      * }
137      * 
138      * @return an iterator that must be manually synchronized on the collection
139      */
140     public Iterator<E> iterator() {
141         return collection.iterator();
142     }
143 
144     public Object[] toArray() {
145         synchronized (lock) {
146             return collection.toArray();
147         }
148     }
149 
150     public <T> T[] toArray(T[] object) {
151         synchronized (lock) {
152             return collection.toArray(object);
153         }
154     }
155 
156     public boolean remove(Object object) {
157         synchronized (lock) {
158             return collection.remove(object);
159         }
160     }
161 
162     public boolean removeAll(Collection<?> coll) {
163         synchronized (lock) {
164             return collection.removeAll(coll);
165         }
166     }
167 
168     public boolean retainAll(Collection<?> coll) {
169         synchronized (lock) {
170             return collection.retainAll(coll);
171         }
172     }
173 
174     public int size() {
175         synchronized (lock) {
176             return collection.size();
177         }
178     }
179 
180     public boolean equals(Object object) {
181         synchronized (lock) {
182             if (object == this) {
183                 return true;
184             }
185             return collection.equals(object);
186         }
187     }
188 
189     public int hashCode() {
190         synchronized (lock) {
191             return collection.hashCode();
192         }
193     }
194 
195     public String toString() {
196         synchronized (lock) {
197             return collection.toString();
198         }
199     }
200 
201 }