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