diff options
author | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2016-03-18 20:29:35 +0000 |
---|---|---|
committer | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2016-03-18 20:29:35 +0000 |
commit | 5a1dcb13eaa6040be54cf3bc6772470a397b2c79 (patch) | |
tree | 0cdd1b74df134896e25feaed039d0f9d2d1f4473 /java/util | |
parent | e7c13ee0cf2005206fbec0eca677f8cf66d5a103 (diff) | |
download | classpath-master.tar.gz |
2016-03-18 Andrew John Hughes <gnu_andrew@member.fsf.org>
Cleanup warnings in java.util.Collections.
* java/util/Collections.java:
(EMPTY_SET): Suppress warnings generated by this
legacy raw type.
(EMPTY_LIST): Likewise.
(EMPTY_MAP): Likewise.
(compare(T, T, Comparator): Suppress warning
generated by casting to Comparable.
(binarySearch(List, Comparator)): Remove unneeded
casts.
(reverse(List)): Suppress warnings when casting
to ListIterator<Object>. Cast is necessary as
lists are modified.
(reverseOrder(Comparator)): Return reverseOrder()
rather than rcInstance.
(reverseOrder()): Create appropriately typed
ReverseComparator rather than using set instance.
(rcInstance): Removed.
(ReverseComparator.compare(T,T)): Suppress warnings
from casting to Comparable<T>.
(rotate(List,int)): Suppress warnings when casting
to List<Object>. Cast is necessary to modify list.
(shuffle(List,Random)): Likewise.
(disjoint(Collection,Collection)): Remove unnecessary
casting.
(sort(List,Comparator)): Suppress warnings when casting
the returned array. Arrays of a generic type can not
be created.
(swap(List,int,int)): Suppress warnings when casting
list to List<Object>. This is necessary to use the
set method.
(entrySet().SynchronizedMapEntry): Type should not have its
own type parameters which hide those of the map.
(entrySet().SynchronizedSet.iterator()): Remove type
parameters from SynchronizedMapEntry creation.
(UnmodifiableList.list): Retain input type parameter
so as to avoid casting.
(UnmodifiableList.UnmodifiableList): Remove cast.
(UnmodifiableList.li): Retain input type parameter
so as to avoid casting.
(UnmodifiableListIterator(ListIterator)): Likewise.
(UnmodifiableMap(Map)): Suppress warnings from casting
down to Map<K,V>.
(UnmodifiableMap.entrySet()): Remove type parameters
from UnmodifiableEntrySet creation.
(UnmodifiableMap.UnmodifiableEntrySet): Remove type
parameters and make instance-specific so we can
use the UnmodifiableMap's type parameters.
(UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableMapEntry):
Likewise.
(UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableMapEntry.e):
Retain input type parameters so as to avoid casting.
(UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableMapEntry.UnmodifiableMapEntry(Map.Entry)):
Likewise.
(UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableEntrySet(Set)):
Likewise.
(UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableIterator.next()):
Remove type parameters from UnmodifiableMapEntry creation.
(UnmodifiableMap.toArray()): Likewise and improve method
structure.
(UnmodifiableMap.toArray(S[])): Remove type parameters from
UnmodifiableMapEntry and suppress warnings from casting
array members.
(UnmodifiableSortedMap.sm): Retain input type parameters
so as to avoid casting.
(UnmodifiableSortedMap.UnmodifiableSortedMap(SortedMap)):
Remove cast.
(CheckedList.addAll(int,Collection)): Replace iterator with for-each
loop, removing the need for casting.
(CheckedMap.entrySet()): Remove type parameters from CheckedEntrySet
creation and klass argument.
(CheckedMap.CheckedEntrySet): Don't create additional
type parameters, use those of the containing map as
a instance class, not a static class.
(keyType): Type should be K to match enclosing CheckedMap.
(valueType): Type should be V to match enclosing CheckedMap.
(CheckedMap.CheckedEntrySet.CheckedEntrySet(Set,Class,Class)):
Suppress warnings when casting Map.Entry class.
(CheckedMap.CheckedEntrySet.iterator()): Make return type
Map.Entry<K,V> and create corresponding CheckedIterator instance.
(CheckedMap.CheckedEntrySet.CheckedIterator.next()): Likewise
with Map.Entry instance.
(CheckedMap.CheckedEntrySet.CheckedIterator.Map.Entry.getKey()):
Make return type K.
(CheckedMap.CheckedEntrySet.CheckedIterator.Map.Entry.getValue()):
Make return type V.
(CheckedMap.CheckedEntrySet.CheckedIterator.Map.Entry.setValue(Object)):
Likewise.
(CheckedMap.putAll(Map)): Use for-each loop instead of Iterator,
avoiding casting.
Diffstat (limited to 'java/util')
-rw-r--r-- | java/util/Collections.java | 167 |
1 files changed, 86 insertions, 81 deletions
diff --git a/java/util/Collections.java b/java/util/Collections.java index b970dd8f6..9fa8b4b7a 100644 --- a/java/util/Collections.java +++ b/java/util/Collections.java @@ -1,5 +1,5 @@ /* Collections.java -- Utility class with methods to operate on collections - Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006 + Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2016 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -110,7 +110,9 @@ public class Collections * An immutable, serializable, empty Set. * @see Serializable */ - public static final Set EMPTY_SET = new EmptySet(); + // Legacy public empty collection constants have no type parameter + @SuppressWarnings("unchecked") + public static final Set EMPTY_SET = new EmptySet<Object>(); /** * Returns an immutable, serializable parameterized empty set. @@ -279,7 +281,9 @@ public class Collections * @see Serializable * @see RandomAccess */ - public static final List EMPTY_LIST = new EmptyList(); + // Legacy public empty collection constants have no type parameter + @SuppressWarnings("unchecked") + public static final List EMPTY_LIST = new EmptyList<Object>(); /** * Returns an immutable, serializable parameterized empty list. @@ -471,7 +475,9 @@ public class Collections * An immutable, serializable, empty Map. * @see Serializable */ - public static final Map EMPTY_MAP = new EmptyMap(); + // Legacy public empty collection constants have no type parameter + @SuppressWarnings("unchecked") + public static final Map EMPTY_MAP = new EmptyMap<Object,Object>(); /** * Returns an immutable, serializable parameterized empty map. @@ -628,9 +634,10 @@ public class Collections * clever, but worth it for removing a duplicate of the search code. * Note: This code is also used in Arrays (for sort as well as search). */ + @SuppressWarnings("unchecked") static final <T> int compare(T o1, T o2, Comparator<? super T> c) { - return c == null ? ((Comparable) o1).compareTo(o2) : c.compare(o1, o2); + return c == null ? ((Comparable<T>) o1).compareTo(o2) : c.compare(o1, o2); } /** @@ -701,7 +708,7 @@ public class Collections // if the list is sequential-access. if (isSequential(l)) { - ListIterator<T> itr = ((List<T>) l).listIterator(); + ListIterator<? extends T> itr = l.listIterator(); int i = 0; T o = itr.next(); // Assumes list is not empty (see isSequential) boolean forward = true; @@ -739,7 +746,7 @@ public class Collections while (low <= hi) { pos = (low + hi) >>> 1; - final int d = compare(((List<T>) l).get(pos), key, c); + final int d = compare(l.get(pos), key, c); if (d == 0) return pos; else if (d > 0) @@ -1188,14 +1195,16 @@ public class Collections */ public static void reverse(List<?> l) { - ListIterator i1 = l.listIterator(); + @SuppressWarnings("unchecked") + ListIterator<Object> i1 = (ListIterator<Object>) l.listIterator(); int pos1 = 1; int pos2 = l.size(); - ListIterator i2 = l.listIterator(pos2); + @SuppressWarnings("unchecked") + ListIterator<Object> i2 = (ListIterator<Object>) l.listIterator(pos2); while (pos1 < pos2) { Object o1 = i1.next(); - Object o2 = i2.previous(); + Object o2 = i2.previous(); i1.set(o2); i2.set(o1); ++pos1; @@ -1220,7 +1229,7 @@ public class Collections public static <T> Comparator<T> reverseOrder(final Comparator<T> c) { if (c == null) - return (Comparator<T>) rcInstance; + return reverseOrder(); return new ReverseComparator<T> () { public int compare(T a, T b) @@ -1243,15 +1252,10 @@ public class Collections */ public static <T> Comparator<T> reverseOrder() { - return (Comparator<T>) rcInstance; + return new ReverseComparator<T>(); } /** - * The object for {@link #reverseOrder()}. - */ - private static final ReverseComparator rcInstance = new ReverseComparator(); - - /** * The implementation of {@link #reverseOrder()}. This class name * is required for compatibility with Sun's JDK serializability. * @@ -1279,9 +1283,10 @@ public class Collections * @param b the second object * @return <, ==, or > 0 according to b.compareTo(a) */ + @SuppressWarnings("unchecked") public int compare(T a, T b) { - return ((Comparable) b).compareTo(a); + return ((Comparable<T>) b).compareTo(a); } } @@ -1348,7 +1353,9 @@ public class Collections // Now, make the swaps. We must take the remainder every time through // the inner loop so that we don't overflow i to negative values. - List<Object> objList = (List<Object>) list; + // We need to be able to modify the list + @SuppressWarnings("unchecked") + List<Object> objList = (List<Object>) list; while (--lcm >= 0) { Object o = objList.get(lcm); @@ -1426,7 +1433,9 @@ public class Collections public static void shuffle(List<?> l, Random r) { int lsize = l.size(); - List<Object> list = (List<Object>) l; + // We need to be able to modify the list + @SuppressWarnings("unchecked") + List<Object> list = (List<Object>) l; ListIterator<Object> i = list.listIterator(lsize); boolean sequential = isSequential(l); Object[] a = null; // stores a copy of the list for the sequential case @@ -1531,8 +1540,7 @@ public class Collections */ public static boolean disjoint(Collection<?> c1, Collection<?> c2) { - Collection<Object> oc1 = (Collection<Object>) c1; - final Iterator<Object> it = oc1.iterator(); + final Iterator<?> it = c1.iterator(); while (it.hasNext()) if (c2.contains(it.next())) return false; @@ -2112,7 +2120,8 @@ public class Collections */ public static <T> void sort(List<T> l, Comparator<? super T> c) { - T[] a = (T[]) l.toArray(); + @SuppressWarnings("unchecked") + T[] a = (T[]) l.toArray(); Arrays.sort(a, c); ListIterator<T> i = l.listIterator(); for (int pos = 0, alen = a.length; pos < alen; pos++) @@ -2136,8 +2145,10 @@ public class Collections */ public static void swap(List<?> l, int i, int j) { - List<Object> list = (List<Object>) l; - list.set(i, list.set(j, list.get(i))); + // We need to be able to modify the list + @SuppressWarnings("unchecked") + List<Object> list = (List<Object>) l; + list.set(i, list.set(j, l.get(i))); } @@ -3304,7 +3315,7 @@ public class Collections public Set<Map.Entry<K, V>> entrySet() { // Define this here to spare some nesting. - class SynchronizedMapEntry<K, V> implements Map.Entry<K, V> + class SynchronizedMapEntry implements Map.Entry<K, V> { final Map.Entry<K, V> e; SynchronizedMapEntry(Map.Entry<K, V> o) @@ -3445,7 +3456,7 @@ public class Collections { synchronized (super.mutex) { - return new SynchronizedMapEntry<K, V>(super.next()); + return new SynchronizedMapEntry(super.next()); } } }; @@ -4541,7 +4552,7 @@ public class Collections * excessive casting. Package visible for use by subclass. * @serial the wrapped list */ - final List<T> list; + final List<? extends T> list; /** * Wrap a given list. @@ -4551,7 +4562,7 @@ public class Collections UnmodifiableList(List<? extends T> l) { super(l); - list = (List<T>) l; + list = l; } /** @@ -4776,13 +4787,13 @@ public class Collections * The wrapped iterator, stored both here and in the superclass to * avoid excessive casting. */ - private final ListIterator<T> li; + private final ListIterator<? extends T> li; /** * Only trusted code creates a wrapper. * @param li the wrapped iterator */ - UnmodifiableListIterator(ListIterator<T> li) + UnmodifiableListIterator(ListIterator<? extends T> li) { super(li); this.li = li; @@ -4905,7 +4916,7 @@ public class Collections * The wrapped map. * @serial the real map */ - private final Map<K, V> m; + private final Map<K,V> m; /** * Cache the entry set. @@ -4927,6 +4938,7 @@ public class Collections * @param m the map to wrap * @throws NullPointerException if m is null */ + @SuppressWarnings("unchecked") UnmodifiableMap(Map<? extends K, ? extends V> m) { this.m = (Map<K,V>) m; @@ -4993,7 +5005,7 @@ public class Collections public Set<Map.Entry<K, V>> entrySet() { if (entries == null) - entries = new UnmodifiableEntrySet<K,V>(m.entrySet()); + entries = new UnmodifiableEntrySet(m.entrySet()); return entries; } @@ -5003,18 +5015,18 @@ public class Collections * * @author Eric Blake (ebb9@email.byu.edu) */ - private static final class UnmodifiableEntrySet<K,V> + private final class UnmodifiableEntrySet extends UnmodifiableSet<Map.Entry<K,V>> implements Serializable { // Unmodifiable implementation of Map.Entry used as return value for // UnmodifiableEntrySet accessors (iterator, toArray, toArray(Object[])) - private static final class UnmodifiableMapEntry<K,V> + private final class UnmodifiableMapEntry implements Map.Entry<K,V> { - private final Map.Entry<K,V> e; + private final Map.Entry<? extends K,? extends V> e; - private UnmodifiableMapEntry(Map.Entry<K,V> e) + private UnmodifiableMapEntry(Map.Entry<? extends K,? extends V> e) { super(); this.e = e; @@ -5097,7 +5109,7 @@ public class Collections * Wrap a given set. * @param s the set to wrap */ - UnmodifiableEntrySet(Set<Map.Entry<K,V>> s) + UnmodifiableEntrySet(Set<? extends Map.Entry<K,V>> s) { super(s); } @@ -5117,7 +5129,7 @@ public class Collections public Map.Entry<K,V> next() { final Map.Entry<K,V> e = super.next(); - return new UnmodifiableMapEntry<K,V>(e); + return new UnmodifiableMapEntry(e); } }; } @@ -5126,21 +5138,23 @@ public class Collections // Map.Entry public Object[] toArray() { - Object[] mapEntryResult = super.toArray(); - UnmodifiableMapEntry<K,V> result[] = null; + Object[] result = super.toArray(); - if (mapEntryResult != null) - { - result = (UnmodifiableMapEntry<K,V>[]) - new UnmodifiableMapEntry[mapEntryResult.length]; - for (int i = 0; i < mapEntryResult.length; ++i) - result[i] = new UnmodifiableMapEntry<K,V>((Map.Entry<K,V>)mapEntryResult[i]); - } + if (result == null) + return null; + + for (int i = 0; i < result.length; ++i) + { + @SuppressWarnings("unchecked") + Map.Entry<K,V> entry = (Map.Entry<K,V>) result[i]; + result[i] = new UnmodifiableMapEntry(entry); + } return result; } // The array returned is an array of UnmodifiableMapEntry instead of // Map.Entry + @SuppressWarnings("unchecked") public <S> S[] toArray(S[] array) { S[] result = super.toArray(array); @@ -5148,7 +5162,7 @@ public class Collections if (result != null) for (int i = 0; i < result.length; i++) array[i] = - (S) new UnmodifiableMapEntry<K,V>((Map.Entry<K,V>) result[i]); + (S) new UnmodifiableMapEntry((Map.Entry<K,V>) result[i]); return array; } @@ -5420,7 +5434,7 @@ public class Collections * excessive casting. * @serial the wrapped map */ - private final SortedMap<K, V> sm; + private final SortedMap<K, ? extends V> sm; /** * Wrap a given map. @@ -5430,7 +5444,7 @@ public class Collections UnmodifiableSortedMap(SortedMap<K, ? extends V> sm) { super(sm); - this.sm = (SortedMap<K,V>) sm; + this.sm = sm; } /** @@ -6179,11 +6193,9 @@ public class Collections */ public boolean addAll(int index, Collection<? extends E> coll) { - Collection<E> typedColl = (Collection<E>) coll; - final Iterator<E> it = typedColl.iterator(); - while (it.hasNext()) + for (E entry : coll) { - if (!type.isInstance(it.next())) + if (!type.isInstance(entry)) throw new ClassCastException("A member of the collection is not of the correct type."); } return list.addAll(index, coll); @@ -6635,12 +6647,7 @@ public class Collections { if (entries == null) { - Class<Map.Entry<K,V>> klass = - (Class<Map.Entry<K,V>>) (Class) Map.Entry.class; - entries = new CheckedEntrySet<Map.Entry<K,V>,K,V>(m.entrySet(), - klass, - keyType, - valueType); + entries = new CheckedEntrySet(m.entrySet(), keyType, valueType); } return entries; } @@ -6652,20 +6659,20 @@ public class Collections * @author Andrew John Hughes (gnu_andrew@member.fsf.org) * @since 1.5 */ - private static final class CheckedEntrySet<E,SK,SV> - extends CheckedSet<E> + private final class CheckedEntrySet + extends CheckedSet<Map.Entry<K,V>> { /** * The type of the map's keys. * @serial the key type. */ - private final Class<SK> keyType; + private final Class<K> keyType; /** * The type of the map's values. * @serial the value type. */ - private final Class<SV> valueType; + private final Class<V> valueType; /** * Wrap a given set of map entries. @@ -6675,18 +6682,19 @@ public class Collections * @param keyType the type of the map's keys. * @param valueType the type of the map's values. */ - CheckedEntrySet(Set<E> s, Class<E> type, Class<SK> keyType, - Class<SV> valueType) + @SuppressWarnings("unchecked") + CheckedEntrySet(Set<Map.Entry<K,V>> s, Class<K> keyType, + Class<V> valueType) { - super(s, type); + super(s, (Class<Map.Entry<K,V>>) (Class<?>) Map.Entry.class); this.keyType = keyType; this.valueType = valueType; } // The iterator must return checked map entries. - public Iterator<E> iterator() + public Iterator<Map.Entry<K,V>> iterator() { - return new CheckedIterator<E>(c.iterator(), type) + return new CheckedIterator<Map.Entry<K,V>>(c.iterator(), type) { /** * Obtains the next element from the underlying set of @@ -6695,10 +6703,10 @@ public class Collections * @return the next element in the collection. * @throws NoSuchElementException if there are no more elements. */ - public E next() + public Map.Entry<K,V> next() { - final Map.Entry e = (Map.Entry) super.next(); - return (E) new Map.Entry() + final Map.Entry<K,V> e = super.next(); + return new Map.Entry<K,V>() { /** * Returns <code>true</code> if the object, o, is also a map @@ -6717,7 +6725,7 @@ public class Collections * * @return the key. */ - public Object getKey() + public K getKey() { return e.getKey(); } @@ -6727,7 +6735,7 @@ public class Collections * * @return the value. */ - public Object getValue() + public V getValue() { return e.getValue(); } @@ -6754,7 +6762,7 @@ public class Collections * a valid type for the underlying * map. */ - public Object setValue(Object value) + public V setValue(V value) { if (valueType.isInstance(value)) return e.setValue(value); @@ -6883,17 +6891,14 @@ public class Collections */ public void putAll(Map<? extends K, ? extends V> map) { - Map<K,V> typedMap = (Map<K,V>) map; - final Iterator<Map.Entry<K,V>> it = typedMap.entrySet().iterator(); - while (it.hasNext()) + for (Map.Entry<? extends K,? extends V> entry : map.entrySet()) { - final Map.Entry<K,V> entry = it.next(); if (!keyType.isInstance(entry.getKey())) throw new ClassCastException("A key is of the wrong type."); if (!valueType.isInstance(entry.getValue())) throw new ClassCastException("A value is of the wrong type."); } - m.putAll(typedMap); + m.putAll(map); } /** |