1 |
|
2 |
/* |
3 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
4 |
* Expert Group and released to the public domain, as explained at |
5 |
* http://creativecommons.org/licenses/publicdomain |
6 |
*/ |
7 |
|
8 |
// Stand-alone version of java.util.Collections.synchronizedCollection |
9 |
import java.util.*; |
10 |
import java.io.*; |
11 |
|
12 |
public final class SynchronizedCollection<E> implements Collection<E>, Serializable { |
13 |
final Collection<E> c; // Backing Collection |
14 |
final Object mutex; // Object on which to synchronize |
15 |
|
16 |
public SynchronizedCollection(Collection<E> c) { |
17 |
if (c==null) |
18 |
throw new NullPointerException(); |
19 |
this.c = c; |
20 |
mutex = this; |
21 |
} |
22 |
public SynchronizedCollection(Collection<E> c, Object mutex) { |
23 |
this.c = c; |
24 |
this.mutex = mutex; |
25 |
} |
26 |
|
27 |
public SynchronizedCollection() { |
28 |
this(new ArrayList<E>()); |
29 |
} |
30 |
|
31 |
public final int size() { |
32 |
synchronized(mutex) {return c.size();} |
33 |
} |
34 |
public final boolean isEmpty() { |
35 |
synchronized(mutex) {return c.isEmpty();} |
36 |
} |
37 |
public final boolean contains(Object o) { |
38 |
synchronized(mutex) {return c.contains(o);} |
39 |
} |
40 |
public final Object[] toArray() { |
41 |
synchronized(mutex) {return c.toArray();} |
42 |
} |
43 |
public final <T> T[] toArray(T[] a) { |
44 |
synchronized(mutex) {return c.toArray(a);} |
45 |
} |
46 |
|
47 |
public final Iterator<E> iterator() { |
48 |
return c.iterator(); |
49 |
} |
50 |
|
51 |
public final boolean add(E e) { |
52 |
synchronized(mutex) {return c.add(e);} |
53 |
} |
54 |
public final boolean remove(Object o) { |
55 |
synchronized(mutex) {return c.remove(o);} |
56 |
} |
57 |
|
58 |
public final boolean containsAll(Collection<?> coll) { |
59 |
synchronized(mutex) {return c.containsAll(coll);} |
60 |
} |
61 |
public final boolean addAll(Collection<? extends E> coll) { |
62 |
synchronized(mutex) {return c.addAll(coll);} |
63 |
} |
64 |
public final boolean removeAll(Collection<?> coll) { |
65 |
synchronized(mutex) {return c.removeAll(coll);} |
66 |
} |
67 |
public final boolean retainAll(Collection<?> coll) { |
68 |
synchronized(mutex) {return c.retainAll(coll);} |
69 |
} |
70 |
public final void clear() { |
71 |
synchronized(mutex) {c.clear();} |
72 |
} |
73 |
public final String toString() { |
74 |
synchronized(mutex) {return c.toString();} |
75 |
} |
76 |
private void writeObject(ObjectOutputStream s) throws IOException { |
77 |
synchronized(mutex) {s.defaultWriteObject();} |
78 |
} |
79 |
} |