6 |
|
*/ |
7 |
|
|
8 |
|
package java.util; |
9 |
+ |
import java.util.*; // for javadoc (till 6280605 is fixed) |
10 |
|
import java.io.Serializable; |
11 |
|
import java.io.ObjectOutputStream; |
12 |
|
import java.io.IOException; |
64 |
|
* two implementations, one of which is appropriate for RandomAccess |
65 |
|
* lists, the other for "sequential." Often, the random access variant |
66 |
|
* yields better performance on small sequential access lists. The |
67 |
< |
* tuning parameters below determine the cutoff point for what constitutes |
67 |
> |
* tuning parameters below determine the cutoff point for what constitutes |
68 |
|
* a "small" sequential access list for each algorithm. The values below |
69 |
|
* were empirically determined to work well for LinkedList. Hopefully |
70 |
|
* they should be reasonable for other sequential access List |
409 |
|
* its list-iterator does not support the <tt>set</tt> operation. |
410 |
|
*/ |
411 |
|
public static void shuffle(List<?> list) { |
412 |
+ |
if (r == null) { |
413 |
+ |
r = new Random(); |
414 |
+ |
} |
415 |
|
shuffle(list, r); |
416 |
|
} |
417 |
< |
private static Random r = new Random(); |
417 |
> |
private static Random r; |
418 |
|
|
419 |
|
/** |
420 |
|
* Randomly permute the specified list using the specified source of |
573 |
|
Iterator<? extends T> i = coll.iterator(); |
574 |
|
T candidate = i.next(); |
575 |
|
|
576 |
< |
while(i.hasNext()) { |
576 |
> |
while (i.hasNext()) { |
577 |
|
T next = i.next(); |
578 |
|
if (next.compareTo(candidate) < 0) |
579 |
|
candidate = next; |
610 |
|
Iterator<? extends T> i = coll.iterator(); |
611 |
|
T candidate = i.next(); |
612 |
|
|
613 |
< |
while(i.hasNext()) { |
613 |
> |
while (i.hasNext()) { |
614 |
|
T next = i.next(); |
615 |
|
if (comp.compare(next, candidate) < 0) |
616 |
|
candidate = next; |
643 |
|
Iterator<? extends T> i = coll.iterator(); |
644 |
|
T candidate = i.next(); |
645 |
|
|
646 |
< |
while(i.hasNext()) { |
646 |
> |
while (i.hasNext()) { |
647 |
|
T next = i.next(); |
648 |
|
if (next.compareTo(candidate) > 0) |
649 |
|
candidate = next; |
680 |
|
Iterator<? extends T> i = coll.iterator(); |
681 |
|
T candidate = i.next(); |
682 |
|
|
683 |
< |
while(i.hasNext()) { |
683 |
> |
while (i.hasNext()) { |
684 |
|
T next = i.next(); |
685 |
|
if (comp.compare(next, candidate) > 0) |
686 |
|
candidate = next; |
1055 |
|
* @param s the set for which an unmodifiable view is to be returned. |
1056 |
|
* @return an unmodifiable view of the specified set. |
1057 |
|
*/ |
1054 |
– |
|
1058 |
|
public static <T> Set<T> unmodifiableSet(Set<? extends T> s) { |
1059 |
|
return new UnmodifiableSet<T>(s); |
1060 |
|
} |
1291 |
|
public V remove(Object key) { |
1292 |
|
throw new UnsupportedOperationException(); |
1293 |
|
} |
1294 |
< |
public void putAll(Map<? extends K, ? extends V> t) { |
1294 |
> |
public void putAll(Map<? extends K, ? extends V> m) { |
1295 |
|
throw new UnsupportedOperationException(); |
1296 |
|
} |
1297 |
|
public void clear() { |
1366 |
|
// We don't pass a to c.toArray, to avoid window of |
1367 |
|
// vulnerability wherein an unscrupulous multithreaded client |
1368 |
|
// could get his hands on raw (unwrapped) Entries from c. |
1369 |
< |
Object[] arr = |
1367 |
< |
c.toArray( |
1368 |
< |
a.length==0 ? a : |
1369 |
< |
(T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), 0)); |
1369 |
> |
Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0)); |
1370 |
|
|
1371 |
|
for (int i=0; i<arr.length; i++) |
1372 |
|
arr[i] = new UnmodifiableEntry<K,V>((Map.Entry<K,V>)arr[i]); |
2246 |
|
* Dump coll into an array of the required type. This serves |
2247 |
|
* three purposes: it insulates us from concurrent changes in |
2248 |
|
* the contents of coll, it type-checks all of the elements in |
2249 |
< |
* coll, and it provides all-or-nothing semantics(which we |
2249 |
> |
* coll, and it provides all-or-nothing semantics (which we |
2250 |
|
* wouldn't get if we type-checked each element as we added it). |
2251 |
|
*/ |
2252 |
|
E[] a = null; |
2253 |
|
try { |
2254 |
|
a = coll.toArray(zeroLengthElementArray()); |
2255 |
< |
} catch(ArrayStoreException e) { |
2255 |
> |
} catch (ArrayStoreException e) { |
2256 |
|
throw new ClassCastException(); |
2257 |
|
} |
2258 |
|
|
2436 |
|
E[] a = null; |
2437 |
|
try { |
2438 |
|
a = c.toArray(zeroLengthElementArray()); |
2439 |
< |
} catch(ArrayStoreException e) { |
2439 |
> |
} catch (ArrayStoreException e) { |
2440 |
|
throw new ClassCastException(); |
2441 |
|
} |
2442 |
|
|
2582 |
|
K[] keys = null; |
2583 |
|
try { |
2584 |
|
keys = t.keySet().toArray(zeroLengthKeyArray()); |
2585 |
< |
} catch(ArrayStoreException e) { |
2585 |
> |
} catch (ArrayStoreException e) { |
2586 |
|
throw new ClassCastException(); |
2587 |
|
} |
2588 |
|
V[] values = null; |
2589 |
|
try { |
2590 |
|
values = t.values().toArray(zeroLengthValueArray()); |
2591 |
< |
} catch(ArrayStoreException e) { |
2591 |
> |
} catch (ArrayStoreException e) { |
2592 |
|
throw new ClassCastException(); |
2593 |
|
} |
2594 |
|
|
2700 |
|
// We don't pass a to s.toArray, to avoid window of |
2701 |
|
// vulnerability wherein an unscrupulous multithreaded client |
2702 |
|
// could get his hands on raw (unwrapped) Entries from s. |
2703 |
< |
Object[] arr = s.toArray(a.length==0 ? a : |
2704 |
< |
(T[])Array.newInstance(a.getClass().getComponentType(), 0)); |
2703 |
> |
Object[] arr = s.toArray(a.length==0 ? a : Arrays.copyOf(a, 0)); |
2704 |
|
|
2705 |
|
for (int i=0; i<arr.length; i++) |
2706 |
|
arr[i] = new CheckedEntry<K,V>((Map.Entry<K,V>)arr[i], |
3196 |
|
* @see List#addAll(int, Collection) |
3197 |
|
*/ |
3198 |
|
public static <T> List<T> nCopies(int n, T o) { |
3199 |
+ |
if (n < 0) |
3200 |
+ |
throw new IllegalArgumentException("List length = " + n); |
3201 |
|
return new CopiesList<T>(n, o); |
3202 |
|
} |
3203 |
|
|
3210 |
|
{ |
3211 |
|
static final long serialVersionUID = 2739099268398711800L; |
3212 |
|
|
3213 |
< |
int n; |
3214 |
< |
E element; |
3213 |
> |
final int n; |
3214 |
> |
final E element; |
3215 |
|
|
3216 |
|
CopiesList(int n, E e) { |
3217 |
< |
if (n < 0) |
3217 |
< |
throw new IllegalArgumentException("List length = " + n); |
3217 |
> |
assert n >= 0; |
3218 |
|
this.n = n; |
3219 |
|
element = e; |
3220 |
|
} |
3227 |
|
return n != 0 && eq(obj, element); |
3228 |
|
} |
3229 |
|
|
3230 |
+ |
public int indexOf(Object o) { |
3231 |
+ |
return contains(o) ? 0 : -1; |
3232 |
+ |
} |
3233 |
+ |
|
3234 |
+ |
public int lastIndexOf(Object o) { |
3235 |
+ |
return contains(o) ? n - 1 : -1; |
3236 |
+ |
} |
3237 |
+ |
|
3238 |
|
public E get(int index) { |
3239 |
< |
if (index<0 || index>=n) |
3239 |
> |
if (index < 0 || index >= n) |
3240 |
|
throw new IndexOutOfBoundsException("Index: "+index+ |
3241 |
|
", Size: "+n); |
3242 |
|
return element; |
3243 |
|
} |
3244 |
+ |
|
3245 |
+ |
public Object[] toArray() { |
3246 |
+ |
final Object[] a = new Object[n]; |
3247 |
+ |
if (element != null) |
3248 |
+ |
Arrays.fill(a, 0, n, element); |
3249 |
+ |
return a; |
3250 |
+ |
} |
3251 |
+ |
|
3252 |
+ |
public <T> T[] toArray(T[] a) { |
3253 |
+ |
final int n = this.n; |
3254 |
+ |
if (a.length < n) { |
3255 |
+ |
a = (T[])java.lang.reflect.Array |
3256 |
+ |
.newInstance(a.getClass().getComponentType(), n); |
3257 |
+ |
if (element != null) |
3258 |
+ |
Arrays.fill(a, 0, n, element); |
3259 |
+ |
} else { |
3260 |
+ |
Arrays.fill(a, 0, n, element); |
3261 |
+ |
if (a.length > n) |
3262 |
+ |
a[n] = null; |
3263 |
+ |
} |
3264 |
+ |
return a; |
3265 |
+ |
} |
3266 |
+ |
|
3267 |
+ |
public List<E> subList(int fromIndex, int toIndex) { |
3268 |
+ |
if (fromIndex < 0) |
3269 |
+ |
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); |
3270 |
+ |
if (toIndex > n) |
3271 |
+ |
throw new IndexOutOfBoundsException("toIndex = " + toIndex); |
3272 |
+ |
if (fromIndex > toIndex) |
3273 |
+ |
throw new IllegalArgumentException("fromIndex(" + fromIndex + |
3274 |
+ |
") > toIndex(" + toIndex + ")"); |
3275 |
+ |
return new CopiesList(toIndex - fromIndex, element); |
3276 |
+ |
} |
3277 |
|
} |
3278 |
|
|
3279 |
|
/** |
3503 |
|
* @throws UnsupportedOperationException if <tt>c</tt> does not support |
3504 |
|
* the <tt>add</tt> operation. |
3505 |
|
* @throws NullPointerException if <tt>elements</tt> contains one or more |
3506 |
< |
* null values and <tt>c</tt> does not support null elements, or |
3506 |
> |
* null values and <tt>c</tt> does not permit null elements, or |
3507 |
|
* if <tt>c</tt> or <tt>elements</tt> are <tt>null</tt> |
3508 |
< |
* @throws IllegalArgumentException if some aspect of a value in |
3508 |
> |
* @throws IllegalArgumentException if some property of a value in |
3509 |
|
* <tt>elements</tt> prevents it from being added to <tt>c</tt> |
3510 |
|
* @see Collection#addAll(Collection) |
3511 |
|
* @since 1.5 |
3537 |
|
* to this method, and no reference to the map is retained, as illustrated |
3538 |
|
* in the following code fragment: |
3539 |
|
* <pre> |
3540 |
< |
* Set<Object> weakHashSet = Collections.asSet( |
3540 |
> |
* Set<Object> weakHashSet = Collections.newSetFromMap( |
3541 |
|
* new WeakHashMap<Object, Boolean>()); |
3542 |
|
* </pre> |
3543 |
|
* |
3544 |
|
* @param map the backing map |
3545 |
|
* @return the set backed by the map |
3546 |
|
* @throws IllegalArgumentException if <tt>map</tt> is not empty |
3547 |
+ |
* @since 1.6 |
3548 |
|
*/ |
3549 |
< |
public static <E> Set<E> asSet(Map<E, Boolean> map) { |
3550 |
< |
return new MapAsSet<E>(map); |
3549 |
> |
public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) { |
3550 |
> |
return new SetFromMap<E>(map); |
3551 |
|
} |
3552 |
|
|
3553 |
< |
private static class MapAsSet<E> extends AbstractSet<E> |
3553 |
> |
private static class SetFromMap<E> extends AbstractSet<E> |
3554 |
|
implements Set<E>, Serializable |
3555 |
|
{ |
3556 |
|
private final Map<E, Boolean> m; // The backing map |
3557 |
|
private transient Set<E> keySet; // Its keySet |
3558 |
|
|
3559 |
< |
MapAsSet(Map<E, Boolean> map) { |
3559 |
> |
SetFromMap(Map<E, Boolean> map) { |
3560 |
|
if (!map.isEmpty()) |
3561 |
|
throw new IllegalArgumentException("Map is non-empty"); |
3562 |
|
m = map; |
3600 |
|
* <tt>remove</tt> is mapped to <tt>pop</tt> and so on. This |
3601 |
|
* view can be useful when you would like to use a method |
3602 |
|
* requiring a <tt>Queue</tt> but you need Lifo ordering. |
3603 |
< |
* @param deque the Deque |
3603 |
> |
* |
3604 |
> |
* @param deque the deque |
3605 |
|
* @return the queue |
3606 |
|
* @since 1.6 |
3607 |
|
*/ |
3611 |
|
|
3612 |
|
static class AsLIFOQueue<E> extends AbstractQueue<E> |
3613 |
|
implements Queue<E>, Serializable { |
3614 |
+ |
private static final long serialVersionUID = 1802017725587941708L; |
3615 |
|
private final Deque<E> q; |
3616 |
|
AsLIFOQueue(Deque<E> q) { this.q = q; } |
3617 |
|
public boolean offer(E e) { return q.offerFirst(e); } |