8301120: Cleanup utility classes java.util.Arrays and java.util.Collections

Reviewed-by: smarks, darcy
This commit is contained in:
Tagir F. Valeev 2023-01-27 19:23:53 +00:00
parent b8e5abc1e8
commit ae0e76d3dd
2 changed files with 22 additions and 28 deletions

View file

@ -25,7 +25,6 @@
package java.util;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.ArraysSupport;
import jdk.internal.vm.annotation.IntrinsicCandidate;
@ -2507,7 +2506,7 @@ public class Arrays {
* @param a2 the other array to be tested for equality
* @return {@code true} if the two arrays are equal
*/
public static boolean equals(short[] a, short a2[]) {
public static boolean equals(short[] a, short[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)

View file

@ -141,7 +141,6 @@ public class Collections {
* found to violate the {@link Comparable} contract
* @see List#sort(Comparator)
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}
@ -175,7 +174,6 @@ public class Collections {
* found to violate the {@link Comparator} contract
* @see List#sort(Comparator)
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> void sort(List<T> list, Comparator<? super T> c) {
list.sort(c);
}
@ -267,7 +265,7 @@ public class Collections {
* list listIterator.
*/
private static <T> T get(ListIterator<? extends T> i, int index) {
T obj = null;
T obj;
int pos = i.nextIndex();
if (pos <= index) {
do {
@ -651,10 +649,10 @@ public class Collections {
* @throws NoSuchElementException if the collection is empty.
* @see Comparable
*/
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({"unchecked"})
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) {
if (comp==null)
return (T)min((Collection) coll);
return (T)min((Collection<Comparable<Object>>) coll);
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
@ -724,10 +722,10 @@ public class Collections {
* @throws NoSuchElementException if the collection is empty.
* @see Comparable
*/
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({"unchecked"})
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
if (comp==null)
return (T)max((Collection) coll);
return (T)max((Collection<Comparable<Object>>) coll);
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
@ -1067,7 +1065,7 @@ public class Collections {
public String toString() {return c.toString();}
public Iterator<E> iterator() {
return new Iterator<E>() {
return new Iterator<>() {
private final Iterator<? extends E> i = c.iterator();
public boolean hasNext() {return i.hasNext();}
@ -1279,7 +1277,6 @@ public class Collections {
private Object readResolve() { return EMPTY_NAVIGABLE_SET; }
}
@SuppressWarnings("rawtypes")
private static final NavigableSet<?> EMPTY_NAVIGABLE_SET =
new EmptyNavigableSet<>();
@ -1392,7 +1389,7 @@ public class Collections {
public ListIterator<E> listIterator() {return listIterator(0);}
public ListIterator<E> listIterator(final int index) {
return new ListIterator<E>() {
return new ListIterator<>() {
private final ListIterator<? extends E> i
= list.listIterator(index);
@ -1634,10 +1631,9 @@ public class Collections {
@java.io.Serial
private static final long serialVersionUID = 7854390611657943733L;
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({"unchecked"})
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);
super((Set<Map.Entry<K, V>>)s);
}
static <K, V> Consumer<Map.Entry<? extends K, ? extends V>> entryConsumer(
@ -1721,7 +1717,7 @@ public class Collections {
}
public Iterator<Map.Entry<K,V>> iterator() {
return new Iterator<Map.Entry<K,V>>() {
return new Iterator<>() {
private final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = c.iterator();
public boolean hasNext() {
@ -1920,7 +1916,7 @@ public class Collections {
private static final long serialVersionUID = -4858195264774772197L;
/**
* A class for the {@link EMPTY_NAVIGABLE_MAP} which needs readResolve
* A class for the {@link #EMPTY_NAVIGABLE_MAP} which needs readResolve
* to preserve singleton property.
*
* @param <K> type of keys, if there were any, and of bounds
@ -1943,7 +1939,7 @@ public class Collections {
}
/**
* Singleton for {@link emptyNavigableMap()} which is also immutable.
* Singleton for {@link #emptyNavigableMap()} which is also immutable.
*/
private static final EmptyNavigableMap<?,?> EMPTY_NAVIGABLE_MAP =
new EmptyNavigableMap<>();
@ -3191,7 +3187,7 @@ public class Collections {
// JDK-6363904 - unwrapped iterator could be typecast to
// ListIterator with unsafe set()
final Iterator<E> it = c.iterator();
return new Iterator<E>() {
return new Iterator<>() {
public boolean hasNext() { return it.hasNext(); }
public E next() { return it.next(); }
public void remove() { it.remove(); }
@ -3582,7 +3578,7 @@ public class Collections {
public ListIterator<E> listIterator(final int index) {
final ListIterator<E> i = list.listIterator(index);
return new ListIterator<E>() {
return new ListIterator<>() {
public boolean hasNext() { return i.hasNext(); }
public E next() { return i.next(); }
public boolean hasPrevious() { return i.hasPrevious(); }
@ -3890,7 +3886,7 @@ public class Collections {
public Iterator<Map.Entry<K,V>> iterator() {
final Iterator<Map.Entry<K, V>> i = s.iterator();
return new Iterator<Map.Entry<K,V>>() {
return new Iterator<>() {
public boolean hasNext() { return i.hasNext(); }
public void remove() { i.remove(); }
@ -4745,7 +4741,6 @@ public class Collections {
// Override default methods in Map
@Override
@SuppressWarnings("unchecked")
public V getOrDefault(Object k, V defaultValue) {
return defaultValue;
}
@ -4826,7 +4821,7 @@ public class Collections {
}
static <E> Iterator<E> singletonIterator(final E e) {
return new Iterator<E>() {
return new Iterator<>() {
private boolean hasNext = true;
public boolean hasNext() {
return hasNext;
@ -4859,7 +4854,7 @@ public class Collections {
* @return A singleton {@code Spliterator}
*/
static <T> Spliterator<T> singletonSpliterator(final T element) {
return new Spliterator<T>() {
return new Spliterator<>() {
long est = 1;
@Override
@ -5061,7 +5056,7 @@ public class Collections {
public Set<Map.Entry<K,V>> entrySet() {
if (entrySet==null)
entrySet = Collections.<Map.Entry<K,V>>singleton(
entrySet = Collections.singleton(
new SimpleImmutableEntry<>(k, v));
return entrySet;
}
@ -5429,8 +5424,8 @@ public class Collections {
public boolean equals(Object o) {
return (o == this) ||
(o instanceof ReverseComparator2 &&
cmp.equals(((ReverseComparator2)o).cmp));
(o instanceof ReverseComparator2<?> that &&
cmp.equals(that.cmp));
}
public int hashCode() {
@ -5459,7 +5454,7 @@ public class Collections {
* @see Enumeration
*/
public static <T> Enumeration<T> enumeration(final Collection<T> c) {
return new Enumeration<T>() {
return new Enumeration<>() {
private final Iterator<T> i = c.iterator();
public boolean hasMoreElements() {