ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SynchronizedCollection.java
Revision: 1.8
Committed: Wed Dec 31 17:00:58 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +1 -1 lines
Log Message:
lexicographic import order

File Contents

# Content
1 /*
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 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 // Stand-alone version of java.util.Collections.synchronizedCollection
8 import java.io.*;
9 import java.util.*;
10
11 public final class SynchronizedCollection<E> implements Collection<E>, Serializable {
12 final Collection<E> c; // Backing Collection
13 final Object mutex; // Object on which to synchronize
14
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 synchronized (mutex) {return c.size();}
32 }
33 public final boolean isEmpty() {
34 synchronized (mutex) {return c.isEmpty();}
35 }
36 public final boolean contains(Object o) {
37 synchronized (mutex) {return c.contains(o);}
38 }
39 public final Object[] toArray() {
40 synchronized (mutex) {return c.toArray();}
41 }
42 public final <T> T[] toArray(T[] a) {
43 synchronized (mutex) {return c.toArray(a);}
44 }
45
46 public final Iterator<E> iterator() {
47 return c.iterator();
48 }
49
50 public final boolean add(E e) {
51 synchronized (mutex) {return c.add(e);}
52 }
53 public final boolean remove(Object o) {
54 synchronized (mutex) {return c.remove(o);}
55 }
56
57 public final boolean containsAll(Collection<?> coll) {
58 synchronized (mutex) {return c.containsAll(coll);}
59 }
60 public final boolean addAll(Collection<? extends E> coll) {
61 synchronized (mutex) {return c.addAll(coll);}
62 }
63 public final boolean removeAll(Collection<?> coll) {
64 synchronized (mutex) {return c.removeAll(coll);}
65 }
66 public final boolean retainAll(Collection<?> coll) {
67 synchronized (mutex) {return c.retainAll(coll);}
68 }
69 public final void clear() {
70 synchronized (mutex) {c.clear();}
71 }
72 public final String toString() {
73 synchronized (mutex) {return c.toString();}
74 }
75 private void writeObject(ObjectOutputStream s) throws IOException {
76 synchronized (mutex) {s.defaultWriteObject();}
77 }
78 }