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; package java.util;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.ArraysSupport; import jdk.internal.util.ArraysSupport;
import jdk.internal.vm.annotation.IntrinsicCandidate; import jdk.internal.vm.annotation.IntrinsicCandidate;
@ -2507,7 +2506,7 @@ public class Arrays {
* @param a2 the other array to be tested for equality * @param a2 the other array to be tested for equality
* @return {@code true} if the two arrays are equal * @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) if (a==a2)
return true; return true;
if (a==null || a2==null) if (a==null || a2==null)

View file

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