author | dl |
Wed, 21 Dec 2016 14:26:52 -0800 | |
changeset 42927 | 1d31e540bfcb |
parent 32991 | b27c76b82713 |
child 44743 | f0bbd698c486 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7518
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
37 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
38 |
import java.util.AbstractSet; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
39 |
import java.util.Collection; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
40 |
import java.util.Iterator; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
41 |
import java.util.Objects; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
42 |
import java.util.Set; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
43 |
import java.util.Spliterator; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
44 |
import java.util.Spliterators; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
45 |
import java.util.function.Consumer; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
46 |
import java.util.function.Predicate; |
2 | 47 |
|
48 |
/** |
|
49 |
* A {@link java.util.Set} that uses an internal {@link CopyOnWriteArrayList} |
|
50 |
* for all of its operations. Thus, it shares the same basic properties: |
|
51 |
* <ul> |
|
52 |
* <li>It is best suited for applications in which set sizes generally |
|
53 |
* stay small, read-only operations |
|
54 |
* vastly outnumber mutative operations, and you need |
|
55 |
* to prevent interference among threads during traversal. |
|
56 |
* <li>It is thread-safe. |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
57 |
* <li>Mutative operations ({@code add}, {@code set}, {@code remove}, etc.) |
2 | 58 |
* are expensive since they usually entail copying the entire underlying |
59 |
* array. |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
60 |
* <li>Iterators do not support the mutative {@code remove} operation. |
2 | 61 |
* <li>Traversal via iterators is fast and cannot encounter |
62 |
* interference from other threads. Iterators rely on |
|
63 |
* unchanging snapshots of the array at the time the iterators were |
|
64 |
* constructed. |
|
65 |
* </ul> |
|
66 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
67 |
* <p><b>Sample Usage.</b> The following code sketch uses a |
2 | 68 |
* copy-on-write set to maintain a set of Handler objects that |
69 |
* perform some action upon state updates. |
|
70 |
* |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
71 |
* <pre> {@code |
2 | 72 |
* class Handler { void handle(); ... } |
73 |
* |
|
74 |
* class X { |
|
7518 | 75 |
* private final CopyOnWriteArraySet<Handler> handlers |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
76 |
* = new CopyOnWriteArraySet<>(); |
7518 | 77 |
* public void addHandler(Handler h) { handlers.add(h); } |
2 | 78 |
* |
7518 | 79 |
* private long internalState; |
80 |
* private synchronized void changeState() { internalState = ...; } |
|
2 | 81 |
* |
7518 | 82 |
* public void update() { |
83 |
* changeState(); |
|
84 |
* for (Handler handler : handlers) |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
85 |
* handler.handle(); |
7518 | 86 |
* } |
87 |
* }}</pre> |
|
2 | 88 |
* |
89 |
* <p>This class is a member of the |
|
90 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
91 |
* Java Collections Framework</a>. |
|
92 |
* |
|
93 |
* @see CopyOnWriteArrayList |
|
94 |
* @since 1.5 |
|
95 |
* @author Doug Lea |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
96 |
* @param <E> the type of elements held in this set |
2 | 97 |
*/ |
98 |
public class CopyOnWriteArraySet<E> extends AbstractSet<E> |
|
99 |
implements java.io.Serializable { |
|
100 |
private static final long serialVersionUID = 5457747651344034263L; |
|
101 |
||
102 |
private final CopyOnWriteArrayList<E> al; |
|
103 |
||
104 |
/** |
|
105 |
* Creates an empty set. |
|
106 |
*/ |
|
107 |
public CopyOnWriteArraySet() { |
|
108 |
al = new CopyOnWriteArrayList<E>(); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Creates a set containing all of the elements of the specified |
|
113 |
* collection. |
|
114 |
* |
|
115 |
* @param c the collection of elements to initially contain |
|
116 |
* @throws NullPointerException if the specified collection is null |
|
117 |
*/ |
|
118 |
public CopyOnWriteArraySet(Collection<? extends E> c) { |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
119 |
if (c.getClass() == CopyOnWriteArraySet.class) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
120 |
@SuppressWarnings("unchecked") CopyOnWriteArraySet<E> cc = |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
121 |
(CopyOnWriteArraySet<E>)c; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
122 |
al = new CopyOnWriteArrayList<E>(cc.al); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
123 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
124 |
else { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
125 |
al = new CopyOnWriteArrayList<E>(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
126 |
al.addAllAbsent(c); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
127 |
} |
2 | 128 |
} |
129 |
||
130 |
/** |
|
131 |
* Returns the number of elements in this set. |
|
132 |
* |
|
133 |
* @return the number of elements in this set |
|
134 |
*/ |
|
135 |
public int size() { |
|
136 |
return al.size(); |
|
137 |
} |
|
138 |
||
139 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
140 |
* Returns {@code true} if this set contains no elements. |
2 | 141 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
142 |
* @return {@code true} if this set contains no elements |
2 | 143 |
*/ |
144 |
public boolean isEmpty() { |
|
145 |
return al.isEmpty(); |
|
146 |
} |
|
147 |
||
148 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
149 |
* Returns {@code true} if this set contains the specified element. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
150 |
* More formally, returns {@code true} if and only if this set |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
151 |
* contains an element {@code e} such that {@code Objects.equals(o, e)}. |
2 | 152 |
* |
153 |
* @param o element whose presence in this set is to be tested |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
154 |
* @return {@code true} if this set contains the specified element |
2 | 155 |
*/ |
156 |
public boolean contains(Object o) { |
|
157 |
return al.contains(o); |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Returns an array containing all of the elements in this set. |
|
162 |
* If this set makes any guarantees as to what order its elements |
|
163 |
* are returned by its iterator, this method must return the |
|
164 |
* elements in the same order. |
|
165 |
* |
|
166 |
* <p>The returned array will be "safe" in that no references to it |
|
167 |
* are maintained by this set. (In other words, this method must |
|
168 |
* allocate a new array even if this set is backed by an array). |
|
169 |
* The caller is thus free to modify the returned array. |
|
170 |
* |
|
171 |
* <p>This method acts as bridge between array-based and collection-based |
|
172 |
* APIs. |
|
173 |
* |
|
174 |
* @return an array containing all the elements in this set |
|
175 |
*/ |
|
176 |
public Object[] toArray() { |
|
177 |
return al.toArray(); |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Returns an array containing all of the elements in this set; the |
|
182 |
* runtime type of the returned array is that of the specified array. |
|
183 |
* If the set fits in the specified array, it is returned therein. |
|
184 |
* Otherwise, a new array is allocated with the runtime type of the |
|
185 |
* specified array and the size of this set. |
|
186 |
* |
|
187 |
* <p>If this set fits in the specified array with room to spare |
|
188 |
* (i.e., the array has more elements than this set), the element in |
|
189 |
* the array immediately following the end of the set is set to |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
190 |
* {@code null}. (This is useful in determining the length of this |
2 | 191 |
* set <i>only</i> if the caller knows that this set does not contain |
192 |
* any null elements.) |
|
193 |
* |
|
194 |
* <p>If this set makes any guarantees as to what order its elements |
|
195 |
* are returned by its iterator, this method must return the elements |
|
196 |
* in the same order. |
|
197 |
* |
|
198 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
|
199 |
* array-based and collection-based APIs. Further, this method allows |
|
200 |
* precise control over the runtime type of the output array, and may, |
|
201 |
* under certain circumstances, be used to save allocation costs. |
|
202 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
203 |
* <p>Suppose {@code x} is a set known to contain only strings. |
2 | 204 |
* The following code can be used to dump the set into a newly allocated |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
205 |
* array of {@code String}: |
2 | 206 |
* |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
207 |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
2 | 208 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
209 |
* Note that {@code toArray(new Object[0])} is identical in function to |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
210 |
* {@code toArray()}. |
2 | 211 |
* |
212 |
* @param a the array into which the elements of this set are to be |
|
213 |
* stored, if it is big enough; otherwise, a new array of the same |
|
214 |
* runtime type is allocated for this purpose. |
|
215 |
* @return an array containing all the elements in this set |
|
216 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
217 |
* is not a supertype of the runtime type of every element in this |
|
218 |
* set |
|
219 |
* @throws NullPointerException if the specified array is null |
|
220 |
*/ |
|
221 |
public <T> T[] toArray(T[] a) { |
|
222 |
return al.toArray(a); |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Removes all of the elements from this set. |
|
227 |
* The set will be empty after this call returns. |
|
228 |
*/ |
|
229 |
public void clear() { |
|
230 |
al.clear(); |
|
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* Removes the specified element from this set if it is present. |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
235 |
* More formally, removes an element {@code e} such that |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
236 |
* {@code Objects.equals(o, e)}, if this set contains such an element. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
237 |
* Returns {@code true} if this set contained the element (or |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
238 |
* equivalently, if this set changed as a result of the call). |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
239 |
* (This set will not contain the element once the call returns.) |
2 | 240 |
* |
241 |
* @param o object to be removed from this set, if present |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
242 |
* @return {@code true} if this set contained the specified element |
2 | 243 |
*/ |
244 |
public boolean remove(Object o) { |
|
245 |
return al.remove(o); |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Adds the specified element to this set if it is not already present. |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
250 |
* More formally, adds the specified element {@code e} to this set if |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
251 |
* the set contains no element {@code e2} such that |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
252 |
* {@code Objects.equals(e, e2)}. |
2 | 253 |
* If this set already contains the element, the call leaves the set |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
254 |
* unchanged and returns {@code false}. |
2 | 255 |
* |
256 |
* @param e element to be added to this set |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
257 |
* @return {@code true} if this set did not already contain the specified |
2 | 258 |
* element |
259 |
*/ |
|
260 |
public boolean add(E e) { |
|
261 |
return al.addIfAbsent(e); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
265 |
* Returns {@code true} if this set contains all of the elements of the |
2 | 266 |
* specified collection. If the specified collection is also a set, this |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
267 |
* method returns {@code true} if it is a <i>subset</i> of this set. |
2 | 268 |
* |
269 |
* @param c collection to be checked for containment in this set |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
270 |
* @return {@code true} if this set contains all of the elements of the |
2 | 271 |
* specified collection |
272 |
* @throws NullPointerException if the specified collection is null |
|
273 |
* @see #contains(Object) |
|
274 |
*/ |
|
275 |
public boolean containsAll(Collection<?> c) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
276 |
return (c instanceof Set) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
277 |
? compareSets(al.getArray(), (Set<?>) c) >= 0 |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
278 |
: al.containsAll(c); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
279 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
280 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
281 |
/** |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
282 |
* Tells whether the objects in snapshot (regarded as a set) are a |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
283 |
* superset of the given set. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
284 |
* |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
285 |
* @return -1 if snapshot is not a superset, 0 if the two sets |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
286 |
* contain precisely the same elements, and 1 if snapshot is a |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
287 |
* proper superset of the given set |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
288 |
*/ |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
289 |
private static int compareSets(Object[] snapshot, Set<?> set) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
290 |
// Uses O(n^2) algorithm, that is only appropriate for small |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
291 |
// sets, which CopyOnWriteArraySets should be. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
292 |
// |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
293 |
// Optimize up to O(n) if the two sets share a long common prefix, |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
294 |
// as might happen if one set was created as a copy of the other set. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
295 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
296 |
final int len = snapshot.length; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
297 |
// Mark matched elements to avoid re-checking |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
298 |
final boolean[] matched = new boolean[len]; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
299 |
|
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
300 |
// j is the largest int with matched[i] true for { i | 0 <= i < j } |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
301 |
int j = 0; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
302 |
outer: for (Object x : set) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
303 |
for (int i = j; i < len; i++) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
304 |
if (!matched[i] && Objects.equals(x, snapshot[i])) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
305 |
matched[i] = true; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
306 |
if (i == j) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
307 |
do { j++; } while (j < len && matched[j]); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
308 |
continue outer; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
309 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
310 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
311 |
return -1; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
312 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
313 |
return (j == len) ? 0 : 1; |
2 | 314 |
} |
315 |
||
316 |
/** |
|
317 |
* Adds all of the elements in the specified collection to this set if |
|
318 |
* they're not already present. If the specified collection is also a |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
319 |
* set, the {@code addAll} operation effectively modifies this set so |
2 | 320 |
* that its value is the <i>union</i> of the two sets. The behavior of |
321 |
* this operation is undefined if the specified collection is modified |
|
322 |
* while the operation is in progress. |
|
323 |
* |
|
324 |
* @param c collection containing elements to be added to this set |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
325 |
* @return {@code true} if this set changed as a result of the call |
2 | 326 |
* @throws NullPointerException if the specified collection is null |
327 |
* @see #add(Object) |
|
328 |
*/ |
|
329 |
public boolean addAll(Collection<? extends E> c) { |
|
330 |
return al.addAllAbsent(c) > 0; |
|
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* Removes from this set all of its elements that are contained in the |
|
335 |
* specified collection. If the specified collection is also a set, |
|
336 |
* this operation effectively modifies this set so that its value is the |
|
337 |
* <i>asymmetric set difference</i> of the two sets. |
|
338 |
* |
|
339 |
* @param c collection containing elements to be removed from this set |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
340 |
* @return {@code true} if this set changed as a result of the call |
2 | 341 |
* @throws ClassCastException if the class of an element of this set |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
342 |
* is incompatible with the specified collection |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
343 |
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>) |
2 | 344 |
* @throws NullPointerException if this set contains a null element and the |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
345 |
* specified collection does not permit null elements |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
346 |
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>), |
2 | 347 |
* or if the specified collection is null |
348 |
* @see #remove(Object) |
|
349 |
*/ |
|
350 |
public boolean removeAll(Collection<?> c) { |
|
351 |
return al.removeAll(c); |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* Retains only the elements in this set that are contained in the |
|
356 |
* specified collection. In other words, removes from this set all of |
|
357 |
* its elements that are not contained in the specified collection. If |
|
358 |
* the specified collection is also a set, this operation effectively |
|
359 |
* modifies this set so that its value is the <i>intersection</i> of the |
|
360 |
* two sets. |
|
361 |
* |
|
362 |
* @param c collection containing elements to be retained in this set |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
363 |
* @return {@code true} if this set changed as a result of the call |
2 | 364 |
* @throws ClassCastException if the class of an element of this set |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
365 |
* is incompatible with the specified collection |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
366 |
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>) |
2 | 367 |
* @throws NullPointerException if this set contains a null element and the |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
368 |
* specified collection does not permit null elements |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
369 |
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>), |
2 | 370 |
* or if the specified collection is null |
371 |
* @see #remove(Object) |
|
372 |
*/ |
|
373 |
public boolean retainAll(Collection<?> c) { |
|
374 |
return al.retainAll(c); |
|
375 |
} |
|
376 |
||
377 |
/** |
|
378 |
* Returns an iterator over the elements contained in this set |
|
379 |
* in the order in which these elements were added. |
|
380 |
* |
|
381 |
* <p>The returned iterator provides a snapshot of the state of the set |
|
382 |
* when the iterator was constructed. No synchronization is needed while |
|
383 |
* traversing the iterator. The iterator does <em>NOT</em> support the |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
384 |
* {@code remove} method. |
2 | 385 |
* |
386 |
* @return an iterator over the elements in this set |
|
387 |
*/ |
|
388 |
public Iterator<E> iterator() { |
|
389 |
return al.iterator(); |
|
390 |
} |
|
391 |
||
392 |
/** |
|
393 |
* Compares the specified object with this set for equality. |
|
394 |
* Returns {@code true} if the specified object is the same object |
|
395 |
* as this object, or if it is also a {@link Set} and the elements |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
396 |
* returned by an {@linkplain Set#iterator() iterator} over the |
2 | 397 |
* specified set are the same as the elements returned by an |
398 |
* iterator over this set. More formally, the two iterators are |
|
399 |
* considered to return the same elements if they return the same |
|
400 |
* number of elements and for every element {@code e1} returned by |
|
401 |
* the iterator over the specified set, there is an element |
|
402 |
* {@code e2} returned by the iterator over this set such that |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
403 |
* {@code Objects.equals(e1, e2)}. |
2 | 404 |
* |
405 |
* @param o object to be compared for equality with this set |
|
406 |
* @return {@code true} if the specified object is equal to this set |
|
407 |
*/ |
|
408 |
public boolean equals(Object o) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
409 |
return (o == this) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
410 |
|| ((o instanceof Set) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
411 |
&& compareSets(al.getArray(), (Set<?>) o) == 0); |
2 | 412 |
} |
413 |
||
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
32991
diff
changeset
|
414 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
32991
diff
changeset
|
415 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
32991
diff
changeset
|
416 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
417 |
public boolean removeIf(Predicate<? super E> filter) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
418 |
return al.removeIf(filter); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
419 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
420 |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
32991
diff
changeset
|
421 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
32991
diff
changeset
|
422 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
32991
diff
changeset
|
423 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
424 |
public void forEach(Consumer<? super E> action) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
425 |
al.forEach(action); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
426 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
427 |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
428 |
/** |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
429 |
* Returns a {@link Spliterator} over the elements in this set in the order |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
430 |
* in which these elements were added. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
431 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
432 |
* <p>The {@code Spliterator} reports {@link Spliterator#IMMUTABLE}, |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
433 |
* {@link Spliterator#DISTINCT}, {@link Spliterator#SIZED}, and |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
434 |
* {@link Spliterator#SUBSIZED}. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
435 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
436 |
* <p>The spliterator provides a snapshot of the state of the set |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
437 |
* when the spliterator was constructed. No synchronization is needed while |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
438 |
* operating on the spliterator. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
439 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
440 |
* @return a {@code Spliterator} over the elements in this set |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
441 |
* @since 1.8 |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
442 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
443 |
public Spliterator<E> spliterator() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
444 |
return Spliterators.spliterator |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
445 |
(al.getArray(), Spliterator.IMMUTABLE | Spliterator.DISTINCT); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
14325
diff
changeset
|
446 |
} |
2 | 447 |
} |