src/share/classes/java/util/Collections.java

Print this page

        

*** 211,221 **** * @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<T> i = list.listIterator(); for (int j=0; j<a.length; j++) { --- 211,221 ---- * @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<T> i = list.listIterator(); for (int j=0; j<a.length; j++) {
*** 416,426 **** * * @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); --- 416,426 ---- * * @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);
*** 495,505 **** * @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)); --- 495,505 ---- * @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));
*** 533,543 **** * @throws IndexOutOfBoundsException if either <tt>i</tt> or <tt>j</tt> * is out of range (i &lt; 0 || i &gt;= list.size() * || j &lt; 0 || j &gt;= 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; --- 533,543 ---- * @throws IndexOutOfBoundsException if either <tt>i</tt> or <tt>j</tt> * is out of range (i &lt; 0 || i &gt;= list.size() * || j &lt; 0 || j &gt;= 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;
*** 667,677 **** * @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) coll); Iterator<? extends T> i = coll.iterator(); --- 667,677 ---- * @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) coll); Iterator<? extends T> i = coll.iterator();
*** 738,748 **** * @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) coll); Iterator<? extends T> i = coll.iterator(); --- 738,748 ---- * @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) coll); Iterator<? extends T> i = coll.iterator();
*** 1401,1411 **** */ 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() { --- 1401,1411 ---- */ 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() {
*** 3170,3180 **** /** * The empty set (immutable). This set is serializable. * * @see #emptySet() */ ! @SuppressWarnings("unchecked") public static final Set EMPTY_SET = new EmptySet<>(); /** * Returns the empty set (immutable). This set is serializable. * Unlike the like-named field, this method is parameterized. --- 3170,3180 ---- /** * The empty set (immutable). This set is serializable. * * @see #emptySet() */ ! @SuppressWarnings("rawtypes") public static final Set EMPTY_SET = new EmptySet<>(); /** * Returns the empty set (immutable). This set is serializable. * Unlike the like-named field, this method is parameterized.
*** 3269,3282 **** // Preserves singleton property private Object readResolve() { return new EmptySortedSet<>(); } ! public Comparator comparator() { return null; } public SortedSet<E> subSet(Object fromElement, Object toElement) { Objects.requireNonNull(fromElement); Objects.requireNonNull(toElement); if (!(fromElement instanceof Comparable) || --- 3269,3285 ---- // Preserves singleton property private Object readResolve() { return new EmptySortedSet<>(); } ! @Override ! public Comparator<? super E> comparator() { return null; } + @Override + @SuppressWarnings("unchecked") public SortedSet<E> subSet(Object fromElement, Object toElement) { Objects.requireNonNull(fromElement); Objects.requireNonNull(toElement); if (!(fromElement instanceof Comparable) ||
*** 3292,3336 **** } return emptySortedSet(); } public SortedSet<E> headSet(Object toElement) { Objects.requireNonNull(toElement); if (!(toElement instanceof Comparable)) { throw new ClassCastException(); } return emptySortedSet(); } public SortedSet<E> tailSet(Object fromElement) { Objects.requireNonNull(fromElement); if (!(fromElement instanceof Comparable)) { throw new ClassCastException(); } return emptySortedSet(); } public E first() { throw new NoSuchElementException(); } public E last() { throw new NoSuchElementException(); } } /** * The empty list (immutable). This list is serializable. * * @see #emptyList() */ ! @SuppressWarnings("unchecked") public static final List EMPTY_LIST = new EmptyList<>(); /** * Returns the empty list (immutable). This list is serializable. * --- 3295,3343 ---- } return emptySortedSet(); } + @Override public SortedSet<E> headSet(Object toElement) { Objects.requireNonNull(toElement); if (!(toElement instanceof Comparable)) { throw new ClassCastException(); } return emptySortedSet(); } + @Override public SortedSet<E> tailSet(Object fromElement) { Objects.requireNonNull(fromElement); if (!(fromElement instanceof Comparable)) { throw new ClassCastException(); } return emptySortedSet(); } + @Override public E first() { throw new NoSuchElementException(); } + @Override public E last() { throw new NoSuchElementException(); } } /** * The empty list (immutable). This list is serializable. * * @see #emptyList() */ ! @SuppressWarnings("rawtypes") public static final List EMPTY_LIST = new EmptyList<>(); /** * Returns the empty list (immutable). This list is serializable. *
*** 3400,3410 **** * The empty map (immutable). This map is serializable. * * @see #emptyMap() * @since 1.3 */ ! @SuppressWarnings("unchecked") public static final Map EMPTY_MAP = new EmptyMap<>(); /** * Returns the empty map (immutable). This map is serializable. * --- 3407,3417 ---- * The empty map (immutable). This map is serializable. * * @see #emptyMap() * @since 1.3 */ ! @SuppressWarnings("rawtypes") public static final Map EMPTY_MAP = new EmptyMap<>(); /** * Returns the empty map (immutable). This map is serializable. *
*** 3683,3692 **** --- 3690,3700 ---- if (element != null) Arrays.fill(a, 0, n, element); return a; } + @SuppressWarnings("unchecked") public <T> T[] toArray(T[] a) { final int n = this.n; if (a.length < n) { a = (T[])java.lang.reflect.Array .newInstance(a.getClass().getComponentType(), n);
*** 3729,3738 **** --- 3737,3747 ---- * @return A comparator that imposes the reverse of the <i>natural * ordering</i> on a collection of objects that implement * the <tt>Comparable</tt> interface. * @see Comparable */ + @SuppressWarnings("unchecked") public static <T> Comparator<T> reverseOrder() { return (Comparator<T>) ReverseComparator.REVERSE_ORDER; } /**