ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SynchronizedCollection.java
Revision: 1.6
Committed: Mon Dec 5 04:08:46 2011 UTC (12 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +2 -2 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.2
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 jsr166 1.5 * http://creativecommons.org/publicdomain/zero/1.0/
6 dl 1.2 */
7    
8 dl 1.1 // 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 jsr166 1.6 final Collection<E> c; // Backing Collection
14     final Object mutex; // Object on which to synchronize
15 dl 1.1
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 jsr166 1.4 synchronized (mutex) {return c.size();}
33 dl 1.1 }
34     public final boolean isEmpty() {
35 jsr166 1.4 synchronized (mutex) {return c.isEmpty();}
36 dl 1.1 }
37     public final boolean contains(Object o) {
38 jsr166 1.4 synchronized (mutex) {return c.contains(o);}
39 dl 1.1 }
40     public final Object[] toArray() {
41 jsr166 1.4 synchronized (mutex) {return c.toArray();}
42 dl 1.1 }
43     public final <T> T[] toArray(T[] a) {
44 jsr166 1.4 synchronized (mutex) {return c.toArray(a);}
45 dl 1.1 }
46    
47     public final Iterator<E> iterator() {
48 dl 1.2 return c.iterator();
49 dl 1.1 }
50    
51     public final boolean add(E e) {
52 jsr166 1.4 synchronized (mutex) {return c.add(e);}
53 dl 1.1 }
54     public final boolean remove(Object o) {
55 jsr166 1.4 synchronized (mutex) {return c.remove(o);}
56 dl 1.1 }
57    
58     public final boolean containsAll(Collection<?> coll) {
59 jsr166 1.4 synchronized (mutex) {return c.containsAll(coll);}
60 dl 1.1 }
61     public final boolean addAll(Collection<? extends E> coll) {
62 jsr166 1.4 synchronized (mutex) {return c.addAll(coll);}
63 dl 1.1 }
64     public final boolean removeAll(Collection<?> coll) {
65 jsr166 1.4 synchronized (mutex) {return c.removeAll(coll);}
66 dl 1.1 }
67     public final boolean retainAll(Collection<?> coll) {
68 jsr166 1.4 synchronized (mutex) {return c.retainAll(coll);}
69 dl 1.1 }
70     public final void clear() {
71 jsr166 1.4 synchronized (mutex) {c.clear();}
72 dl 1.1 }
73     public final String toString() {
74 jsr166 1.4 synchronized (mutex) {return c.toString();}
75 dl 1.1 }
76     private void writeObject(ObjectOutputStream s) throws IOException {
77 jsr166 1.4 synchronized (mutex) {s.defaultWriteObject();}
78 dl 1.1 }
79     }