1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
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; |
38 |
|
* example, invoking the <tt>sort</tt> method on an unmodifiable list that is |
39 |
|
* already sorted may or may not throw <tt>UnsupportedOperationException</tt>. |
40 |
|
* |
41 |
< |
* <p>This class is a member of the |
41 |
> |
* <p>This class is a member of the |
42 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
43 |
|
* Java Collections Framework</a>. |
44 |
|
* |
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 |
98 |
|
* The sorting algorithm is a modified mergesort (in which the merge is |
99 |
|
* omitted if the highest element in the low sublist is less than the |
100 |
|
* lowest element in the high sublist). This algorithm offers guaranteed |
101 |
< |
* n log(n) performance. |
101 |
> |
* n log(n) performance. |
102 |
|
* |
103 |
|
* This implementation dumps the specified list into an array, sorts |
104 |
|
* the array, and iterates over the list resetting each element |
136 |
|
* The sorting algorithm is a modified mergesort (in which the merge is |
137 |
|
* omitted if the highest element in the low sublist is less than the |
138 |
|
* lowest element in the high sublist). This algorithm offers guaranteed |
139 |
< |
* n log(n) performance. |
139 |
> |
* n log(n) performance. |
140 |
|
* |
141 |
|
* The specified list must be modifiable, but need not be resizable. |
142 |
|
* This implementation dumps the specified list into an array, sorts |
183 |
|
* |
184 |
|
* @param list the list to be searched. |
185 |
|
* @param key the key to be searched for. |
186 |
< |
* @return index of the search key, if it is contained in the list; |
186 |
> |
* @return the index of the search key, if it is contained in the list; |
187 |
|
* otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The |
188 |
|
* <i>insertion point</i> is defined as the point at which the |
189 |
|
* key would be inserted into the list: the index of the first |
288 |
|
* @param c the comparator by which the list is ordered. A |
289 |
|
* <tt>null</tt> value indicates that the elements' <i>natural |
290 |
|
* ordering</i> should be used. |
291 |
< |
* @return index of the search key, if it is contained in the list; |
291 |
> |
* @return the index of the search key, if it is contained in the list; |
292 |
|
* otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The |
293 |
|
* <i>insertion point</i> is defined as the point at which the |
294 |
|
* key would be inserted into the list: the index of the first |
362 |
|
* |
363 |
|
* @param list the list whose elements are to be reversed. |
364 |
|
* @throws UnsupportedOperationException if the specified list or |
365 |
< |
* its list-iterator does not support the <tt>set</tt> method. |
365 |
> |
* its list-iterator does not support the <tt>set</tt> operation. |
366 |
|
*/ |
367 |
|
public static void reverse(List<?> list) { |
368 |
|
int size = list.size(); |
406 |
|
* |
407 |
|
* @param list the list to be shuffled. |
408 |
|
* @throws UnsupportedOperationException if the specified list or |
409 |
< |
* its list-iterator does not support the <tt>set</tt> method. |
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; |
716 |
|
* Collections.rotate(l.subList(1, 4), -1); |
717 |
|
* </pre> |
718 |
|
* The resulting list is <tt>[a, c, d, b, e]</tt>. |
719 |
< |
* |
719 |
> |
* |
720 |
|
* <p>To move more than one element forward, increase the absolute value |
721 |
|
* of the rotation distance. To move elements backward, use a positive |
722 |
|
* shift distance. |
740 |
|
* constraints on this value; it may be zero, negative, or |
741 |
|
* greater than <tt>list.size()</tt>. |
742 |
|
* @throws UnsupportedOperationException if the specified list or |
743 |
< |
* its list-iterator does not support the <tt>set</tt> method. |
743 |
> |
* its list-iterator does not support the <tt>set</tt> operation. |
744 |
|
* @since 1.4 |
745 |
|
*/ |
746 |
|
public static void rotate(List<?> list, int distance) { |
776 |
|
private static void rotate2(List<?> list, int distance) { |
777 |
|
int size = list.size(); |
778 |
|
if (size == 0) |
779 |
< |
return; |
779 |
> |
return; |
780 |
|
int mid = -distance % size; |
781 |
|
if (mid < 0) |
782 |
|
mid += size; |
803 |
|
* <tt>e</tt> such that |
804 |
|
* <tt>(oldVal==null ? e==null : oldVal.equals(e))</tt>. |
805 |
|
* @throws UnsupportedOperationException if the specified list or |
806 |
< |
* its list-iterator does not support the <tt>set</tt> method. |
806 |
> |
* its list-iterator does not support the <tt>set</tt> operation. |
807 |
|
* @since 1.4 |
808 |
|
*/ |
809 |
|
public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) { |
974 |
|
* that the backing collection is a set or a list.<p> |
975 |
|
* |
976 |
|
* The returned collection will be serializable if the specified collection |
977 |
< |
* is serializable. |
977 |
> |
* is serializable. |
978 |
|
* |
979 |
|
* @param c the collection for which an unmodifiable view is to be |
980 |
|
* returned. |
1018 |
|
}; |
1019 |
|
} |
1020 |
|
|
1021 |
< |
public boolean add(E o){ |
1021 |
> |
public boolean add(E e){ |
1022 |
|
throw new UnsupportedOperationException(); |
1023 |
|
} |
1024 |
|
public boolean remove(Object o) { |
1050 |
|
* iterator, result in an <tt>UnsupportedOperationException</tt>.<p> |
1051 |
|
* |
1052 |
|
* The returned set will be serializable if the specified set |
1053 |
< |
* is serializable. |
1053 |
> |
* is serializable. |
1054 |
|
* |
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 |
|
} |
1081 |
|
* an <tt>UnsupportedOperationException</tt>.<p> |
1082 |
|
* |
1083 |
|
* The returned sorted set will be serializable if the specified sorted set |
1084 |
< |
* is serializable. |
1084 |
> |
* is serializable. |
1085 |
|
* |
1086 |
|
* @param s the sorted set for which an unmodifiable view is to be |
1087 |
< |
* returned. |
1087 |
> |
* returned. |
1088 |
|
* @return an unmodifiable view of the specified sorted set. |
1089 |
|
*/ |
1090 |
|
public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) { |
1186 |
|
public void remove() { |
1187 |
|
throw new UnsupportedOperationException(); |
1188 |
|
} |
1189 |
< |
public void set(E o) { |
1189 |
> |
public void set(E e) { |
1190 |
|
throw new UnsupportedOperationException(); |
1191 |
|
} |
1192 |
< |
public void add(E o) { |
1192 |
> |
public void add(E e) { |
1193 |
|
throw new UnsupportedOperationException(); |
1194 |
|
} |
1195 |
|
}; |
1255 |
|
* <tt>UnsupportedOperationException</tt>.<p> |
1256 |
|
* |
1257 |
|
* The returned map will be serializable if the specified map |
1258 |
< |
* is serializable. |
1258 |
> |
* is serializable. |
1259 |
|
* |
1260 |
|
* @param m the map for which an unmodifiable view is to be returned. |
1261 |
|
* @return an unmodifiable view of the specified map. |
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() { |
1337 |
|
private static final long serialVersionUID = 7854390611657943733L; |
1338 |
|
|
1339 |
|
UnmodifiableEntrySet(Set<? extends Map.Entry<? extends K, ? extends V>> s) { |
1340 |
< |
super((Set<Map.Entry<K,V>>)(Set)s); |
1340 |
> |
super((Set)s); |
1341 |
|
} |
1342 |
|
public Iterator<Map.Entry<K,V>> iterator() { |
1343 |
|
return new Iterator<Map.Entry<K,V>>() { |
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]); |
1456 |
|
* an <tt>UnsupportedOperationException</tt>.<p> |
1457 |
|
* |
1458 |
|
* The returned sorted map will be serializable if the specified sorted map |
1459 |
< |
* is serializable. |
1459 |
> |
* is serializable. |
1460 |
|
* |
1461 |
|
* @param m the sorted map for which an unmodifiable view is to be |
1462 |
< |
* returned. |
1462 |
> |
* returned. |
1463 |
|
* @return an unmodifiable view of the specified sorted map. |
1464 |
|
*/ |
1465 |
|
public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) { |
1523 |
|
* that the backing collection is a set or a list.<p> |
1524 |
|
* |
1525 |
|
* The returned collection will be serializable if the specified collection |
1526 |
< |
* is serializable. |
1526 |
> |
* is serializable. |
1527 |
|
* |
1528 |
|
* @param c the collection to be "wrapped" in a synchronized collection. |
1529 |
|
* @return a synchronized view of the specified collection. |
1577 |
|
return c.iterator(); // Must be manually synched by user! |
1578 |
|
} |
1579 |
|
|
1580 |
< |
public boolean add(E o) { |
1581 |
< |
synchronized(mutex) {return c.add(o);} |
1580 |
> |
public boolean add(E e) { |
1581 |
> |
synchronized(mutex) {return c.add(e);} |
1582 |
|
} |
1583 |
|
public boolean remove(Object o) { |
1584 |
|
synchronized(mutex) {return c.remove(o);} |
1673 |
|
* sorted set when iterating over it or any of its <tt>subSet</tt>, |
1674 |
|
* <tt>headSet</tt>, or <tt>tailSet</tt> views. |
1675 |
|
* <pre> |
1676 |
< |
* SortedSet s = Collections.synchronizedSortedSet(new HashSortedSet()); |
1676 |
> |
* SortedSet s = Collections.synchronizedSortedSet(new TreeSet()); |
1677 |
|
* ... |
1678 |
|
* synchronized(s) { |
1679 |
|
* Iterator i = s.iterator(); // Must be in the synchronized block |
1683 |
|
* </pre> |
1684 |
|
* or: |
1685 |
|
* <pre> |
1686 |
< |
* SortedSet s = Collections.synchronizedSortedSet(new HashSortedSet()); |
1686 |
> |
* SortedSet s = Collections.synchronizedSortedSet(new TreeSet()); |
1687 |
|
* SortedSet s2 = s.headSet(foo); |
1688 |
|
* ... |
1689 |
|
* synchronized(s) { // Note: s, not s2!!! |
2007 |
|
public Set<Map.Entry<K,V>> entrySet() { |
2008 |
|
synchronized(mutex) { |
2009 |
|
if (entrySet==null) |
2010 |
< |
entrySet = new SynchronizedSet<Map.Entry<K,V>>((Set<Map.Entry<K,V>>)m.entrySet(), mutex); |
2010 |
> |
entrySet = new SynchronizedSet<Map.Entry<K,V>>(m.entrySet(), mutex); |
2011 |
|
return entrySet; |
2012 |
|
} |
2013 |
|
} |
2045 |
|
* collections views of any of its <tt>subMap</tt>, <tt>headMap</tt> or |
2046 |
|
* <tt>tailMap</tt> views. |
2047 |
|
* <pre> |
2048 |
< |
* SortedMap m = Collections.synchronizedSortedMap(new HashSortedMap()); |
2048 |
> |
* SortedMap m = Collections.synchronizedSortedMap(new TreeMap()); |
2049 |
|
* ... |
2050 |
|
* Set s = m.keySet(); // Needn't be in synchronized block |
2051 |
|
* ... |
2057 |
|
* </pre> |
2058 |
|
* or: |
2059 |
|
* <pre> |
2060 |
< |
* SortedMap m = Collections.synchronizedSortedMap(new HashSortedMap()); |
2060 |
> |
* SortedMap m = Collections.synchronizedSortedMap(new TreeMap()); |
2061 |
|
* SortedMap m2 = m.subMap(foo, bar); |
2062 |
|
* ... |
2063 |
|
* Set s2 = m2.keySet(); // Needn't be in synchronized block |
2191 |
|
Class<E> type) { |
2192 |
|
return new CheckedCollection<E>(c, type); |
2193 |
|
} |
2194 |
< |
|
2194 |
> |
|
2195 |
|
/** |
2196 |
|
* @serial include |
2197 |
|
*/ |
2236 |
|
c.clear(); |
2237 |
|
} |
2238 |
|
|
2239 |
< |
public boolean add(E o){ |
2240 |
< |
typeCheck(o); |
2241 |
< |
return c.add(o); |
2239 |
> |
public boolean add(E e){ |
2240 |
> |
typeCheck(e); |
2241 |
> |
return c.add(e); |
2242 |
|
} |
2243 |
|
|
2244 |
|
public boolean addAll(Collection<? extends E> coll) { |
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 |
|
|
2265 |
|
private E[] zeroLengthElementArray = null; // Lazily initialized |
2266 |
|
|
2267 |
|
/* |
2268 |
< |
* We don't need locking or volatile, because it's OK if we create |
2268 |
> |
* We don't need locking or volatile, because it's OK if we create |
2269 |
|
* several zeroLengthElementArrays, and they're immutable. |
2270 |
|
*/ |
2271 |
|
E[] zeroLengthElementArray() { |
2300 |
|
public static <E> Set<E> checkedSet(Set<E> s, Class<E> type) { |
2301 |
|
return new CheckedSet<E>(s, type); |
2302 |
|
} |
2303 |
< |
|
2303 |
> |
|
2304 |
|
/** |
2305 |
|
* @serial include |
2306 |
|
*/ |
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 |
|
|
2456 |
|
public int previousIndex() { return i.previousIndex(); } |
2457 |
|
public void remove() { i.remove(); } |
2458 |
|
|
2459 |
< |
public void set(E o) { |
2460 |
< |
typeCheck(o); |
2461 |
< |
i.set(o); |
2459 |
> |
public void set(E e) { |
2460 |
> |
typeCheck(e); |
2461 |
> |
i.set(e); |
2462 |
|
} |
2463 |
|
|
2464 |
< |
public void add(E o) { |
2465 |
< |
typeCheck(o); |
2466 |
< |
i.add(o); |
2464 |
> |
public void add(E e) { |
2465 |
> |
typeCheck(e); |
2466 |
> |
i.add(e); |
2467 |
|
} |
2468 |
|
}; |
2469 |
|
} |
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 |
|
|
2604 |
|
private V[] zeroLengthValueArray = null; |
2605 |
|
|
2606 |
|
/* |
2607 |
< |
* We don't need locking or volatile, because it's OK if we create |
2607 |
> |
* We don't need locking or volatile, because it's OK if we create |
2608 |
|
* several zeroLengthValueArrays, and they're immutable. |
2609 |
|
*/ |
2610 |
|
private K[] zeroLengthKeyArray() { |
2658 |
|
s.clear(); |
2659 |
|
} |
2660 |
|
|
2661 |
< |
public boolean add(Map.Entry<K, V> o){ |
2661 |
> |
public boolean add(Map.Entry<K, V> e){ |
2662 |
|
throw new UnsupportedOperationException(); |
2663 |
|
} |
2664 |
|
public boolean addAll(Collection<? extends Map.Entry<K, V>> coll) { |
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], |
3059 |
|
|
3060 |
|
final private E element; |
3061 |
|
|
3062 |
< |
SingletonSet(E o) {element = o;} |
3062 |
> |
SingletonSet(E e) {element = e;} |
3063 |
|
|
3064 |
|
public Iterator<E> iterator() { |
3065 |
|
return new Iterator<E>() { |
3167 |
|
|
3168 |
|
public Set<Map.Entry<K,V>> entrySet() { |
3169 |
|
if (entrySet==null) |
3170 |
< |
entrySet = singleton((Map.Entry<K,V>)new ImmutableEntry<K,V>(k, v)); |
3170 |
> |
entrySet = Collections.<Map.Entry<K,V>>singleton( |
3171 |
> |
new SimpleImmutableEntry<K,V>(k, v)); |
3172 |
|
return entrySet; |
3173 |
|
} |
3174 |
|
|
3178 |
|
return values; |
3179 |
|
} |
3180 |
|
|
3181 |
– |
private static class ImmutableEntry<K,V> |
3182 |
– |
implements Map.Entry<K,V> { |
3183 |
– |
final K k; |
3184 |
– |
final V v; |
3185 |
– |
|
3186 |
– |
ImmutableEntry(K key, V value) { |
3187 |
– |
k = key; |
3188 |
– |
v = value; |
3189 |
– |
} |
3190 |
– |
|
3191 |
– |
public K getKey() {return k;} |
3192 |
– |
|
3193 |
– |
public V getValue() {return v;} |
3194 |
– |
|
3195 |
– |
public V setValue(V value) { |
3196 |
– |
throw new UnsupportedOperationException(); |
3197 |
– |
} |
3198 |
– |
|
3199 |
– |
public boolean equals(Object o) { |
3200 |
– |
if (!(o instanceof Map.Entry)) |
3201 |
– |
return false; |
3202 |
– |
Map.Entry e = (Map.Entry)o; |
3203 |
– |
return eq(e.getKey(), k) && eq(e.getValue(), v); |
3204 |
– |
} |
3205 |
– |
|
3206 |
– |
public int hashCode() { |
3207 |
– |
return ((k==null ? 0 : k.hashCode()) ^ |
3208 |
– |
(v==null ? 0 : v.hashCode())); |
3209 |
– |
} |
3210 |
– |
|
3211 |
– |
public String toString() { |
3212 |
– |
return k+"="+v; |
3213 |
– |
} |
3214 |
– |
} |
3181 |
|
} |
3182 |
|
|
3183 |
|
/** |
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 o) { |
3217 |
< |
if (n < 0) |
3250 |
< |
throw new IllegalArgumentException("List length = " + n); |
3216 |
> |
CopiesList(int n, E e) { |
3217 |
> |
assert n >= 0; |
3218 |
|
this.n = n; |
3219 |
< |
element = o; |
3219 |
> |
element = e; |
3220 |
|
} |
3221 |
|
|
3222 |
|
public int size() { |
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 |
|
/** |
3332 |
|
public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) { |
3333 |
|
if (cmp == null) |
3334 |
|
return new ReverseComparator(); // Unchecked warning!! |
3335 |
< |
|
3335 |
> |
|
3336 |
|
return new ReverseComparator2<T>(cmp); |
3337 |
|
} |
3338 |
< |
|
3338 |
> |
|
3339 |
|
/** |
3340 |
|
* @serial include |
3341 |
|
*/ |
3343 |
|
Serializable |
3344 |
|
{ |
3345 |
|
private static final long serialVersionUID = 4374092139857L; |
3346 |
< |
|
3346 |
> |
|
3347 |
|
/** |
3348 |
|
* The comparator specified in the static factory. This will never |
3349 |
|
* be null, as the static factory returns a ReverseComparator |
3352 |
|
* @serial |
3353 |
|
*/ |
3354 |
|
private Comparator<T> cmp; |
3355 |
< |
|
3355 |
> |
|
3356 |
|
ReverseComparator2(Comparator<T> cmp) { |
3357 |
|
assert cmp != null; |
3358 |
|
this.cmp = cmp; |
3359 |
|
} |
3360 |
< |
|
3360 |
> |
|
3361 |
|
public int compare(T t1, T t2) { |
3362 |
|
return cmp.compare(t2, t1); |
3363 |
|
} |
3477 |
|
c1 = c2; |
3478 |
|
c2 = tmp; |
3479 |
|
} |
3480 |
< |
|
3480 |
> |
|
3481 |
|
for (Object e : c1) |
3482 |
|
if (c2.contains(e)) |
3483 |
|
return false; |
3498 |
|
* </pre> |
3499 |
|
* |
3500 |
|
* @param c the collection into which <tt>elements</tt> are to be inserted |
3501 |
< |
* @param a the elements to insert into <tt>c</tt> |
3501 |
> |
* @param elements the elements to insert into <tt>c</tt> |
3502 |
|
* @return <tt>true</tt> if the collection changed as a result of the call |
3503 |
|
* @throws UnsupportedOperationException if <tt>c</tt> does not support |
3504 |
< |
* the <tt>add</tt> method |
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 |
3512 |
|
*/ |
3513 |
< |
public static <T> boolean addAll(Collection<? super T> c, T... a) { |
3513 |
> |
public static <T> boolean addAll(Collection<? super T> c, T... elements) { |
3514 |
|
boolean result = false; |
3515 |
< |
for (T e : a) |
3516 |
< |
result |= c.add(e); |
3515 |
> |
for (T element : elements) |
3516 |
> |
result |= c.add(element); |
3517 |
|
return result; |
3518 |
|
} |
3519 |
|
|
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 |
|
*/ |
3609 |
|
return new AsLIFOQueue<T>(deque); |
3610 |
|
} |
3611 |
|
|
3612 |
< |
static class AsLIFOQueue<E> extends AbstractQueue<E> |
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 o) { return q.offerFirst(o); } |
3617 |
> |
public boolean offer(E e) { return q.offerFirst(e); } |
3618 |
|
public E poll() { return q.pollFirst(); } |
3619 |
|
public E remove() { return q.removeFirst(); } |
3620 |
|
public E peek() { return q.peekFirst(); } |
3625 |
|
public Iterator<E> iterator() { return q.iterator(); } |
3626 |
|
public Object[] toArray() { return q.toArray(); } |
3627 |
|
public <T> T[] toArray(T[] a) { return q.toArray(a); } |
3628 |
< |
public boolean add(E o) { return q.offerFirst(o); } |
3628 |
> |
public boolean add(E e) { return q.offerFirst(e); } |
3629 |
|
public boolean remove(Object o) { return q.remove(o); } |
3630 |
|
public void clear() { q.clear(); } |
3631 |
|
} |