src/share/classes/java/util/Collections.java
Print this page
@@ -148,10 +148,11 @@
* list-iterator does not support the {@code set} operation.
* @throws IllegalArgumentException (optional) if the implementation
* detects that the natural ordering of the list elements is
* found to violate the {@link Comparable} contract
*/
+ @SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
@@ -210,17 +211,18 @@
* @throws UnsupportedOperationException if the specified list's
* list-iterator does not support the {@code set} operation.
* @throws IllegalArgumentException (optional) if the comparator is
* found to violate the {@link Comparator} contract
*/
+ @SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> void sort(List<T> list, Comparator<? super T> c) {
Object[] a = list.toArray();
Arrays.sort(a, (Comparator)c);
- ListIterator i = list.listIterator();
+ ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
- i.set(a[j]);
+ i.set((T)a[j]);
}
}
/**
@@ -355,13 +357,14 @@
* @throws ClassCastException if the list contains elements that are not
* <i>mutually comparable</i> using the specified comparator,
* or the search key is not mutually comparable with the
* elements of the list using this comparator.
*/
+ @SuppressWarnings("unchecked")
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) {
if (c==null)
- return binarySearch((List) list, key);
+ return binarySearch((List<? extends Comparable<? super T>>) list, key);
if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
return Collections.indexedBinarySearch(list, key, c);
else
return Collections.iteratorBinarySearch(list, key, c);
@@ -404,28 +407,29 @@
return mid; // key found
}
return -(low + 1); // key not found
}
- private interface SelfComparable extends Comparable<SelfComparable> {}
-
-
/**
* Reverses the order of the elements in the specified list.<p>
*
* This method runs in linear time.
*
* @param list the list whose elements are to be reversed.
* @throws UnsupportedOperationException if the specified list or
* its list-iterator does not support the <tt>set</tt> operation.
*/
+ @SuppressWarnings({ "rawtypes", "unchecked" })
public static void reverse(List<?> list) {
int size = list.size();
if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {
for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--)
swap(list, i, j);
} else {
+ // instead of using a raw type here, it's possible to capture
+ // the wildcard but it will require a call to a supplementary
+ // private method
ListIterator fwd = list.listIterator();
ListIterator rev = list.listIterator(size);
for (int i=0, mid=list.size()>>1; i<mid; i++) {
Object tmp = fwd.next();
fwd.set(rev.previous());
@@ -491,10 +495,11 @@
* @param list the list to be shuffled.
* @param rnd the source of randomness to use to shuffle the list.
* @throws UnsupportedOperationException if the specified list or its
* list-iterator does not support the <tt>set</tt> operation.
*/
+ @SuppressWarnings({ "rawtypes", "unchecked" })
public static void shuffle(List<?> list, Random rnd) {
int size = list.size();
if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
for (int i=size; i>1; i--)
swap(list, i-1, rnd.nextInt(i));
@@ -504,10 +509,13 @@
// Shuffle array
for (int i=size; i>1; i--)
swap(arr, i-1, rnd.nextInt(i));
// Dump array back into list
+ // instead of using a raw type here, it's possible to capture
+ // the wildcard but it will require a call to a supplementary
+ // private method
ListIterator it = list.listIterator();
for (int i=0; i<arr.length; i++) {
it.next();
it.set(arr[i]);
}
@@ -525,11 +533,15 @@
* @throws IndexOutOfBoundsException if either <tt>i</tt> or <tt>j</tt>
* is out of range (i < 0 || i >= list.size()
* || j < 0 || j >= list.size()).
* @since 1.4
*/
+ @SuppressWarnings({ "rawtypes", "unchecked" })
public static void swap(List<?> list, int i, int j) {
+ // instead of using a raw type here, it's possible to capture
+ // the wildcard but it will require a call to a supplementary
+ // private method
final List l = list;
l.set(i, l.set(j, l.get(i)));
}
/**
@@ -655,13 +667,14 @@
* @throws ClassCastException if the collection contains elements that are
* not <i>mutually comparable</i> using the specified comparator.
* @throws NoSuchElementException if the collection is empty.
* @see Comparable
*/
+ @SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) {
if (comp==null)
- return (T)min((Collection<SelfComparable>) (Collection) coll);
+ return (T)min((Collection) coll);
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
while (i.hasNext()) {
@@ -725,13 +738,14 @@
* @throws ClassCastException if the collection contains elements that are
* not <i>mutually comparable</i> using the specified comparator.
* @throws NoSuchElementException if the collection is empty.
* @see Comparable
*/
+ @SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
if (comp==null)
- return (T)max((Collection<SelfComparable>) (Collection) coll);
+ return (T)max((Collection) coll);
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
while (i.hasNext()) {
@@ -1387,11 +1401,13 @@
*/
static class UnmodifiableEntrySet<K,V>
extends UnmodifiableSet<Map.Entry<K,V>> {
private static final long serialVersionUID = 7854390611657943733L;
+ @SuppressWarnings({ "unchecked", "rawtypes" })
UnmodifiableEntrySet(Set<? extends Map.Entry<? extends K, ? extends V>> s) {
+ // Need to cast to raw in order to work around a limitation in the type system
super((Set)s);
}
public Iterator<Map.Entry<K,V>> iterator() {
return new Iterator<Map.Entry<K,V>>() {
private final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = c.iterator();
@@ -1406,25 +1422,27 @@
throw new UnsupportedOperationException();
}
};
}
+ @SuppressWarnings("unchecked")
public Object[] toArray() {
Object[] a = c.toArray();
for (int i=0; i<a.length; i++)
- a[i] = new UnmodifiableEntry<>((Map.Entry<K,V>)a[i]);
+ a[i] = new UnmodifiableEntry<>((Map.Entry<? extends K, ? extends V>)a[i]);
return a;
}
+ @SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
// We don't pass a to c.toArray, to avoid window of
// vulnerability wherein an unscrupulous multithreaded client
// could get his hands on raw (unwrapped) Entries from c.
Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
for (int i=0; i<arr.length; i++)
- arr[i] = new UnmodifiableEntry<>((Map.Entry<K,V>)arr[i]);
+ arr[i] = new UnmodifiableEntry<>((Map.Entry<? extends K, ? extends V>)arr[i]);
if (arr.length > a.length)
return (T[])arr;
System.arraycopy(arr, 0, a, 0, arr.length);
@@ -1462,11 +1480,11 @@
if (o == this)
return true;
if (!(o instanceof Set))
return false;
- Set s = (Set) o;
+ Set<?> s = (Set<?>) o;
if (s.size() != c.size())
return false;
return containsAll(s); // Invokes safe containsAll() above
}
@@ -1491,11 +1509,11 @@
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Map.Entry))
return false;
- Map.Entry t = (Map.Entry)o;
+ Map.Entry<?,?> t = (Map.Entry<?,?>)o;
return eq(e.getKey(), t.getKey()) &&
eq(e.getValue(), t.getValue());
}
public String toString() {return e.toString();}
}