1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.collection;
18
19 import java.io.Serializable;
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.Objects;
23 import java.util.function.Predicate;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class SynchronizedCollection<E> implements Collection<E>, Serializable {
45
46
47 private static final long serialVersionUID = 2412805092710877986L;
48
49
50
51
52
53
54
55
56
57
58 public static <T> SynchronizedCollection<T> synchronizedCollection(final Collection<T> coll) {
59 return new SynchronizedCollection<>(coll);
60 }
61
62 private final Collection<E> collection;
63
64
65 protected final Object lock;
66
67
68
69
70
71
72
73 protected SynchronizedCollection(final Collection<E> collection) {
74 this.collection = Objects.requireNonNull(collection, "collection");
75 this.lock = this;
76 }
77
78
79
80
81
82
83
84
85 protected SynchronizedCollection(final Collection<E> collection, final Object lock) {
86 this.collection = Objects.requireNonNull(collection, "collection");
87 this.lock = Objects.requireNonNull(lock, "lock");
88 }
89
90 @Override
91 public boolean add(final E object) {
92 synchronized (lock) {
93 return decorated().add(object);
94 }
95 }
96
97 @Override
98 public boolean addAll(final Collection<? extends E> coll) {
99 synchronized (lock) {
100 return decorated().addAll(coll);
101 }
102 }
103
104 @Override
105 public void clear() {
106 synchronized (lock) {
107 decorated().clear();
108 }
109 }
110
111 @Override
112 public boolean contains(final Object object) {
113 synchronized (lock) {
114 return decorated().contains(object);
115 }
116 }
117
118 @Override
119 public boolean containsAll(final Collection<?> coll) {
120 synchronized (lock) {
121 return decorated().containsAll(coll);
122 }
123 }
124
125
126
127
128
129
130 protected Collection<E> decorated() {
131 return collection;
132 }
133
134 @Override
135 public boolean equals(final Object object) {
136 synchronized (lock) {
137 if (object == this) {
138 return true;
139 }
140 return object == this || decorated().equals(object);
141 }
142 }
143
144 @Override
145 public int hashCode() {
146 synchronized (lock) {
147 return decorated().hashCode();
148 }
149 }
150
151 @Override
152 public boolean isEmpty() {
153 synchronized (lock) {
154 return decorated().isEmpty();
155 }
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169 @Override
170 public Iterator<E> iterator() {
171 return decorated().iterator();
172 }
173
174 @Override
175 public boolean remove(final Object object) {
176 synchronized (lock) {
177 return decorated().remove(object);
178 }
179 }
180
181 @Override
182 public boolean removeAll(final Collection<?> coll) {
183 synchronized (lock) {
184 return decorated().removeAll(coll);
185 }
186 }
187
188
189
190
191 @Override
192 public boolean removeIf(final Predicate<? super E> filter) {
193 synchronized (lock) {
194 return decorated().removeIf(filter);
195 }
196 }
197
198 @Override
199 public boolean retainAll(final Collection<?> coll) {
200 synchronized (lock) {
201 return decorated().retainAll(coll);
202 }
203 }
204
205 @Override
206 public int size() {
207 synchronized (lock) {
208 return decorated().size();
209 }
210 }
211
212 @Override
213 public Object[] toArray() {
214 synchronized (lock) {
215 return decorated().toArray();
216 }
217 }
218
219 @Override
220 public <T> T[] toArray(final T[] object) {
221 synchronized (lock) {
222 return decorated().toArray(object);
223 }
224 }
225
226 @Override
227 public String toString() {
228 synchronized (lock) {
229 return decorated().toString();
230 }
231 }
232
233 }