author | psandoz |
Thu, 16 Jan 2014 10:27:57 +0100 | |
changeset 22285 | 6c67737a5ac0 |
parent 22078 | bdec5d53e98c |
child 23746 | ce60f7b62312 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
27 |
import java.io.Serializable; |
|
28 |
import java.io.ObjectOutputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.lang.reflect.Array; |
|
16867 | 31 |
import java.util.function.BiConsumer; |
32 |
import java.util.function.BiFunction; |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
33 |
import java.util.function.Consumer; |
16867 | 34 |
import java.util.function.Function; |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
35 |
import java.util.function.Predicate; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
36 |
import java.util.function.UnaryOperator; |
19603
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
37 |
import java.util.stream.IntStream; |
18794
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
38 |
import java.util.stream.Stream; |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
39 |
import java.util.stream.StreamSupport; |
2 | 40 |
|
41 |
/** |
|
42 |
* This class consists exclusively of static methods that operate on or return |
|
43 |
* collections. It contains polymorphic algorithms that operate on |
|
44 |
* collections, "wrappers", which return a new collection backed by a |
|
45 |
* specified collection, and a few other odds and ends. |
|
46 |
* |
|
47 |
* <p>The methods of this class all throw a <tt>NullPointerException</tt> |
|
48 |
* if the collections or class objects provided to them are null. |
|
49 |
* |
|
50 |
* <p>The documentation for the polymorphic algorithms contained in this class |
|
51 |
* generally includes a brief description of the <i>implementation</i>. Such |
|
52 |
* descriptions should be regarded as <i>implementation notes</i>, rather than |
|
53 |
* parts of the <i>specification</i>. Implementors should feel free to |
|
54 |
* substitute other algorithms, so long as the specification itself is adhered |
|
55 |
* to. (For example, the algorithm used by <tt>sort</tt> does not have to be |
|
56 |
* a mergesort, but it does have to be <i>stable</i>.) |
|
57 |
* |
|
58 |
* <p>The "destructive" algorithms contained in this class, that is, the |
|
59 |
* algorithms that modify the collection on which they operate, are specified |
|
60 |
* to throw <tt>UnsupportedOperationException</tt> if the collection does not |
|
61 |
* support the appropriate mutation primitive(s), such as the <tt>set</tt> |
|
62 |
* method. These algorithms may, but are not required to, throw this |
|
63 |
* exception if an invocation would have no effect on the collection. For |
|
64 |
* example, invoking the <tt>sort</tt> method on an unmodifiable list that is |
|
65 |
* already sorted may or may not throw <tt>UnsupportedOperationException</tt>. |
|
66 |
* |
|
67 |
* <p>This class is a member of the |
|
68 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
69 |
* Java Collections Framework</a>. |
|
70 |
* |
|
71 |
* @author Josh Bloch |
|
72 |
* @author Neal Gafter |
|
73 |
* @see Collection |
|
74 |
* @see Set |
|
75 |
* @see List |
|
76 |
* @see Map |
|
77 |
* @since 1.2 |
|
78 |
*/ |
|
79 |
||
80 |
public class Collections { |
|
81 |
// Suppresses default constructor, ensuring non-instantiability. |
|
82 |
private Collections() { |
|
83 |
} |
|
84 |
||
85 |
// Algorithms |
|
86 |
||
87 |
/* |
|
88 |
* Tuning parameters for algorithms - Many of the List algorithms have |
|
89 |
* two implementations, one of which is appropriate for RandomAccess |
|
90 |
* lists, the other for "sequential." Often, the random access variant |
|
91 |
* yields better performance on small sequential access lists. The |
|
92 |
* tuning parameters below determine the cutoff point for what constitutes |
|
93 |
* a "small" sequential access list for each algorithm. The values below |
|
94 |
* were empirically determined to work well for LinkedList. Hopefully |
|
95 |
* they should be reasonable for other sequential access List |
|
96 |
* implementations. Those doing performance work on this code would |
|
97 |
* do well to validate the values of these parameters from time to time. |
|
98 |
* (The first word of each tuning parameter name is the algorithm to which |
|
99 |
* it applies.) |
|
100 |
*/ |
|
101 |
private static final int BINARYSEARCH_THRESHOLD = 5000; |
|
102 |
private static final int REVERSE_THRESHOLD = 18; |
|
103 |
private static final int SHUFFLE_THRESHOLD = 5; |
|
104 |
private static final int FILL_THRESHOLD = 25; |
|
105 |
private static final int ROTATE_THRESHOLD = 100; |
|
106 |
private static final int COPY_THRESHOLD = 10; |
|
107 |
private static final int REPLACEALL_THRESHOLD = 11; |
|
108 |
private static final int INDEXOFSUBLIST_THRESHOLD = 35; |
|
109 |
||
110 |
/** |
|
111 |
* Sorts the specified list into ascending order, according to the |
|
3420
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
112 |
* {@linkplain Comparable natural ordering} of its elements. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
113 |
* All elements in the list must implement the {@link Comparable} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
114 |
* interface. Furthermore, all elements in the list must be |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
115 |
* <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
116 |
* must not throw a {@code ClassCastException} for any elements |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
117 |
* {@code e1} and {@code e2} in the list). |
2 | 118 |
* |
3420
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
119 |
* <p>This sort is guaranteed to be <i>stable</i>: equal elements will |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
120 |
* not be reordered as a result of the sort. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
121 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
122 |
* <p>The specified list must be modifiable, but need not be resizable. |
2 | 123 |
* |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
124 |
* @implNote |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
125 |
* This implementation defers to the {@link List#sort(Comparator)} |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
126 |
* method using the specified list and a {@code null} comparator. |
2 | 127 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
128 |
* @param <T> the class of the objects in the list |
2 | 129 |
* @param list the list to be sorted. |
130 |
* @throws ClassCastException if the list contains elements that are not |
|
131 |
* <i>mutually comparable</i> (for example, strings and integers). |
|
132 |
* @throws UnsupportedOperationException if the specified list's |
|
3420
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
133 |
* list-iterator does not support the {@code set} operation. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
134 |
* @throws IllegalArgumentException (optional) if the implementation |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
135 |
* detects that the natural ordering of the list elements is |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
136 |
* found to violate the {@link Comparable} contract |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
137 |
* @see List#sort(Comparator) |
2 | 138 |
*/ |
12448 | 139 |
@SuppressWarnings("unchecked") |
2 | 140 |
public static <T extends Comparable<? super T>> void sort(List<T> list) { |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
141 |
list.sort(null); |
2 | 142 |
} |
143 |
||
144 |
/** |
|
145 |
* Sorts the specified list according to the order induced by the |
|
146 |
* specified comparator. All elements in the list must be <i>mutually |
|
147 |
* comparable</i> using the specified comparator (that is, |
|
3420
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
148 |
* {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
149 |
* for any elements {@code e1} and {@code e2} in the list). |
2 | 150 |
* |
3420
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
151 |
* <p>This sort is guaranteed to be <i>stable</i>: equal elements will |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
152 |
* not be reordered as a result of the sort. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
153 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
154 |
* <p>The specified list must be modifiable, but need not be resizable. |
2 | 155 |
* |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
156 |
* @implNote |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
157 |
* This implementation defers to the {@link List#sort(Comparator)} |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
158 |
* method using the specified list and comparator. |
2 | 159 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
160 |
* @param <T> the class of the objects in the list |
2 | 161 |
* @param list the list to be sorted. |
162 |
* @param c the comparator to determine the order of the list. A |
|
3420
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
163 |
* {@code null} value indicates that the elements' <i>natural |
2 | 164 |
* ordering</i> should be used. |
165 |
* @throws ClassCastException if the list contains elements that are not |
|
166 |
* <i>mutually comparable</i> using the specified comparator. |
|
167 |
* @throws UnsupportedOperationException if the specified list's |
|
3420
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
168 |
* list-iterator does not support the {@code set} operation. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
169 |
* @throws IllegalArgumentException (optional) if the comparator is |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
2
diff
changeset
|
170 |
* found to violate the {@link Comparator} contract |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
171 |
* @see List#sort(Comparator) |
2 | 172 |
*/ |
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
173 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
2 | 174 |
public static <T> void sort(List<T> list, Comparator<? super T> c) { |
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
175 |
list.sort(c); |
2 | 176 |
} |
177 |
||
178 |
||
179 |
/** |
|
180 |
* Searches the specified list for the specified object using the binary |
|
181 |
* search algorithm. The list must be sorted into ascending order |
|
182 |
* according to the {@linkplain Comparable natural ordering} of its |
|
183 |
* elements (as by the {@link #sort(List)} method) prior to making this |
|
184 |
* call. If it is not sorted, the results are undefined. If the list |
|
185 |
* contains multiple elements equal to the specified object, there is no |
|
186 |
* guarantee which one will be found. |
|
187 |
* |
|
188 |
* <p>This method runs in log(n) time for a "random access" list (which |
|
189 |
* provides near-constant-time positional access). If the specified list |
|
190 |
* does not implement the {@link RandomAccess} interface and is large, |
|
191 |
* this method will do an iterator-based binary search that performs |
|
192 |
* O(n) link traversals and O(log n) element comparisons. |
|
193 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
194 |
* @param <T> the class of the objects in the list |
2 | 195 |
* @param list the list to be searched. |
196 |
* @param key the key to be searched for. |
|
197 |
* @return the index of the search key, if it is contained in the list; |
|
198 |
* otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The |
|
199 |
* <i>insertion point</i> is defined as the point at which the |
|
200 |
* key would be inserted into the list: the index of the first |
|
201 |
* element greater than the key, or <tt>list.size()</tt> if all |
|
202 |
* elements in the list are less than the specified key. Note |
|
203 |
* that this guarantees that the return value will be >= 0 if |
|
204 |
* and only if the key is found. |
|
205 |
* @throws ClassCastException if the list contains elements that are not |
|
206 |
* <i>mutually comparable</i> (for example, strings and |
|
207 |
* integers), or the search key is not mutually comparable |
|
208 |
* with the elements of the list. |
|
209 |
*/ |
|
210 |
public static <T> |
|
211 |
int binarySearch(List<? extends Comparable<? super T>> list, T key) { |
|
212 |
if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD) |
|
213 |
return Collections.indexedBinarySearch(list, key); |
|
214 |
else |
|
215 |
return Collections.iteratorBinarySearch(list, key); |
|
216 |
} |
|
217 |
||
218 |
private static <T> |
|
16867 | 219 |
int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key) { |
2 | 220 |
int low = 0; |
221 |
int high = list.size()-1; |
|
222 |
||
223 |
while (low <= high) { |
|
224 |
int mid = (low + high) >>> 1; |
|
225 |
Comparable<? super T> midVal = list.get(mid); |
|
226 |
int cmp = midVal.compareTo(key); |
|
227 |
||
228 |
if (cmp < 0) |
|
229 |
low = mid + 1; |
|
230 |
else if (cmp > 0) |
|
231 |
high = mid - 1; |
|
232 |
else |
|
233 |
return mid; // key found |
|
234 |
} |
|
235 |
return -(low + 1); // key not found |
|
236 |
} |
|
237 |
||
238 |
private static <T> |
|
239 |
int iteratorBinarySearch(List<? extends Comparable<? super T>> list, T key) |
|
240 |
{ |
|
241 |
int low = 0; |
|
242 |
int high = list.size()-1; |
|
243 |
ListIterator<? extends Comparable<? super T>> i = list.listIterator(); |
|
244 |
||
245 |
while (low <= high) { |
|
246 |
int mid = (low + high) >>> 1; |
|
247 |
Comparable<? super T> midVal = get(i, mid); |
|
248 |
int cmp = midVal.compareTo(key); |
|
249 |
||
250 |
if (cmp < 0) |
|
251 |
low = mid + 1; |
|
252 |
else if (cmp > 0) |
|
253 |
high = mid - 1; |
|
254 |
else |
|
255 |
return mid; // key found |
|
256 |
} |
|
257 |
return -(low + 1); // key not found |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* Gets the ith element from the given list by repositioning the specified |
|
262 |
* list listIterator. |
|
263 |
*/ |
|
264 |
private static <T> T get(ListIterator<? extends T> i, int index) { |
|
265 |
T obj = null; |
|
266 |
int pos = i.nextIndex(); |
|
267 |
if (pos <= index) { |
|
268 |
do { |
|
269 |
obj = i.next(); |
|
270 |
} while (pos++ < index); |
|
271 |
} else { |
|
272 |
do { |
|
273 |
obj = i.previous(); |
|
274 |
} while (--pos > index); |
|
275 |
} |
|
276 |
return obj; |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Searches the specified list for the specified object using the binary |
|
281 |
* search algorithm. The list must be sorted into ascending order |
|
282 |
* according to the specified comparator (as by the |
|
283 |
* {@link #sort(List, Comparator) sort(List, Comparator)} |
|
284 |
* method), prior to making this call. If it is |
|
285 |
* not sorted, the results are undefined. If the list contains multiple |
|
286 |
* elements equal to the specified object, there is no guarantee which one |
|
287 |
* will be found. |
|
288 |
* |
|
289 |
* <p>This method runs in log(n) time for a "random access" list (which |
|
290 |
* provides near-constant-time positional access). If the specified list |
|
291 |
* does not implement the {@link RandomAccess} interface and is large, |
|
292 |
* this method will do an iterator-based binary search that performs |
|
293 |
* O(n) link traversals and O(log n) element comparisons. |
|
294 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
295 |
* @param <T> the class of the objects in the list |
2 | 296 |
* @param list the list to be searched. |
297 |
* @param key the key to be searched for. |
|
298 |
* @param c the comparator by which the list is ordered. |
|
299 |
* A <tt>null</tt> value indicates that the elements' |
|
300 |
* {@linkplain Comparable natural ordering} should be used. |
|
301 |
* @return the index of the search key, if it is contained in the list; |
|
302 |
* otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The |
|
303 |
* <i>insertion point</i> is defined as the point at which the |
|
304 |
* key would be inserted into the list: the index of the first |
|
305 |
* element greater than the key, or <tt>list.size()</tt> if all |
|
306 |
* elements in the list are less than the specified key. Note |
|
307 |
* that this guarantees that the return value will be >= 0 if |
|
308 |
* and only if the key is found. |
|
309 |
* @throws ClassCastException if the list contains elements that are not |
|
310 |
* <i>mutually comparable</i> using the specified comparator, |
|
311 |
* or the search key is not mutually comparable with the |
|
312 |
* elements of the list using this comparator. |
|
313 |
*/ |
|
12448 | 314 |
@SuppressWarnings("unchecked") |
2 | 315 |
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) { |
316 |
if (c==null) |
|
12448 | 317 |
return binarySearch((List<? extends Comparable<? super T>>) list, key); |
2 | 318 |
|
319 |
if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD) |
|
320 |
return Collections.indexedBinarySearch(list, key, c); |
|
321 |
else |
|
322 |
return Collections.iteratorBinarySearch(list, key, c); |
|
323 |
} |
|
324 |
||
325 |
private static <T> int indexedBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) { |
|
326 |
int low = 0; |
|
327 |
int high = l.size()-1; |
|
328 |
||
329 |
while (low <= high) { |
|
330 |
int mid = (low + high) >>> 1; |
|
331 |
T midVal = l.get(mid); |
|
332 |
int cmp = c.compare(midVal, key); |
|
333 |
||
334 |
if (cmp < 0) |
|
335 |
low = mid + 1; |
|
336 |
else if (cmp > 0) |
|
337 |
high = mid - 1; |
|
338 |
else |
|
339 |
return mid; // key found |
|
340 |
} |
|
341 |
return -(low + 1); // key not found |
|
342 |
} |
|
343 |
||
344 |
private static <T> int iteratorBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) { |
|
345 |
int low = 0; |
|
346 |
int high = l.size()-1; |
|
347 |
ListIterator<? extends T> i = l.listIterator(); |
|
348 |
||
349 |
while (low <= high) { |
|
350 |
int mid = (low + high) >>> 1; |
|
351 |
T midVal = get(i, mid); |
|
352 |
int cmp = c.compare(midVal, key); |
|
353 |
||
354 |
if (cmp < 0) |
|
355 |
low = mid + 1; |
|
356 |
else if (cmp > 0) |
|
357 |
high = mid - 1; |
|
358 |
else |
|
359 |
return mid; // key found |
|
360 |
} |
|
361 |
return -(low + 1); // key not found |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Reverses the order of the elements in the specified list.<p> |
|
366 |
* |
|
367 |
* This method runs in linear time. |
|
368 |
* |
|
369 |
* @param list the list whose elements are to be reversed. |
|
370 |
* @throws UnsupportedOperationException if the specified list or |
|
371 |
* its list-iterator does not support the <tt>set</tt> operation. |
|
372 |
*/ |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
373 |
@SuppressWarnings({"rawtypes", "unchecked"}) |
2 | 374 |
public static void reverse(List<?> list) { |
375 |
int size = list.size(); |
|
376 |
if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) { |
|
377 |
for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--) |
|
378 |
swap(list, i, j); |
|
379 |
} else { |
|
12448 | 380 |
// instead of using a raw type here, it's possible to capture |
381 |
// the wildcard but it will require a call to a supplementary |
|
382 |
// private method |
|
2 | 383 |
ListIterator fwd = list.listIterator(); |
384 |
ListIterator rev = list.listIterator(size); |
|
385 |
for (int i=0, mid=list.size()>>1; i<mid; i++) { |
|
386 |
Object tmp = fwd.next(); |
|
387 |
fwd.set(rev.previous()); |
|
388 |
rev.set(tmp); |
|
389 |
} |
|
390 |
} |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* Randomly permutes the specified list using a default source of |
|
395 |
* randomness. All permutations occur with approximately equal |
|
16867 | 396 |
* likelihood. |
2 | 397 |
* |
16867 | 398 |
* <p>The hedge "approximately" is used in the foregoing description because |
2 | 399 |
* default source of randomness is only approximately an unbiased source |
400 |
* of independently chosen bits. If it were a perfect source of randomly |
|
401 |
* chosen bits, then the algorithm would choose permutations with perfect |
|
16867 | 402 |
* uniformity. |
2 | 403 |
* |
16867 | 404 |
* <p>This implementation traverses the list backwards, from the last |
405 |
* element up to the second, repeatedly swapping a randomly selected element |
|
406 |
* into the "current position". Elements are randomly selected from the |
|
2 | 407 |
* portion of the list that runs from the first element to the current |
16867 | 408 |
* position, inclusive. |
2 | 409 |
* |
16867 | 410 |
* <p>This method runs in linear time. If the specified list does not |
2 | 411 |
* implement the {@link RandomAccess} interface and is large, this |
412 |
* implementation dumps the specified list into an array before shuffling |
|
413 |
* it, and dumps the shuffled array back into the list. This avoids the |
|
414 |
* quadratic behavior that would result from shuffling a "sequential |
|
415 |
* access" list in place. |
|
416 |
* |
|
417 |
* @param list the list to be shuffled. |
|
418 |
* @throws UnsupportedOperationException if the specified list or |
|
419 |
* its list-iterator does not support the <tt>set</tt> operation. |
|
420 |
*/ |
|
421 |
public static void shuffle(List<?> list) { |
|
5781
11a42d91eb56
6959259: Minor improvements to static Random field caching
martin
parents:
5506
diff
changeset
|
422 |
Random rnd = r; |
11a42d91eb56
6959259: Minor improvements to static Random field caching
martin
parents:
5506
diff
changeset
|
423 |
if (rnd == null) |
16867 | 424 |
r = rnd = new Random(); // harmless race. |
5781
11a42d91eb56
6959259: Minor improvements to static Random field caching
martin
parents:
5506
diff
changeset
|
425 |
shuffle(list, rnd); |
2 | 426 |
} |
16867 | 427 |
|
2 | 428 |
private static Random r; |
429 |
||
430 |
/** |
|
431 |
* Randomly permute the specified list using the specified source of |
|
432 |
* randomness. All permutations occur with equal likelihood |
|
433 |
* assuming that the source of randomness is fair.<p> |
|
434 |
* |
|
435 |
* This implementation traverses the list backwards, from the last element |
|
436 |
* up to the second, repeatedly swapping a randomly selected element into |
|
437 |
* the "current position". Elements are randomly selected from the |
|
438 |
* portion of the list that runs from the first element to the current |
|
439 |
* position, inclusive.<p> |
|
440 |
* |
|
441 |
* This method runs in linear time. If the specified list does not |
|
442 |
* implement the {@link RandomAccess} interface and is large, this |
|
443 |
* implementation dumps the specified list into an array before shuffling |
|
444 |
* it, and dumps the shuffled array back into the list. This avoids the |
|
445 |
* quadratic behavior that would result from shuffling a "sequential |
|
446 |
* access" list in place. |
|
447 |
* |
|
448 |
* @param list the list to be shuffled. |
|
449 |
* @param rnd the source of randomness to use to shuffle the list. |
|
450 |
* @throws UnsupportedOperationException if the specified list or its |
|
451 |
* list-iterator does not support the <tt>set</tt> operation. |
|
452 |
*/ |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
453 |
@SuppressWarnings({"rawtypes", "unchecked"}) |
2 | 454 |
public static void shuffle(List<?> list, Random rnd) { |
455 |
int size = list.size(); |
|
456 |
if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) { |
|
457 |
for (int i=size; i>1; i--) |
|
458 |
swap(list, i-1, rnd.nextInt(i)); |
|
459 |
} else { |
|
460 |
Object arr[] = list.toArray(); |
|
461 |
||
462 |
// Shuffle array |
|
463 |
for (int i=size; i>1; i--) |
|
464 |
swap(arr, i-1, rnd.nextInt(i)); |
|
465 |
||
466 |
// Dump array back into list |
|
12448 | 467 |
// instead of using a raw type here, it's possible to capture |
468 |
// the wildcard but it will require a call to a supplementary |
|
469 |
// private method |
|
2 | 470 |
ListIterator it = list.listIterator(); |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19855
diff
changeset
|
471 |
for (Object e : arr) { |
2 | 472 |
it.next(); |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19855
diff
changeset
|
473 |
it.set(e); |
2 | 474 |
} |
475 |
} |
|
476 |
} |
|
477 |
||
478 |
/** |
|
479 |
* Swaps the elements at the specified positions in the specified list. |
|
480 |
* (If the specified positions are equal, invoking this method leaves |
|
481 |
* the list unchanged.) |
|
482 |
* |
|
483 |
* @param list The list in which to swap elements. |
|
484 |
* @param i the index of one element to be swapped. |
|
485 |
* @param j the index of the other element to be swapped. |
|
486 |
* @throws IndexOutOfBoundsException if either <tt>i</tt> or <tt>j</tt> |
|
487 |
* is out of range (i < 0 || i >= list.size() |
|
488 |
* || j < 0 || j >= list.size()). |
|
489 |
* @since 1.4 |
|
490 |
*/ |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
491 |
@SuppressWarnings({"rawtypes", "unchecked"}) |
2 | 492 |
public static void swap(List<?> list, int i, int j) { |
12448 | 493 |
// instead of using a raw type here, it's possible to capture |
494 |
// the wildcard but it will require a call to a supplementary |
|
495 |
// private method |
|
2 | 496 |
final List l = list; |
497 |
l.set(i, l.set(j, l.get(i))); |
|
498 |
} |
|
499 |
||
500 |
/** |
|
501 |
* Swaps the two specified elements in the specified array. |
|
502 |
*/ |
|
503 |
private static void swap(Object[] arr, int i, int j) { |
|
504 |
Object tmp = arr[i]; |
|
505 |
arr[i] = arr[j]; |
|
506 |
arr[j] = tmp; |
|
507 |
} |
|
508 |
||
509 |
/** |
|
510 |
* Replaces all of the elements of the specified list with the specified |
|
511 |
* element. <p> |
|
512 |
* |
|
513 |
* This method runs in linear time. |
|
514 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
515 |
* @param <T> the class of the objects in the list |
2 | 516 |
* @param list the list to be filled with the specified element. |
517 |
* @param obj The element with which to fill the specified list. |
|
518 |
* @throws UnsupportedOperationException if the specified list or its |
|
519 |
* list-iterator does not support the <tt>set</tt> operation. |
|
520 |
*/ |
|
521 |
public static <T> void fill(List<? super T> list, T obj) { |
|
522 |
int size = list.size(); |
|
523 |
||
524 |
if (size < FILL_THRESHOLD || list instanceof RandomAccess) { |
|
525 |
for (int i=0; i<size; i++) |
|
526 |
list.set(i, obj); |
|
527 |
} else { |
|
528 |
ListIterator<? super T> itr = list.listIterator(); |
|
529 |
for (int i=0; i<size; i++) { |
|
530 |
itr.next(); |
|
531 |
itr.set(obj); |
|
532 |
} |
|
533 |
} |
|
534 |
} |
|
535 |
||
536 |
/** |
|
537 |
* Copies all of the elements from one list into another. After the |
|
538 |
* operation, the index of each copied element in the destination list |
|
539 |
* will be identical to its index in the source list. The destination |
|
540 |
* list must be at least as long as the source list. If it is longer, the |
|
541 |
* remaining elements in the destination list are unaffected. <p> |
|
542 |
* |
|
543 |
* This method runs in linear time. |
|
544 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
545 |
* @param <T> the class of the objects in the lists |
2 | 546 |
* @param dest The destination list. |
547 |
* @param src The source list. |
|
548 |
* @throws IndexOutOfBoundsException if the destination list is too small |
|
549 |
* to contain the entire source List. |
|
550 |
* @throws UnsupportedOperationException if the destination list's |
|
551 |
* list-iterator does not support the <tt>set</tt> operation. |
|
552 |
*/ |
|
553 |
public static <T> void copy(List<? super T> dest, List<? extends T> src) { |
|
554 |
int srcSize = src.size(); |
|
555 |
if (srcSize > dest.size()) |
|
556 |
throw new IndexOutOfBoundsException("Source does not fit in dest"); |
|
557 |
||
558 |
if (srcSize < COPY_THRESHOLD || |
|
559 |
(src instanceof RandomAccess && dest instanceof RandomAccess)) { |
|
560 |
for (int i=0; i<srcSize; i++) |
|
561 |
dest.set(i, src.get(i)); |
|
562 |
} else { |
|
563 |
ListIterator<? super T> di=dest.listIterator(); |
|
564 |
ListIterator<? extends T> si=src.listIterator(); |
|
565 |
for (int i=0; i<srcSize; i++) { |
|
566 |
di.next(); |
|
567 |
di.set(si.next()); |
|
568 |
} |
|
569 |
} |
|
570 |
} |
|
571 |
||
572 |
/** |
|
573 |
* Returns the minimum element of the given collection, according to the |
|
574 |
* <i>natural ordering</i> of its elements. All elements in the |
|
575 |
* collection must implement the <tt>Comparable</tt> interface. |
|
576 |
* Furthermore, all elements in the collection must be <i>mutually |
|
577 |
* comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a |
|
578 |
* <tt>ClassCastException</tt> for any elements <tt>e1</tt> and |
|
579 |
* <tt>e2</tt> in the collection).<p> |
|
580 |
* |
|
581 |
* This method iterates over the entire collection, hence it requires |
|
582 |
* time proportional to the size of the collection. |
|
583 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
584 |
* @param <T> the class of the objects in the collection |
2 | 585 |
* @param coll the collection whose minimum element is to be determined. |
586 |
* @return the minimum element of the given collection, according |
|
587 |
* to the <i>natural ordering</i> of its elements. |
|
588 |
* @throws ClassCastException if the collection contains elements that are |
|
589 |
* not <i>mutually comparable</i> (for example, strings and |
|
590 |
* integers). |
|
591 |
* @throws NoSuchElementException if the collection is empty. |
|
592 |
* @see Comparable |
|
593 |
*/ |
|
594 |
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) { |
|
595 |
Iterator<? extends T> i = coll.iterator(); |
|
596 |
T candidate = i.next(); |
|
597 |
||
598 |
while (i.hasNext()) { |
|
599 |
T next = i.next(); |
|
600 |
if (next.compareTo(candidate) < 0) |
|
601 |
candidate = next; |
|
602 |
} |
|
603 |
return candidate; |
|
604 |
} |
|
605 |
||
606 |
/** |
|
607 |
* Returns the minimum element of the given collection, according to the |
|
608 |
* order induced by the specified comparator. All elements in the |
|
609 |
* collection must be <i>mutually comparable</i> by the specified |
|
610 |
* comparator (that is, <tt>comp.compare(e1, e2)</tt> must not throw a |
|
611 |
* <tt>ClassCastException</tt> for any elements <tt>e1</tt> and |
|
612 |
* <tt>e2</tt> in the collection).<p> |
|
613 |
* |
|
614 |
* This method iterates over the entire collection, hence it requires |
|
615 |
* time proportional to the size of the collection. |
|
616 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
617 |
* @param <T> the class of the objects in the collection |
2 | 618 |
* @param coll the collection whose minimum element is to be determined. |
619 |
* @param comp the comparator with which to determine the minimum element. |
|
620 |
* A <tt>null</tt> value indicates that the elements' <i>natural |
|
621 |
* ordering</i> should be used. |
|
622 |
* @return the minimum element of the given collection, according |
|
623 |
* to the specified comparator. |
|
624 |
* @throws ClassCastException if the collection contains elements that are |
|
625 |
* not <i>mutually comparable</i> using the specified comparator. |
|
626 |
* @throws NoSuchElementException if the collection is empty. |
|
627 |
* @see Comparable |
|
628 |
*/ |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
629 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
2 | 630 |
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) { |
631 |
if (comp==null) |
|
12448 | 632 |
return (T)min((Collection) coll); |
2 | 633 |
|
634 |
Iterator<? extends T> i = coll.iterator(); |
|
635 |
T candidate = i.next(); |
|
636 |
||
637 |
while (i.hasNext()) { |
|
638 |
T next = i.next(); |
|
639 |
if (comp.compare(next, candidate) < 0) |
|
640 |
candidate = next; |
|
641 |
} |
|
642 |
return candidate; |
|
643 |
} |
|
644 |
||
645 |
/** |
|
646 |
* Returns the maximum element of the given collection, according to the |
|
647 |
* <i>natural ordering</i> of its elements. All elements in the |
|
648 |
* collection must implement the <tt>Comparable</tt> interface. |
|
649 |
* Furthermore, all elements in the collection must be <i>mutually |
|
650 |
* comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a |
|
651 |
* <tt>ClassCastException</tt> for any elements <tt>e1</tt> and |
|
652 |
* <tt>e2</tt> in the collection).<p> |
|
653 |
* |
|
654 |
* This method iterates over the entire collection, hence it requires |
|
655 |
* time proportional to the size of the collection. |
|
656 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
657 |
* @param <T> the class of the objects in the collection |
2 | 658 |
* @param coll the collection whose maximum element is to be determined. |
659 |
* @return the maximum element of the given collection, according |
|
660 |
* to the <i>natural ordering</i> of its elements. |
|
661 |
* @throws ClassCastException if the collection contains elements that are |
|
662 |
* not <i>mutually comparable</i> (for example, strings and |
|
663 |
* integers). |
|
664 |
* @throws NoSuchElementException if the collection is empty. |
|
665 |
* @see Comparable |
|
666 |
*/ |
|
667 |
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) { |
|
668 |
Iterator<? extends T> i = coll.iterator(); |
|
669 |
T candidate = i.next(); |
|
670 |
||
671 |
while (i.hasNext()) { |
|
672 |
T next = i.next(); |
|
673 |
if (next.compareTo(candidate) > 0) |
|
674 |
candidate = next; |
|
675 |
} |
|
676 |
return candidate; |
|
677 |
} |
|
678 |
||
679 |
/** |
|
680 |
* Returns the maximum element of the given collection, according to the |
|
681 |
* order induced by the specified comparator. All elements in the |
|
682 |
* collection must be <i>mutually comparable</i> by the specified |
|
683 |
* comparator (that is, <tt>comp.compare(e1, e2)</tt> must not throw a |
|
684 |
* <tt>ClassCastException</tt> for any elements <tt>e1</tt> and |
|
685 |
* <tt>e2</tt> in the collection).<p> |
|
686 |
* |
|
687 |
* This method iterates over the entire collection, hence it requires |
|
688 |
* time proportional to the size of the collection. |
|
689 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
690 |
* @param <T> the class of the objects in the collection |
2 | 691 |
* @param coll the collection whose maximum element is to be determined. |
692 |
* @param comp the comparator with which to determine the maximum element. |
|
693 |
* A <tt>null</tt> value indicates that the elements' <i>natural |
|
694 |
* ordering</i> should be used. |
|
695 |
* @return the maximum element of the given collection, according |
|
696 |
* to the specified comparator. |
|
697 |
* @throws ClassCastException if the collection contains elements that are |
|
698 |
* not <i>mutually comparable</i> using the specified comparator. |
|
699 |
* @throws NoSuchElementException if the collection is empty. |
|
700 |
* @see Comparable |
|
701 |
*/ |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
702 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
2 | 703 |
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) { |
704 |
if (comp==null) |
|
12448 | 705 |
return (T)max((Collection) coll); |
2 | 706 |
|
707 |
Iterator<? extends T> i = coll.iterator(); |
|
708 |
T candidate = i.next(); |
|
709 |
||
710 |
while (i.hasNext()) { |
|
711 |
T next = i.next(); |
|
712 |
if (comp.compare(next, candidate) > 0) |
|
713 |
candidate = next; |
|
714 |
} |
|
715 |
return candidate; |
|
716 |
} |
|
717 |
||
718 |
/** |
|
719 |
* Rotates the elements in the specified list by the specified distance. |
|
720 |
* After calling this method, the element at index <tt>i</tt> will be |
|
721 |
* the element previously at index <tt>(i - distance)</tt> mod |
|
722 |
* <tt>list.size()</tt>, for all values of <tt>i</tt> between <tt>0</tt> |
|
723 |
* and <tt>list.size()-1</tt>, inclusive. (This method has no effect on |
|
724 |
* the size of the list.) |
|
725 |
* |
|
726 |
* <p>For example, suppose <tt>list</tt> comprises<tt> [t, a, n, k, s]</tt>. |
|
727 |
* After invoking <tt>Collections.rotate(list, 1)</tt> (or |
|
728 |
* <tt>Collections.rotate(list, -4)</tt>), <tt>list</tt> will comprise |
|
729 |
* <tt>[s, t, a, n, k]</tt>. |
|
730 |
* |
|
731 |
* <p>Note that this method can usefully be applied to sublists to |
|
732 |
* move one or more elements within a list while preserving the |
|
733 |
* order of the remaining elements. For example, the following idiom |
|
734 |
* moves the element at index <tt>j</tt> forward to position |
|
735 |
* <tt>k</tt> (which must be greater than or equal to <tt>j</tt>): |
|
736 |
* <pre> |
|
737 |
* Collections.rotate(list.subList(j, k+1), -1); |
|
738 |
* </pre> |
|
739 |
* To make this concrete, suppose <tt>list</tt> comprises |
|
740 |
* <tt>[a, b, c, d, e]</tt>. To move the element at index <tt>1</tt> |
|
741 |
* (<tt>b</tt>) forward two positions, perform the following invocation: |
|
742 |
* <pre> |
|
743 |
* Collections.rotate(l.subList(1, 4), -1); |
|
744 |
* </pre> |
|
745 |
* The resulting list is <tt>[a, c, d, b, e]</tt>. |
|
746 |
* |
|
747 |
* <p>To move more than one element forward, increase the absolute value |
|
748 |
* of the rotation distance. To move elements backward, use a positive |
|
749 |
* shift distance. |
|
750 |
* |
|
751 |
* <p>If the specified list is small or implements the {@link |
|
752 |
* RandomAccess} interface, this implementation exchanges the first |
|
753 |
* element into the location it should go, and then repeatedly exchanges |
|
754 |
* the displaced element into the location it should go until a displaced |
|
755 |
* element is swapped into the first element. If necessary, the process |
|
756 |
* is repeated on the second and successive elements, until the rotation |
|
757 |
* is complete. If the specified list is large and doesn't implement the |
|
758 |
* <tt>RandomAccess</tt> interface, this implementation breaks the |
|
759 |
* list into two sublist views around index <tt>-distance mod size</tt>. |
|
760 |
* Then the {@link #reverse(List)} method is invoked on each sublist view, |
|
761 |
* and finally it is invoked on the entire list. For a more complete |
|
762 |
* description of both algorithms, see Section 2.3 of Jon Bentley's |
|
763 |
* <i>Programming Pearls</i> (Addison-Wesley, 1986). |
|
764 |
* |
|
765 |
* @param list the list to be rotated. |
|
766 |
* @param distance the distance to rotate the list. There are no |
|
767 |
* constraints on this value; it may be zero, negative, or |
|
768 |
* greater than <tt>list.size()</tt>. |
|
769 |
* @throws UnsupportedOperationException if the specified list or |
|
770 |
* its list-iterator does not support the <tt>set</tt> operation. |
|
771 |
* @since 1.4 |
|
772 |
*/ |
|
773 |
public static void rotate(List<?> list, int distance) { |
|
774 |
if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD) |
|
775 |
rotate1(list, distance); |
|
776 |
else |
|
777 |
rotate2(list, distance); |
|
778 |
} |
|
779 |
||
780 |
private static <T> void rotate1(List<T> list, int distance) { |
|
781 |
int size = list.size(); |
|
782 |
if (size == 0) |
|
783 |
return; |
|
784 |
distance = distance % size; |
|
785 |
if (distance < 0) |
|
786 |
distance += size; |
|
787 |
if (distance == 0) |
|
788 |
return; |
|
789 |
||
790 |
for (int cycleStart = 0, nMoved = 0; nMoved != size; cycleStart++) { |
|
791 |
T displaced = list.get(cycleStart); |
|
792 |
int i = cycleStart; |
|
793 |
do { |
|
794 |
i += distance; |
|
795 |
if (i >= size) |
|
796 |
i -= size; |
|
797 |
displaced = list.set(i, displaced); |
|
798 |
nMoved ++; |
|
7518 | 799 |
} while (i != cycleStart); |
2 | 800 |
} |
801 |
} |
|
802 |
||
803 |
private static void rotate2(List<?> list, int distance) { |
|
804 |
int size = list.size(); |
|
805 |
if (size == 0) |
|
806 |
return; |
|
807 |
int mid = -distance % size; |
|
808 |
if (mid < 0) |
|
809 |
mid += size; |
|
810 |
if (mid == 0) |
|
811 |
return; |
|
812 |
||
813 |
reverse(list.subList(0, mid)); |
|
814 |
reverse(list.subList(mid, size)); |
|
815 |
reverse(list); |
|
816 |
} |
|
817 |
||
818 |
/** |
|
819 |
* Replaces all occurrences of one specified value in a list with another. |
|
820 |
* More formally, replaces with <tt>newVal</tt> each element <tt>e</tt> |
|
821 |
* in <tt>list</tt> such that |
|
822 |
* <tt>(oldVal==null ? e==null : oldVal.equals(e))</tt>. |
|
823 |
* (This method has no effect on the size of the list.) |
|
824 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
825 |
* @param <T> the class of the objects in the list |
2 | 826 |
* @param list the list in which replacement is to occur. |
827 |
* @param oldVal the old value to be replaced. |
|
828 |
* @param newVal the new value with which <tt>oldVal</tt> is to be |
|
829 |
* replaced. |
|
830 |
* @return <tt>true</tt> if <tt>list</tt> contained one or more elements |
|
831 |
* <tt>e</tt> such that |
|
832 |
* <tt>(oldVal==null ? e==null : oldVal.equals(e))</tt>. |
|
833 |
* @throws UnsupportedOperationException if the specified list or |
|
834 |
* its list-iterator does not support the <tt>set</tt> operation. |
|
835 |
* @since 1.4 |
|
836 |
*/ |
|
837 |
public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) { |
|
838 |
boolean result = false; |
|
839 |
int size = list.size(); |
|
840 |
if (size < REPLACEALL_THRESHOLD || list instanceof RandomAccess) { |
|
841 |
if (oldVal==null) { |
|
842 |
for (int i=0; i<size; i++) { |
|
843 |
if (list.get(i)==null) { |
|
844 |
list.set(i, newVal); |
|
845 |
result = true; |
|
846 |
} |
|
847 |
} |
|
848 |
} else { |
|
849 |
for (int i=0; i<size; i++) { |
|
850 |
if (oldVal.equals(list.get(i))) { |
|
851 |
list.set(i, newVal); |
|
852 |
result = true; |
|
853 |
} |
|
854 |
} |
|
855 |
} |
|
856 |
} else { |
|
857 |
ListIterator<T> itr=list.listIterator(); |
|
858 |
if (oldVal==null) { |
|
859 |
for (int i=0; i<size; i++) { |
|
860 |
if (itr.next()==null) { |
|
861 |
itr.set(newVal); |
|
862 |
result = true; |
|
863 |
} |
|
864 |
} |
|
865 |
} else { |
|
866 |
for (int i=0; i<size; i++) { |
|
867 |
if (oldVal.equals(itr.next())) { |
|
868 |
itr.set(newVal); |
|
869 |
result = true; |
|
870 |
} |
|
871 |
} |
|
872 |
} |
|
873 |
} |
|
874 |
return result; |
|
875 |
} |
|
876 |
||
877 |
/** |
|
878 |
* Returns the starting position of the first occurrence of the specified |
|
879 |
* target list within the specified source list, or -1 if there is no |
|
880 |
* such occurrence. More formally, returns the lowest index <tt>i</tt> |
|
18156 | 881 |
* such that {@code source.subList(i, i+target.size()).equals(target)}, |
2 | 882 |
* or -1 if there is no such index. (Returns -1 if |
18156 | 883 |
* {@code target.size() > source.size()}) |
2 | 884 |
* |
885 |
* <p>This implementation uses the "brute force" technique of scanning |
|
886 |
* over the source list, looking for a match with the target at each |
|
887 |
* location in turn. |
|
888 |
* |
|
889 |
* @param source the list in which to search for the first occurrence |
|
890 |
* of <tt>target</tt>. |
|
891 |
* @param target the list to search for as a subList of <tt>source</tt>. |
|
892 |
* @return the starting position of the first occurrence of the specified |
|
893 |
* target list within the specified source list, or -1 if there |
|
894 |
* is no such occurrence. |
|
895 |
* @since 1.4 |
|
896 |
*/ |
|
897 |
public static int indexOfSubList(List<?> source, List<?> target) { |
|
898 |
int sourceSize = source.size(); |
|
899 |
int targetSize = target.size(); |
|
900 |
int maxCandidate = sourceSize - targetSize; |
|
901 |
||
902 |
if (sourceSize < INDEXOFSUBLIST_THRESHOLD || |
|
903 |
(source instanceof RandomAccess&&target instanceof RandomAccess)) { |
|
904 |
nextCand: |
|
905 |
for (int candidate = 0; candidate <= maxCandidate; candidate++) { |
|
906 |
for (int i=0, j=candidate; i<targetSize; i++, j++) |
|
907 |
if (!eq(target.get(i), source.get(j))) |
|
908 |
continue nextCand; // Element mismatch, try next cand |
|
909 |
return candidate; // All elements of candidate matched target |
|
910 |
} |
|
911 |
} else { // Iterator version of above algorithm |
|
912 |
ListIterator<?> si = source.listIterator(); |
|
913 |
nextCand: |
|
914 |
for (int candidate = 0; candidate <= maxCandidate; candidate++) { |
|
915 |
ListIterator<?> ti = target.listIterator(); |
|
916 |
for (int i=0; i<targetSize; i++) { |
|
917 |
if (!eq(ti.next(), si.next())) { |
|
918 |
// Back up source iterator to next candidate |
|
919 |
for (int j=0; j<i; j++) |
|
920 |
si.previous(); |
|
921 |
continue nextCand; |
|
922 |
} |
|
923 |
} |
|
924 |
return candidate; |
|
925 |
} |
|
926 |
} |
|
927 |
return -1; // No candidate matched the target |
|
928 |
} |
|
929 |
||
930 |
/** |
|
931 |
* Returns the starting position of the last occurrence of the specified |
|
932 |
* target list within the specified source list, or -1 if there is no such |
|
933 |
* occurrence. More formally, returns the highest index <tt>i</tt> |
|
18156 | 934 |
* such that {@code source.subList(i, i+target.size()).equals(target)}, |
2 | 935 |
* or -1 if there is no such index. (Returns -1 if |
18156 | 936 |
* {@code target.size() > source.size()}) |
2 | 937 |
* |
938 |
* <p>This implementation uses the "brute force" technique of iterating |
|
939 |
* over the source list, looking for a match with the target at each |
|
940 |
* location in turn. |
|
941 |
* |
|
942 |
* @param source the list in which to search for the last occurrence |
|
943 |
* of <tt>target</tt>. |
|
944 |
* @param target the list to search for as a subList of <tt>source</tt>. |
|
945 |
* @return the starting position of the last occurrence of the specified |
|
946 |
* target list within the specified source list, or -1 if there |
|
947 |
* is no such occurrence. |
|
948 |
* @since 1.4 |
|
949 |
*/ |
|
950 |
public static int lastIndexOfSubList(List<?> source, List<?> target) { |
|
951 |
int sourceSize = source.size(); |
|
952 |
int targetSize = target.size(); |
|
953 |
int maxCandidate = sourceSize - targetSize; |
|
954 |
||
955 |
if (sourceSize < INDEXOFSUBLIST_THRESHOLD || |
|
956 |
source instanceof RandomAccess) { // Index access version |
|
957 |
nextCand: |
|
958 |
for (int candidate = maxCandidate; candidate >= 0; candidate--) { |
|
959 |
for (int i=0, j=candidate; i<targetSize; i++, j++) |
|
960 |
if (!eq(target.get(i), source.get(j))) |
|
961 |
continue nextCand; // Element mismatch, try next cand |
|
962 |
return candidate; // All elements of candidate matched target |
|
963 |
} |
|
964 |
} else { // Iterator version of above algorithm |
|
965 |
if (maxCandidate < 0) |
|
966 |
return -1; |
|
967 |
ListIterator<?> si = source.listIterator(maxCandidate); |
|
968 |
nextCand: |
|
969 |
for (int candidate = maxCandidate; candidate >= 0; candidate--) { |
|
970 |
ListIterator<?> ti = target.listIterator(); |
|
971 |
for (int i=0; i<targetSize; i++) { |
|
972 |
if (!eq(ti.next(), si.next())) { |
|
973 |
if (candidate != 0) { |
|
974 |
// Back up source iterator to next candidate |
|
975 |
for (int j=0; j<=i+1; j++) |
|
976 |
si.previous(); |
|
977 |
} |
|
978 |
continue nextCand; |
|
979 |
} |
|
980 |
} |
|
981 |
return candidate; |
|
982 |
} |
|
983 |
} |
|
984 |
return -1; // No candidate matched the target |
|
985 |
} |
|
986 |
||
987 |
||
988 |
// Unmodifiable Wrappers |
|
989 |
||
990 |
/** |
|
991 |
* Returns an unmodifiable view of the specified collection. This method |
|
992 |
* allows modules to provide users with "read-only" access to internal |
|
993 |
* collections. Query operations on the returned collection "read through" |
|
994 |
* to the specified collection, and attempts to modify the returned |
|
995 |
* collection, whether direct or via its iterator, result in an |
|
996 |
* <tt>UnsupportedOperationException</tt>.<p> |
|
997 |
* |
|
998 |
* The returned collection does <i>not</i> pass the hashCode and equals |
|
999 |
* operations through to the backing collection, but relies on |
|
1000 |
* <tt>Object</tt>'s <tt>equals</tt> and <tt>hashCode</tt> methods. This |
|
1001 |
* is necessary to preserve the contracts of these operations in the case |
|
1002 |
* that the backing collection is a set or a list.<p> |
|
1003 |
* |
|
1004 |
* The returned collection will be serializable if the specified collection |
|
1005 |
* is serializable. |
|
1006 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1007 |
* @param <T> the class of the objects in the collection |
2 | 1008 |
* @param c the collection for which an unmodifiable view is to be |
1009 |
* returned. |
|
1010 |
* @return an unmodifiable view of the specified collection. |
|
1011 |
*/ |
|
1012 |
public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1013 |
return new UnmodifiableCollection<>(c); |
2 | 1014 |
} |
1015 |
||
1016 |
/** |
|
1017 |
* @serial include |
|
1018 |
*/ |
|
1019 |
static class UnmodifiableCollection<E> implements Collection<E>, Serializable { |
|
1020 |
private static final long serialVersionUID = 1820017752578914078L; |
|
1021 |
||
1022 |
final Collection<? extends E> c; |
|
1023 |
||
1024 |
UnmodifiableCollection(Collection<? extends E> c) { |
|
1025 |
if (c==null) |
|
1026 |
throw new NullPointerException(); |
|
1027 |
this.c = c; |
|
1028 |
} |
|
1029 |
||
1030 |
public int size() {return c.size();} |
|
1031 |
public boolean isEmpty() {return c.isEmpty();} |
|
1032 |
public boolean contains(Object o) {return c.contains(o);} |
|
1033 |
public Object[] toArray() {return c.toArray();} |
|
1034 |
public <T> T[] toArray(T[] a) {return c.toArray(a);} |
|
1035 |
public String toString() {return c.toString();} |
|
1036 |
||
1037 |
public Iterator<E> iterator() { |
|
1038 |
return new Iterator<E>() { |
|
1039 |
private final Iterator<? extends E> i = c.iterator(); |
|
1040 |
||
1041 |
public boolean hasNext() {return i.hasNext();} |
|
1042 |
public E next() {return i.next();} |
|
1043 |
public void remove() { |
|
1044 |
throw new UnsupportedOperationException(); |
|
1045 |
} |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1046 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1047 |
public void forEachRemaining(Consumer<? super E> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1048 |
// Use backing collection version |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1049 |
i.forEachRemaining(action); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1050 |
} |
2 | 1051 |
}; |
1052 |
} |
|
1053 |
||
1054 |
public boolean add(E e) { |
|
1055 |
throw new UnsupportedOperationException(); |
|
1056 |
} |
|
1057 |
public boolean remove(Object o) { |
|
1058 |
throw new UnsupportedOperationException(); |
|
1059 |
} |
|
1060 |
||
1061 |
public boolean containsAll(Collection<?> coll) { |
|
1062 |
return c.containsAll(coll); |
|
1063 |
} |
|
1064 |
public boolean addAll(Collection<? extends E> coll) { |
|
1065 |
throw new UnsupportedOperationException(); |
|
1066 |
} |
|
1067 |
public boolean removeAll(Collection<?> coll) { |
|
1068 |
throw new UnsupportedOperationException(); |
|
1069 |
} |
|
1070 |
public boolean retainAll(Collection<?> coll) { |
|
1071 |
throw new UnsupportedOperationException(); |
|
1072 |
} |
|
1073 |
public void clear() { |
|
1074 |
throw new UnsupportedOperationException(); |
|
1075 |
} |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1076 |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1077 |
// Override default methods in Collection |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1078 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1079 |
public void forEach(Consumer<? super E> action) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1080 |
c.forEach(action); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1081 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1082 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1083 |
public boolean removeIf(Predicate<? super E> filter) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1084 |
throw new UnsupportedOperationException(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1085 |
} |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1086 |
@SuppressWarnings("unchecked") |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1087 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1088 |
public Spliterator<E> spliterator() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1089 |
return (Spliterator<E>)c.spliterator(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1090 |
} |
19603
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1091 |
@SuppressWarnings("unchecked") |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1092 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1093 |
public Stream<E> stream() { |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1094 |
return (Stream<E>)c.stream(); |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1095 |
} |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1096 |
@SuppressWarnings("unchecked") |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1097 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1098 |
public Stream<E> parallelStream() { |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1099 |
return (Stream<E>)c.parallelStream(); |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1100 |
} |
2 | 1101 |
} |
1102 |
||
1103 |
/** |
|
1104 |
* Returns an unmodifiable view of the specified set. This method allows |
|
1105 |
* modules to provide users with "read-only" access to internal sets. |
|
1106 |
* Query operations on the returned set "read through" to the specified |
|
1107 |
* set, and attempts to modify the returned set, whether direct or via its |
|
1108 |
* iterator, result in an <tt>UnsupportedOperationException</tt>.<p> |
|
1109 |
* |
|
1110 |
* The returned set will be serializable if the specified set |
|
1111 |
* is serializable. |
|
1112 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1113 |
* @param <T> the class of the objects in the set |
2 | 1114 |
* @param s the set for which an unmodifiable view is to be returned. |
1115 |
* @return an unmodifiable view of the specified set. |
|
1116 |
*/ |
|
1117 |
public static <T> Set<T> unmodifiableSet(Set<? extends T> s) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1118 |
return new UnmodifiableSet<>(s); |
2 | 1119 |
} |
1120 |
||
1121 |
/** |
|
1122 |
* @serial include |
|
1123 |
*/ |
|
1124 |
static class UnmodifiableSet<E> extends UnmodifiableCollection<E> |
|
1125 |
implements Set<E>, Serializable { |
|
1126 |
private static final long serialVersionUID = -9215047833775013803L; |
|
1127 |
||
1128 |
UnmodifiableSet(Set<? extends E> s) {super(s);} |
|
1129 |
public boolean equals(Object o) {return o == this || c.equals(o);} |
|
1130 |
public int hashCode() {return c.hashCode();} |
|
1131 |
} |
|
1132 |
||
1133 |
/** |
|
1134 |
* Returns an unmodifiable view of the specified sorted set. This method |
|
1135 |
* allows modules to provide users with "read-only" access to internal |
|
1136 |
* sorted sets. Query operations on the returned sorted set "read |
|
1137 |
* through" to the specified sorted set. Attempts to modify the returned |
|
1138 |
* sorted set, whether direct, via its iterator, or via its |
|
1139 |
* <tt>subSet</tt>, <tt>headSet</tt>, or <tt>tailSet</tt> views, result in |
|
1140 |
* an <tt>UnsupportedOperationException</tt>.<p> |
|
1141 |
* |
|
1142 |
* The returned sorted set will be serializable if the specified sorted set |
|
1143 |
* is serializable. |
|
1144 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1145 |
* @param <T> the class of the objects in the set |
2 | 1146 |
* @param s the sorted set for which an unmodifiable view is to be |
1147 |
* returned. |
|
1148 |
* @return an unmodifiable view of the specified sorted set. |
|
1149 |
*/ |
|
1150 |
public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1151 |
return new UnmodifiableSortedSet<>(s); |
2 | 1152 |
} |
1153 |
||
1154 |
/** |
|
1155 |
* @serial include |
|
1156 |
*/ |
|
1157 |
static class UnmodifiableSortedSet<E> |
|
1158 |
extends UnmodifiableSet<E> |
|
1159 |
implements SortedSet<E>, Serializable { |
|
1160 |
private static final long serialVersionUID = -4929149591599911165L; |
|
1161 |
private final SortedSet<E> ss; |
|
1162 |
||
1163 |
UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;} |
|
1164 |
||
1165 |
public Comparator<? super E> comparator() {return ss.comparator();} |
|
1166 |
||
1167 |
public SortedSet<E> subSet(E fromElement, E toElement) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1168 |
return new UnmodifiableSortedSet<>(ss.subSet(fromElement,toElement)); |
2 | 1169 |
} |
1170 |
public SortedSet<E> headSet(E toElement) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1171 |
return new UnmodifiableSortedSet<>(ss.headSet(toElement)); |
2 | 1172 |
} |
1173 |
public SortedSet<E> tailSet(E fromElement) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1174 |
return new UnmodifiableSortedSet<>(ss.tailSet(fromElement)); |
2 | 1175 |
} |
1176 |
||
1177 |
public E first() {return ss.first();} |
|
1178 |
public E last() {return ss.last();} |
|
1179 |
} |
|
1180 |
||
1181 |
/** |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1182 |
* Returns an unmodifiable view of the specified navigable set. This method |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1183 |
* allows modules to provide users with "read-only" access to internal |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1184 |
* navigable sets. Query operations on the returned navigable set "read |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1185 |
* through" to the specified navigable set. Attempts to modify the returned |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1186 |
* navigable set, whether direct, via its iterator, or via its |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1187 |
* {@code subSet}, {@code headSet}, or {@code tailSet} views, result in |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1188 |
* an {@code UnsupportedOperationException}.<p> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1189 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1190 |
* The returned navigable set will be serializable if the specified |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1191 |
* navigable set is serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1192 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1193 |
* @param <T> the class of the objects in the set |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1194 |
* @param s the navigable set for which an unmodifiable view is to be |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1195 |
* returned |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1196 |
* @return an unmodifiable view of the specified navigable set |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1197 |
* @since 1.8 |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1198 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1199 |
public static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1200 |
return new UnmodifiableNavigableSet<>(s); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1201 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1202 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1203 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1204 |
* Wraps a navigable set and disables all of the mutative operations. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1205 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1206 |
* @param <E> type of elements |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1207 |
* @serial include |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1208 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1209 |
static class UnmodifiableNavigableSet<E> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1210 |
extends UnmodifiableSortedSet<E> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1211 |
implements NavigableSet<E>, Serializable { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1212 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1213 |
private static final long serialVersionUID = -6027448201786391929L; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1214 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1215 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1216 |
* A singleton empty unmodifiable navigable set used for |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1217 |
* {@link #emptyNavigableSet()}. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1218 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1219 |
* @param <E> type of elements, if there were any, and bounds |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1220 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1221 |
private static class EmptyNavigableSet<E> extends UnmodifiableNavigableSet<E> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1222 |
implements Serializable { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1223 |
private static final long serialVersionUID = -6291252904449939134L; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1224 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1225 |
public EmptyNavigableSet() { |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19855
diff
changeset
|
1226 |
super(new TreeSet<>()); |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1227 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1228 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1229 |
private Object readResolve() { return EMPTY_NAVIGABLE_SET; } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1230 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1231 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1232 |
@SuppressWarnings("rawtypes") |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1233 |
private static final NavigableSet<?> EMPTY_NAVIGABLE_SET = |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1234 |
new EmptyNavigableSet<>(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1235 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1236 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1237 |
* The instance we are protecting. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1238 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1239 |
private final NavigableSet<E> ns; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1240 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1241 |
UnmodifiableNavigableSet(NavigableSet<E> s) {super(s); ns = s;} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1242 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1243 |
public E lower(E e) { return ns.lower(e); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1244 |
public E floor(E e) { return ns.floor(e); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1245 |
public E ceiling(E e) { return ns.ceiling(e); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1246 |
public E higher(E e) { return ns.higher(e); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1247 |
public E pollFirst() { throw new UnsupportedOperationException(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1248 |
public E pollLast() { throw new UnsupportedOperationException(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1249 |
public NavigableSet<E> descendingSet() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1250 |
{ return new UnmodifiableNavigableSet<>(ns.descendingSet()); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1251 |
public Iterator<E> descendingIterator() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1252 |
{ return descendingSet().iterator(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1253 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1254 |
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1255 |
return new UnmodifiableNavigableSet<>( |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1256 |
ns.subSet(fromElement, fromInclusive, toElement, toInclusive)); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1257 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1258 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1259 |
public NavigableSet<E> headSet(E toElement, boolean inclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1260 |
return new UnmodifiableNavigableSet<>( |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1261 |
ns.headSet(toElement, inclusive)); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1262 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1263 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1264 |
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1265 |
return new UnmodifiableNavigableSet<>( |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1266 |
ns.tailSet(fromElement, inclusive)); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1267 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1268 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1269 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1270 |
/** |
2 | 1271 |
* Returns an unmodifiable view of the specified list. This method allows |
1272 |
* modules to provide users with "read-only" access to internal |
|
1273 |
* lists. Query operations on the returned list "read through" to the |
|
1274 |
* specified list, and attempts to modify the returned list, whether |
|
1275 |
* direct or via its iterator, result in an |
|
1276 |
* <tt>UnsupportedOperationException</tt>.<p> |
|
1277 |
* |
|
1278 |
* The returned list will be serializable if the specified list |
|
1279 |
* is serializable. Similarly, the returned list will implement |
|
1280 |
* {@link RandomAccess} if the specified list does. |
|
1281 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1282 |
* @param <T> the class of the objects in the list |
2 | 1283 |
* @param list the list for which an unmodifiable view is to be returned. |
1284 |
* @return an unmodifiable view of the specified list. |
|
1285 |
*/ |
|
1286 |
public static <T> List<T> unmodifiableList(List<? extends T> list) { |
|
1287 |
return (list instanceof RandomAccess ? |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1288 |
new UnmodifiableRandomAccessList<>(list) : |
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1289 |
new UnmodifiableList<>(list)); |
2 | 1290 |
} |
1291 |
||
1292 |
/** |
|
1293 |
* @serial include |
|
1294 |
*/ |
|
1295 |
static class UnmodifiableList<E> extends UnmodifiableCollection<E> |
|
1296 |
implements List<E> { |
|
1297 |
private static final long serialVersionUID = -283967356065247728L; |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1298 |
|
2 | 1299 |
final List<? extends E> list; |
1300 |
||
1301 |
UnmodifiableList(List<? extends E> list) { |
|
1302 |
super(list); |
|
1303 |
this.list = list; |
|
1304 |
} |
|
1305 |
||
1306 |
public boolean equals(Object o) {return o == this || list.equals(o);} |
|
1307 |
public int hashCode() {return list.hashCode();} |
|
1308 |
||
1309 |
public E get(int index) {return list.get(index);} |
|
1310 |
public E set(int index, E element) { |
|
1311 |
throw new UnsupportedOperationException(); |
|
1312 |
} |
|
1313 |
public void add(int index, E element) { |
|
1314 |
throw new UnsupportedOperationException(); |
|
1315 |
} |
|
1316 |
public E remove(int index) { |
|
1317 |
throw new UnsupportedOperationException(); |
|
1318 |
} |
|
1319 |
public int indexOf(Object o) {return list.indexOf(o);} |
|
1320 |
public int lastIndexOf(Object o) {return list.lastIndexOf(o);} |
|
1321 |
public boolean addAll(int index, Collection<? extends E> c) { |
|
1322 |
throw new UnsupportedOperationException(); |
|
1323 |
} |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1324 |
|
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1325 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1326 |
public void replaceAll(UnaryOperator<E> operator) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1327 |
throw new UnsupportedOperationException(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1328 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1329 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1330 |
public void sort(Comparator<? super E> c) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1331 |
throw new UnsupportedOperationException(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1332 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
1333 |
|
2 | 1334 |
public ListIterator<E> listIterator() {return listIterator(0);} |
1335 |
||
1336 |
public ListIterator<E> listIterator(final int index) { |
|
1337 |
return new ListIterator<E>() { |
|
1338 |
private final ListIterator<? extends E> i |
|
1339 |
= list.listIterator(index); |
|
1340 |
||
1341 |
public boolean hasNext() {return i.hasNext();} |
|
1342 |
public E next() {return i.next();} |
|
1343 |
public boolean hasPrevious() {return i.hasPrevious();} |
|
1344 |
public E previous() {return i.previous();} |
|
1345 |
public int nextIndex() {return i.nextIndex();} |
|
1346 |
public int previousIndex() {return i.previousIndex();} |
|
1347 |
||
1348 |
public void remove() { |
|
1349 |
throw new UnsupportedOperationException(); |
|
1350 |
} |
|
1351 |
public void set(E e) { |
|
1352 |
throw new UnsupportedOperationException(); |
|
1353 |
} |
|
1354 |
public void add(E e) { |
|
1355 |
throw new UnsupportedOperationException(); |
|
1356 |
} |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1357 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1358 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1359 |
public void forEachRemaining(Consumer<? super E> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1360 |
i.forEachRemaining(action); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1361 |
} |
2 | 1362 |
}; |
1363 |
} |
|
1364 |
||
1365 |
public List<E> subList(int fromIndex, int toIndex) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1366 |
return new UnmodifiableList<>(list.subList(fromIndex, toIndex)); |
2 | 1367 |
} |
1368 |
||
1369 |
/** |
|
1370 |
* UnmodifiableRandomAccessList instances are serialized as |
|
1371 |
* UnmodifiableList instances to allow them to be deserialized |
|
1372 |
* in pre-1.4 JREs (which do not have UnmodifiableRandomAccessList). |
|
1373 |
* This method inverts the transformation. As a beneficial |
|
1374 |
* side-effect, it also grafts the RandomAccess marker onto |
|
1375 |
* UnmodifiableList instances that were serialized in pre-1.4 JREs. |
|
1376 |
* |
|
1377 |
* Note: Unfortunately, UnmodifiableRandomAccessList instances |
|
1378 |
* serialized in 1.4.1 and deserialized in 1.4 will become |
|
1379 |
* UnmodifiableList instances, as this method was missing in 1.4. |
|
1380 |
*/ |
|
1381 |
private Object readResolve() { |
|
1382 |
return (list instanceof RandomAccess |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1383 |
? new UnmodifiableRandomAccessList<>(list) |
2 | 1384 |
: this); |
1385 |
} |
|
1386 |
} |
|
1387 |
||
1388 |
/** |
|
1389 |
* @serial include |
|
1390 |
*/ |
|
1391 |
static class UnmodifiableRandomAccessList<E> extends UnmodifiableList<E> |
|
1392 |
implements RandomAccess |
|
1393 |
{ |
|
1394 |
UnmodifiableRandomAccessList(List<? extends E> list) { |
|
1395 |
super(list); |
|
1396 |
} |
|
1397 |
||
1398 |
public List<E> subList(int fromIndex, int toIndex) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1399 |
return new UnmodifiableRandomAccessList<>( |
2 | 1400 |
list.subList(fromIndex, toIndex)); |
1401 |
} |
|
1402 |
||
1403 |
private static final long serialVersionUID = -2542308836966382001L; |
|
1404 |
||
1405 |
/** |
|
1406 |
* Allows instances to be deserialized in pre-1.4 JREs (which do |
|
1407 |
* not have UnmodifiableRandomAccessList). UnmodifiableList has |
|
1408 |
* a readResolve method that inverts this transformation upon |
|
1409 |
* deserialization. |
|
1410 |
*/ |
|
1411 |
private Object writeReplace() { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1412 |
return new UnmodifiableList<>(list); |
2 | 1413 |
} |
1414 |
} |
|
1415 |
||
1416 |
/** |
|
1417 |
* Returns an unmodifiable view of the specified map. This method |
|
1418 |
* allows modules to provide users with "read-only" access to internal |
|
1419 |
* maps. Query operations on the returned map "read through" |
|
1420 |
* to the specified map, and attempts to modify the returned |
|
1421 |
* map, whether direct or via its collection views, result in an |
|
1422 |
* <tt>UnsupportedOperationException</tt>.<p> |
|
1423 |
* |
|
1424 |
* The returned map will be serializable if the specified map |
|
1425 |
* is serializable. |
|
1426 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1427 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1428 |
* @param <V> the class of the map values |
2 | 1429 |
* @param m the map for which an unmodifiable view is to be returned. |
1430 |
* @return an unmodifiable view of the specified map. |
|
1431 |
*/ |
|
1432 |
public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1433 |
return new UnmodifiableMap<>(m); |
2 | 1434 |
} |
1435 |
||
1436 |
/** |
|
1437 |
* @serial include |
|
1438 |
*/ |
|
1439 |
private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable { |
|
1440 |
private static final long serialVersionUID = -1034234728574286014L; |
|
1441 |
||
1442 |
private final Map<? extends K, ? extends V> m; |
|
1443 |
||
1444 |
UnmodifiableMap(Map<? extends K, ? extends V> m) { |
|
1445 |
if (m==null) |
|
1446 |
throw new NullPointerException(); |
|
1447 |
this.m = m; |
|
1448 |
} |
|
1449 |
||
1450 |
public int size() {return m.size();} |
|
1451 |
public boolean isEmpty() {return m.isEmpty();} |
|
1452 |
public boolean containsKey(Object key) {return m.containsKey(key);} |
|
1453 |
public boolean containsValue(Object val) {return m.containsValue(val);} |
|
1454 |
public V get(Object key) {return m.get(key);} |
|
1455 |
||
1456 |
public V put(K key, V value) { |
|
1457 |
throw new UnsupportedOperationException(); |
|
1458 |
} |
|
1459 |
public V remove(Object key) { |
|
1460 |
throw new UnsupportedOperationException(); |
|
1461 |
} |
|
1462 |
public void putAll(Map<? extends K, ? extends V> m) { |
|
1463 |
throw new UnsupportedOperationException(); |
|
1464 |
} |
|
1465 |
public void clear() { |
|
1466 |
throw new UnsupportedOperationException(); |
|
1467 |
} |
|
1468 |
||
1469 |
private transient Set<K> keySet = null; |
|
1470 |
private transient Set<Map.Entry<K,V>> entrySet = null; |
|
1471 |
private transient Collection<V> values = null; |
|
1472 |
||
1473 |
public Set<K> keySet() { |
|
1474 |
if (keySet==null) |
|
1475 |
keySet = unmodifiableSet(m.keySet()); |
|
1476 |
return keySet; |
|
1477 |
} |
|
1478 |
||
1479 |
public Set<Map.Entry<K,V>> entrySet() { |
|
1480 |
if (entrySet==null) |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1481 |
entrySet = new UnmodifiableEntrySet<>(m.entrySet()); |
2 | 1482 |
return entrySet; |
1483 |
} |
|
1484 |
||
1485 |
public Collection<V> values() { |
|
1486 |
if (values==null) |
|
1487 |
values = unmodifiableCollection(m.values()); |
|
1488 |
return values; |
|
1489 |
} |
|
1490 |
||
1491 |
public boolean equals(Object o) {return o == this || m.equals(o);} |
|
1492 |
public int hashCode() {return m.hashCode();} |
|
1493 |
public String toString() {return m.toString();} |
|
1494 |
||
16867 | 1495 |
// Override default methods in Map |
1496 |
@Override |
|
1497 |
@SuppressWarnings("unchecked") |
|
1498 |
public V getOrDefault(Object k, V defaultValue) { |
|
1499 |
// Safe cast as we don't change the value |
|
1500 |
return ((Map<K, V>)m).getOrDefault(k, defaultValue); |
|
1501 |
} |
|
1502 |
||
1503 |
@Override |
|
1504 |
public void forEach(BiConsumer<? super K, ? super V> action) { |
|
1505 |
m.forEach(action); |
|
1506 |
} |
|
1507 |
||
1508 |
@Override |
|
1509 |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
|
1510 |
throw new UnsupportedOperationException(); |
|
1511 |
} |
|
1512 |
||
1513 |
@Override |
|
1514 |
public V putIfAbsent(K key, V value) { |
|
1515 |
throw new UnsupportedOperationException(); |
|
1516 |
} |
|
1517 |
||
1518 |
@Override |
|
1519 |
public boolean remove(Object key, Object value) { |
|
1520 |
throw new UnsupportedOperationException(); |
|
1521 |
} |
|
1522 |
||
1523 |
@Override |
|
1524 |
public boolean replace(K key, V oldValue, V newValue) { |
|
1525 |
throw new UnsupportedOperationException(); |
|
1526 |
} |
|
1527 |
||
1528 |
@Override |
|
1529 |
public V replace(K key, V value) { |
|
1530 |
throw new UnsupportedOperationException(); |
|
1531 |
} |
|
1532 |
||
1533 |
@Override |
|
1534 |
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { |
|
1535 |
throw new UnsupportedOperationException(); |
|
1536 |
} |
|
1537 |
||
1538 |
@Override |
|
1539 |
public V computeIfPresent(K key, |
|
1540 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
1541 |
throw new UnsupportedOperationException(); |
|
1542 |
} |
|
1543 |
||
1544 |
@Override |
|
1545 |
public V compute(K key, |
|
1546 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
1547 |
throw new UnsupportedOperationException(); |
|
1548 |
} |
|
1549 |
||
1550 |
@Override |
|
1551 |
public V merge(K key, V value, |
|
1552 |
BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
|
1553 |
throw new UnsupportedOperationException(); |
|
1554 |
} |
|
1555 |
||
2 | 1556 |
/** |
1557 |
* We need this class in addition to UnmodifiableSet as |
|
1558 |
* Map.Entries themselves permit modification of the backing Map |
|
1559 |
* via their setValue operation. This class is subtle: there are |
|
1560 |
* many possible attacks that must be thwarted. |
|
1561 |
* |
|
1562 |
* @serial include |
|
1563 |
*/ |
|
1564 |
static class UnmodifiableEntrySet<K,V> |
|
1565 |
extends UnmodifiableSet<Map.Entry<K,V>> { |
|
1566 |
private static final long serialVersionUID = 7854390611657943733L; |
|
1567 |
||
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
1568 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
2 | 1569 |
UnmodifiableEntrySet(Set<? extends Map.Entry<? extends K, ? extends V>> s) { |
12448 | 1570 |
// Need to cast to raw in order to work around a limitation in the type system |
2 | 1571 |
super((Set)s); |
1572 |
} |
|
18794
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1573 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1574 |
static <K, V> Consumer<Map.Entry<K, V>> entryConsumer(Consumer<? super Entry<K, V>> action) { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1575 |
return e -> action.accept(new UnmodifiableEntry<>(e)); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1576 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1577 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1578 |
public void forEach(Consumer<? super Entry<K, V>> action) { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1579 |
Objects.requireNonNull(action); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1580 |
c.forEach(entryConsumer(action)); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1581 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1582 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1583 |
static final class UnmodifiableEntrySetSpliterator<K, V> |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1584 |
implements Spliterator<Entry<K,V>> { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1585 |
final Spliterator<Map.Entry<K, V>> s; |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1586 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1587 |
UnmodifiableEntrySetSpliterator(Spliterator<Entry<K, V>> s) { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1588 |
this.s = s; |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1589 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1590 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1591 |
@Override |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1592 |
public boolean tryAdvance(Consumer<? super Entry<K, V>> action) { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1593 |
Objects.requireNonNull(action); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1594 |
return s.tryAdvance(entryConsumer(action)); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1595 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1596 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1597 |
@Override |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1598 |
public void forEachRemaining(Consumer<? super Entry<K, V>> action) { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1599 |
Objects.requireNonNull(action); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1600 |
s.forEachRemaining(entryConsumer(action)); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1601 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1602 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1603 |
@Override |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1604 |
public Spliterator<Entry<K, V>> trySplit() { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1605 |
Spliterator<Entry<K, V>> split = s.trySplit(); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1606 |
return split == null |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1607 |
? null |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1608 |
: new UnmodifiableEntrySetSpliterator<>(split); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1609 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1610 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1611 |
@Override |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1612 |
public long estimateSize() { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1613 |
return s.estimateSize(); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1614 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1615 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1616 |
@Override |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1617 |
public long getExactSizeIfKnown() { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1618 |
return s.getExactSizeIfKnown(); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1619 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1620 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1621 |
@Override |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1622 |
public int characteristics() { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1623 |
return s.characteristics(); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1624 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1625 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1626 |
@Override |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1627 |
public boolean hasCharacteristics(int characteristics) { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1628 |
return s.hasCharacteristics(characteristics); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1629 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1630 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1631 |
@Override |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1632 |
public Comparator<? super Entry<K, V>> getComparator() { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1633 |
return s.getComparator(); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1634 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1635 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1636 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1637 |
@SuppressWarnings("unchecked") |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1638 |
public Spliterator<Entry<K,V>> spliterator() { |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1639 |
return new UnmodifiableEntrySetSpliterator<>( |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1640 |
(Spliterator<Map.Entry<K, V>>) c.spliterator()); |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1641 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1642 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1643 |
@Override |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1644 |
public Stream<Entry<K,V>> stream() { |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18818
diff
changeset
|
1645 |
return StreamSupport.stream(spliterator(), false); |
18794
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1646 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1647 |
|
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1648 |
@Override |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1649 |
public Stream<Entry<K,V>> parallelStream() { |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18818
diff
changeset
|
1650 |
return StreamSupport.stream(spliterator(), true); |
18794
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1651 |
} |
17d9c2ec5e47
8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents:
18571
diff
changeset
|
1652 |
|
2 | 1653 |
public Iterator<Map.Entry<K,V>> iterator() { |
1654 |
return new Iterator<Map.Entry<K,V>>() { |
|
1655 |
private final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = c.iterator(); |
|
1656 |
||
1657 |
public boolean hasNext() { |
|
1658 |
return i.hasNext(); |
|
1659 |
} |
|
1660 |
public Map.Entry<K,V> next() { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1661 |
return new UnmodifiableEntry<>(i.next()); |
2 | 1662 |
} |
1663 |
public void remove() { |
|
1664 |
throw new UnsupportedOperationException(); |
|
1665 |
} |
|
1666 |
}; |
|
1667 |
} |
|
1668 |
||
12448 | 1669 |
@SuppressWarnings("unchecked") |
2 | 1670 |
public Object[] toArray() { |
1671 |
Object[] a = c.toArray(); |
|
1672 |
for (int i=0; i<a.length; i++) |
|
12448 | 1673 |
a[i] = new UnmodifiableEntry<>((Map.Entry<? extends K, ? extends V>)a[i]); |
2 | 1674 |
return a; |
1675 |
} |
|
1676 |
||
12448 | 1677 |
@SuppressWarnings("unchecked") |
2 | 1678 |
public <T> T[] toArray(T[] a) { |
1679 |
// We don't pass a to c.toArray, to avoid window of |
|
1680 |
// vulnerability wherein an unscrupulous multithreaded client |
|
1681 |
// could get his hands on raw (unwrapped) Entries from c. |
|
1682 |
Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0)); |
|
1683 |
||
1684 |
for (int i=0; i<arr.length; i++) |
|
12448 | 1685 |
arr[i] = new UnmodifiableEntry<>((Map.Entry<? extends K, ? extends V>)arr[i]); |
2 | 1686 |
|
1687 |
if (arr.length > a.length) |
|
1688 |
return (T[])arr; |
|
1689 |
||
1690 |
System.arraycopy(arr, 0, a, 0, arr.length); |
|
1691 |
if (a.length > arr.length) |
|
1692 |
a[arr.length] = null; |
|
1693 |
return a; |
|
1694 |
} |
|
1695 |
||
1696 |
/** |
|
1697 |
* This method is overridden to protect the backing set against |
|
1698 |
* an object with a nefarious equals function that senses |
|
1699 |
* that the equality-candidate is Map.Entry and calls its |
|
1700 |
* setValue method. |
|
1701 |
*/ |
|
1702 |
public boolean contains(Object o) { |
|
1703 |
if (!(o instanceof Map.Entry)) |
|
1704 |
return false; |
|
1705 |
return c.contains( |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1706 |
new UnmodifiableEntry<>((Map.Entry<?,?>) o)); |
2 | 1707 |
} |
1708 |
||
1709 |
/** |
|
1710 |
* The next two methods are overridden to protect against |
|
1711 |
* an unscrupulous List whose contains(Object o) method senses |
|
1712 |
* when o is a Map.Entry, and calls o.setValue. |
|
1713 |
*/ |
|
1714 |
public boolean containsAll(Collection<?> coll) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7816
diff
changeset
|
1715 |
for (Object e : coll) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7816
diff
changeset
|
1716 |
if (!contains(e)) // Invokes safe contains() above |
2 | 1717 |
return false; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7816
diff
changeset
|
1718 |
} |
2 | 1719 |
return true; |
1720 |
} |
|
1721 |
public boolean equals(Object o) { |
|
1722 |
if (o == this) |
|
1723 |
return true; |
|
1724 |
||
1725 |
if (!(o instanceof Set)) |
|
1726 |
return false; |
|
12448 | 1727 |
Set<?> s = (Set<?>) o; |
2 | 1728 |
if (s.size() != c.size()) |
1729 |
return false; |
|
1730 |
return containsAll(s); // Invokes safe containsAll() above |
|
1731 |
} |
|
1732 |
||
1733 |
/** |
|
1734 |
* This "wrapper class" serves two purposes: it prevents |
|
1735 |
* the client from modifying the backing Map, by short-circuiting |
|
1736 |
* the setValue method, and it protects the backing Map against |
|
1737 |
* an ill-behaved Map.Entry that attempts to modify another |
|
1738 |
* Map Entry when asked to perform an equality check. |
|
1739 |
*/ |
|
1740 |
private static class UnmodifiableEntry<K,V> implements Map.Entry<K,V> { |
|
1741 |
private Map.Entry<? extends K, ? extends V> e; |
|
1742 |
||
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1743 |
UnmodifiableEntry(Map.Entry<? extends K, ? extends V> e) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1744 |
{this.e = Objects.requireNonNull(e);} |
2 | 1745 |
|
7518 | 1746 |
public K getKey() {return e.getKey();} |
1747 |
public V getValue() {return e.getValue();} |
|
2 | 1748 |
public V setValue(V value) { |
1749 |
throw new UnsupportedOperationException(); |
|
1750 |
} |
|
7518 | 1751 |
public int hashCode() {return e.hashCode();} |
2 | 1752 |
public boolean equals(Object o) { |
11987
a6dde4427e9d
7144488: Infinite recursion for some equals tests in Collections
coffeys
parents:
10896
diff
changeset
|
1753 |
if (this == o) |
a6dde4427e9d
7144488: Infinite recursion for some equals tests in Collections
coffeys
parents:
10896
diff
changeset
|
1754 |
return true; |
2 | 1755 |
if (!(o instanceof Map.Entry)) |
1756 |
return false; |
|
12448 | 1757 |
Map.Entry<?,?> t = (Map.Entry<?,?>)o; |
2 | 1758 |
return eq(e.getKey(), t.getKey()) && |
1759 |
eq(e.getValue(), t.getValue()); |
|
1760 |
} |
|
7518 | 1761 |
public String toString() {return e.toString();} |
2 | 1762 |
} |
1763 |
} |
|
1764 |
} |
|
1765 |
||
1766 |
/** |
|
1767 |
* Returns an unmodifiable view of the specified sorted map. This method |
|
1768 |
* allows modules to provide users with "read-only" access to internal |
|
1769 |
* sorted maps. Query operations on the returned sorted map "read through" |
|
1770 |
* to the specified sorted map. Attempts to modify the returned |
|
1771 |
* sorted map, whether direct, via its collection views, or via its |
|
1772 |
* <tt>subMap</tt>, <tt>headMap</tt>, or <tt>tailMap</tt> views, result in |
|
1773 |
* an <tt>UnsupportedOperationException</tt>.<p> |
|
1774 |
* |
|
1775 |
* The returned sorted map will be serializable if the specified sorted map |
|
1776 |
* is serializable. |
|
1777 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1778 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1779 |
* @param <V> the class of the map values |
2 | 1780 |
* @param m the sorted map for which an unmodifiable view is to be |
1781 |
* returned. |
|
1782 |
* @return an unmodifiable view of the specified sorted map. |
|
1783 |
*/ |
|
1784 |
public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1785 |
return new UnmodifiableSortedMap<>(m); |
2 | 1786 |
} |
1787 |
||
1788 |
/** |
|
1789 |
* @serial include |
|
1790 |
*/ |
|
1791 |
static class UnmodifiableSortedMap<K,V> |
|
1792 |
extends UnmodifiableMap<K,V> |
|
1793 |
implements SortedMap<K,V>, Serializable { |
|
1794 |
private static final long serialVersionUID = -8806743815996713206L; |
|
1795 |
||
1796 |
private final SortedMap<K, ? extends V> sm; |
|
1797 |
||
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1798 |
UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m; } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1799 |
public Comparator<? super K> comparator() { return sm.comparator(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1800 |
public SortedMap<K,V> subMap(K fromKey, K toKey) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1801 |
{ return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey)); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1802 |
public SortedMap<K,V> headMap(K toKey) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1803 |
{ return new UnmodifiableSortedMap<>(sm.headMap(toKey)); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1804 |
public SortedMap<K,V> tailMap(K fromKey) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1805 |
{ return new UnmodifiableSortedMap<>(sm.tailMap(fromKey)); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1806 |
public K firstKey() { return sm.firstKey(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1807 |
public K lastKey() { return sm.lastKey(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1808 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1809 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1810 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1811 |
* Returns an unmodifiable view of the specified navigable map. This method |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1812 |
* allows modules to provide users with "read-only" access to internal |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1813 |
* navigable maps. Query operations on the returned navigable map "read |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1814 |
* through" to the specified navigable map. Attempts to modify the returned |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1815 |
* navigable map, whether direct, via its collection views, or via its |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1816 |
* {@code subMap}, {@code headMap}, or {@code tailMap} views, result in |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1817 |
* an {@code UnsupportedOperationException}.<p> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1818 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1819 |
* The returned navigable map will be serializable if the specified |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1820 |
* navigable map is serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1821 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1822 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1823 |
* @param <V> the class of the map values |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1824 |
* @param m the navigable map for which an unmodifiable view is to be |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1825 |
* returned |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1826 |
* @return an unmodifiable view of the specified navigable map |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1827 |
* @since 1.8 |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1828 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1829 |
public static <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K, ? extends V> m) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1830 |
return new UnmodifiableNavigableMap<>(m); |
2 | 1831 |
} |
1832 |
||
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1833 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1834 |
* @serial include |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1835 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1836 |
static class UnmodifiableNavigableMap<K,V> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1837 |
extends UnmodifiableSortedMap<K,V> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1838 |
implements NavigableMap<K,V>, Serializable { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1839 |
private static final long serialVersionUID = -4858195264774772197L; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1840 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1841 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1842 |
* A class for the {@link EMPTY_NAVIGABLE_MAP} which needs readResolve |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1843 |
* to preserve singleton property. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1844 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1845 |
* @param <K> type of keys, if there were any, and of bounds |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1846 |
* @param <V> type of values, if there were any |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1847 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1848 |
private static class EmptyNavigableMap<K,V> extends UnmodifiableNavigableMap<K,V> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1849 |
implements Serializable { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1850 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1851 |
private static final long serialVersionUID = -2239321462712562324L; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1852 |
|
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19855
diff
changeset
|
1853 |
EmptyNavigableMap() { super(new TreeMap<>()); } |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1854 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1855 |
@Override |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1856 |
public NavigableSet<K> navigableKeySet() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1857 |
{ return emptyNavigableSet(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1858 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1859 |
private Object readResolve() { return EMPTY_NAVIGABLE_MAP; } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1860 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1861 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1862 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1863 |
* Singleton for {@link emptyNavigableMap()} which is also immutable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1864 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1865 |
private static final EmptyNavigableMap<?,?> EMPTY_NAVIGABLE_MAP = |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1866 |
new EmptyNavigableMap<>(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1867 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1868 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1869 |
* The instance we wrap and protect. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1870 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1871 |
private final NavigableMap<K, ? extends V> nm; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1872 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1873 |
UnmodifiableNavigableMap(NavigableMap<K, ? extends V> m) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1874 |
{super(m); nm = m;} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1875 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1876 |
public K lowerKey(K key) { return nm.lowerKey(key); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1877 |
public K floorKey(K key) { return nm.floorKey(key); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1878 |
public K ceilingKey(K key) { return nm.ceilingKey(key); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1879 |
public K higherKey(K key) { return nm.higherKey(key); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1880 |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1881 |
@SuppressWarnings("unchecked") |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1882 |
public Entry<K, V> lowerEntry(K key) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1883 |
Entry<K,V> lower = (Entry<K, V>) nm.lowerEntry(key); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1884 |
return (null != lower) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1885 |
? new UnmodifiableEntrySet.UnmodifiableEntry<>(lower) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1886 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1887 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1888 |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1889 |
@SuppressWarnings("unchecked") |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1890 |
public Entry<K, V> floorEntry(K key) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1891 |
Entry<K,V> floor = (Entry<K, V>) nm.floorEntry(key); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1892 |
return (null != floor) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1893 |
? new UnmodifiableEntrySet.UnmodifiableEntry<>(floor) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1894 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1895 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1896 |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1897 |
@SuppressWarnings("unchecked") |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1898 |
public Entry<K, V> ceilingEntry(K key) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1899 |
Entry<K,V> ceiling = (Entry<K, V>) nm.ceilingEntry(key); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1900 |
return (null != ceiling) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1901 |
? new UnmodifiableEntrySet.UnmodifiableEntry<>(ceiling) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1902 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1903 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1904 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1905 |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1906 |
@SuppressWarnings("unchecked") |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1907 |
public Entry<K, V> higherEntry(K key) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1908 |
Entry<K,V> higher = (Entry<K, V>) nm.higherEntry(key); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1909 |
return (null != higher) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1910 |
? new UnmodifiableEntrySet.UnmodifiableEntry<>(higher) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1911 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1912 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1913 |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1914 |
@SuppressWarnings("unchecked") |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1915 |
public Entry<K, V> firstEntry() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1916 |
Entry<K,V> first = (Entry<K, V>) nm.firstEntry(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1917 |
return (null != first) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1918 |
? new UnmodifiableEntrySet.UnmodifiableEntry<>(first) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1919 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1920 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1921 |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1922 |
@SuppressWarnings("unchecked") |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1923 |
public Entry<K, V> lastEntry() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1924 |
Entry<K,V> last = (Entry<K, V>) nm.lastEntry(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1925 |
return (null != last) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
1926 |
? new UnmodifiableEntrySet.UnmodifiableEntry<>(last) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1927 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1928 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1929 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1930 |
public Entry<K, V> pollFirstEntry() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1931 |
{ throw new UnsupportedOperationException(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1932 |
public Entry<K, V> pollLastEntry() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1933 |
{ throw new UnsupportedOperationException(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1934 |
public NavigableMap<K, V> descendingMap() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1935 |
{ return unmodifiableNavigableMap(nm.descendingMap()); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1936 |
public NavigableSet<K> navigableKeySet() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1937 |
{ return unmodifiableNavigableSet(nm.navigableKeySet()); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1938 |
public NavigableSet<K> descendingKeySet() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1939 |
{ return unmodifiableNavigableSet(nm.descendingKeySet()); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1940 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1941 |
public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1942 |
return unmodifiableNavigableMap( |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1943 |
nm.subMap(fromKey, fromInclusive, toKey, toInclusive)); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1944 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1945 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1946 |
public NavigableMap<K, V> headMap(K toKey, boolean inclusive) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1947 |
{ return unmodifiableNavigableMap(nm.headMap(toKey, inclusive)); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1948 |
public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1949 |
{ return unmodifiableNavigableMap(nm.tailMap(fromKey, inclusive)); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
1950 |
} |
2 | 1951 |
|
1952 |
// Synch Wrappers |
|
1953 |
||
1954 |
/** |
|
1955 |
* Returns a synchronized (thread-safe) collection backed by the specified |
|
1956 |
* collection. In order to guarantee serial access, it is critical that |
|
1957 |
* <strong>all</strong> access to the backing collection is accomplished |
|
1958 |
* through the returned collection.<p> |
|
1959 |
* |
|
1960 |
* It is imperative that the user manually synchronize on the returned |
|
19603
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1961 |
* collection when traversing it via {@link Iterator}, {@link Spliterator} |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
1962 |
* or {@link Stream}: |
2 | 1963 |
* <pre> |
1964 |
* Collection c = Collections.synchronizedCollection(myCollection); |
|
1965 |
* ... |
|
7518 | 1966 |
* synchronized (c) { |
2 | 1967 |
* Iterator i = c.iterator(); // Must be in the synchronized block |
1968 |
* while (i.hasNext()) |
|
1969 |
* foo(i.next()); |
|
1970 |
* } |
|
1971 |
* </pre> |
|
1972 |
* Failure to follow this advice may result in non-deterministic behavior. |
|
1973 |
* |
|
16867 | 1974 |
* <p>The returned collection does <i>not</i> pass the {@code hashCode} |
1975 |
* and {@code equals} operations through to the backing collection, but |
|
1976 |
* relies on {@code Object}'s equals and hashCode methods. This is |
|
2 | 1977 |
* necessary to preserve the contracts of these operations in the case |
1978 |
* that the backing collection is a set or a list.<p> |
|
1979 |
* |
|
1980 |
* The returned collection will be serializable if the specified collection |
|
1981 |
* is serializable. |
|
1982 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
1983 |
* @param <T> the class of the objects in the collection |
2 | 1984 |
* @param c the collection to be "wrapped" in a synchronized collection. |
1985 |
* @return a synchronized view of the specified collection. |
|
1986 |
*/ |
|
1987 |
public static <T> Collection<T> synchronizedCollection(Collection<T> c) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1988 |
return new SynchronizedCollection<>(c); |
2 | 1989 |
} |
1990 |
||
1991 |
static <T> Collection<T> synchronizedCollection(Collection<T> c, Object mutex) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
1992 |
return new SynchronizedCollection<>(c, mutex); |
2 | 1993 |
} |
1994 |
||
1995 |
/** |
|
1996 |
* @serial include |
|
1997 |
*/ |
|
1998 |
static class SynchronizedCollection<E> implements Collection<E>, Serializable { |
|
1999 |
private static final long serialVersionUID = 3053995032091335093L; |
|
2000 |
||
2001 |
final Collection<E> c; // Backing Collection |
|
2002 |
final Object mutex; // Object on which to synchronize |
|
2003 |
||
2004 |
SynchronizedCollection(Collection<E> c) { |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2005 |
this.c = Objects.requireNonNull(c); |
2 | 2006 |
mutex = this; |
2007 |
} |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2008 |
|
2 | 2009 |
SynchronizedCollection(Collection<E> c, Object mutex) { |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2010 |
this.c = Objects.requireNonNull(c); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2011 |
this.mutex = Objects.requireNonNull(mutex); |
2 | 2012 |
} |
2013 |
||
2014 |
public int size() { |
|
7518 | 2015 |
synchronized (mutex) {return c.size();} |
2 | 2016 |
} |
2017 |
public boolean isEmpty() { |
|
7518 | 2018 |
synchronized (mutex) {return c.isEmpty();} |
2 | 2019 |
} |
2020 |
public boolean contains(Object o) { |
|
7518 | 2021 |
synchronized (mutex) {return c.contains(o);} |
2 | 2022 |
} |
2023 |
public Object[] toArray() { |
|
7518 | 2024 |
synchronized (mutex) {return c.toArray();} |
2 | 2025 |
} |
2026 |
public <T> T[] toArray(T[] a) { |
|
7518 | 2027 |
synchronized (mutex) {return c.toArray(a);} |
2 | 2028 |
} |
2029 |
||
2030 |
public Iterator<E> iterator() { |
|
2031 |
return c.iterator(); // Must be manually synched by user! |
|
2032 |
} |
|
2033 |
||
2034 |
public boolean add(E e) { |
|
7518 | 2035 |
synchronized (mutex) {return c.add(e);} |
2 | 2036 |
} |
2037 |
public boolean remove(Object o) { |
|
7518 | 2038 |
synchronized (mutex) {return c.remove(o);} |
2 | 2039 |
} |
2040 |
||
2041 |
public boolean containsAll(Collection<?> coll) { |
|
7518 | 2042 |
synchronized (mutex) {return c.containsAll(coll);} |
2 | 2043 |
} |
2044 |
public boolean addAll(Collection<? extends E> coll) { |
|
7518 | 2045 |
synchronized (mutex) {return c.addAll(coll);} |
2 | 2046 |
} |
2047 |
public boolean removeAll(Collection<?> coll) { |
|
7518 | 2048 |
synchronized (mutex) {return c.removeAll(coll);} |
2 | 2049 |
} |
2050 |
public boolean retainAll(Collection<?> coll) { |
|
7518 | 2051 |
synchronized (mutex) {return c.retainAll(coll);} |
2 | 2052 |
} |
2053 |
public void clear() { |
|
7518 | 2054 |
synchronized (mutex) {c.clear();} |
2 | 2055 |
} |
2056 |
public String toString() { |
|
7518 | 2057 |
synchronized (mutex) {return c.toString();} |
2 | 2058 |
} |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2059 |
// Override default methods in Collection |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2060 |
@Override |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2061 |
public void forEach(Consumer<? super E> consumer) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2062 |
synchronized (mutex) {c.forEach(consumer);} |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2063 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2064 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2065 |
public boolean removeIf(Predicate<? super E> filter) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2066 |
synchronized (mutex) {return c.removeIf(filter);} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2067 |
} |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2068 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2069 |
public Spliterator<E> spliterator() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2070 |
return c.spliterator(); // Must be manually synched by user! |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2071 |
} |
19603
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
2072 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
2073 |
public Stream<E> stream() { |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
2074 |
return c.stream(); // Must be manually synched by user! |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
2075 |
} |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
2076 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
2077 |
public Stream<E> parallelStream() { |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
2078 |
return c.parallelStream(); // Must be manually synched by user! |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
2079 |
} |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2080 |
private void writeObject(ObjectOutputStream s) throws IOException { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2081 |
synchronized (mutex) {s.defaultWriteObject();} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2082 |
} |
2 | 2083 |
} |
2084 |
||
2085 |
/** |
|
2086 |
* Returns a synchronized (thread-safe) set backed by the specified |
|
2087 |
* set. In order to guarantee serial access, it is critical that |
|
2088 |
* <strong>all</strong> access to the backing set is accomplished |
|
2089 |
* through the returned set.<p> |
|
2090 |
* |
|
2091 |
* It is imperative that the user manually synchronize on the returned |
|
2092 |
* set when iterating over it: |
|
2093 |
* <pre> |
|
2094 |
* Set s = Collections.synchronizedSet(new HashSet()); |
|
2095 |
* ... |
|
7518 | 2096 |
* synchronized (s) { |
2 | 2097 |
* Iterator i = s.iterator(); // Must be in the synchronized block |
2098 |
* while (i.hasNext()) |
|
2099 |
* foo(i.next()); |
|
2100 |
* } |
|
2101 |
* </pre> |
|
2102 |
* Failure to follow this advice may result in non-deterministic behavior. |
|
2103 |
* |
|
2104 |
* <p>The returned set will be serializable if the specified set is |
|
2105 |
* serializable. |
|
2106 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
2107 |
* @param <T> the class of the objects in the set |
2 | 2108 |
* @param s the set to be "wrapped" in a synchronized set. |
2109 |
* @return a synchronized view of the specified set. |
|
2110 |
*/ |
|
2111 |
public static <T> Set<T> synchronizedSet(Set<T> s) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2112 |
return new SynchronizedSet<>(s); |
2 | 2113 |
} |
2114 |
||
2115 |
static <T> Set<T> synchronizedSet(Set<T> s, Object mutex) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2116 |
return new SynchronizedSet<>(s, mutex); |
2 | 2117 |
} |
2118 |
||
2119 |
/** |
|
2120 |
* @serial include |
|
2121 |
*/ |
|
2122 |
static class SynchronizedSet<E> |
|
2123 |
extends SynchronizedCollection<E> |
|
2124 |
implements Set<E> { |
|
2125 |
private static final long serialVersionUID = 487447009682186044L; |
|
2126 |
||
2127 |
SynchronizedSet(Set<E> s) { |
|
2128 |
super(s); |
|
2129 |
} |
|
2130 |
SynchronizedSet(Set<E> s, Object mutex) { |
|
2131 |
super(s, mutex); |
|
2132 |
} |
|
2133 |
||
2134 |
public boolean equals(Object o) { |
|
11987
a6dde4427e9d
7144488: Infinite recursion for some equals tests in Collections
coffeys
parents:
10896
diff
changeset
|
2135 |
if (this == o) |
a6dde4427e9d
7144488: Infinite recursion for some equals tests in Collections
coffeys
parents:
10896
diff
changeset
|
2136 |
return true; |
7518 | 2137 |
synchronized (mutex) {return c.equals(o);} |
2 | 2138 |
} |
2139 |
public int hashCode() { |
|
7518 | 2140 |
synchronized (mutex) {return c.hashCode();} |
2 | 2141 |
} |
2142 |
} |
|
2143 |
||
2144 |
/** |
|
2145 |
* Returns a synchronized (thread-safe) sorted set backed by the specified |
|
2146 |
* sorted set. In order to guarantee serial access, it is critical that |
|
2147 |
* <strong>all</strong> access to the backing sorted set is accomplished |
|
2148 |
* through the returned sorted set (or its views).<p> |
|
2149 |
* |
|
2150 |
* It is imperative that the user manually synchronize on the returned |
|
2151 |
* sorted set when iterating over it or any of its <tt>subSet</tt>, |
|
2152 |
* <tt>headSet</tt>, or <tt>tailSet</tt> views. |
|
2153 |
* <pre> |
|
2154 |
* SortedSet s = Collections.synchronizedSortedSet(new TreeSet()); |
|
2155 |
* ... |
|
7518 | 2156 |
* synchronized (s) { |
2 | 2157 |
* Iterator i = s.iterator(); // Must be in the synchronized block |
2158 |
* while (i.hasNext()) |
|
2159 |
* foo(i.next()); |
|
2160 |
* } |
|
2161 |
* </pre> |
|
2162 |
* or: |
|
2163 |
* <pre> |
|
2164 |
* SortedSet s = Collections.synchronizedSortedSet(new TreeSet()); |
|
2165 |
* SortedSet s2 = s.headSet(foo); |
|
2166 |
* ... |
|
7518 | 2167 |
* synchronized (s) { // Note: s, not s2!!! |
2 | 2168 |
* Iterator i = s2.iterator(); // Must be in the synchronized block |
2169 |
* while (i.hasNext()) |
|
2170 |
* foo(i.next()); |
|
2171 |
* } |
|
2172 |
* </pre> |
|
2173 |
* Failure to follow this advice may result in non-deterministic behavior. |
|
2174 |
* |
|
2175 |
* <p>The returned sorted set will be serializable if the specified |
|
2176 |
* sorted set is serializable. |
|
2177 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
2178 |
* @param <T> the class of the objects in the set |
2 | 2179 |
* @param s the sorted set to be "wrapped" in a synchronized sorted set. |
2180 |
* @return a synchronized view of the specified sorted set. |
|
2181 |
*/ |
|
2182 |
public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2183 |
return new SynchronizedSortedSet<>(s); |
2 | 2184 |
} |
2185 |
||
2186 |
/** |
|
2187 |
* @serial include |
|
2188 |
*/ |
|
2189 |
static class SynchronizedSortedSet<E> |
|
2190 |
extends SynchronizedSet<E> |
|
2191 |
implements SortedSet<E> |
|
2192 |
{ |
|
2193 |
private static final long serialVersionUID = 8695801310862127406L; |
|
2194 |
||
7518 | 2195 |
private final SortedSet<E> ss; |
2 | 2196 |
|
2197 |
SynchronizedSortedSet(SortedSet<E> s) { |
|
2198 |
super(s); |
|
2199 |
ss = s; |
|
2200 |
} |
|
2201 |
SynchronizedSortedSet(SortedSet<E> s, Object mutex) { |
|
2202 |
super(s, mutex); |
|
2203 |
ss = s; |
|
2204 |
} |
|
2205 |
||
2206 |
public Comparator<? super E> comparator() { |
|
7518 | 2207 |
synchronized (mutex) {return ss.comparator();} |
2 | 2208 |
} |
2209 |
||
2210 |
public SortedSet<E> subSet(E fromElement, E toElement) { |
|
7518 | 2211 |
synchronized (mutex) { |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2212 |
return new SynchronizedSortedSet<>( |
2 | 2213 |
ss.subSet(fromElement, toElement), mutex); |
2214 |
} |
|
2215 |
} |
|
2216 |
public SortedSet<E> headSet(E toElement) { |
|
7518 | 2217 |
synchronized (mutex) { |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2218 |
return new SynchronizedSortedSet<>(ss.headSet(toElement), mutex); |
2 | 2219 |
} |
2220 |
} |
|
2221 |
public SortedSet<E> tailSet(E fromElement) { |
|
7518 | 2222 |
synchronized (mutex) { |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2223 |
return new SynchronizedSortedSet<>(ss.tailSet(fromElement),mutex); |
2 | 2224 |
} |
2225 |
} |
|
2226 |
||
2227 |
public E first() { |
|
7518 | 2228 |
synchronized (mutex) {return ss.first();} |
2 | 2229 |
} |
2230 |
public E last() { |
|
7518 | 2231 |
synchronized (mutex) {return ss.last();} |
2 | 2232 |
} |
2233 |
} |
|
2234 |
||
2235 |
/** |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2236 |
* Returns a synchronized (thread-safe) navigable set backed by the |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2237 |
* specified navigable set. In order to guarantee serial access, it is |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2238 |
* critical that <strong>all</strong> access to the backing navigable set is |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2239 |
* accomplished through the returned navigable set (or its views).<p> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2240 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2241 |
* It is imperative that the user manually synchronize on the returned |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2242 |
* navigable set when iterating over it or any of its {@code subSet}, |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2243 |
* {@code headSet}, or {@code tailSet} views. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2244 |
* <pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2245 |
* NavigableSet s = Collections.synchronizedNavigableSet(new TreeSet()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2246 |
* ... |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2247 |
* synchronized (s) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2248 |
* Iterator i = s.iterator(); // Must be in the synchronized block |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2249 |
* while (i.hasNext()) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2250 |
* foo(i.next()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2251 |
* } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2252 |
* </pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2253 |
* or: |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2254 |
* <pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2255 |
* NavigableSet s = Collections.synchronizedNavigableSet(new TreeSet()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2256 |
* NavigableSet s2 = s.headSet(foo, true); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2257 |
* ... |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2258 |
* synchronized (s) { // Note: s, not s2!!! |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2259 |
* Iterator i = s2.iterator(); // Must be in the synchronized block |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2260 |
* while (i.hasNext()) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2261 |
* foo(i.next()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2262 |
* } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2263 |
* </pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2264 |
* Failure to follow this advice may result in non-deterministic behavior. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2265 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2266 |
* <p>The returned navigable set will be serializable if the specified |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2267 |
* navigable set is serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2268 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
2269 |
* @param <T> the class of the objects in the set |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2270 |
* @param s the navigable set to be "wrapped" in a synchronized navigable |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2271 |
* set |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2272 |
* @return a synchronized view of the specified navigable set |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2273 |
* @since 1.8 |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2274 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2275 |
public static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2276 |
return new SynchronizedNavigableSet<>(s); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2277 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2278 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2279 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2280 |
* @serial include |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2281 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2282 |
static class SynchronizedNavigableSet<E> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2283 |
extends SynchronizedSortedSet<E> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2284 |
implements NavigableSet<E> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2285 |
{ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2286 |
private static final long serialVersionUID = -5505529816273629798L; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2287 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2288 |
private final NavigableSet<E> ns; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2289 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2290 |
SynchronizedNavigableSet(NavigableSet<E> s) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2291 |
super(s); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2292 |
ns = s; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2293 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2294 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2295 |
SynchronizedNavigableSet(NavigableSet<E> s, Object mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2296 |
super(s, mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2297 |
ns = s; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2298 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2299 |
public E lower(E e) { synchronized (mutex) {return ns.lower(e);} } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2300 |
public E floor(E e) { synchronized (mutex) {return ns.floor(e);} } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2301 |
public E ceiling(E e) { synchronized (mutex) {return ns.ceiling(e);} } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2302 |
public E higher(E e) { synchronized (mutex) {return ns.higher(e);} } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2303 |
public E pollFirst() { synchronized (mutex) {return ns.pollFirst();} } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2304 |
public E pollLast() { synchronized (mutex) {return ns.pollLast();} } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2305 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2306 |
public NavigableSet<E> descendingSet() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2307 |
synchronized (mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2308 |
return new SynchronizedNavigableSet<>(ns.descendingSet(), mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2309 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2310 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2311 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2312 |
public Iterator<E> descendingIterator() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2313 |
{ synchronized (mutex) { return descendingSet().iterator(); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2314 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2315 |
public NavigableSet<E> subSet(E fromElement, E toElement) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2316 |
synchronized (mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2317 |
return new SynchronizedNavigableSet<>(ns.subSet(fromElement, true, toElement, false), mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2318 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2319 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2320 |
public NavigableSet<E> headSet(E toElement) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2321 |
synchronized (mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2322 |
return new SynchronizedNavigableSet<>(ns.headSet(toElement, false), mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2323 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2324 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2325 |
public NavigableSet<E> tailSet(E fromElement) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2326 |
synchronized (mutex) { |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
2327 |
return new SynchronizedNavigableSet<>(ns.tailSet(fromElement, true), mutex); |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2328 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2329 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2330 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2331 |
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2332 |
synchronized (mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2333 |
return new SynchronizedNavigableSet<>(ns.subSet(fromElement, fromInclusive, toElement, toInclusive), mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2334 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2335 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2336 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2337 |
public NavigableSet<E> headSet(E toElement, boolean inclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2338 |
synchronized (mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2339 |
return new SynchronizedNavigableSet<>(ns.headSet(toElement, inclusive), mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2340 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2341 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2342 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2343 |
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2344 |
synchronized (mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2345 |
return new SynchronizedNavigableSet<>(ns.tailSet(fromElement, inclusive)); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2346 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2347 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2348 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2349 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2350 |
/** |
2 | 2351 |
* Returns a synchronized (thread-safe) list backed by the specified |
2352 |
* list. In order to guarantee serial access, it is critical that |
|
2353 |
* <strong>all</strong> access to the backing list is accomplished |
|
2354 |
* through the returned list.<p> |
|
2355 |
* |
|
2356 |
* It is imperative that the user manually synchronize on the returned |
|
2357 |
* list when iterating over it: |
|
2358 |
* <pre> |
|
2359 |
* List list = Collections.synchronizedList(new ArrayList()); |
|
2360 |
* ... |
|
7518 | 2361 |
* synchronized (list) { |
2 | 2362 |
* Iterator i = list.iterator(); // Must be in synchronized block |
2363 |
* while (i.hasNext()) |
|
2364 |
* foo(i.next()); |
|
2365 |
* } |
|
2366 |
* </pre> |
|
2367 |
* Failure to follow this advice may result in non-deterministic behavior. |
|
2368 |
* |
|
2369 |
* <p>The returned list will be serializable if the specified list is |
|
2370 |
* serializable. |
|
2371 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
2372 |
* @param <T> the class of the objects in the list |
2 | 2373 |
* @param list the list to be "wrapped" in a synchronized list. |
2374 |
* @return a synchronized view of the specified list. |
|
2375 |
*/ |
|
2376 |
public static <T> List<T> synchronizedList(List<T> list) { |
|
2377 |
return (list instanceof RandomAccess ? |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2378 |
new SynchronizedRandomAccessList<>(list) : |
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2379 |
new SynchronizedList<>(list)); |
2 | 2380 |
} |
2381 |
||
2382 |
static <T> List<T> synchronizedList(List<T> list, Object mutex) { |
|
2383 |
return (list instanceof RandomAccess ? |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2384 |
new SynchronizedRandomAccessList<>(list, mutex) : |
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2385 |
new SynchronizedList<>(list, mutex)); |
2 | 2386 |
} |
2387 |
||
2388 |
/** |
|
2389 |
* @serial include |
|
2390 |
*/ |
|
2391 |
static class SynchronizedList<E> |
|
2392 |
extends SynchronizedCollection<E> |
|
2393 |
implements List<E> { |
|
2394 |
private static final long serialVersionUID = -7754090372962971524L; |
|
2395 |
||
2396 |
final List<E> list; |
|
2397 |
||
2398 |
SynchronizedList(List<E> list) { |
|
2399 |
super(list); |
|
2400 |
this.list = list; |
|
2401 |
} |
|
2402 |
SynchronizedList(List<E> list, Object mutex) { |
|
2403 |
super(list, mutex); |
|
2404 |
this.list = list; |
|
2405 |
} |
|
2406 |
||
2407 |
public boolean equals(Object o) { |
|
11987
a6dde4427e9d
7144488: Infinite recursion for some equals tests in Collections
coffeys
parents:
10896
diff
changeset
|
2408 |
if (this == o) |
a6dde4427e9d
7144488: Infinite recursion for some equals tests in Collections
coffeys
parents:
10896
diff
changeset
|
2409 |
return true; |
7518 | 2410 |
synchronized (mutex) {return list.equals(o);} |
2 | 2411 |
} |
2412 |
public int hashCode() { |
|
7518 | 2413 |
synchronized (mutex) {return list.hashCode();} |
2 | 2414 |
} |
2415 |
||
2416 |
public E get(int index) { |
|
7518 | 2417 |
synchronized (mutex) {return list.get(index);} |
2 | 2418 |
} |
2419 |
public E set(int index, E element) { |
|
7518 | 2420 |
synchronized (mutex) {return list.set(index, element);} |
2 | 2421 |
} |
2422 |
public void add(int index, E element) { |
|
7518 | 2423 |
synchronized (mutex) {list.add(index, element);} |
2 | 2424 |
} |
2425 |
public E remove(int index) { |
|
7518 | 2426 |
synchronized (mutex) {return list.remove(index);} |
2 | 2427 |
} |
2428 |
||
2429 |
public int indexOf(Object o) { |
|
7518 | 2430 |
synchronized (mutex) {return list.indexOf(o);} |
2 | 2431 |
} |
2432 |
public int lastIndexOf(Object o) { |
|
7518 | 2433 |
synchronized (mutex) {return list.lastIndexOf(o);} |
2 | 2434 |
} |
2435 |
||
2436 |
public boolean addAll(int index, Collection<? extends E> c) { |
|
7518 | 2437 |
synchronized (mutex) {return list.addAll(index, c);} |
2 | 2438 |
} |
2439 |
||
2440 |
public ListIterator<E> listIterator() { |
|
2441 |
return list.listIterator(); // Must be manually synched by user |
|
2442 |
} |
|
2443 |
||
2444 |
public ListIterator<E> listIterator(int index) { |
|
2445 |
return list.listIterator(index); // Must be manually synched by user |
|
2446 |
} |
|
2447 |
||
2448 |
public List<E> subList(int fromIndex, int toIndex) { |
|
7518 | 2449 |
synchronized (mutex) { |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2450 |
return new SynchronizedList<>(list.subList(fromIndex, toIndex), |
2 | 2451 |
mutex); |
2452 |
} |
|
2453 |
} |
|
2454 |
||
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2455 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2456 |
public void replaceAll(UnaryOperator<E> operator) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2457 |
synchronized (mutex) {list.replaceAll(operator);} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2458 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2459 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2460 |
public void sort(Comparator<? super E> c) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2461 |
synchronized (mutex) {list.sort(c);} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2462 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
2463 |
|
2 | 2464 |
/** |
2465 |
* SynchronizedRandomAccessList instances are serialized as |
|
2466 |
* SynchronizedList instances to allow them to be deserialized |
|
2467 |
* in pre-1.4 JREs (which do not have SynchronizedRandomAccessList). |
|
2468 |
* This method inverts the transformation. As a beneficial |
|
2469 |
* side-effect, it also grafts the RandomAccess marker onto |
|
2470 |
* SynchronizedList instances that were serialized in pre-1.4 JREs. |
|
2471 |
* |
|
2472 |
* Note: Unfortunately, SynchronizedRandomAccessList instances |
|
2473 |
* serialized in 1.4.1 and deserialized in 1.4 will become |
|
2474 |
* SynchronizedList instances, as this method was missing in 1.4. |
|
2475 |
*/ |
|
2476 |
private Object readResolve() { |
|
2477 |
return (list instanceof RandomAccess |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2478 |
? new SynchronizedRandomAccessList<>(list) |
2 | 2479 |
: this); |
2480 |
} |
|
2481 |
} |
|
2482 |
||
2483 |
/** |
|
2484 |
* @serial include |
|
2485 |
*/ |
|
2486 |
static class SynchronizedRandomAccessList<E> |
|
2487 |
extends SynchronizedList<E> |
|
2488 |
implements RandomAccess { |
|
2489 |
||
2490 |
SynchronizedRandomAccessList(List<E> list) { |
|
2491 |
super(list); |
|
2492 |
} |
|
2493 |
||
2494 |
SynchronizedRandomAccessList(List<E> list, Object mutex) { |
|
2495 |
super(list, mutex); |
|
2496 |
} |
|
2497 |
||
2498 |
public List<E> subList(int fromIndex, int toIndex) { |
|
7518 | 2499 |
synchronized (mutex) { |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2500 |
return new SynchronizedRandomAccessList<>( |
2 | 2501 |
list.subList(fromIndex, toIndex), mutex); |
2502 |
} |
|
2503 |
} |
|
2504 |
||
2505 |
private static final long serialVersionUID = 1530674583602358482L; |
|
2506 |
||
2507 |
/** |
|
2508 |
* Allows instances to be deserialized in pre-1.4 JREs (which do |
|
2509 |
* not have SynchronizedRandomAccessList). SynchronizedList has |
|
2510 |
* a readResolve method that inverts this transformation upon |
|
2511 |
* deserialization. |
|
2512 |
*/ |
|
2513 |
private Object writeReplace() { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2514 |
return new SynchronizedList<>(list); |
2 | 2515 |
} |
2516 |
} |
|
2517 |
||
2518 |
/** |
|
2519 |
* Returns a synchronized (thread-safe) map backed by the specified |
|
2520 |
* map. In order to guarantee serial access, it is critical that |
|
2521 |
* <strong>all</strong> access to the backing map is accomplished |
|
2522 |
* through the returned map.<p> |
|
2523 |
* |
|
2524 |
* It is imperative that the user manually synchronize on the returned |
|
2525 |
* map when iterating over any of its collection views: |
|
2526 |
* <pre> |
|
2527 |
* Map m = Collections.synchronizedMap(new HashMap()); |
|
2528 |
* ... |
|
2529 |
* Set s = m.keySet(); // Needn't be in synchronized block |
|
2530 |
* ... |
|
7518 | 2531 |
* synchronized (m) { // Synchronizing on m, not s! |
2 | 2532 |
* Iterator i = s.iterator(); // Must be in synchronized block |
2533 |
* while (i.hasNext()) |
|
2534 |
* foo(i.next()); |
|
2535 |
* } |
|
2536 |
* </pre> |
|
2537 |
* Failure to follow this advice may result in non-deterministic behavior. |
|
2538 |
* |
|
2539 |
* <p>The returned map will be serializable if the specified map is |
|
2540 |
* serializable. |
|
2541 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
2542 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
2543 |
* @param <V> the class of the map values |
2 | 2544 |
* @param m the map to be "wrapped" in a synchronized map. |
2545 |
* @return a synchronized view of the specified map. |
|
2546 |
*/ |
|
2547 |
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2548 |
return new SynchronizedMap<>(m); |
2 | 2549 |
} |
2550 |
||
2551 |
/** |
|
2552 |
* @serial include |
|
2553 |
*/ |
|
2554 |
private static class SynchronizedMap<K,V> |
|
2555 |
implements Map<K,V>, Serializable { |
|
2556 |
private static final long serialVersionUID = 1978198479659022715L; |
|
2557 |
||
2558 |
private final Map<K,V> m; // Backing Map |
|
2559 |
final Object mutex; // Object on which to synchronize |
|
2560 |
||
2561 |
SynchronizedMap(Map<K,V> m) { |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2562 |
this.m = Objects.requireNonNull(m); |
2 | 2563 |
mutex = this; |
2564 |
} |
|
2565 |
||
2566 |
SynchronizedMap(Map<K,V> m, Object mutex) { |
|
2567 |
this.m = m; |
|
2568 |
this.mutex = mutex; |
|
2569 |
} |
|
2570 |
||
2571 |
public int size() { |
|
7518 | 2572 |
synchronized (mutex) {return m.size();} |
2 | 2573 |
} |
2574 |
public boolean isEmpty() { |
|
7518 | 2575 |
synchronized (mutex) {return m.isEmpty();} |
2 | 2576 |
} |
2577 |
public boolean containsKey(Object key) { |
|
7518 | 2578 |
synchronized (mutex) {return m.containsKey(key);} |
2 | 2579 |
} |
2580 |
public boolean containsValue(Object value) { |
|
7518 | 2581 |
synchronized (mutex) {return m.containsValue(value);} |
2 | 2582 |
} |
2583 |
public V get(Object key) { |
|
7518 | 2584 |
synchronized (mutex) {return m.get(key);} |
2 | 2585 |
} |
2586 |
||
2587 |
public V put(K key, V value) { |
|
7518 | 2588 |
synchronized (mutex) {return m.put(key, value);} |
2 | 2589 |
} |
2590 |
public V remove(Object key) { |
|
7518 | 2591 |
synchronized (mutex) {return m.remove(key);} |
2 | 2592 |
} |
2593 |
public void putAll(Map<? extends K, ? extends V> map) { |
|
7518 | 2594 |
synchronized (mutex) {m.putAll(map);} |
2 | 2595 |
} |
2596 |
public void clear() { |
|
7518 | 2597 |
synchronized (mutex) {m.clear();} |
2 | 2598 |
} |
2599 |
||
2600 |
private transient Set<K> keySet = null; |
|
2601 |
private transient Set<Map.Entry<K,V>> entrySet = null; |
|
2602 |
private transient Collection<V> values = null; |
|
2603 |
||
2604 |
public Set<K> keySet() { |
|
7518 | 2605 |
synchronized (mutex) { |
2 | 2606 |
if (keySet==null) |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2607 |
keySet = new SynchronizedSet<>(m.keySet(), mutex); |
2 | 2608 |
return keySet; |
2609 |
} |
|
2610 |
} |
|
2611 |
||
2612 |
public Set<Map.Entry<K,V>> entrySet() { |
|
7518 | 2613 |
synchronized (mutex) { |
2 | 2614 |
if (entrySet==null) |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2615 |
entrySet = new SynchronizedSet<>(m.entrySet(), mutex); |
2 | 2616 |
return entrySet; |
2617 |
} |
|
2618 |
} |
|
2619 |
||
2620 |
public Collection<V> values() { |
|
7518 | 2621 |
synchronized (mutex) { |
2 | 2622 |
if (values==null) |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2623 |
values = new SynchronizedCollection<>(m.values(), mutex); |
2 | 2624 |
return values; |
2625 |
} |
|
2626 |
} |
|
2627 |
||
2628 |
public boolean equals(Object o) { |
|
11987
a6dde4427e9d
7144488: Infinite recursion for some equals tests in Collections
coffeys
parents:
10896
diff
changeset
|
2629 |
if (this == o) |
a6dde4427e9d
7144488: Infinite recursion for some equals tests in Collections
coffeys
parents:
10896
diff
changeset
|
2630 |
return true; |
7518 | 2631 |
synchronized (mutex) {return m.equals(o);} |
2 | 2632 |
} |
2633 |
public int hashCode() { |
|
7518 | 2634 |
synchronized (mutex) {return m.hashCode();} |
2 | 2635 |
} |
2636 |
public String toString() { |
|
7518 | 2637 |
synchronized (mutex) {return m.toString();} |
2 | 2638 |
} |
16867 | 2639 |
|
2640 |
// Override default methods in Map |
|
2641 |
@Override |
|
2642 |
public V getOrDefault(Object k, V defaultValue) { |
|
2643 |
synchronized (mutex) {return m.getOrDefault(k, defaultValue);} |
|
2644 |
} |
|
2645 |
@Override |
|
2646 |
public void forEach(BiConsumer<? super K, ? super V> action) { |
|
2647 |
synchronized (mutex) {m.forEach(action);} |
|
2648 |
} |
|
2649 |
@Override |
|
2650 |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
|
2651 |
synchronized (mutex) {m.replaceAll(function);} |
|
2652 |
} |
|
2653 |
@Override |
|
2654 |
public V putIfAbsent(K key, V value) { |
|
2655 |
synchronized (mutex) {return m.putIfAbsent(key, value);} |
|
2656 |
} |
|
2657 |
@Override |
|
2658 |
public boolean remove(Object key, Object value) { |
|
2659 |
synchronized (mutex) {return m.remove(key, value);} |
|
2660 |
} |
|
2661 |
@Override |
|
2662 |
public boolean replace(K key, V oldValue, V newValue) { |
|
2663 |
synchronized (mutex) {return m.replace(key, oldValue, newValue);} |
|
2664 |
} |
|
2665 |
@Override |
|
2666 |
public V replace(K key, V value) { |
|
2667 |
synchronized (mutex) {return m.replace(key, value);} |
|
2668 |
} |
|
2669 |
@Override |
|
2670 |
public V computeIfAbsent(K key, |
|
2671 |
Function<? super K, ? extends V> mappingFunction) { |
|
2672 |
synchronized (mutex) {return m.computeIfAbsent(key, mappingFunction);} |
|
2673 |
} |
|
2674 |
@Override |
|
2675 |
public V computeIfPresent(K key, |
|
2676 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
2677 |
synchronized (mutex) {return m.computeIfPresent(key, remappingFunction);} |
|
2678 |
} |
|
2679 |
@Override |
|
2680 |
public V compute(K key, |
|
2681 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
2682 |
synchronized (mutex) {return m.compute(key, remappingFunction);} |
|
2683 |
} |
|
2684 |
@Override |
|
2685 |
public V merge(K key, V value, |
|
2686 |
BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
|
2687 |
synchronized (mutex) {return m.merge(key, value, remappingFunction);} |
|
2688 |
} |
|
2689 |
||
2 | 2690 |
private void writeObject(ObjectOutputStream s) throws IOException { |
7518 | 2691 |
synchronized (mutex) {s.defaultWriteObject();} |
2 | 2692 |
} |
2693 |
} |
|
2694 |
||
2695 |
/** |
|
2696 |
* Returns a synchronized (thread-safe) sorted map backed by the specified |
|
2697 |
* sorted map. In order to guarantee serial access, it is critical that |
|
2698 |
* <strong>all</strong> access to the backing sorted map is accomplished |
|
2699 |
* through the returned sorted map (or its views).<p> |
|
2700 |
* |
|
2701 |
* It is imperative that the user manually synchronize on the returned |
|
2702 |
* sorted map when iterating over any of its collection views, or the |
|
2703 |
* collections views of any of its <tt>subMap</tt>, <tt>headMap</tt> or |
|
2704 |
* <tt>tailMap</tt> views. |
|
2705 |
* <pre> |
|
2706 |
* SortedMap m = Collections.synchronizedSortedMap(new TreeMap()); |
|
2707 |
* ... |
|
2708 |
* Set s = m.keySet(); // Needn't be in synchronized block |
|
2709 |
* ... |
|
7518 | 2710 |
* synchronized (m) { // Synchronizing on m, not s! |
2 | 2711 |
* Iterator i = s.iterator(); // Must be in synchronized block |
2712 |
* while (i.hasNext()) |
|
2713 |
* foo(i.next()); |
|
2714 |
* } |
|
2715 |
* </pre> |
|
2716 |
* or: |
|
2717 |
* <pre> |
|
2718 |
* SortedMap m = Collections.synchronizedSortedMap(new TreeMap()); |
|
2719 |
* SortedMap m2 = m.subMap(foo, bar); |
|
2720 |
* ... |
|
2721 |
* Set s2 = m2.keySet(); // Needn't be in synchronized block |
|
2722 |
* ... |
|
7518 | 2723 |
* synchronized (m) { // Synchronizing on m, not m2 or s2! |
2 | 2724 |
* Iterator i = s.iterator(); // Must be in synchronized block |
2725 |
* while (i.hasNext()) |
|
2726 |
* foo(i.next()); |
|
2727 |
* } |
|
2728 |
* </pre> |
|
2729 |
* Failure to follow this advice may result in non-deterministic behavior. |
|
2730 |
* |
|
2731 |
* <p>The returned sorted map will be serializable if the specified |
|
2732 |
* sorted map is serializable. |
|
2733 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
2734 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
2735 |
* @param <V> the class of the map values |
2 | 2736 |
* @param m the sorted map to be "wrapped" in a synchronized sorted map. |
2737 |
* @return a synchronized view of the specified sorted map. |
|
2738 |
*/ |
|
2739 |
public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2740 |
return new SynchronizedSortedMap<>(m); |
2 | 2741 |
} |
2742 |
||
2743 |
/** |
|
2744 |
* @serial include |
|
2745 |
*/ |
|
2746 |
static class SynchronizedSortedMap<K,V> |
|
2747 |
extends SynchronizedMap<K,V> |
|
2748 |
implements SortedMap<K,V> |
|
2749 |
{ |
|
2750 |
private static final long serialVersionUID = -8798146769416483793L; |
|
2751 |
||
2752 |
private final SortedMap<K,V> sm; |
|
2753 |
||
2754 |
SynchronizedSortedMap(SortedMap<K,V> m) { |
|
2755 |
super(m); |
|
2756 |
sm = m; |
|
2757 |
} |
|
2758 |
SynchronizedSortedMap(SortedMap<K,V> m, Object mutex) { |
|
2759 |
super(m, mutex); |
|
2760 |
sm = m; |
|
2761 |
} |
|
2762 |
||
2763 |
public Comparator<? super K> comparator() { |
|
7518 | 2764 |
synchronized (mutex) {return sm.comparator();} |
2 | 2765 |
} |
2766 |
||
2767 |
public SortedMap<K,V> subMap(K fromKey, K toKey) { |
|
7518 | 2768 |
synchronized (mutex) { |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2769 |
return new SynchronizedSortedMap<>( |
2 | 2770 |
sm.subMap(fromKey, toKey), mutex); |
2771 |
} |
|
2772 |
} |
|
2773 |
public SortedMap<K,V> headMap(K toKey) { |
|
7518 | 2774 |
synchronized (mutex) { |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2775 |
return new SynchronizedSortedMap<>(sm.headMap(toKey), mutex); |
2 | 2776 |
} |
2777 |
} |
|
2778 |
public SortedMap<K,V> tailMap(K fromKey) { |
|
7518 | 2779 |
synchronized (mutex) { |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
2780 |
return new SynchronizedSortedMap<>(sm.tailMap(fromKey),mutex); |
2 | 2781 |
} |
2782 |
} |
|
2783 |
||
2784 |
public K firstKey() { |
|
7518 | 2785 |
synchronized (mutex) {return sm.firstKey();} |
2 | 2786 |
} |
2787 |
public K lastKey() { |
|
7518 | 2788 |
synchronized (mutex) {return sm.lastKey();} |
2 | 2789 |
} |
2790 |
} |
|
2791 |
||
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2792 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2793 |
* Returns a synchronized (thread-safe) navigable map backed by the |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2794 |
* specified navigable map. In order to guarantee serial access, it is |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2795 |
* critical that <strong>all</strong> access to the backing navigable map is |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2796 |
* accomplished through the returned navigable map (or its views).<p> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2797 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2798 |
* It is imperative that the user manually synchronize on the returned |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2799 |
* navigable map when iterating over any of its collection views, or the |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2800 |
* collections views of any of its {@code subMap}, {@code headMap} or |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2801 |
* {@code tailMap} views. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2802 |
* <pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2803 |
* NavigableMap m = Collections.synchronizedNavigableMap(new TreeMap()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2804 |
* ... |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2805 |
* Set s = m.keySet(); // Needn't be in synchronized block |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2806 |
* ... |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2807 |
* synchronized (m) { // Synchronizing on m, not s! |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2808 |
* Iterator i = s.iterator(); // Must be in synchronized block |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2809 |
* while (i.hasNext()) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2810 |
* foo(i.next()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2811 |
* } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2812 |
* </pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2813 |
* or: |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2814 |
* <pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2815 |
* NavigableMap m = Collections.synchronizedNavigableMap(new TreeMap()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2816 |
* NavigableMap m2 = m.subMap(foo, true, bar, false); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2817 |
* ... |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2818 |
* Set s2 = m2.keySet(); // Needn't be in synchronized block |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2819 |
* ... |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2820 |
* synchronized (m) { // Synchronizing on m, not m2 or s2! |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2821 |
* Iterator i = s.iterator(); // Must be in synchronized block |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2822 |
* while (i.hasNext()) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2823 |
* foo(i.next()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2824 |
* } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2825 |
* </pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2826 |
* Failure to follow this advice may result in non-deterministic behavior. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2827 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2828 |
* <p>The returned navigable map will be serializable if the specified |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2829 |
* navigable map is serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2830 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
2831 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
2832 |
* @param <V> the class of the map values |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2833 |
* @param m the navigable map to be "wrapped" in a synchronized navigable |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2834 |
* map |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2835 |
* @return a synchronized view of the specified navigable map. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2836 |
* @since 1.8 |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2837 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2838 |
public static <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2839 |
return new SynchronizedNavigableMap<>(m); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2840 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2841 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2842 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2843 |
* A synchronized NavigableMap. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2844 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2845 |
* @serial include |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2846 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2847 |
static class SynchronizedNavigableMap<K,V> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2848 |
extends SynchronizedSortedMap<K,V> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2849 |
implements NavigableMap<K,V> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2850 |
{ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2851 |
private static final long serialVersionUID = 699392247599746807L; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2852 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2853 |
private final NavigableMap<K,V> nm; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2854 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2855 |
SynchronizedNavigableMap(NavigableMap<K,V> m) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2856 |
super(m); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2857 |
nm = m; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2858 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2859 |
SynchronizedNavigableMap(NavigableMap<K,V> m, Object mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2860 |
super(m, mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2861 |
nm = m; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2862 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2863 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2864 |
public Entry<K, V> lowerEntry(K key) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2865 |
{ synchronized (mutex) { return nm.lowerEntry(key); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2866 |
public K lowerKey(K key) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2867 |
{ synchronized (mutex) { return nm.lowerKey(key); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2868 |
public Entry<K, V> floorEntry(K key) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2869 |
{ synchronized (mutex) { return nm.floorEntry(key); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2870 |
public K floorKey(K key) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2871 |
{ synchronized (mutex) { return nm.floorKey(key); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2872 |
public Entry<K, V> ceilingEntry(K key) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2873 |
{ synchronized (mutex) { return nm.ceilingEntry(key); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2874 |
public K ceilingKey(K key) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2875 |
{ synchronized (mutex) { return nm.ceilingKey(key); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2876 |
public Entry<K, V> higherEntry(K key) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2877 |
{ synchronized (mutex) { return nm.higherEntry(key); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2878 |
public K higherKey(K key) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2879 |
{ synchronized (mutex) { return nm.higherKey(key); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2880 |
public Entry<K, V> firstEntry() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2881 |
{ synchronized (mutex) { return nm.firstEntry(); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2882 |
public Entry<K, V> lastEntry() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2883 |
{ synchronized (mutex) { return nm.lastEntry(); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2884 |
public Entry<K, V> pollFirstEntry() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2885 |
{ synchronized (mutex) { return nm.pollFirstEntry(); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2886 |
public Entry<K, V> pollLastEntry() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2887 |
{ synchronized (mutex) { return nm.pollLastEntry(); } } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2888 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2889 |
public NavigableMap<K, V> descendingMap() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2890 |
synchronized (mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2891 |
return |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
2892 |
new SynchronizedNavigableMap<>(nm.descendingMap(), mutex); |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2893 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2894 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2895 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2896 |
public NavigableSet<K> keySet() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2897 |
return navigableKeySet(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2898 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2899 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2900 |
public NavigableSet<K> navigableKeySet() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2901 |
synchronized (mutex) { |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
2902 |
return new SynchronizedNavigableSet<>(nm.navigableKeySet(), mutex); |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2903 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2904 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2905 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2906 |
public NavigableSet<K> descendingKeySet() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2907 |
synchronized (mutex) { |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
2908 |
return new SynchronizedNavigableSet<>(nm.descendingKeySet(), mutex); |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2909 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2910 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2911 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2912 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2913 |
public SortedMap<K,V> subMap(K fromKey, K toKey) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2914 |
synchronized (mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2915 |
return new SynchronizedNavigableMap<>( |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2916 |
nm.subMap(fromKey, true, toKey, false), mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2917 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2918 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2919 |
public SortedMap<K,V> headMap(K toKey) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2920 |
synchronized (mutex) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2921 |
return new SynchronizedNavigableMap<>(nm.headMap(toKey, false), mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2922 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2923 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2924 |
public SortedMap<K,V> tailMap(K fromKey) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2925 |
synchronized (mutex) { |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
2926 |
return new SynchronizedNavigableMap<>(nm.tailMap(fromKey, true),mutex); |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2927 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2928 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2929 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2930 |
public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2931 |
synchronized (mutex) { |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
2932 |
return new SynchronizedNavigableMap<>( |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2933 |
nm.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2934 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2935 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2936 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2937 |
public NavigableMap<K, V> headMap(K toKey, boolean inclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2938 |
synchronized (mutex) { |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
2939 |
return new SynchronizedNavigableMap<>( |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2940 |
nm.headMap(toKey, inclusive), mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2941 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2942 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2943 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2944 |
public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2945 |
synchronized (mutex) { |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
2946 |
return new SynchronizedNavigableMap<>( |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2947 |
nm.tailMap(fromKey, inclusive), mutex); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2948 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2949 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2950 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2951 |
|
2 | 2952 |
// Dynamically typesafe collection wrappers |
2953 |
||
2954 |
/** |
|
2955 |
* Returns a dynamically typesafe view of the specified collection. |
|
2956 |
* Any attempt to insert an element of the wrong type will result in an |
|
2957 |
* immediate {@link ClassCastException}. Assuming a collection |
|
2958 |
* contains no incorrectly typed elements prior to the time a |
|
2959 |
* dynamically typesafe view is generated, and that all subsequent |
|
2960 |
* access to the collection takes place through the view, it is |
|
2961 |
* <i>guaranteed</i> that the collection cannot contain an incorrectly |
|
2962 |
* typed element. |
|
2963 |
* |
|
2964 |
* <p>The generics mechanism in the language provides compile-time |
|
2965 |
* (static) type checking, but it is possible to defeat this mechanism |
|
2966 |
* with unchecked casts. Usually this is not a problem, as the compiler |
|
2967 |
* issues warnings on all such unchecked operations. There are, however, |
|
2968 |
* times when static type checking alone is not sufficient. For example, |
|
2969 |
* suppose a collection is passed to a third-party library and it is |
|
2970 |
* imperative that the library code not corrupt the collection by |
|
2971 |
* inserting an element of the wrong type. |
|
2972 |
* |
|
2973 |
* <p>Another use of dynamically typesafe views is debugging. Suppose a |
|
2974 |
* program fails with a {@code ClassCastException}, indicating that an |
|
2975 |
* incorrectly typed element was put into a parameterized collection. |
|
2976 |
* Unfortunately, the exception can occur at any time after the erroneous |
|
2977 |
* element is inserted, so it typically provides little or no information |
|
2978 |
* as to the real source of the problem. If the problem is reproducible, |
|
2979 |
* one can quickly determine its source by temporarily modifying the |
|
2980 |
* program to wrap the collection with a dynamically typesafe view. |
|
2981 |
* For example, this declaration: |
|
2982 |
* <pre> {@code |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2983 |
* Collection<String> c = new HashSet<>(); |
2 | 2984 |
* }</pre> |
2985 |
* may be replaced temporarily by this one: |
|
2986 |
* <pre> {@code |
|
2987 |
* Collection<String> c = Collections.checkedCollection( |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
2988 |
* new HashSet<>(), String.class); |
2 | 2989 |
* }</pre> |
2990 |
* Running the program again will cause it to fail at the point where |
|
2991 |
* an incorrectly typed element is inserted into the collection, clearly |
|
2992 |
* identifying the source of the problem. Once the problem is fixed, the |
|
2993 |
* modified declaration may be reverted back to the original. |
|
2994 |
* |
|
2995 |
* <p>The returned collection does <i>not</i> pass the hashCode and equals |
|
2996 |
* operations through to the backing collection, but relies on |
|
2997 |
* {@code Object}'s {@code equals} and {@code hashCode} methods. This |
|
2998 |
* is necessary to preserve the contracts of these operations in the case |
|
2999 |
* that the backing collection is a set or a list. |
|
3000 |
* |
|
3001 |
* <p>The returned collection will be serializable if the specified |
|
3002 |
* collection is serializable. |
|
3003 |
* |
|
3004 |
* <p>Since {@code null} is considered to be a value of any reference |
|
3005 |
* type, the returned collection permits insertion of null elements |
|
3006 |
* whenever the backing collection does. |
|
3007 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
3008 |
* @param <E> the class of the objects in the collection |
2 | 3009 |
* @param c the collection for which a dynamically typesafe view is to be |
3010 |
* returned |
|
3011 |
* @param type the type of element that {@code c} is permitted to hold |
|
3012 |
* @return a dynamically typesafe view of the specified collection |
|
3013 |
* @since 1.5 |
|
3014 |
*/ |
|
3015 |
public static <E> Collection<E> checkedCollection(Collection<E> c, |
|
3016 |
Class<E> type) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3017 |
return new CheckedCollection<>(c, type); |
2 | 3018 |
} |
3019 |
||
3020 |
@SuppressWarnings("unchecked") |
|
3021 |
static <T> T[] zeroLengthArray(Class<T> type) { |
|
3022 |
return (T[]) Array.newInstance(type, 0); |
|
3023 |
} |
|
3024 |
||
3025 |
/** |
|
3026 |
* @serial include |
|
3027 |
*/ |
|
3028 |
static class CheckedCollection<E> implements Collection<E>, Serializable { |
|
3029 |
private static final long serialVersionUID = 1578914078182001775L; |
|
3030 |
||
3031 |
final Collection<E> c; |
|
3032 |
final Class<E> type; |
|
3033 |
||
3034 |
void typeCheck(Object o) { |
|
3035 |
if (o != null && !type.isInstance(o)) |
|
3036 |
throw new ClassCastException(badElementMsg(o)); |
|
3037 |
} |
|
3038 |
||
3039 |
private String badElementMsg(Object o) { |
|
3040 |
return "Attempt to insert " + o.getClass() + |
|
3041 |
" element into collection with element type " + type; |
|
3042 |
} |
|
3043 |
||
3044 |
CheckedCollection(Collection<E> c, Class<E> type) { |
|
3045 |
if (c==null || type == null) |
|
3046 |
throw new NullPointerException(); |
|
3047 |
this.c = c; |
|
3048 |
this.type = type; |
|
3049 |
} |
|
3050 |
||
3051 |
public int size() { return c.size(); } |
|
3052 |
public boolean isEmpty() { return c.isEmpty(); } |
|
3053 |
public boolean contains(Object o) { return c.contains(o); } |
|
3054 |
public Object[] toArray() { return c.toArray(); } |
|
3055 |
public <T> T[] toArray(T[] a) { return c.toArray(a); } |
|
3056 |
public String toString() { return c.toString(); } |
|
3057 |
public boolean remove(Object o) { return c.remove(o); } |
|
3058 |
public void clear() { c.clear(); } |
|
3059 |
||
3060 |
public boolean containsAll(Collection<?> coll) { |
|
3061 |
return c.containsAll(coll); |
|
3062 |
} |
|
3063 |
public boolean removeAll(Collection<?> coll) { |
|
3064 |
return c.removeAll(coll); |
|
3065 |
} |
|
3066 |
public boolean retainAll(Collection<?> coll) { |
|
3067 |
return c.retainAll(coll); |
|
3068 |
} |
|
3069 |
||
3070 |
public Iterator<E> iterator() { |
|
16867 | 3071 |
// JDK-6363904 - unwrapped iterator could be typecast to |
3072 |
// ListIterator with unsafe set() |
|
2 | 3073 |
final Iterator<E> it = c.iterator(); |
3074 |
return new Iterator<E>() { |
|
3075 |
public boolean hasNext() { return it.hasNext(); } |
|
3076 |
public E next() { return it.next(); } |
|
3077 |
public void remove() { it.remove(); }}; |
|
3078 |
} |
|
3079 |
||
3080 |
public boolean add(E e) { |
|
3081 |
typeCheck(e); |
|
3082 |
return c.add(e); |
|
3083 |
} |
|
3084 |
||
3085 |
private E[] zeroLengthElementArray = null; // Lazily initialized |
|
3086 |
||
3087 |
private E[] zeroLengthElementArray() { |
|
3088 |
return zeroLengthElementArray != null ? zeroLengthElementArray : |
|
3089 |
(zeroLengthElementArray = zeroLengthArray(type)); |
|
3090 |
} |
|
3091 |
||
3092 |
@SuppressWarnings("unchecked") |
|
3093 |
Collection<E> checkedCopyOf(Collection<? extends E> coll) { |
|
3094 |
Object[] a = null; |
|
3095 |
try { |
|
3096 |
E[] z = zeroLengthElementArray(); |
|
3097 |
a = coll.toArray(z); |
|
3098 |
// Defend against coll violating the toArray contract |
|
3099 |
if (a.getClass() != z.getClass()) |
|
3100 |
a = Arrays.copyOf(a, a.length, z.getClass()); |
|
3101 |
} catch (ArrayStoreException ignore) { |
|
3102 |
// To get better and consistent diagnostics, |
|
3103 |
// we call typeCheck explicitly on each element. |
|
3104 |
// We call clone() to defend against coll retaining a |
|
3105 |
// reference to the returned array and storing a bad |
|
3106 |
// element into it after it has been type checked. |
|
3107 |
a = coll.toArray().clone(); |
|
3108 |
for (Object o : a) |
|
3109 |
typeCheck(o); |
|
3110 |
} |
|
3111 |
// A slight abuse of the type system, but safe here. |
|
3112 |
return (Collection<E>) Arrays.asList(a); |
|
3113 |
} |
|
3114 |
||
3115 |
public boolean addAll(Collection<? extends E> coll) { |
|
3116 |
// Doing things this way insulates us from concurrent changes |
|
3117 |
// in the contents of coll and provides all-or-nothing |
|
3118 |
// semantics (which we wouldn't get if we type-checked each |
|
3119 |
// element as we added it) |
|
3120 |
return c.addAll(checkedCopyOf(coll)); |
|
3121 |
} |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3122 |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
3123 |
// Override default methods in Collection |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3124 |
@Override |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
3125 |
public void forEach(Consumer<? super E> action) {c.forEach(action);} |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3126 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3127 |
public boolean removeIf(Predicate<? super E> filter) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3128 |
return c.removeIf(filter); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3129 |
} |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
3130 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
3131 |
public Spliterator<E> spliterator() {return c.spliterator();} |
19603
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
3132 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
3133 |
public Stream<E> stream() {return c.stream();} |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
3134 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
3135 |
public Stream<E> parallelStream() {return c.parallelStream();} |
2 | 3136 |
} |
3137 |
||
3138 |
/** |
|
10790 | 3139 |
* Returns a dynamically typesafe view of the specified queue. |
3140 |
* Any attempt to insert an element of the wrong type will result in |
|
3141 |
* an immediate {@link ClassCastException}. Assuming a queue contains |
|
3142 |
* no incorrectly typed elements prior to the time a dynamically typesafe |
|
3143 |
* view is generated, and that all subsequent access to the queue |
|
3144 |
* takes place through the view, it is <i>guaranteed</i> that the |
|
3145 |
* queue cannot contain an incorrectly typed element. |
|
3146 |
* |
|
3147 |
* <p>A discussion of the use of dynamically typesafe views may be |
|
3148 |
* found in the documentation for the {@link #checkedCollection |
|
3149 |
* checkedCollection} method. |
|
3150 |
* |
|
3151 |
* <p>The returned queue will be serializable if the specified queue |
|
3152 |
* is serializable. |
|
3153 |
* |
|
3154 |
* <p>Since {@code null} is considered to be a value of any reference |
|
3155 |
* type, the returned queue permits insertion of {@code null} elements |
|
3156 |
* whenever the backing queue does. |
|
3157 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
3158 |
* @param <E> the class of the objects in the queue |
10790 | 3159 |
* @param queue the queue for which a dynamically typesafe view is to be |
3160 |
* returned |
|
3161 |
* @param type the type of element that {@code queue} is permitted to hold |
|
3162 |
* @return a dynamically typesafe view of the specified queue |
|
3163 |
* @since 1.8 |
|
3164 |
*/ |
|
3165 |
public static <E> Queue<E> checkedQueue(Queue<E> queue, Class<E> type) { |
|
3166 |
return new CheckedQueue<>(queue, type); |
|
3167 |
} |
|
3168 |
||
3169 |
/** |
|
3170 |
* @serial include |
|
3171 |
*/ |
|
3172 |
static class CheckedQueue<E> |
|
3173 |
extends CheckedCollection<E> |
|
3174 |
implements Queue<E>, Serializable |
|
3175 |
{ |
|
3176 |
private static final long serialVersionUID = 1433151992604707767L; |
|
3177 |
final Queue<E> queue; |
|
3178 |
||
3179 |
CheckedQueue(Queue<E> queue, Class<E> elementType) { |
|
3180 |
super(queue, elementType); |
|
3181 |
this.queue = queue; |
|
3182 |
} |
|
3183 |
||
3184 |
public E element() {return queue.element();} |
|
3185 |
public boolean equals(Object o) {return o == this || c.equals(o);} |
|
3186 |
public int hashCode() {return c.hashCode();} |
|
3187 |
public E peek() {return queue.peek();} |
|
3188 |
public E poll() {return queue.poll();} |
|
3189 |
public E remove() {return queue.remove();} |
|
3190 |
||
3191 |
public boolean offer(E e) { |
|
3192 |
typeCheck(e); |
|
3193 |
return add(e); |
|
3194 |
} |
|
3195 |
} |
|
3196 |
||
3197 |
/** |
|
2 | 3198 |
* Returns a dynamically typesafe view of the specified set. |
3199 |
* Any attempt to insert an element of the wrong type will result in |
|
3200 |
* an immediate {@link ClassCastException}. Assuming a set contains |
|
3201 |
* no incorrectly typed elements prior to the time a dynamically typesafe |
|
3202 |
* view is generated, and that all subsequent access to the set |
|
3203 |
* takes place through the view, it is <i>guaranteed</i> that the |
|
3204 |
* set cannot contain an incorrectly typed element. |
|
3205 |
* |
|
3206 |
* <p>A discussion of the use of dynamically typesafe views may be |
|
3207 |
* found in the documentation for the {@link #checkedCollection |
|
3208 |
* checkedCollection} method. |
|
3209 |
* |
|
3210 |
* <p>The returned set will be serializable if the specified set is |
|
3211 |
* serializable. |
|
3212 |
* |
|
3213 |
* <p>Since {@code null} is considered to be a value of any reference |
|
3214 |
* type, the returned set permits insertion of null elements whenever |
|
3215 |
* the backing set does. |
|
3216 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
3217 |
* @param <E> the class of the objects in the set |
2 | 3218 |
* @param s the set for which a dynamically typesafe view is to be |
3219 |
* returned |
|
3220 |
* @param type the type of element that {@code s} is permitted to hold |
|
3221 |
* @return a dynamically typesafe view of the specified set |
|
3222 |
* @since 1.5 |
|
3223 |
*/ |
|
3224 |
public static <E> Set<E> checkedSet(Set<E> s, Class<E> type) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3225 |
return new CheckedSet<>(s, type); |
2 | 3226 |
} |
3227 |
||
3228 |
/** |
|
3229 |
* @serial include |
|
3230 |
*/ |
|
3231 |
static class CheckedSet<E> extends CheckedCollection<E> |
|
3232 |
implements Set<E>, Serializable |
|
3233 |
{ |
|
3234 |
private static final long serialVersionUID = 4694047833775013803L; |
|
3235 |
||
3236 |
CheckedSet(Set<E> s, Class<E> elementType) { super(s, elementType); } |
|
3237 |
||
3238 |
public boolean equals(Object o) { return o == this || c.equals(o); } |
|
3239 |
public int hashCode() { return c.hashCode(); } |
|
3240 |
} |
|
3241 |
||
3242 |
/** |
|
3243 |
* Returns a dynamically typesafe view of the specified sorted set. |
|
3244 |
* Any attempt to insert an element of the wrong type will result in an |
|
3245 |
* immediate {@link ClassCastException}. Assuming a sorted set |
|
3246 |
* contains no incorrectly typed elements prior to the time a |
|
3247 |
* dynamically typesafe view is generated, and that all subsequent |
|
3248 |
* access to the sorted set takes place through the view, it is |
|
3249 |
* <i>guaranteed</i> that the sorted set cannot contain an incorrectly |
|
3250 |
* typed element. |
|
3251 |
* |
|
3252 |
* <p>A discussion of the use of dynamically typesafe views may be |
|
3253 |
* found in the documentation for the {@link #checkedCollection |
|
3254 |
* checkedCollection} method. |
|
3255 |
* |
|
3256 |
* <p>The returned sorted set will be serializable if the specified sorted |
|
3257 |
* set is serializable. |
|
3258 |
* |
|
3259 |
* <p>Since {@code null} is considered to be a value of any reference |
|
3260 |
* type, the returned sorted set permits insertion of null elements |
|
3261 |
* whenever the backing sorted set does. |
|
3262 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
3263 |
* @param <E> the class of the objects in the set |
2 | 3264 |
* @param s the sorted set for which a dynamically typesafe view is to be |
3265 |
* returned |
|
3266 |
* @param type the type of element that {@code s} is permitted to hold |
|
3267 |
* @return a dynamically typesafe view of the specified sorted set |
|
3268 |
* @since 1.5 |
|
3269 |
*/ |
|
3270 |
public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s, |
|
3271 |
Class<E> type) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3272 |
return new CheckedSortedSet<>(s, type); |
2 | 3273 |
} |
3274 |
||
3275 |
/** |
|
3276 |
* @serial include |
|
3277 |
*/ |
|
3278 |
static class CheckedSortedSet<E> extends CheckedSet<E> |
|
3279 |
implements SortedSet<E>, Serializable |
|
3280 |
{ |
|
3281 |
private static final long serialVersionUID = 1599911165492914959L; |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3282 |
|
2 | 3283 |
private final SortedSet<E> ss; |
3284 |
||
3285 |
CheckedSortedSet(SortedSet<E> s, Class<E> type) { |
|
3286 |
super(s, type); |
|
3287 |
ss = s; |
|
3288 |
} |
|
3289 |
||
3290 |
public Comparator<? super E> comparator() { return ss.comparator(); } |
|
3291 |
public E first() { return ss.first(); } |
|
3292 |
public E last() { return ss.last(); } |
|
3293 |
||
3294 |
public SortedSet<E> subSet(E fromElement, E toElement) { |
|
3295 |
return checkedSortedSet(ss.subSet(fromElement,toElement), type); |
|
3296 |
} |
|
3297 |
public SortedSet<E> headSet(E toElement) { |
|
3298 |
return checkedSortedSet(ss.headSet(toElement), type); |
|
3299 |
} |
|
3300 |
public SortedSet<E> tailSet(E fromElement) { |
|
3301 |
return checkedSortedSet(ss.tailSet(fromElement), type); |
|
3302 |
} |
|
3303 |
} |
|
3304 |
||
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3305 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3306 |
* Returns a dynamically typesafe view of the specified navigable set. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3307 |
* Any attempt to insert an element of the wrong type will result in an |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3308 |
* immediate {@link ClassCastException}. Assuming a navigable set |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3309 |
* contains no incorrectly typed elements prior to the time a |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3310 |
* dynamically typesafe view is generated, and that all subsequent |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3311 |
* access to the navigable set takes place through the view, it is |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3312 |
* <em>guaranteed</em> that the navigable set cannot contain an incorrectly |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3313 |
* typed element. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3314 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3315 |
* <p>A discussion of the use of dynamically typesafe views may be |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3316 |
* found in the documentation for the {@link #checkedCollection |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3317 |
* checkedCollection} method. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3318 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3319 |
* <p>The returned navigable set will be serializable if the specified |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3320 |
* navigable set is serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3321 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3322 |
* <p>Since {@code null} is considered to be a value of any reference |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3323 |
* type, the returned navigable set permits insertion of null elements |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3324 |
* whenever the backing sorted set does. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3325 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
3326 |
* @param <E> the class of the objects in the set |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3327 |
* @param s the navigable set for which a dynamically typesafe view is to be |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3328 |
* returned |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3329 |
* @param type the type of element that {@code s} is permitted to hold |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3330 |
* @return a dynamically typesafe view of the specified navigable set |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3331 |
* @since 1.8 |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3332 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3333 |
public static <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> s, |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3334 |
Class<E> type) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3335 |
return new CheckedNavigableSet<>(s, type); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3336 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3337 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3338 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3339 |
* @serial include |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3340 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3341 |
static class CheckedNavigableSet<E> extends CheckedSortedSet<E> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3342 |
implements NavigableSet<E>, Serializable |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3343 |
{ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3344 |
private static final long serialVersionUID = -5429120189805438922L; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3345 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3346 |
private final NavigableSet<E> ns; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3347 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3348 |
CheckedNavigableSet(NavigableSet<E> s, Class<E> type) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3349 |
super(s, type); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3350 |
ns = s; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3351 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3352 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3353 |
public E lower(E e) { return ns.lower(e); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3354 |
public E floor(E e) { return ns.floor(e); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3355 |
public E ceiling(E e) { return ns.ceiling(e); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3356 |
public E higher(E e) { return ns.higher(e); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3357 |
public E pollFirst() { return ns.pollFirst(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3358 |
public E pollLast() {return ns.pollLast(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3359 |
public NavigableSet<E> descendingSet() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3360 |
{ return checkedNavigableSet(ns.descendingSet(), type); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3361 |
public Iterator<E> descendingIterator() |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3362 |
{return checkedNavigableSet(ns.descendingSet(), type).iterator(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3363 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3364 |
public NavigableSet<E> subSet(E fromElement, E toElement) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3365 |
return checkedNavigableSet(ns.subSet(fromElement, true, toElement, false), type); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3366 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3367 |
public NavigableSet<E> headSet(E toElement) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3368 |
return checkedNavigableSet(ns.headSet(toElement, false), type); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3369 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3370 |
public NavigableSet<E> tailSet(E fromElement) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3371 |
return checkedNavigableSet(ns.tailSet(fromElement, true), type); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3372 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3373 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3374 |
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3375 |
return checkedNavigableSet(ns.subSet(fromElement, fromInclusive, toElement, toInclusive), type); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3376 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3377 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3378 |
public NavigableSet<E> headSet(E toElement, boolean inclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3379 |
return checkedNavigableSet(ns.headSet(toElement, inclusive), type); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3380 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3381 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3382 |
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3383 |
return checkedNavigableSet(ns.tailSet(fromElement, inclusive), type); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3384 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3385 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3386 |
|
2 | 3387 |
/** |
3388 |
* Returns a dynamically typesafe view of the specified list. |
|
3389 |
* Any attempt to insert an element of the wrong type will result in |
|
3390 |
* an immediate {@link ClassCastException}. Assuming a list contains |
|
3391 |
* no incorrectly typed elements prior to the time a dynamically typesafe |
|
3392 |
* view is generated, and that all subsequent access to the list |
|
3393 |
* takes place through the view, it is <i>guaranteed</i> that the |
|
3394 |
* list cannot contain an incorrectly typed element. |
|
3395 |
* |
|
3396 |
* <p>A discussion of the use of dynamically typesafe views may be |
|
3397 |
* found in the documentation for the {@link #checkedCollection |
|
3398 |
* checkedCollection} method. |
|
3399 |
* |
|
3400 |
* <p>The returned list will be serializable if the specified list |
|
3401 |
* is serializable. |
|
3402 |
* |
|
3403 |
* <p>Since {@code null} is considered to be a value of any reference |
|
3404 |
* type, the returned list permits insertion of null elements whenever |
|
3405 |
* the backing list does. |
|
3406 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
3407 |
* @param <E> the class of the objects in the list |
2 | 3408 |
* @param list the list for which a dynamically typesafe view is to be |
3409 |
* returned |
|
3410 |
* @param type the type of element that {@code list} is permitted to hold |
|
3411 |
* @return a dynamically typesafe view of the specified list |
|
3412 |
* @since 1.5 |
|
3413 |
*/ |
|
3414 |
public static <E> List<E> checkedList(List<E> list, Class<E> type) { |
|
3415 |
return (list instanceof RandomAccess ? |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3416 |
new CheckedRandomAccessList<>(list, type) : |
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3417 |
new CheckedList<>(list, type)); |
2 | 3418 |
} |
3419 |
||
3420 |
/** |
|
3421 |
* @serial include |
|
3422 |
*/ |
|
3423 |
static class CheckedList<E> |
|
3424 |
extends CheckedCollection<E> |
|
3425 |
implements List<E> |
|
3426 |
{ |
|
3427 |
private static final long serialVersionUID = 65247728283967356L; |
|
3428 |
final List<E> list; |
|
3429 |
||
3430 |
CheckedList(List<E> list, Class<E> type) { |
|
3431 |
super(list, type); |
|
3432 |
this.list = list; |
|
3433 |
} |
|
3434 |
||
3435 |
public boolean equals(Object o) { return o == this || list.equals(o); } |
|
3436 |
public int hashCode() { return list.hashCode(); } |
|
3437 |
public E get(int index) { return list.get(index); } |
|
3438 |
public E remove(int index) { return list.remove(index); } |
|
3439 |
public int indexOf(Object o) { return list.indexOf(o); } |
|
3440 |
public int lastIndexOf(Object o) { return list.lastIndexOf(o); } |
|
3441 |
||
3442 |
public E set(int index, E element) { |
|
3443 |
typeCheck(element); |
|
3444 |
return list.set(index, element); |
|
3445 |
} |
|
3446 |
||
3447 |
public void add(int index, E element) { |
|
3448 |
typeCheck(element); |
|
3449 |
list.add(index, element); |
|
3450 |
} |
|
3451 |
||
3452 |
public boolean addAll(int index, Collection<? extends E> c) { |
|
3453 |
return list.addAll(index, checkedCopyOf(c)); |
|
3454 |
} |
|
3455 |
public ListIterator<E> listIterator() { return listIterator(0); } |
|
3456 |
||
3457 |
public ListIterator<E> listIterator(final int index) { |
|
3458 |
final ListIterator<E> i = list.listIterator(index); |
|
3459 |
||
3460 |
return new ListIterator<E>() { |
|
3461 |
public boolean hasNext() { return i.hasNext(); } |
|
3462 |
public E next() { return i.next(); } |
|
3463 |
public boolean hasPrevious() { return i.hasPrevious(); } |
|
3464 |
public E previous() { return i.previous(); } |
|
3465 |
public int nextIndex() { return i.nextIndex(); } |
|
3466 |
public int previousIndex() { return i.previousIndex(); } |
|
3467 |
public void remove() { i.remove(); } |
|
3468 |
||
3469 |
public void set(E e) { |
|
3470 |
typeCheck(e); |
|
3471 |
i.set(e); |
|
3472 |
} |
|
3473 |
||
3474 |
public void add(E e) { |
|
3475 |
typeCheck(e); |
|
3476 |
i.add(e); |
|
3477 |
} |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
3478 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
3479 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
3480 |
public void forEachRemaining(Consumer<? super E> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
3481 |
i.forEachRemaining(action); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
3482 |
} |
2 | 3483 |
}; |
3484 |
} |
|
3485 |
||
3486 |
public List<E> subList(int fromIndex, int toIndex) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3487 |
return new CheckedList<>(list.subList(fromIndex, toIndex), type); |
2 | 3488 |
} |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3489 |
|
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3490 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3491 |
public void replaceAll(UnaryOperator<E> operator) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3492 |
list.replaceAll(operator); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3493 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3494 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3495 |
public void sort(Comparator<? super E> c) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3496 |
list.sort(c); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
3497 |
} |
2 | 3498 |
} |
3499 |
||
3500 |
/** |
|
3501 |
* @serial include |
|
3502 |
*/ |
|
3503 |
static class CheckedRandomAccessList<E> extends CheckedList<E> |
|
3504 |
implements RandomAccess |
|
3505 |
{ |
|
3506 |
private static final long serialVersionUID = 1638200125423088369L; |
|
3507 |
||
3508 |
CheckedRandomAccessList(List<E> list, Class<E> type) { |
|
3509 |
super(list, type); |
|
3510 |
} |
|
3511 |
||
3512 |
public List<E> subList(int fromIndex, int toIndex) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3513 |
return new CheckedRandomAccessList<>( |
16867 | 3514 |
list.subList(fromIndex, toIndex), type); |
2 | 3515 |
} |
3516 |
} |
|
3517 |
||
3518 |
/** |
|
3519 |
* Returns a dynamically typesafe view of the specified map. |
|
3520 |
* Any attempt to insert a mapping whose key or value have the wrong |
|
3521 |
* type will result in an immediate {@link ClassCastException}. |
|
3522 |
* Similarly, any attempt to modify the value currently associated with |
|
3523 |
* a key will result in an immediate {@link ClassCastException}, |
|
3524 |
* whether the modification is attempted directly through the map |
|
3525 |
* itself, or through a {@link Map.Entry} instance obtained from the |
|
3526 |
* map's {@link Map#entrySet() entry set} view. |
|
3527 |
* |
|
3528 |
* <p>Assuming a map contains no incorrectly typed keys or values |
|
3529 |
* prior to the time a dynamically typesafe view is generated, and |
|
3530 |
* that all subsequent access to the map takes place through the view |
|
3531 |
* (or one of its collection views), it is <i>guaranteed</i> that the |
|
3532 |
* map cannot contain an incorrectly typed key or value. |
|
3533 |
* |
|
3534 |
* <p>A discussion of the use of dynamically typesafe views may be |
|
3535 |
* found in the documentation for the {@link #checkedCollection |
|
3536 |
* checkedCollection} method. |
|
3537 |
* |
|
3538 |
* <p>The returned map will be serializable if the specified map is |
|
3539 |
* serializable. |
|
3540 |
* |
|
3541 |
* <p>Since {@code null} is considered to be a value of any reference |
|
3542 |
* type, the returned map permits insertion of null keys or values |
|
3543 |
* whenever the backing map does. |
|
3544 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
3545 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
3546 |
* @param <V> the class of the map values |
2 | 3547 |
* @param m the map for which a dynamically typesafe view is to be |
3548 |
* returned |
|
3549 |
* @param keyType the type of key that {@code m} is permitted to hold |
|
3550 |
* @param valueType the type of value that {@code m} is permitted to hold |
|
3551 |
* @return a dynamically typesafe view of the specified map |
|
3552 |
* @since 1.5 |
|
3553 |
*/ |
|
3554 |
public static <K, V> Map<K, V> checkedMap(Map<K, V> m, |
|
3555 |
Class<K> keyType, |
|
3556 |
Class<V> valueType) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3557 |
return new CheckedMap<>(m, keyType, valueType); |
2 | 3558 |
} |
3559 |
||
3560 |
||
3561 |
/** |
|
3562 |
* @serial include |
|
3563 |
*/ |
|
3564 |
private static class CheckedMap<K,V> |
|
3565 |
implements Map<K,V>, Serializable |
|
3566 |
{ |
|
3567 |
private static final long serialVersionUID = 5742860141034234728L; |
|
3568 |
||
3569 |
private final Map<K, V> m; |
|
3570 |
final Class<K> keyType; |
|
3571 |
final Class<V> valueType; |
|
3572 |
||
3573 |
private void typeCheck(Object key, Object value) { |
|
3574 |
if (key != null && !keyType.isInstance(key)) |
|
3575 |
throw new ClassCastException(badKeyMsg(key)); |
|
3576 |
||
3577 |
if (value != null && !valueType.isInstance(value)) |
|
3578 |
throw new ClassCastException(badValueMsg(value)); |
|
3579 |
} |
|
3580 |
||
16867 | 3581 |
private BiFunction<? super K, ? super V, ? extends V> typeCheck( |
3582 |
BiFunction<? super K, ? super V, ? extends V> func) { |
|
3583 |
Objects.requireNonNull(func); |
|
3584 |
return (k, v) -> { |
|
3585 |
V newValue = func.apply(k, v); |
|
3586 |
typeCheck(k, newValue); |
|
3587 |
return newValue; |
|
3588 |
}; |
|
3589 |
} |
|
3590 |
||
2 | 3591 |
private String badKeyMsg(Object key) { |
3592 |
return "Attempt to insert " + key.getClass() + |
|
16867 | 3593 |
" key into map with key type " + keyType; |
2 | 3594 |
} |
3595 |
||
3596 |
private String badValueMsg(Object value) { |
|
3597 |
return "Attempt to insert " + value.getClass() + |
|
16867 | 3598 |
" value into map with value type " + valueType; |
2 | 3599 |
} |
3600 |
||
3601 |
CheckedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) { |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3602 |
this.m = Objects.requireNonNull(m); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3603 |
this.keyType = Objects.requireNonNull(keyType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3604 |
this.valueType = Objects.requireNonNull(valueType); |
2 | 3605 |
} |
3606 |
||
3607 |
public int size() { return m.size(); } |
|
3608 |
public boolean isEmpty() { return m.isEmpty(); } |
|
3609 |
public boolean containsKey(Object key) { return m.containsKey(key); } |
|
3610 |
public boolean containsValue(Object v) { return m.containsValue(v); } |
|
3611 |
public V get(Object key) { return m.get(key); } |
|
3612 |
public V remove(Object key) { return m.remove(key); } |
|
3613 |
public void clear() { m.clear(); } |
|
3614 |
public Set<K> keySet() { return m.keySet(); } |
|
3615 |
public Collection<V> values() { return m.values(); } |
|
3616 |
public boolean equals(Object o) { return o == this || m.equals(o); } |
|
3617 |
public int hashCode() { return m.hashCode(); } |
|
3618 |
public String toString() { return m.toString(); } |
|
3619 |
||
3620 |
public V put(K key, V value) { |
|
3621 |
typeCheck(key, value); |
|
3622 |
return m.put(key, value); |
|
3623 |
} |
|
3624 |
||
3625 |
@SuppressWarnings("unchecked") |
|
3626 |
public void putAll(Map<? extends K, ? extends V> t) { |
|
3627 |
// Satisfy the following goals: |
|
3628 |
// - good diagnostics in case of type mismatch |
|
3629 |
// - all-or-nothing semantics |
|
3630 |
// - protection from malicious t |
|
3631 |
// - correct behavior if t is a concurrent map |
|
3632 |
Object[] entries = t.entrySet().toArray(); |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3633 |
List<Map.Entry<K,V>> checked = new ArrayList<>(entries.length); |
2 | 3634 |
for (Object o : entries) { |
3635 |
Map.Entry<?,?> e = (Map.Entry<?,?>) o; |
|
3636 |
Object k = e.getKey(); |
|
3637 |
Object v = e.getValue(); |
|
3638 |
typeCheck(k, v); |
|
3639 |
checked.add( |
|
16867 | 3640 |
new AbstractMap.SimpleImmutableEntry<>((K)k, (V)v)); |
2 | 3641 |
} |
3642 |
for (Map.Entry<K,V> e : checked) |
|
3643 |
m.put(e.getKey(), e.getValue()); |
|
3644 |
} |
|
3645 |
||
3646 |
private transient Set<Map.Entry<K,V>> entrySet = null; |
|
3647 |
||
3648 |
public Set<Map.Entry<K,V>> entrySet() { |
|
3649 |
if (entrySet==null) |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3650 |
entrySet = new CheckedEntrySet<>(m.entrySet(), valueType); |
2 | 3651 |
return entrySet; |
3652 |
} |
|
3653 |
||
16867 | 3654 |
// Override default methods in Map |
3655 |
@Override |
|
3656 |
public void forEach(BiConsumer<? super K, ? super V> action) { |
|
3657 |
m.forEach(action); |
|
3658 |
} |
|
3659 |
||
3660 |
@Override |
|
3661 |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
|
3662 |
m.replaceAll(typeCheck(function)); |
|
3663 |
} |
|
3664 |
||
3665 |
@Override |
|
3666 |
public V putIfAbsent(K key, V value) { |
|
3667 |
typeCheck(key, value); |
|
3668 |
return m.putIfAbsent(key, value); |
|
3669 |
} |
|
3670 |
||
3671 |
@Override |
|
3672 |
public boolean remove(Object key, Object value) { |
|
3673 |
return m.remove(key, value); |
|
3674 |
} |
|
3675 |
||
3676 |
@Override |
|
3677 |
public boolean replace(K key, V oldValue, V newValue) { |
|
3678 |
typeCheck(key, newValue); |
|
3679 |
return m.replace(key, oldValue, newValue); |
|
3680 |
} |
|
3681 |
||
3682 |
@Override |
|
3683 |
public V replace(K key, V value) { |
|
3684 |
typeCheck(key, value); |
|
3685 |
return m.replace(key, value); |
|
3686 |
} |
|
3687 |
||
3688 |
@Override |
|
3689 |
public V computeIfAbsent(K key, |
|
3690 |
Function<? super K, ? extends V> mappingFunction) { |
|
3691 |
Objects.requireNonNull(mappingFunction); |
|
3692 |
return m.computeIfAbsent(key, k -> { |
|
3693 |
V value = mappingFunction.apply(k); |
|
3694 |
typeCheck(k, value); |
|
3695 |
return value; |
|
3696 |
}); |
|
3697 |
} |
|
3698 |
||
3699 |
@Override |
|
3700 |
public V computeIfPresent(K key, |
|
3701 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
3702 |
return m.computeIfPresent(key, typeCheck(remappingFunction)); |
|
3703 |
} |
|
3704 |
||
3705 |
@Override |
|
3706 |
public V compute(K key, |
|
3707 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
3708 |
return m.compute(key, typeCheck(remappingFunction)); |
|
3709 |
} |
|
3710 |
||
3711 |
@Override |
|
3712 |
public V merge(K key, V value, |
|
3713 |
BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
|
3714 |
Objects.requireNonNull(remappingFunction); |
|
3715 |
return m.merge(key, value, (v1, v2) -> { |
|
3716 |
V newValue = remappingFunction.apply(v1, v2); |
|
3717 |
typeCheck(null, newValue); |
|
3718 |
return newValue; |
|
3719 |
}); |
|
3720 |
} |
|
3721 |
||
2 | 3722 |
/** |
3723 |
* We need this class in addition to CheckedSet as Map.Entry permits |
|
3724 |
* modification of the backing Map via the setValue operation. This |
|
3725 |
* class is subtle: there are many possible attacks that must be |
|
3726 |
* thwarted. |
|
3727 |
* |
|
3728 |
* @serial exclude |
|
3729 |
*/ |
|
3730 |
static class CheckedEntrySet<K,V> implements Set<Map.Entry<K,V>> { |
|
3731 |
private final Set<Map.Entry<K,V>> s; |
|
3732 |
private final Class<V> valueType; |
|
3733 |
||
3734 |
CheckedEntrySet(Set<Map.Entry<K, V>> s, Class<V> valueType) { |
|
3735 |
this.s = s; |
|
3736 |
this.valueType = valueType; |
|
3737 |
} |
|
3738 |
||
3739 |
public int size() { return s.size(); } |
|
3740 |
public boolean isEmpty() { return s.isEmpty(); } |
|
3741 |
public String toString() { return s.toString(); } |
|
3742 |
public int hashCode() { return s.hashCode(); } |
|
3743 |
public void clear() { s.clear(); } |
|
3744 |
||
3745 |
public boolean add(Map.Entry<K, V> e) { |
|
3746 |
throw new UnsupportedOperationException(); |
|
3747 |
} |
|
3748 |
public boolean addAll(Collection<? extends Map.Entry<K, V>> coll) { |
|
3749 |
throw new UnsupportedOperationException(); |
|
3750 |
} |
|
3751 |
||
3752 |
public Iterator<Map.Entry<K,V>> iterator() { |
|
3753 |
final Iterator<Map.Entry<K, V>> i = s.iterator(); |
|
3754 |
final Class<V> valueType = this.valueType; |
|
3755 |
||
3756 |
return new Iterator<Map.Entry<K,V>>() { |
|
3757 |
public boolean hasNext() { return i.hasNext(); } |
|
3758 |
public void remove() { i.remove(); } |
|
3759 |
||
3760 |
public Map.Entry<K,V> next() { |
|
3761 |
return checkedEntry(i.next(), valueType); |
|
3762 |
} |
|
3763 |
}; |
|
3764 |
} |
|
3765 |
||
3766 |
@SuppressWarnings("unchecked") |
|
3767 |
public Object[] toArray() { |
|
3768 |
Object[] source = s.toArray(); |
|
3769 |
||
3770 |
/* |
|
3771 |
* Ensure that we don't get an ArrayStoreException even if |
|
3772 |
* s.toArray returns an array of something other than Object |
|
3773 |
*/ |
|
3774 |
Object[] dest = (CheckedEntry.class.isInstance( |
|
3775 |
source.getClass().getComponentType()) ? source : |
|
3776 |
new Object[source.length]); |
|
3777 |
||
3778 |
for (int i = 0; i < source.length; i++) |
|
3779 |
dest[i] = checkedEntry((Map.Entry<K,V>)source[i], |
|
3780 |
valueType); |
|
3781 |
return dest; |
|
3782 |
} |
|
3783 |
||
3784 |
@SuppressWarnings("unchecked") |
|
3785 |
public <T> T[] toArray(T[] a) { |
|
3786 |
// We don't pass a to s.toArray, to avoid window of |
|
3787 |
// vulnerability wherein an unscrupulous multithreaded client |
|
3788 |
// could get his hands on raw (unwrapped) Entries from s. |
|
3789 |
T[] arr = s.toArray(a.length==0 ? a : Arrays.copyOf(a, 0)); |
|
3790 |
||
3791 |
for (int i=0; i<arr.length; i++) |
|
3792 |
arr[i] = (T) checkedEntry((Map.Entry<K,V>)arr[i], |
|
3793 |
valueType); |
|
3794 |
if (arr.length > a.length) |
|
3795 |
return arr; |
|
3796 |
||
3797 |
System.arraycopy(arr, 0, a, 0, arr.length); |
|
3798 |
if (a.length > arr.length) |
|
3799 |
a[arr.length] = null; |
|
3800 |
return a; |
|
3801 |
} |
|
3802 |
||
3803 |
/** |
|
3804 |
* This method is overridden to protect the backing set against |
|
3805 |
* an object with a nefarious equals function that senses |
|
3806 |
* that the equality-candidate is Map.Entry and calls its |
|
3807 |
* setValue method. |
|
3808 |
*/ |
|
3809 |
public boolean contains(Object o) { |
|
3810 |
if (!(o instanceof Map.Entry)) |
|
3811 |
return false; |
|
3812 |
Map.Entry<?,?> e = (Map.Entry<?,?>) o; |
|
3813 |
return s.contains( |
|
3814 |
(e instanceof CheckedEntry) ? e : checkedEntry(e, valueType)); |
|
3815 |
} |
|
3816 |
||
3817 |
/** |
|
3818 |
* The bulk collection methods are overridden to protect |
|
3819 |
* against an unscrupulous collection whose contains(Object o) |
|
3820 |
* method senses when o is a Map.Entry, and calls o.setValue. |
|
3821 |
*/ |
|
3822 |
public boolean containsAll(Collection<?> c) { |
|
3823 |
for (Object o : c) |
|
3824 |
if (!contains(o)) // Invokes safe contains() above |
|
3825 |
return false; |
|
3826 |
return true; |
|
3827 |
} |
|
3828 |
||
3829 |
public boolean remove(Object o) { |
|
3830 |
if (!(o instanceof Map.Entry)) |
|
3831 |
return false; |
|
3832 |
return s.remove(new AbstractMap.SimpleImmutableEntry |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3833 |
<>((Map.Entry<?,?>)o)); |
2 | 3834 |
} |
3835 |
||
3836 |
public boolean removeAll(Collection<?> c) { |
|
3837 |
return batchRemove(c, false); |
|
3838 |
} |
|
3839 |
public boolean retainAll(Collection<?> c) { |
|
3840 |
return batchRemove(c, true); |
|
3841 |
} |
|
3842 |
private boolean batchRemove(Collection<?> c, boolean complement) { |
|
19855 | 3843 |
Objects.requireNonNull(c); |
2 | 3844 |
boolean modified = false; |
3845 |
Iterator<Map.Entry<K,V>> it = iterator(); |
|
3846 |
while (it.hasNext()) { |
|
3847 |
if (c.contains(it.next()) != complement) { |
|
3848 |
it.remove(); |
|
3849 |
modified = true; |
|
3850 |
} |
|
3851 |
} |
|
3852 |
return modified; |
|
3853 |
} |
|
3854 |
||
3855 |
public boolean equals(Object o) { |
|
3856 |
if (o == this) |
|
3857 |
return true; |
|
3858 |
if (!(o instanceof Set)) |
|
3859 |
return false; |
|
3860 |
Set<?> that = (Set<?>) o; |
|
3861 |
return that.size() == s.size() |
|
3862 |
&& containsAll(that); // Invokes safe containsAll() above |
|
3863 |
} |
|
3864 |
||
3865 |
static <K,V,T> CheckedEntry<K,V,T> checkedEntry(Map.Entry<K,V> e, |
|
3866 |
Class<T> valueType) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3867 |
return new CheckedEntry<>(e, valueType); |
2 | 3868 |
} |
3869 |
||
3870 |
/** |
|
3871 |
* This "wrapper class" serves two purposes: it prevents |
|
3872 |
* the client from modifying the backing Map, by short-circuiting |
|
3873 |
* the setValue method, and it protects the backing Map against |
|
3874 |
* an ill-behaved Map.Entry that attempts to modify another |
|
3875 |
* Map.Entry when asked to perform an equality check. |
|
3876 |
*/ |
|
3877 |
private static class CheckedEntry<K,V,T> implements Map.Entry<K,V> { |
|
3878 |
private final Map.Entry<K, V> e; |
|
3879 |
private final Class<T> valueType; |
|
3880 |
||
3881 |
CheckedEntry(Map.Entry<K, V> e, Class<T> valueType) { |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3882 |
this.e = Objects.requireNonNull(e); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3883 |
this.valueType = Objects.requireNonNull(valueType); |
2 | 3884 |
} |
3885 |
||
3886 |
public K getKey() { return e.getKey(); } |
|
3887 |
public V getValue() { return e.getValue(); } |
|
3888 |
public int hashCode() { return e.hashCode(); } |
|
3889 |
public String toString() { return e.toString(); } |
|
3890 |
||
3891 |
public V setValue(V value) { |
|
3892 |
if (value != null && !valueType.isInstance(value)) |
|
3893 |
throw new ClassCastException(badValueMsg(value)); |
|
3894 |
return e.setValue(value); |
|
3895 |
} |
|
3896 |
||
3897 |
private String badValueMsg(Object value) { |
|
3898 |
return "Attempt to insert " + value.getClass() + |
|
3899 |
" value into map with value type " + valueType; |
|
3900 |
} |
|
3901 |
||
3902 |
public boolean equals(Object o) { |
|
3903 |
if (o == this) |
|
3904 |
return true; |
|
3905 |
if (!(o instanceof Map.Entry)) |
|
3906 |
return false; |
|
3907 |
return e.equals(new AbstractMap.SimpleImmutableEntry |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3908 |
<>((Map.Entry<?,?>)o)); |
2 | 3909 |
} |
3910 |
} |
|
3911 |
} |
|
3912 |
} |
|
3913 |
||
3914 |
/** |
|
3915 |
* Returns a dynamically typesafe view of the specified sorted map. |
|
3916 |
* Any attempt to insert a mapping whose key or value have the wrong |
|
3917 |
* type will result in an immediate {@link ClassCastException}. |
|
3918 |
* Similarly, any attempt to modify the value currently associated with |
|
3919 |
* a key will result in an immediate {@link ClassCastException}, |
|
3920 |
* whether the modification is attempted directly through the map |
|
3921 |
* itself, or through a {@link Map.Entry} instance obtained from the |
|
3922 |
* map's {@link Map#entrySet() entry set} view. |
|
3923 |
* |
|
3924 |
* <p>Assuming a map contains no incorrectly typed keys or values |
|
3925 |
* prior to the time a dynamically typesafe view is generated, and |
|
3926 |
* that all subsequent access to the map takes place through the view |
|
3927 |
* (or one of its collection views), it is <i>guaranteed</i> that the |
|
3928 |
* map cannot contain an incorrectly typed key or value. |
|
3929 |
* |
|
3930 |
* <p>A discussion of the use of dynamically typesafe views may be |
|
3931 |
* found in the documentation for the {@link #checkedCollection |
|
3932 |
* checkedCollection} method. |
|
3933 |
* |
|
3934 |
* <p>The returned map will be serializable if the specified map is |
|
3935 |
* serializable. |
|
3936 |
* |
|
3937 |
* <p>Since {@code null} is considered to be a value of any reference |
|
3938 |
* type, the returned map permits insertion of null keys or values |
|
3939 |
* whenever the backing map does. |
|
3940 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
3941 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
3942 |
* @param <V> the class of the map values |
2 | 3943 |
* @param m the map for which a dynamically typesafe view is to be |
3944 |
* returned |
|
3945 |
* @param keyType the type of key that {@code m} is permitted to hold |
|
3946 |
* @param valueType the type of value that {@code m} is permitted to hold |
|
3947 |
* @return a dynamically typesafe view of the specified map |
|
3948 |
* @since 1.5 |
|
3949 |
*/ |
|
3950 |
public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K, V> m, |
|
3951 |
Class<K> keyType, |
|
3952 |
Class<V> valueType) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
3953 |
return new CheckedSortedMap<>(m, keyType, valueType); |
2 | 3954 |
} |
3955 |
||
3956 |
/** |
|
3957 |
* @serial include |
|
3958 |
*/ |
|
3959 |
static class CheckedSortedMap<K,V> extends CheckedMap<K,V> |
|
3960 |
implements SortedMap<K,V>, Serializable |
|
3961 |
{ |
|
3962 |
private static final long serialVersionUID = 1599671320688067438L; |
|
3963 |
||
3964 |
private final SortedMap<K, V> sm; |
|
3965 |
||
3966 |
CheckedSortedMap(SortedMap<K, V> m, |
|
3967 |
Class<K> keyType, Class<V> valueType) { |
|
3968 |
super(m, keyType, valueType); |
|
3969 |
sm = m; |
|
3970 |
} |
|
3971 |
||
3972 |
public Comparator<? super K> comparator() { return sm.comparator(); } |
|
3973 |
public K firstKey() { return sm.firstKey(); } |
|
3974 |
public K lastKey() { return sm.lastKey(); } |
|
3975 |
||
3976 |
public SortedMap<K,V> subMap(K fromKey, K toKey) { |
|
3977 |
return checkedSortedMap(sm.subMap(fromKey, toKey), |
|
3978 |
keyType, valueType); |
|
3979 |
} |
|
3980 |
public SortedMap<K,V> headMap(K toKey) { |
|
3981 |
return checkedSortedMap(sm.headMap(toKey), keyType, valueType); |
|
3982 |
} |
|
3983 |
public SortedMap<K,V> tailMap(K fromKey) { |
|
3984 |
return checkedSortedMap(sm.tailMap(fromKey), keyType, valueType); |
|
3985 |
} |
|
3986 |
} |
|
3987 |
||
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3988 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3989 |
* Returns a dynamically typesafe view of the specified navigable map. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3990 |
* Any attempt to insert a mapping whose key or value have the wrong |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3991 |
* type will result in an immediate {@link ClassCastException}. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3992 |
* Similarly, any attempt to modify the value currently associated with |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3993 |
* a key will result in an immediate {@link ClassCastException}, |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3994 |
* whether the modification is attempted directly through the map |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3995 |
* itself, or through a {@link Map.Entry} instance obtained from the |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3996 |
* map's {@link Map#entrySet() entry set} view. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3997 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3998 |
* <p>Assuming a map contains no incorrectly typed keys or values |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
3999 |
* prior to the time a dynamically typesafe view is generated, and |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4000 |
* that all subsequent access to the map takes place through the view |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4001 |
* (or one of its collection views), it is <em>guaranteed</em> that the |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4002 |
* map cannot contain an incorrectly typed key or value. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4003 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4004 |
* <p>A discussion of the use of dynamically typesafe views may be |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4005 |
* found in the documentation for the {@link #checkedCollection |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4006 |
* checkedCollection} method. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4007 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4008 |
* <p>The returned map will be serializable if the specified map is |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4009 |
* serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4010 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4011 |
* <p>Since {@code null} is considered to be a value of any reference |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4012 |
* type, the returned map permits insertion of null keys or values |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4013 |
* whenever the backing map does. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4014 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4015 |
* @param <K> type of map keys |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4016 |
* @param <V> type of map values |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4017 |
* @param m the map for which a dynamically typesafe view is to be |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4018 |
* returned |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4019 |
* @param keyType the type of key that {@code m} is permitted to hold |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4020 |
* @param valueType the type of value that {@code m} is permitted to hold |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4021 |
* @return a dynamically typesafe view of the specified map |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4022 |
* @since 1.8 |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4023 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4024 |
public static <K,V> NavigableMap<K,V> checkedNavigableMap(NavigableMap<K, V> m, |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4025 |
Class<K> keyType, |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4026 |
Class<V> valueType) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4027 |
return new CheckedNavigableMap<>(m, keyType, valueType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4028 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4029 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4030 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4031 |
* @serial include |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4032 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4033 |
static class CheckedNavigableMap<K,V> extends CheckedSortedMap<K,V> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4034 |
implements NavigableMap<K,V>, Serializable |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4035 |
{ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4036 |
private static final long serialVersionUID = -4852462692372534096L; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4037 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4038 |
private final NavigableMap<K, V> nm; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4039 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4040 |
CheckedNavigableMap(NavigableMap<K, V> m, |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4041 |
Class<K> keyType, Class<V> valueType) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4042 |
super(m, keyType, valueType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4043 |
nm = m; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4044 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4045 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4046 |
public Comparator<? super K> comparator() { return nm.comparator(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4047 |
public K firstKey() { return nm.firstKey(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4048 |
public K lastKey() { return nm.lastKey(); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4049 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4050 |
public Entry<K, V> lowerEntry(K key) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4051 |
Entry<K,V> lower = nm.lowerEntry(key); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4052 |
return (null != lower) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
4053 |
? new CheckedMap.CheckedEntrySet.CheckedEntry<>(lower, valueType) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4054 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4055 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4056 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4057 |
public K lowerKey(K key) { return nm.lowerKey(key); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4058 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4059 |
public Entry<K, V> floorEntry(K key) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4060 |
Entry<K,V> floor = nm.floorEntry(key); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4061 |
return (null != floor) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
4062 |
? new CheckedMap.CheckedEntrySet.CheckedEntry<>(floor, valueType) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4063 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4064 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4065 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4066 |
public K floorKey(K key) { return nm.floorKey(key); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4067 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4068 |
public Entry<K, V> ceilingEntry(K key) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4069 |
Entry<K,V> ceiling = nm.ceilingEntry(key); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4070 |
return (null != ceiling) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
4071 |
? new CheckedMap.CheckedEntrySet.CheckedEntry<>(ceiling, valueType) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4072 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4073 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4074 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4075 |
public K ceilingKey(K key) { return nm.ceilingKey(key); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4076 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4077 |
public Entry<K, V> higherEntry(K key) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4078 |
Entry<K,V> higher = nm.higherEntry(key); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4079 |
return (null != higher) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
4080 |
? new CheckedMap.CheckedEntrySet.CheckedEntry<>(higher, valueType) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4081 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4082 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4083 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4084 |
public K higherKey(K key) { return nm.higherKey(key); } |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4085 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4086 |
public Entry<K, V> firstEntry() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4087 |
Entry<K,V> first = nm.firstEntry(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4088 |
return (null != first) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
4089 |
? new CheckedMap.CheckedEntrySet.CheckedEntry<>(first, valueType) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4090 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4091 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4092 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4093 |
public Entry<K, V> lastEntry() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4094 |
Entry<K,V> last = nm.lastEntry(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4095 |
return (null != last) |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
4096 |
? new CheckedMap.CheckedEntrySet.CheckedEntry<>(last, valueType) |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4097 |
: null; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4098 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4099 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4100 |
public Entry<K, V> pollFirstEntry() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4101 |
Entry<K,V> entry = nm.pollFirstEntry(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4102 |
return (null == entry) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4103 |
? null |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
4104 |
: new CheckedMap.CheckedEntrySet.CheckedEntry<>(entry, valueType); |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4105 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4106 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4107 |
public Entry<K, V> pollLastEntry() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4108 |
Entry<K,V> entry = nm.pollLastEntry(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4109 |
return (null == entry) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4110 |
? null |
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18829
diff
changeset
|
4111 |
: new CheckedMap.CheckedEntrySet.CheckedEntry<>(entry, valueType); |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4112 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4113 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4114 |
public NavigableMap<K, V> descendingMap() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4115 |
return checkedNavigableMap(nm.descendingMap(), keyType, valueType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4116 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4117 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4118 |
public NavigableSet<K> keySet() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4119 |
return navigableKeySet(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4120 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4121 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4122 |
public NavigableSet<K> navigableKeySet() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4123 |
return checkedNavigableSet(nm.navigableKeySet(), keyType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4124 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4125 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4126 |
public NavigableSet<K> descendingKeySet() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4127 |
return checkedNavigableSet(nm.descendingKeySet(), keyType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4128 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4129 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4130 |
@Override |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4131 |
public NavigableMap<K,V> subMap(K fromKey, K toKey) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4132 |
return checkedNavigableMap(nm.subMap(fromKey, true, toKey, false), |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4133 |
keyType, valueType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4134 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4135 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4136 |
@Override |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4137 |
public NavigableMap<K,V> headMap(K toKey) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4138 |
return checkedNavigableMap(nm.headMap(toKey, false), keyType, valueType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4139 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4140 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4141 |
@Override |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4142 |
public NavigableMap<K,V> tailMap(K fromKey) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4143 |
return checkedNavigableMap(nm.tailMap(fromKey, true), keyType, valueType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4144 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4145 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4146 |
public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4147 |
return checkedNavigableMap(nm.subMap(fromKey, fromInclusive, toKey, toInclusive), keyType, valueType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4148 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4149 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4150 |
public NavigableMap<K, V> headMap(K toKey, boolean inclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4151 |
return checkedNavigableMap(nm.headMap(toKey, inclusive), keyType, valueType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4152 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4153 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4154 |
public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4155 |
return checkedNavigableMap(nm.tailMap(fromKey, inclusive), keyType, valueType); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4156 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4157 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4158 |
|
2 | 4159 |
// Empty collections |
4160 |
||
4161 |
/** |
|
4162 |
* Returns an iterator that has no elements. More precisely, |
|
4163 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4164 |
* <ul> |
2 | 4165 |
* <li>{@link Iterator#hasNext hasNext} always returns {@code |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4166 |
* false}.</li> |
2 | 4167 |
* <li>{@link Iterator#next next} always throws {@link |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4168 |
* NoSuchElementException}.</li> |
2 | 4169 |
* <li>{@link Iterator#remove remove} always throws {@link |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4170 |
* IllegalStateException}.</li> |
2 | 4171 |
* </ul> |
4172 |
* |
|
4173 |
* <p>Implementations of this method are permitted, but not |
|
4174 |
* required, to return the same object from multiple invocations. |
|
4175 |
* |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4176 |
* @param <T> type of elements, if there were any, in the iterator |
2 | 4177 |
* @return an empty iterator |
4178 |
* @since 1.7 |
|
4179 |
*/ |
|
4180 |
@SuppressWarnings("unchecked") |
|
4181 |
public static <T> Iterator<T> emptyIterator() { |
|
4182 |
return (Iterator<T>) EmptyIterator.EMPTY_ITERATOR; |
|
4183 |
} |
|
4184 |
||
4185 |
private static class EmptyIterator<E> implements Iterator<E> { |
|
4186 |
static final EmptyIterator<Object> EMPTY_ITERATOR |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4187 |
= new EmptyIterator<>(); |
2 | 4188 |
|
4189 |
public boolean hasNext() { return false; } |
|
4190 |
public E next() { throw new NoSuchElementException(); } |
|
4191 |
public void remove() { throw new IllegalStateException(); } |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4192 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4193 |
public void forEachRemaining(Consumer<? super E> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4194 |
Objects.requireNonNull(action); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4195 |
} |
2 | 4196 |
} |
4197 |
||
4198 |
/** |
|
4199 |
* Returns a list iterator that has no elements. More precisely, |
|
4200 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4201 |
* <ul> |
2 | 4202 |
* <li>{@link Iterator#hasNext hasNext} and {@link |
4203 |
* ListIterator#hasPrevious hasPrevious} always return {@code |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4204 |
* false}.</li> |
2 | 4205 |
* <li>{@link Iterator#next next} and {@link ListIterator#previous |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4206 |
* previous} always throw {@link NoSuchElementException}.</li> |
2 | 4207 |
* <li>{@link Iterator#remove remove} and {@link ListIterator#set |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4208 |
* set} always throw {@link IllegalStateException}.</li> |
2 | 4209 |
* <li>{@link ListIterator#add add} always throws {@link |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4210 |
* UnsupportedOperationException}.</li> |
2 | 4211 |
* <li>{@link ListIterator#nextIndex nextIndex} always returns |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4212 |
* {@code 0}.</li> |
2 | 4213 |
* <li>{@link ListIterator#previousIndex previousIndex} always |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4214 |
* returns {@code -1}.</li> |
2 | 4215 |
* </ul> |
4216 |
* |
|
4217 |
* <p>Implementations of this method are permitted, but not |
|
4218 |
* required, to return the same object from multiple invocations. |
|
4219 |
* |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4220 |
* @param <T> type of elements, if there were any, in the iterator |
2 | 4221 |
* @return an empty list iterator |
4222 |
* @since 1.7 |
|
4223 |
*/ |
|
4224 |
@SuppressWarnings("unchecked") |
|
4225 |
public static <T> ListIterator<T> emptyListIterator() { |
|
4226 |
return (ListIterator<T>) EmptyListIterator.EMPTY_ITERATOR; |
|
4227 |
} |
|
4228 |
||
4229 |
private static class EmptyListIterator<E> |
|
4230 |
extends EmptyIterator<E> |
|
4231 |
implements ListIterator<E> |
|
4232 |
{ |
|
4233 |
static final EmptyListIterator<Object> EMPTY_ITERATOR |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4234 |
= new EmptyListIterator<>(); |
2 | 4235 |
|
4236 |
public boolean hasPrevious() { return false; } |
|
4237 |
public E previous() { throw new NoSuchElementException(); } |
|
4238 |
public int nextIndex() { return 0; } |
|
4239 |
public int previousIndex() { return -1; } |
|
4240 |
public void set(E e) { throw new IllegalStateException(); } |
|
4241 |
public void add(E e) { throw new UnsupportedOperationException(); } |
|
4242 |
} |
|
4243 |
||
4244 |
/** |
|
4245 |
* Returns an enumeration that has no elements. More precisely, |
|
4246 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4247 |
* <ul> |
2 | 4248 |
* <li>{@link Enumeration#hasMoreElements hasMoreElements} always |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4249 |
* returns {@code false}.</li> |
2 | 4250 |
* <li> {@link Enumeration#nextElement nextElement} always throws |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4251 |
* {@link NoSuchElementException}.</li> |
2 | 4252 |
* </ul> |
4253 |
* |
|
4254 |
* <p>Implementations of this method are permitted, but not |
|
4255 |
* required, to return the same object from multiple invocations. |
|
4256 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4257 |
* @param <T> the class of the objects in the enumeration |
2 | 4258 |
* @return an empty enumeration |
4259 |
* @since 1.7 |
|
4260 |
*/ |
|
4261 |
@SuppressWarnings("unchecked") |
|
4262 |
public static <T> Enumeration<T> emptyEnumeration() { |
|
4263 |
return (Enumeration<T>) EmptyEnumeration.EMPTY_ENUMERATION; |
|
4264 |
} |
|
4265 |
||
4266 |
private static class EmptyEnumeration<E> implements Enumeration<E> { |
|
4267 |
static final EmptyEnumeration<Object> EMPTY_ENUMERATION |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4268 |
= new EmptyEnumeration<>(); |
2 | 4269 |
|
4270 |
public boolean hasMoreElements() { return false; } |
|
4271 |
public E nextElement() { throw new NoSuchElementException(); } |
|
4272 |
} |
|
4273 |
||
4274 |
/** |
|
4275 |
* The empty set (immutable). This set is serializable. |
|
4276 |
* |
|
4277 |
* @see #emptySet() |
|
4278 |
*/ |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
4279 |
@SuppressWarnings("rawtypes") |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4280 |
public static final Set EMPTY_SET = new EmptySet<>(); |
2 | 4281 |
|
4282 |
/** |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4283 |
* Returns an empty set (immutable). This set is serializable. |
2 | 4284 |
* Unlike the like-named field, this method is parameterized. |
4285 |
* |
|
4286 |
* <p>This example illustrates the type-safe way to obtain an empty set: |
|
4287 |
* <pre> |
|
4288 |
* Set<String> s = Collections.emptySet(); |
|
4289 |
* </pre> |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4290 |
* @implNote Implementations of this method need not create a separate |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4291 |
* {@code Set} object for each call. Using this method is likely to have |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4292 |
* comparable cost to using the like-named field. (Unlike this method, the |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4293 |
* field does not provide type safety.) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4294 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4295 |
* @param <T> the class of the objects in the set |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4296 |
* @return the empty set |
2 | 4297 |
* |
4298 |
* @see #EMPTY_SET |
|
4299 |
* @since 1.5 |
|
4300 |
*/ |
|
4301 |
@SuppressWarnings("unchecked") |
|
4302 |
public static final <T> Set<T> emptySet() { |
|
4303 |
return (Set<T>) EMPTY_SET; |
|
4304 |
} |
|
4305 |
||
4306 |
/** |
|
4307 |
* @serial include |
|
4308 |
*/ |
|
4309 |
private static class EmptySet<E> |
|
4310 |
extends AbstractSet<E> |
|
4311 |
implements Serializable |
|
4312 |
{ |
|
4313 |
private static final long serialVersionUID = 1582296315990362920L; |
|
4314 |
||
4315 |
public Iterator<E> iterator() { return emptyIterator(); } |
|
4316 |
||
4317 |
public int size() {return 0;} |
|
4318 |
public boolean isEmpty() {return true;} |
|
4319 |
||
4320 |
public boolean contains(Object obj) {return false;} |
|
4321 |
public boolean containsAll(Collection<?> c) { return c.isEmpty(); } |
|
4322 |
||
4323 |
public Object[] toArray() { return new Object[0]; } |
|
4324 |
||
4325 |
public <T> T[] toArray(T[] a) { |
|
4326 |
if (a.length > 0) |
|
4327 |
a[0] = null; |
|
4328 |
return a; |
|
4329 |
} |
|
4330 |
||
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4331 |
// Override default methods in Collection |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4332 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4333 |
public void forEach(Consumer<? super E> action) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4334 |
Objects.requireNonNull(action); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4335 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4336 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4337 |
public boolean removeIf(Predicate<? super E> filter) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4338 |
Objects.requireNonNull(filter); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4339 |
return false; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4340 |
} |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4341 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4342 |
public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); } |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4343 |
|
2 | 4344 |
// Preserves singleton property |
4345 |
private Object readResolve() { |
|
4346 |
return EMPTY_SET; |
|
4347 |
} |
|
4348 |
} |
|
4349 |
||
4350 |
/** |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4351 |
* Returns an empty sorted set (immutable). This set is serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4352 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4353 |
* <p>This example illustrates the type-safe way to obtain an empty |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4354 |
* sorted set: |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4355 |
* <pre> {@code |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4356 |
* SortedSet<String> s = Collections.emptySortedSet(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4357 |
* }</pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4358 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4359 |
* @implNote Implementations of this method need not create a separate |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4360 |
* {@code SortedSet} object for each call. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4361 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4362 |
* @param <E> type of elements, if there were any, in the set |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4363 |
* @return the empty sorted set |
10896 | 4364 |
* @since 1.8 |
4365 |
*/ |
|
4366 |
@SuppressWarnings("unchecked") |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4367 |
public static <E> SortedSet<E> emptySortedSet() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4368 |
return (SortedSet<E>) UnmodifiableNavigableSet.EMPTY_NAVIGABLE_SET; |
10896 | 4369 |
} |
4370 |
||
4371 |
/** |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4372 |
* Returns an empty navigable set (immutable). This set is serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4373 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4374 |
* <p>This example illustrates the type-safe way to obtain an empty |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4375 |
* navigable set: |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4376 |
* <pre> {@code |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4377 |
* NavigableSet<String> s = Collections.emptyNavigableSet(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4378 |
* }</pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4379 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4380 |
* @implNote Implementations of this method need not |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4381 |
* create a separate {@code NavigableSet} object for each call. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4382 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4383 |
* @param <E> type of elements, if there were any, in the set |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4384 |
* @return the empty navigable set |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4385 |
* @since 1.8 |
10896 | 4386 |
*/ |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4387 |
@SuppressWarnings("unchecked") |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4388 |
public static <E> NavigableSet<E> emptyNavigableSet() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4389 |
return (NavigableSet<E>) UnmodifiableNavigableSet.EMPTY_NAVIGABLE_SET; |
10896 | 4390 |
} |
4391 |
||
4392 |
/** |
|
2 | 4393 |
* The empty list (immutable). This list is serializable. |
4394 |
* |
|
4395 |
* @see #emptyList() |
|
4396 |
*/ |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
4397 |
@SuppressWarnings("rawtypes") |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4398 |
public static final List EMPTY_LIST = new EmptyList<>(); |
2 | 4399 |
|
4400 |
/** |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4401 |
* Returns an empty list (immutable). This list is serializable. |
2 | 4402 |
* |
4403 |
* <p>This example illustrates the type-safe way to obtain an empty list: |
|
4404 |
* <pre> |
|
4405 |
* List<String> s = Collections.emptyList(); |
|
4406 |
* </pre> |
|
22285
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
4407 |
* |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
4408 |
* @implNote |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
4409 |
* Implementations of this method need not create a separate <tt>List</tt> |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
4410 |
* object for each call. Using this method is likely to have comparable |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
4411 |
* cost to using the like-named field. (Unlike this method, the field does |
6c67737a5ac0
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents:
22078
diff
changeset
|
4412 |
* not provide type safety.) |
2 | 4413 |
* |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4414 |
* @param <T> type of elements, if there were any, in the list |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4415 |
* @return an empty immutable list |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4416 |
* |
2 | 4417 |
* @see #EMPTY_LIST |
4418 |
* @since 1.5 |
|
4419 |
*/ |
|
4420 |
@SuppressWarnings("unchecked") |
|
4421 |
public static final <T> List<T> emptyList() { |
|
4422 |
return (List<T>) EMPTY_LIST; |
|
4423 |
} |
|
4424 |
||
4425 |
/** |
|
4426 |
* @serial include |
|
4427 |
*/ |
|
4428 |
private static class EmptyList<E> |
|
4429 |
extends AbstractList<E> |
|
4430 |
implements RandomAccess, Serializable { |
|
4431 |
private static final long serialVersionUID = 8842843931221139166L; |
|
4432 |
||
4433 |
public Iterator<E> iterator() { |
|
4434 |
return emptyIterator(); |
|
4435 |
} |
|
4436 |
public ListIterator<E> listIterator() { |
|
4437 |
return emptyListIterator(); |
|
4438 |
} |
|
4439 |
||
4440 |
public int size() {return 0;} |
|
4441 |
public boolean isEmpty() {return true;} |
|
4442 |
||
4443 |
public boolean contains(Object obj) {return false;} |
|
4444 |
public boolean containsAll(Collection<?> c) { return c.isEmpty(); } |
|
4445 |
||
4446 |
public Object[] toArray() { return new Object[0]; } |
|
4447 |
||
4448 |
public <T> T[] toArray(T[] a) { |
|
4449 |
if (a.length > 0) |
|
4450 |
a[0] = null; |
|
4451 |
return a; |
|
4452 |
} |
|
4453 |
||
4454 |
public E get(int index) { |
|
4455 |
throw new IndexOutOfBoundsException("Index: "+index); |
|
4456 |
} |
|
4457 |
||
4458 |
public boolean equals(Object o) { |
|
4459 |
return (o instanceof List) && ((List<?>)o).isEmpty(); |
|
4460 |
} |
|
4461 |
||
4462 |
public int hashCode() { return 1; } |
|
4463 |
||
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4464 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4465 |
public boolean removeIf(Predicate<? super E> filter) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4466 |
Objects.requireNonNull(filter); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4467 |
return false; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4468 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4469 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4470 |
public void replaceAll(UnaryOperator<E> operator) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4471 |
Objects.requireNonNull(operator); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4472 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4473 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4474 |
public void sort(Comparator<? super E> c) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4475 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4476 |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4477 |
// Override default methods in Collection |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4478 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4479 |
public void forEach(Consumer<? super E> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4480 |
Objects.requireNonNull(action); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4481 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4482 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4483 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4484 |
public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); } |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4485 |
|
2 | 4486 |
// Preserves singleton property |
4487 |
private Object readResolve() { |
|
4488 |
return EMPTY_LIST; |
|
4489 |
} |
|
4490 |
} |
|
4491 |
||
4492 |
/** |
|
4493 |
* The empty map (immutable). This map is serializable. |
|
4494 |
* |
|
4495 |
* @see #emptyMap() |
|
4496 |
* @since 1.3 |
|
4497 |
*/ |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
4498 |
@SuppressWarnings("rawtypes") |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4499 |
public static final Map EMPTY_MAP = new EmptyMap<>(); |
2 | 4500 |
|
4501 |
/** |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4502 |
* Returns an empty map (immutable). This map is serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4503 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4504 |
* <p>This example illustrates the type-safe way to obtain an empty map: |
2 | 4505 |
* <pre> |
4506 |
* Map<String, Date> s = Collections.emptyMap(); |
|
4507 |
* </pre> |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4508 |
* @implNote Implementations of this method need not create a separate |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4509 |
* {@code Map} object for each call. Using this method is likely to have |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4510 |
* comparable cost to using the like-named field. (Unlike this method, the |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4511 |
* field does not provide type safety.) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4512 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4513 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4514 |
* @param <V> the class of the map values |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4515 |
* @return an empty map |
2 | 4516 |
* @see #EMPTY_MAP |
4517 |
* @since 1.5 |
|
4518 |
*/ |
|
4519 |
@SuppressWarnings("unchecked") |
|
4520 |
public static final <K,V> Map<K,V> emptyMap() { |
|
4521 |
return (Map<K,V>) EMPTY_MAP; |
|
4522 |
} |
|
4523 |
||
4524 |
/** |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4525 |
* Returns an empty sorted map (immutable). This map is serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4526 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4527 |
* <p>This example illustrates the type-safe way to obtain an empty map: |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4528 |
* <pre> {@code |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4529 |
* SortedMap<String, Date> s = Collections.emptySortedMap(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4530 |
* }</pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4531 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4532 |
* @implNote Implementations of this method need not create a separate |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4533 |
* {@code SortedMap} object for each call. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4534 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4535 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4536 |
* @param <V> the class of the map values |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4537 |
* @return an empty sorted map |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4538 |
* @since 1.8 |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4539 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4540 |
@SuppressWarnings("unchecked") |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4541 |
public static final <K,V> SortedMap<K,V> emptySortedMap() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4542 |
return (SortedMap<K,V>) UnmodifiableNavigableMap.EMPTY_NAVIGABLE_MAP; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4543 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4544 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4545 |
/** |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4546 |
* Returns an empty navigable map (immutable). This map is serializable. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4547 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4548 |
* <p>This example illustrates the type-safe way to obtain an empty map: |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4549 |
* <pre> {@code |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4550 |
* NavigableMap<String, Date> s = Collections.emptyNavigableMap(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4551 |
* }</pre> |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4552 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4553 |
* @implNote Implementations of this method need not create a separate |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4554 |
* {@code NavigableMap} object for each call. |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4555 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4556 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4557 |
* @param <V> the class of the map values |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4558 |
* @return an empty navigable map |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4559 |
* @since 1.8 |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4560 |
*/ |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4561 |
@SuppressWarnings("unchecked") |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4562 |
public static final <K,V> NavigableMap<K,V> emptyNavigableMap() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4563 |
return (NavigableMap<K,V>) UnmodifiableNavigableMap.EMPTY_NAVIGABLE_MAP; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4564 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4565 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4566 |
/** |
2 | 4567 |
* @serial include |
4568 |
*/ |
|
4569 |
private static class EmptyMap<K,V> |
|
4570 |
extends AbstractMap<K,V> |
|
4571 |
implements Serializable |
|
4572 |
{ |
|
4573 |
private static final long serialVersionUID = 6428348081105594320L; |
|
4574 |
||
4575 |
public int size() {return 0;} |
|
4576 |
public boolean isEmpty() {return true;} |
|
4577 |
public boolean containsKey(Object key) {return false;} |
|
4578 |
public boolean containsValue(Object value) {return false;} |
|
4579 |
public V get(Object key) {return null;} |
|
4580 |
public Set<K> keySet() {return emptySet();} |
|
4581 |
public Collection<V> values() {return emptySet();} |
|
4582 |
public Set<Map.Entry<K,V>> entrySet() {return emptySet();} |
|
4583 |
||
4584 |
public boolean equals(Object o) { |
|
4585 |
return (o instanceof Map) && ((Map<?,?>)o).isEmpty(); |
|
4586 |
} |
|
4587 |
||
4588 |
public int hashCode() {return 0;} |
|
4589 |
||
16867 | 4590 |
// Override default methods in Map |
4591 |
@Override |
|
4592 |
@SuppressWarnings("unchecked") |
|
4593 |
public V getOrDefault(Object k, V defaultValue) { |
|
4594 |
return defaultValue; |
|
4595 |
} |
|
4596 |
||
4597 |
@Override |
|
4598 |
public void forEach(BiConsumer<? super K, ? super V> action) { |
|
4599 |
Objects.requireNonNull(action); |
|
4600 |
} |
|
4601 |
||
4602 |
@Override |
|
4603 |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
|
4604 |
Objects.requireNonNull(function); |
|
4605 |
} |
|
4606 |
||
4607 |
@Override |
|
4608 |
public V putIfAbsent(K key, V value) { |
|
4609 |
throw new UnsupportedOperationException(); |
|
4610 |
} |
|
4611 |
||
4612 |
@Override |
|
4613 |
public boolean remove(Object key, Object value) { |
|
4614 |
throw new UnsupportedOperationException(); |
|
4615 |
} |
|
4616 |
||
4617 |
@Override |
|
4618 |
public boolean replace(K key, V oldValue, V newValue) { |
|
4619 |
throw new UnsupportedOperationException(); |
|
4620 |
} |
|
4621 |
||
4622 |
@Override |
|
4623 |
public V replace(K key, V value) { |
|
4624 |
throw new UnsupportedOperationException(); |
|
4625 |
} |
|
4626 |
||
4627 |
@Override |
|
4628 |
public V computeIfAbsent(K key, |
|
4629 |
Function<? super K, ? extends V> mappingFunction) { |
|
4630 |
throw new UnsupportedOperationException(); |
|
4631 |
} |
|
4632 |
||
4633 |
@Override |
|
4634 |
public V computeIfPresent(K key, |
|
4635 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
4636 |
throw new UnsupportedOperationException(); |
|
4637 |
} |
|
4638 |
||
4639 |
@Override |
|
4640 |
public V compute(K key, |
|
4641 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
4642 |
throw new UnsupportedOperationException(); |
|
4643 |
} |
|
4644 |
||
4645 |
@Override |
|
4646 |
public V merge(K key, V value, |
|
4647 |
BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
|
4648 |
throw new UnsupportedOperationException(); |
|
4649 |
} |
|
4650 |
||
2 | 4651 |
// Preserves singleton property |
4652 |
private Object readResolve() { |
|
4653 |
return EMPTY_MAP; |
|
4654 |
} |
|
4655 |
} |
|
4656 |
||
4657 |
// Singleton collections |
|
4658 |
||
4659 |
/** |
|
4660 |
* Returns an immutable set containing only the specified object. |
|
4661 |
* The returned set is serializable. |
|
4662 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4663 |
* @param <T> the class of the objects in the set |
2 | 4664 |
* @param o the sole object to be stored in the returned set. |
4665 |
* @return an immutable set containing only the specified object. |
|
4666 |
*/ |
|
4667 |
public static <T> Set<T> singleton(T o) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4668 |
return new SingletonSet<>(o); |
2 | 4669 |
} |
4670 |
||
4671 |
static <E> Iterator<E> singletonIterator(final E e) { |
|
4672 |
return new Iterator<E>() { |
|
4673 |
private boolean hasNext = true; |
|
4674 |
public boolean hasNext() { |
|
4675 |
return hasNext; |
|
4676 |
} |
|
4677 |
public E next() { |
|
4678 |
if (hasNext) { |
|
4679 |
hasNext = false; |
|
4680 |
return e; |
|
4681 |
} |
|
4682 |
throw new NoSuchElementException(); |
|
4683 |
} |
|
4684 |
public void remove() { |
|
4685 |
throw new UnsupportedOperationException(); |
|
4686 |
} |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4687 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4688 |
public void forEachRemaining(Consumer<? super E> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4689 |
Objects.requireNonNull(action); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4690 |
if (hasNext) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4691 |
action.accept(e); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4692 |
hasNext = false; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4693 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4694 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4695 |
}; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4696 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4697 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4698 |
/** |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4699 |
* Creates a {@code Spliterator} with only the specified element |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4700 |
* |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4701 |
* @param <T> Type of elements |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4702 |
* @return A singleton {@code Spliterator} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4703 |
*/ |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4704 |
static <T> Spliterator<T> singletonSpliterator(final T element) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4705 |
return new Spliterator<T>() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4706 |
long est = 1; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4707 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4708 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4709 |
public Spliterator<T> trySplit() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4710 |
return null; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4711 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4712 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4713 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4714 |
public boolean tryAdvance(Consumer<? super T> consumer) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4715 |
Objects.requireNonNull(consumer); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4716 |
if (est > 0) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4717 |
est--; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4718 |
consumer.accept(element); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4719 |
return true; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4720 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4721 |
return false; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4722 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4723 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4724 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4725 |
public void forEachRemaining(Consumer<? super T> consumer) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4726 |
tryAdvance(consumer); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4727 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4728 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4729 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4730 |
public long estimateSize() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4731 |
return est; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4732 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4733 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4734 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4735 |
public int characteristics() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4736 |
int value = (element != null) ? Spliterator.NONNULL : 0; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4737 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4738 |
return value | Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE | |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4739 |
Spliterator.DISTINCT | Spliterator.ORDERED; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4740 |
} |
2 | 4741 |
}; |
4742 |
} |
|
4743 |
||
4744 |
/** |
|
4745 |
* @serial include |
|
4746 |
*/ |
|
4747 |
private static class SingletonSet<E> |
|
4748 |
extends AbstractSet<E> |
|
4749 |
implements Serializable |
|
4750 |
{ |
|
4751 |
private static final long serialVersionUID = 3193687207550431679L; |
|
4752 |
||
7518 | 4753 |
private final E element; |
2 | 4754 |
|
4755 |
SingletonSet(E e) {element = e;} |
|
4756 |
||
4757 |
public Iterator<E> iterator() { |
|
4758 |
return singletonIterator(element); |
|
4759 |
} |
|
4760 |
||
4761 |
public int size() {return 1;} |
|
4762 |
||
4763 |
public boolean contains(Object o) {return eq(o, element);} |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4764 |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4765 |
// Override default methods for Collection |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4766 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4767 |
public void forEach(Consumer<? super E> action) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4768 |
action.accept(element); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4769 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4770 |
@Override |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4771 |
public Spliterator<E> spliterator() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4772 |
return singletonSpliterator(element); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4773 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4774 |
@Override |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4775 |
public boolean removeIf(Predicate<? super E> filter) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4776 |
throw new UnsupportedOperationException(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4777 |
} |
2 | 4778 |
} |
4779 |
||
4780 |
/** |
|
4781 |
* Returns an immutable list containing only the specified object. |
|
4782 |
* The returned list is serializable. |
|
4783 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4784 |
* @param <T> the class of the objects in the list |
2 | 4785 |
* @param o the sole object to be stored in the returned list. |
4786 |
* @return an immutable list containing only the specified object. |
|
4787 |
* @since 1.3 |
|
4788 |
*/ |
|
4789 |
public static <T> List<T> singletonList(T o) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4790 |
return new SingletonList<>(o); |
2 | 4791 |
} |
4792 |
||
4793 |
/** |
|
4794 |
* @serial include |
|
4795 |
*/ |
|
4796 |
private static class SingletonList<E> |
|
4797 |
extends AbstractList<E> |
|
4798 |
implements RandomAccess, Serializable { |
|
4799 |
||
4800 |
private static final long serialVersionUID = 3093736618740652951L; |
|
4801 |
||
4802 |
private final E element; |
|
4803 |
||
4804 |
SingletonList(E obj) {element = obj;} |
|
4805 |
||
4806 |
public Iterator<E> iterator() { |
|
4807 |
return singletonIterator(element); |
|
4808 |
} |
|
4809 |
||
4810 |
public int size() {return 1;} |
|
4811 |
||
4812 |
public boolean contains(Object obj) {return eq(obj, element);} |
|
4813 |
||
4814 |
public E get(int index) { |
|
4815 |
if (index != 0) |
|
4816 |
throw new IndexOutOfBoundsException("Index: "+index+", Size: 1"); |
|
4817 |
return element; |
|
4818 |
} |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4819 |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4820 |
// Override default methods for Collection |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4821 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4822 |
public void forEach(Consumer<? super E> action) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4823 |
action.accept(element); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4824 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4825 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4826 |
public boolean removeIf(Predicate<? super E> filter) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4827 |
throw new UnsupportedOperationException(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4828 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4829 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4830 |
public void replaceAll(UnaryOperator<E> operator) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4831 |
throw new UnsupportedOperationException(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4832 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4833 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4834 |
public void sort(Comparator<? super E> c) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
4835 |
} |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4836 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4837 |
public Spliterator<E> spliterator() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4838 |
return singletonSpliterator(element); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
4839 |
} |
2 | 4840 |
} |
4841 |
||
4842 |
/** |
|
4843 |
* Returns an immutable map, mapping only the specified key to the |
|
4844 |
* specified value. The returned map is serializable. |
|
4845 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4846 |
* @param <K> the class of the map keys |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4847 |
* @param <V> the class of the map values |
2 | 4848 |
* @param key the sole key to be stored in the returned map. |
4849 |
* @param value the value to which the returned map maps <tt>key</tt>. |
|
4850 |
* @return an immutable map containing only the specified key-value |
|
4851 |
* mapping. |
|
4852 |
* @since 1.3 |
|
4853 |
*/ |
|
4854 |
public static <K,V> Map<K,V> singletonMap(K key, V value) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4855 |
return new SingletonMap<>(key, value); |
2 | 4856 |
} |
4857 |
||
4858 |
/** |
|
4859 |
* @serial include |
|
4860 |
*/ |
|
4861 |
private static class SingletonMap<K,V> |
|
4862 |
extends AbstractMap<K,V> |
|
4863 |
implements Serializable { |
|
4864 |
private static final long serialVersionUID = -6979724477215052911L; |
|
4865 |
||
4866 |
private final K k; |
|
4867 |
private final V v; |
|
4868 |
||
4869 |
SingletonMap(K key, V value) { |
|
4870 |
k = key; |
|
4871 |
v = value; |
|
4872 |
} |
|
4873 |
||
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4874 |
public int size() {return 1;} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4875 |
public boolean isEmpty() {return false;} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4876 |
public boolean containsKey(Object key) {return eq(key, k);} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4877 |
public boolean containsValue(Object value) {return eq(value, v);} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
4878 |
public V get(Object key) {return (eq(key, k) ? v : null);} |
2 | 4879 |
|
4880 |
private transient Set<K> keySet = null; |
|
4881 |
private transient Set<Map.Entry<K,V>> entrySet = null; |
|
4882 |
private transient Collection<V> values = null; |
|
4883 |
||
4884 |
public Set<K> keySet() { |
|
4885 |
if (keySet==null) |
|
4886 |
keySet = singleton(k); |
|
4887 |
return keySet; |
|
4888 |
} |
|
4889 |
||
4890 |
public Set<Map.Entry<K,V>> entrySet() { |
|
4891 |
if (entrySet==null) |
|
4892 |
entrySet = Collections.<Map.Entry<K,V>>singleton( |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4893 |
new SimpleImmutableEntry<>(k, v)); |
2 | 4894 |
return entrySet; |
4895 |
} |
|
4896 |
||
4897 |
public Collection<V> values() { |
|
4898 |
if (values==null) |
|
4899 |
values = singleton(v); |
|
4900 |
return values; |
|
4901 |
} |
|
4902 |
||
16867 | 4903 |
// Override default methods in Map |
4904 |
@Override |
|
4905 |
public V getOrDefault(Object key, V defaultValue) { |
|
4906 |
return eq(key, k) ? v : defaultValue; |
|
4907 |
} |
|
4908 |
||
4909 |
@Override |
|
4910 |
public void forEach(BiConsumer<? super K, ? super V> action) { |
|
4911 |
action.accept(k, v); |
|
4912 |
} |
|
4913 |
||
4914 |
@Override |
|
4915 |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
|
4916 |
throw new UnsupportedOperationException(); |
|
4917 |
} |
|
4918 |
||
4919 |
@Override |
|
4920 |
public V putIfAbsent(K key, V value) { |
|
4921 |
throw new UnsupportedOperationException(); |
|
4922 |
} |
|
4923 |
||
4924 |
@Override |
|
4925 |
public boolean remove(Object key, Object value) { |
|
4926 |
throw new UnsupportedOperationException(); |
|
4927 |
} |
|
4928 |
||
4929 |
@Override |
|
4930 |
public boolean replace(K key, V oldValue, V newValue) { |
|
4931 |
throw new UnsupportedOperationException(); |
|
4932 |
} |
|
4933 |
||
4934 |
@Override |
|
4935 |
public V replace(K key, V value) { |
|
4936 |
throw new UnsupportedOperationException(); |
|
4937 |
} |
|
4938 |
||
4939 |
@Override |
|
4940 |
public V computeIfAbsent(K key, |
|
4941 |
Function<? super K, ? extends V> mappingFunction) { |
|
4942 |
throw new UnsupportedOperationException(); |
|
4943 |
} |
|
4944 |
||
4945 |
@Override |
|
4946 |
public V computeIfPresent(K key, |
|
4947 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
4948 |
throw new UnsupportedOperationException(); |
|
4949 |
} |
|
4950 |
||
4951 |
@Override |
|
4952 |
public V compute(K key, |
|
4953 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
4954 |
throw new UnsupportedOperationException(); |
|
4955 |
} |
|
4956 |
||
4957 |
@Override |
|
4958 |
public V merge(K key, V value, |
|
4959 |
BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
|
4960 |
throw new UnsupportedOperationException(); |
|
4961 |
} |
|
2 | 4962 |
} |
4963 |
||
4964 |
// Miscellaneous |
|
4965 |
||
4966 |
/** |
|
4967 |
* Returns an immutable list consisting of <tt>n</tt> copies of the |
|
4968 |
* specified object. The newly allocated data object is tiny (it contains |
|
4969 |
* a single reference to the data object). This method is useful in |
|
4970 |
* combination with the <tt>List.addAll</tt> method to grow lists. |
|
4971 |
* The returned list is serializable. |
|
4972 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4973 |
* @param <T> the class of the object to copy and of the objects |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
4974 |
* in the returned list. |
2 | 4975 |
* @param n the number of elements in the returned list. |
4976 |
* @param o the element to appear repeatedly in the returned list. |
|
4977 |
* @return an immutable list consisting of <tt>n</tt> copies of the |
|
4978 |
* specified object. |
|
7518 | 4979 |
* @throws IllegalArgumentException if {@code n < 0} |
2 | 4980 |
* @see List#addAll(Collection) |
4981 |
* @see List#addAll(int, Collection) |
|
4982 |
*/ |
|
4983 |
public static <T> List<T> nCopies(int n, T o) { |
|
4984 |
if (n < 0) |
|
4985 |
throw new IllegalArgumentException("List length = " + n); |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
4986 |
return new CopiesList<>(n, o); |
2 | 4987 |
} |
4988 |
||
4989 |
/** |
|
4990 |
* @serial include |
|
4991 |
*/ |
|
4992 |
private static class CopiesList<E> |
|
4993 |
extends AbstractList<E> |
|
4994 |
implements RandomAccess, Serializable |
|
4995 |
{ |
|
4996 |
private static final long serialVersionUID = 2739099268398711800L; |
|
4997 |
||
4998 |
final int n; |
|
4999 |
final E element; |
|
5000 |
||
5001 |
CopiesList(int n, E e) { |
|
5002 |
assert n >= 0; |
|
5003 |
this.n = n; |
|
5004 |
element = e; |
|
5005 |
} |
|
5006 |
||
5007 |
public int size() { |
|
5008 |
return n; |
|
5009 |
} |
|
5010 |
||
5011 |
public boolean contains(Object obj) { |
|
5012 |
return n != 0 && eq(obj, element); |
|
5013 |
} |
|
5014 |
||
5015 |
public int indexOf(Object o) { |
|
5016 |
return contains(o) ? 0 : -1; |
|
5017 |
} |
|
5018 |
||
5019 |
public int lastIndexOf(Object o) { |
|
5020 |
return contains(o) ? n - 1 : -1; |
|
5021 |
} |
|
5022 |
||
5023 |
public E get(int index) { |
|
5024 |
if (index < 0 || index >= n) |
|
5025 |
throw new IndexOutOfBoundsException("Index: "+index+ |
|
5026 |
", Size: "+n); |
|
5027 |
return element; |
|
5028 |
} |
|
5029 |
||
5030 |
public Object[] toArray() { |
|
5031 |
final Object[] a = new Object[n]; |
|
5032 |
if (element != null) |
|
5033 |
Arrays.fill(a, 0, n, element); |
|
5034 |
return a; |
|
5035 |
} |
|
5036 |
||
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
5037 |
@SuppressWarnings("unchecked") |
2 | 5038 |
public <T> T[] toArray(T[] a) { |
5039 |
final int n = this.n; |
|
5040 |
if (a.length < n) { |
|
5041 |
a = (T[])java.lang.reflect.Array |
|
5042 |
.newInstance(a.getClass().getComponentType(), n); |
|
5043 |
if (element != null) |
|
5044 |
Arrays.fill(a, 0, n, element); |
|
5045 |
} else { |
|
5046 |
Arrays.fill(a, 0, n, element); |
|
5047 |
if (a.length > n) |
|
5048 |
a[n] = null; |
|
5049 |
} |
|
5050 |
return a; |
|
5051 |
} |
|
5052 |
||
5053 |
public List<E> subList(int fromIndex, int toIndex) { |
|
5054 |
if (fromIndex < 0) |
|
5055 |
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); |
|
5056 |
if (toIndex > n) |
|
5057 |
throw new IndexOutOfBoundsException("toIndex = " + toIndex); |
|
5058 |
if (fromIndex > toIndex) |
|
5059 |
throw new IllegalArgumentException("fromIndex(" + fromIndex + |
|
5060 |
") > toIndex(" + toIndex + ")"); |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
5061 |
return new CopiesList<>(toIndex - fromIndex, element); |
2 | 5062 |
} |
19603
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5063 |
|
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5064 |
// Override default methods in Collection |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5065 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5066 |
public Stream<E> stream() { |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5067 |
return IntStream.range(0, n).mapToObj(i -> element); |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5068 |
} |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5069 |
|
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5070 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5071 |
public Stream<E> parallelStream() { |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5072 |
return IntStream.range(0, n).parallel().mapToObj(i -> element); |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5073 |
} |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5074 |
|
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5075 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5076 |
public Spliterator<E> spliterator() { |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5077 |
return stream().spliterator(); |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5078 |
} |
2 | 5079 |
} |
5080 |
||
5081 |
/** |
|
9004
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5082 |
* Returns a comparator that imposes the reverse of the <em>natural |
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5083 |
* ordering</em> on a collection of objects that implement the |
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5084 |
* {@code Comparable} interface. (The natural ordering is the ordering |
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5085 |
* imposed by the objects' own {@code compareTo} method.) This enables a |
2 | 5086 |
* simple idiom for sorting (or maintaining) collections (or arrays) of |
9004
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5087 |
* objects that implement the {@code Comparable} interface in |
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5088 |
* reverse-natural-order. For example, suppose {@code a} is an array of |
2 | 5089 |
* strings. Then: <pre> |
5090 |
* Arrays.sort(a, Collections.reverseOrder()); |
|
5091 |
* </pre> sorts the array in reverse-lexicographic (alphabetical) order.<p> |
|
5092 |
* |
|
5093 |
* The returned comparator is serializable. |
|
5094 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
5095 |
* @param <T> the class of the objects compared by the comparator |
9004
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5096 |
* @return A comparator that imposes the reverse of the <i>natural |
2 | 5097 |
* ordering</i> on a collection of objects that implement |
5098 |
* the <tt>Comparable</tt> interface. |
|
5099 |
* @see Comparable |
|
5100 |
*/ |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
12448
diff
changeset
|
5101 |
@SuppressWarnings("unchecked") |
2 | 5102 |
public static <T> Comparator<T> reverseOrder() { |
5103 |
return (Comparator<T>) ReverseComparator.REVERSE_ORDER; |
|
5104 |
} |
|
5105 |
||
5106 |
/** |
|
5107 |
* @serial include |
|
5108 |
*/ |
|
5109 |
private static class ReverseComparator |
|
5110 |
implements Comparator<Comparable<Object>>, Serializable { |
|
5111 |
||
5112 |
private static final long serialVersionUID = 7207038068494060240L; |
|
5113 |
||
5114 |
static final ReverseComparator REVERSE_ORDER |
|
5115 |
= new ReverseComparator(); |
|
5116 |
||
5117 |
public int compare(Comparable<Object> c1, Comparable<Object> c2) { |
|
5118 |
return c2.compareTo(c1); |
|
5119 |
} |
|
5120 |
||
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
13795
diff
changeset
|
5121 |
private Object readResolve() { return Collections.reverseOrder(); } |
18571 | 5122 |
|
5123 |
@Override |
|
5124 |
public Comparator<Comparable<Object>> reversed() { |
|
5125 |
return Comparator.naturalOrder(); |
|
5126 |
} |
|
2 | 5127 |
} |
5128 |
||
5129 |
/** |
|
5130 |
* Returns a comparator that imposes the reverse ordering of the specified |
|
9004
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5131 |
* comparator. If the specified comparator is {@code null}, this method is |
2 | 5132 |
* equivalent to {@link #reverseOrder()} (in other words, it returns a |
9004
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5133 |
* comparator that imposes the reverse of the <em>natural ordering</em> on |
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5134 |
* a collection of objects that implement the Comparable interface). |
2 | 5135 |
* |
5136 |
* <p>The returned comparator is serializable (assuming the specified |
|
9004
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5137 |
* comparator is also serializable or {@code null}). |
2 | 5138 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
5139 |
* @param <T> the class of the objects compared by the comparator |
9004
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5140 |
* @param cmp a comparator who's ordering is to be reversed by the returned |
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5141 |
* comparator or {@code null} |
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5142 |
* @return A comparator that imposes the reverse ordering of the |
284f923de1bf
7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents:
8151
diff
changeset
|
5143 |
* specified comparator. |
2 | 5144 |
* @since 1.5 |
5145 |
*/ |
|
5146 |
public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) { |
|
5147 |
if (cmp == null) |
|
5148 |
return reverseOrder(); |
|
5149 |
||
5150 |
if (cmp instanceof ReverseComparator2) |
|
5151 |
return ((ReverseComparator2<T>)cmp).cmp; |
|
5152 |
||
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
5153 |
return new ReverseComparator2<>(cmp); |
2 | 5154 |
} |
5155 |
||
5156 |
/** |
|
5157 |
* @serial include |
|
5158 |
*/ |
|
5159 |
private static class ReverseComparator2<T> implements Comparator<T>, |
|
5160 |
Serializable |
|
5161 |
{ |
|
5162 |
private static final long serialVersionUID = 4374092139857L; |
|
5163 |
||
5164 |
/** |
|
5165 |
* The comparator specified in the static factory. This will never |
|
5166 |
* be null, as the static factory returns a ReverseComparator |
|
5167 |
* instance if its argument is null. |
|
5168 |
* |
|
5169 |
* @serial |
|
5170 |
*/ |
|
5171 |
final Comparator<T> cmp; |
|
5172 |
||
5173 |
ReverseComparator2(Comparator<T> cmp) { |
|
5174 |
assert cmp != null; |
|
5175 |
this.cmp = cmp; |
|
5176 |
} |
|
5177 |
||
5178 |
public int compare(T t1, T t2) { |
|
5179 |
return cmp.compare(t2, t1); |
|
5180 |
} |
|
5181 |
||
5182 |
public boolean equals(Object o) { |
|
5183 |
return (o == this) || |
|
5184 |
(o instanceof ReverseComparator2 && |
|
5185 |
cmp.equals(((ReverseComparator2)o).cmp)); |
|
5186 |
} |
|
5187 |
||
5188 |
public int hashCode() { |
|
5189 |
return cmp.hashCode() ^ Integer.MIN_VALUE; |
|
5190 |
} |
|
18571 | 5191 |
|
5192 |
@Override |
|
5193 |
public Comparator<T> reversed() { |
|
5194 |
return cmp; |
|
5195 |
} |
|
2 | 5196 |
} |
5197 |
||
5198 |
/** |
|
5199 |
* Returns an enumeration over the specified collection. This provides |
|
5200 |
* interoperability with legacy APIs that require an enumeration |
|
5201 |
* as input. |
|
5202 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
5203 |
* @param <T> the class of the objects in the collection |
2 | 5204 |
* @param c the collection for which an enumeration is to be returned. |
5205 |
* @return an enumeration over the specified collection. |
|
5206 |
* @see Enumeration |
|
5207 |
*/ |
|
5208 |
public static <T> Enumeration<T> enumeration(final Collection<T> c) { |
|
5209 |
return new Enumeration<T>() { |
|
5210 |
private final Iterator<T> i = c.iterator(); |
|
5211 |
||
5212 |
public boolean hasMoreElements() { |
|
5213 |
return i.hasNext(); |
|
5214 |
} |
|
5215 |
||
5216 |
public T nextElement() { |
|
5217 |
return i.next(); |
|
5218 |
} |
|
5219 |
}; |
|
5220 |
} |
|
5221 |
||
5222 |
/** |
|
5223 |
* Returns an array list containing the elements returned by the |
|
5224 |
* specified enumeration in the order they are returned by the |
|
5225 |
* enumeration. This method provides interoperability between |
|
5226 |
* legacy APIs that return enumerations and new APIs that require |
|
5227 |
* collections. |
|
5228 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
5229 |
* @param <T> the class of the objects returned by the enumeration |
2 | 5230 |
* @param e enumeration providing elements for the returned |
5231 |
* array list |
|
5232 |
* @return an array list containing the elements returned |
|
5233 |
* by the specified enumeration. |
|
5234 |
* @since 1.4 |
|
5235 |
* @see Enumeration |
|
5236 |
* @see ArrayList |
|
5237 |
*/ |
|
5238 |
public static <T> ArrayList<T> list(Enumeration<T> e) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
5239 |
ArrayList<T> l = new ArrayList<>(); |
2 | 5240 |
while (e.hasMoreElements()) |
5241 |
l.add(e.nextElement()); |
|
5242 |
return l; |
|
5243 |
} |
|
5244 |
||
5245 |
/** |
|
5246 |
* Returns true if the specified arguments are equal, or both null. |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
5247 |
* |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
18794
diff
changeset
|
5248 |
* NB: Do not replace with Object.equals until JDK-8015417 is resolved. |
2 | 5249 |
*/ |
5250 |
static boolean eq(Object o1, Object o2) { |
|
5251 |
return o1==null ? o2==null : o1.equals(o2); |
|
5252 |
} |
|
5253 |
||
5254 |
/** |
|
5255 |
* Returns the number of elements in the specified collection equal to the |
|
5256 |
* specified object. More formally, returns the number of elements |
|
5257 |
* <tt>e</tt> in the collection such that |
|
5258 |
* <tt>(o == null ? e == null : o.equals(e))</tt>. |
|
5259 |
* |
|
5260 |
* @param c the collection in which to determine the frequency |
|
5261 |
* of <tt>o</tt> |
|
5262 |
* @param o the object whose frequency is to be determined |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
5263 |
* @return the number of elements in {@code c} equal to {@code o} |
2 | 5264 |
* @throws NullPointerException if <tt>c</tt> is null |
5265 |
* @since 1.5 |
|
5266 |
*/ |
|
5267 |
public static int frequency(Collection<?> c, Object o) { |
|
5268 |
int result = 0; |
|
5269 |
if (o == null) { |
|
5270 |
for (Object e : c) |
|
5271 |
if (e == null) |
|
5272 |
result++; |
|
5273 |
} else { |
|
5274 |
for (Object e : c) |
|
5275 |
if (o.equals(e)) |
|
5276 |
result++; |
|
5277 |
} |
|
5278 |
return result; |
|
5279 |
} |
|
5280 |
||
5281 |
/** |
|
7987
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5282 |
* Returns {@code true} if the two specified collections have no |
2 | 5283 |
* elements in common. |
5284 |
* |
|
5285 |
* <p>Care must be exercised if this method is used on collections that |
|
7987
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5286 |
* do not comply with the general contract for {@code Collection}. |
2 | 5287 |
* Implementations may elect to iterate over either collection and test |
5288 |
* for containment in the other collection (or to perform any equivalent |
|
5289 |
* computation). If either collection uses a nonstandard equality test |
|
7987
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5290 |
* (as does a {@link SortedSet} whose ordering is not <em>compatible with |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5291 |
* equals</em>, or the key set of an {@link IdentityHashMap}), both |
2 | 5292 |
* collections must use the same nonstandard equality test, or the |
5293 |
* result of this method is undefined. |
|
5294 |
* |
|
7987
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5295 |
* <p>Care must also be exercised when using collections that have |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5296 |
* restrictions on the elements that they may contain. Collection |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5297 |
* implementations are allowed to throw exceptions for any operation |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5298 |
* involving elements they deem ineligible. For absolute safety the |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5299 |
* specified collections should contain only elements which are |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5300 |
* eligible elements for both collections. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5301 |
* |
2 | 5302 |
* <p>Note that it is permissible to pass the same collection in both |
7987
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5303 |
* parameters, in which case the method will return {@code true} if and |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5304 |
* only if the collection is empty. |
2 | 5305 |
* |
5306 |
* @param c1 a collection |
|
5307 |
* @param c2 a collection |
|
7987
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5308 |
* @return {@code true} if the two specified collections have no |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5309 |
* elements in common. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5310 |
* @throws NullPointerException if either collection is {@code null}. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5311 |
* @throws NullPointerException if one collection contains a {@code null} |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5312 |
* element and {@code null} is not an eligible element for the other collection. |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9050
diff
changeset
|
5313 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
7987
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5314 |
* @throws ClassCastException if one collection contains an element that is |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9050
diff
changeset
|
5315 |
* of a type which is ineligible for the other collection. |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9050
diff
changeset
|
5316 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 5317 |
* @since 1.5 |
5318 |
*/ |
|
5319 |
public static boolean disjoint(Collection<?> c1, Collection<?> c2) { |
|
7987
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5320 |
// The collection to be used for contains(). Preference is given to |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5321 |
// the collection who's contains() has lower O() complexity. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5322 |
Collection<?> contains = c2; |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5323 |
// The collection to be iterated. If the collections' contains() impl |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5324 |
// are of different O() complexity, the collection with slower |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5325 |
// contains() will be used for iteration. For collections who's |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5326 |
// contains() are of the same complexity then best performance is |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5327 |
// achieved by iterating the smaller collection. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5328 |
Collection<?> iterate = c1; |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5329 |
|
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5330 |
// Performance optimization cases. The heuristics: |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5331 |
// 1. Generally iterate over c1. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5332 |
// 2. If c1 is a Set then iterate over c2. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5333 |
// 3. If either collection is empty then result is always true. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5334 |
// 4. Iterate over the smaller Collection. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5335 |
if (c1 instanceof Set) { |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5336 |
// Use c1 for contains as a Set's contains() is expected to perform |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5337 |
// better than O(N/2) |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5338 |
iterate = c2; |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5339 |
contains = c1; |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5340 |
} else if (!(c2 instanceof Set)) { |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5341 |
// Both are mere Collections. Iterate over smaller collection. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5342 |
// Example: If c1 contains 3 elements and c2 contains 50 elements and |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5343 |
// assuming contains() requires ceiling(N/2) comparisons then |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5344 |
// checking for all c1 elements in c2 would require 75 comparisons |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5345 |
// (3 * ceiling(50/2)) vs. checking all c2 elements in c1 requiring |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5346 |
// 100 comparisons (50 * ceiling(3/2)). |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5347 |
int c1size = c1.size(); |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5348 |
int c2size = c2.size(); |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5349 |
if (c1size == 0 || c2size == 0) { |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5350 |
// At least one collection is empty. Nothing will match. |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5351 |
return true; |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5352 |
} |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5353 |
|
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5354 |
if (c1size > c2size) { |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5355 |
iterate = c2; |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5356 |
contains = c1; |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5357 |
} |
2 | 5358 |
} |
5359 |
||
7987
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5360 |
for (Object e : iterate) { |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5361 |
if (contains.contains(e)) { |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5362 |
// Found a common element. Collections are not disjoint. |
2 | 5363 |
return false; |
7987
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5364 |
} |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5365 |
} |
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5366 |
|
04c9822b55c9
6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents:
7976
diff
changeset
|
5367 |
// No common elements were found. |
2 | 5368 |
return true; |
5369 |
} |
|
5370 |
||
5371 |
/** |
|
5372 |
* Adds all of the specified elements to the specified collection. |
|
5373 |
* Elements to be added may be specified individually or as an array. |
|
5374 |
* The behavior of this convenience method is identical to that of |
|
5375 |
* <tt>c.addAll(Arrays.asList(elements))</tt>, but this method is likely |
|
5376 |
* to run significantly faster under most implementations. |
|
5377 |
* |
|
5378 |
* <p>When elements are specified individually, this method provides a |
|
5379 |
* convenient way to add a few elements to an existing collection: |
|
5380 |
* <pre> |
|
5381 |
* Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon"); |
|
5382 |
* </pre> |
|
5383 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
5384 |
* @param <T> the class of the elements to add and of the collection |
2 | 5385 |
* @param c the collection into which <tt>elements</tt> are to be inserted |
5386 |
* @param elements the elements to insert into <tt>c</tt> |
|
5387 |
* @return <tt>true</tt> if the collection changed as a result of the call |
|
5388 |
* @throws UnsupportedOperationException if <tt>c</tt> does not support |
|
5389 |
* the <tt>add</tt> operation |
|
5390 |
* @throws NullPointerException if <tt>elements</tt> contains one or more |
|
5391 |
* null values and <tt>c</tt> does not permit null elements, or |
|
5392 |
* if <tt>c</tt> or <tt>elements</tt> are <tt>null</tt> |
|
5393 |
* @throws IllegalArgumentException if some property of a value in |
|
5394 |
* <tt>elements</tt> prevents it from being added to <tt>c</tt> |
|
5395 |
* @see Collection#addAll(Collection) |
|
5396 |
* @since 1.5 |
|
5397 |
*/ |
|
8151
88b01a6d5f51
7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents:
7987
diff
changeset
|
5398 |
@SafeVarargs |
2 | 5399 |
public static <T> boolean addAll(Collection<? super T> c, T... elements) { |
5400 |
boolean result = false; |
|
5401 |
for (T element : elements) |
|
5402 |
result |= c.add(element); |
|
5403 |
return result; |
|
5404 |
} |
|
5405 |
||
5406 |
/** |
|
5407 |
* Returns a set backed by the specified map. The resulting set displays |
|
5408 |
* the same ordering, concurrency, and performance characteristics as the |
|
5409 |
* backing map. In essence, this factory method provides a {@link Set} |
|
5410 |
* implementation corresponding to any {@link Map} implementation. There |
|
5411 |
* is no need to use this method on a {@link Map} implementation that |
|
5412 |
* already has a corresponding {@link Set} implementation (such as {@link |
|
5413 |
* HashMap} or {@link TreeMap}). |
|
5414 |
* |
|
5415 |
* <p>Each method invocation on the set returned by this method results in |
|
5416 |
* exactly one method invocation on the backing map or its <tt>keySet</tt> |
|
5417 |
* view, with one exception. The <tt>addAll</tt> method is implemented |
|
5418 |
* as a sequence of <tt>put</tt> invocations on the backing map. |
|
5419 |
* |
|
5420 |
* <p>The specified map must be empty at the time this method is invoked, |
|
5421 |
* and should not be accessed directly after this method returns. These |
|
5422 |
* conditions are ensured if the map is created empty, passed directly |
|
5423 |
* to this method, and no reference to the map is retained, as illustrated |
|
5424 |
* in the following code fragment: |
|
5425 |
* <pre> |
|
5426 |
* Set<Object> weakHashSet = Collections.newSetFromMap( |
|
5427 |
* new WeakHashMap<Object, Boolean>()); |
|
5428 |
* </pre> |
|
5429 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
5430 |
* @param <E> the class of the map keys and of the objects in the |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
5431 |
* returned set |
2 | 5432 |
* @param map the backing map |
5433 |
* @return the set backed by the map |
|
5434 |
* @throws IllegalArgumentException if <tt>map</tt> is not empty |
|
5435 |
* @since 1.6 |
|
5436 |
*/ |
|
5437 |
public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
5438 |
return new SetFromMap<>(map); |
2 | 5439 |
} |
5440 |
||
5441 |
/** |
|
5442 |
* @serial include |
|
5443 |
*/ |
|
5444 |
private static class SetFromMap<E> extends AbstractSet<E> |
|
5445 |
implements Set<E>, Serializable |
|
5446 |
{ |
|
5447 |
private final Map<E, Boolean> m; // The backing map |
|
5448 |
private transient Set<E> s; // Its keySet |
|
5449 |
||
5450 |
SetFromMap(Map<E, Boolean> map) { |
|
5451 |
if (!map.isEmpty()) |
|
5452 |
throw new IllegalArgumentException("Map is non-empty"); |
|
5453 |
m = map; |
|
5454 |
s = map.keySet(); |
|
5455 |
} |
|
5456 |
||
5457 |
public void clear() { m.clear(); } |
|
5458 |
public int size() { return m.size(); } |
|
5459 |
public boolean isEmpty() { return m.isEmpty(); } |
|
5460 |
public boolean contains(Object o) { return m.containsKey(o); } |
|
5461 |
public boolean remove(Object o) { return m.remove(o) != null; } |
|
5462 |
public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; } |
|
5463 |
public Iterator<E> iterator() { return s.iterator(); } |
|
5464 |
public Object[] toArray() { return s.toArray(); } |
|
5465 |
public <T> T[] toArray(T[] a) { return s.toArray(a); } |
|
5466 |
public String toString() { return s.toString(); } |
|
5467 |
public int hashCode() { return s.hashCode(); } |
|
5468 |
public boolean equals(Object o) { return o == this || s.equals(o); } |
|
5469 |
public boolean containsAll(Collection<?> c) {return s.containsAll(c);} |
|
5470 |
public boolean removeAll(Collection<?> c) {return s.removeAll(c);} |
|
5471 |
public boolean retainAll(Collection<?> c) {return s.retainAll(c);} |
|
5472 |
// addAll is the only inherited implementation |
|
5473 |
||
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
5474 |
// Override default methods in Collection |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5475 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5476 |
public void forEach(Consumer<? super E> action) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5477 |
s.forEach(action); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5478 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5479 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5480 |
public boolean removeIf(Predicate<? super E> filter) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5481 |
return s.removeIf(filter); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5482 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5483 |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
5484 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
5485 |
public Spliterator<E> spliterator() {return s.spliterator();} |
19603
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5486 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5487 |
public Stream<E> stream() {return s.stream();} |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5488 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5489 |
public Stream<E> parallelStream() {return s.parallelStream();} |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
5490 |
|
2 | 5491 |
private static final long serialVersionUID = 2454657854757543876L; |
5492 |
||
5493 |
private void readObject(java.io.ObjectInputStream stream) |
|
5494 |
throws IOException, ClassNotFoundException |
|
5495 |
{ |
|
5496 |
stream.defaultReadObject(); |
|
5497 |
s = m.keySet(); |
|
5498 |
} |
|
5499 |
} |
|
5500 |
||
5501 |
/** |
|
5502 |
* Returns a view of a {@link Deque} as a Last-in-first-out (Lifo) |
|
5503 |
* {@link Queue}. Method <tt>add</tt> is mapped to <tt>push</tt>, |
|
5504 |
* <tt>remove</tt> is mapped to <tt>pop</tt> and so on. This |
|
5505 |
* view can be useful when you would like to use a method |
|
5506 |
* requiring a <tt>Queue</tt> but you need Lifo ordering. |
|
5507 |
* |
|
5508 |
* <p>Each method invocation on the queue returned by this method |
|
5509 |
* results in exactly one method invocation on the backing deque, with |
|
5510 |
* one exception. The {@link Queue#addAll addAll} method is |
|
5511 |
* implemented as a sequence of {@link Deque#addFirst addFirst} |
|
5512 |
* invocations on the backing deque. |
|
5513 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18822
diff
changeset
|
5514 |
* @param <T> the class of the objects in the deque |
2 | 5515 |
* @param deque the deque |
5516 |
* @return the queue |
|
5517 |
* @since 1.6 |
|
5518 |
*/ |
|
5519 |
public static <T> Queue<T> asLifoQueue(Deque<T> deque) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7518
diff
changeset
|
5520 |
return new AsLIFOQueue<>(deque); |
2 | 5521 |
} |
5522 |
||
5523 |
/** |
|
5524 |
* @serial include |
|
5525 |
*/ |
|
5526 |
static class AsLIFOQueue<E> extends AbstractQueue<E> |
|
5527 |
implements Queue<E>, Serializable { |
|
5528 |
private static final long serialVersionUID = 1802017725587941708L; |
|
5529 |
private final Deque<E> q; |
|
5530 |
AsLIFOQueue(Deque<E> q) { this.q = q; } |
|
5531 |
public boolean add(E e) { q.addFirst(e); return true; } |
|
5532 |
public boolean offer(E e) { return q.offerFirst(e); } |
|
5533 |
public E poll() { return q.pollFirst(); } |
|
5534 |
public E remove() { return q.removeFirst(); } |
|
5535 |
public E peek() { return q.peekFirst(); } |
|
5536 |
public E element() { return q.getFirst(); } |
|
5537 |
public void clear() { q.clear(); } |
|
5538 |
public int size() { return q.size(); } |
|
5539 |
public boolean isEmpty() { return q.isEmpty(); } |
|
5540 |
public boolean contains(Object o) { return q.contains(o); } |
|
5541 |
public boolean remove(Object o) { return q.remove(o); } |
|
5542 |
public Iterator<E> iterator() { return q.iterator(); } |
|
5543 |
public Object[] toArray() { return q.toArray(); } |
|
5544 |
public <T> T[] toArray(T[] a) { return q.toArray(a); } |
|
5545 |
public String toString() { return q.toString(); } |
|
5546 |
public boolean containsAll(Collection<?> c) {return q.containsAll(c);} |
|
5547 |
public boolean removeAll(Collection<?> c) {return q.removeAll(c);} |
|
5548 |
public boolean retainAll(Collection<?> c) {return q.retainAll(c);} |
|
5549 |
// We use inherited addAll; forwarding addAll would be wrong |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5550 |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
5551 |
// Override default methods in Collection |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5552 |
@Override |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
5553 |
public void forEach(Consumer<? super E> action) {q.forEach(action);} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
5554 |
@Override |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5555 |
public boolean removeIf(Predicate<? super E> filter) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5556 |
return q.removeIf(filter); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
16867
diff
changeset
|
5557 |
} |
19603
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5558 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5559 |
public Spliterator<E> spliterator() {return q.spliterator();} |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5560 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5561 |
public Stream<E> stream() {return q.stream();} |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5562 |
@Override |
636cd05ce8e5
8023275: Wrapping collections should override default methods
henryjen
parents:
19445
diff
changeset
|
5563 |
public Stream<E> parallelStream() {return q.parallelStream();} |
2 | 5564 |
} |
5565 |
} |