[COLLECTIONS-485] Added wildcards to copy-constructors and unmodifiable decorators / iterators, thanks to Hollis Waite.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1533984 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-10-20 21:12:51 +00:00
parent ba1e716261
commit 4fca8268a9
48 changed files with 158 additions and 109 deletions

View File

@ -22,6 +22,10 @@
<body> <body>
<release version="4.0" date="TBA" description="Next release"> <release version="4.0" date="TBA" description="Next release">
<action issue="COLLECTIONS-485" dev="tn" type="fix" due-to="Hollis Waite">
Accept wildcard input where possible, e.g. in copy-constructors, Unmodifiable* decorators
and iterators.
</action>
<action issue="COLLECTIONS-481" dev="tn" type="fix" due-to="Hollis Waite"> <action issue="COLLECTIONS-481" dev="tn" type="fix" due-to="Hollis Waite">
No collision detection/resolution was performed when calling "CompositeSet#addComposited(...)" No collision detection/resolution was performed when calling "CompositeSet#addComposited(...)"
with more than one Set as argument. Additionally use varargs parameters instead of arrays with more than one Set as argument. Additionally use varargs parameters instead of arrays

View File

@ -92,7 +92,7 @@ public class BagUtils {
* @return an unmodifiable view of that bag * @return an unmodifiable view of that bag
* @throws IllegalArgumentException if the Bag is null * @throws IllegalArgumentException if the Bag is null
*/ */
public static <E> Bag<E> unmodifiableBag(final Bag<E> bag) { public static <E> Bag<E> unmodifiableBag(final Bag<? extends E> bag) {
return UnmodifiableBag.unmodifiableBag(bag); return UnmodifiableBag.unmodifiableBag(bag);
} }

View File

@ -1701,7 +1701,7 @@ public class CollectionUtils {
* @return an unmodifiable collection backed by the given collection * @return an unmodifiable collection backed by the given collection
* @throws IllegalArgumentException if the collection is null * @throws IllegalArgumentException if the collection is null
*/ */
public static <C> Collection<C> unmodifiableCollection(final Collection<C> collection) { public static <C> Collection<C> unmodifiableCollection(final Collection<? extends C> collection) {
return UnmodifiableCollection.unmodifiableCollection(collection); return UnmodifiableCollection.unmodifiableCollection(collection);
} }

View File

@ -47,7 +47,7 @@ public class EnumerationUtils {
* @return a list containing all elements of the given enumeration * @return a list containing all elements of the given enumeration
* @throws NullPointerException if the enumeration parameter is <code>null</code>. * @throws NullPointerException if the enumeration parameter is <code>null</code>.
*/ */
public static <E> List<E> toList(final Enumeration<E> enumeration) { public static <E> List<E> toList(final Enumeration<? extends E> enumeration) {
return IteratorUtils.toList(new EnumerationIterator<E>(enumeration)); return IteratorUtils.toList(new EnumerationIterator<E>(enumeration));
} }

View File

@ -398,7 +398,7 @@ public class ListUtils {
* @return an unmodifiable list backed by the given list * @return an unmodifiable list backed by the given list
* @throws IllegalArgumentException if the list is null * @throws IllegalArgumentException if the list is null
*/ */
public static <E> List<E> unmodifiableList(final List<E> list) { public static <E> List<E> unmodifiableList(final List<? extends E> list) {
return UnmodifiableList.unmodifiableList(list); return UnmodifiableList.unmodifiableList(list);
} }

View File

@ -1258,7 +1258,7 @@ public class MapUtils {
* @return an unmodifiable map backed by the given map * @return an unmodifiable map backed by the given map
* @throws IllegalArgumentException if the map is null * @throws IllegalArgumentException if the map is null
*/ */
public static <K, V> Map<K, V> unmodifiableMap(final Map<K, V> map) { public static <K, V> Map<K, V> unmodifiableMap(final Map<? extends K, ? extends V> map) {
return UnmodifiableMap.unmodifiableMap(map); return UnmodifiableMap.unmodifiableMap(map);
} }
@ -1517,7 +1517,7 @@ public class MapUtils {
* @return an unmodifiable map backed by the given map * @return an unmodifiable map backed by the given map
* @throws IllegalArgumentException if the map is null * @throws IllegalArgumentException if the map is null
*/ */
public static <K, V> SortedMap<K, V> unmodifiableSortedMap(final SortedMap<K, V> map) { public static <K, V> SortedMap<K, V> unmodifiableSortedMap(final SortedMap<K, ? extends V> map) {
return UnmodifiableSortedMap.unmodifiableSortedMap(map); return UnmodifiableSortedMap.unmodifiableSortedMap(map);
} }

View File

@ -51,7 +51,7 @@ public class QueueUtils {
* @return an unmodifiable queue backed by that queue * @return an unmodifiable queue backed by that queue
* @throws IllegalArgumentException if the Queue is null * @throws IllegalArgumentException if the Queue is null
*/ */
public static <E> Queue<E> unmodifiableQueue(final Queue<E> queue) { public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) {
return UnmodifiableQueue.unmodifiableQueue(queue); return UnmodifiableQueue.unmodifiableQueue(queue);
} }

View File

@ -188,7 +188,7 @@ public class SetUtils {
* @return an unmodifiable set backed by the given set * @return an unmodifiable set backed by the given set
* @throws IllegalArgumentException if the set is null * @throws IllegalArgumentException if the set is null
*/ */
public static <E> Set<E> unmodifiableSet(final Set<E> set) { public static <E> Set<E> unmodifiableSet(final Set<? extends E> set) {
return UnmodifiableSet.unmodifiableSet(set); return UnmodifiableSet.unmodifiableSet(set);
} }

View File

@ -41,7 +41,7 @@ public class TrieUtils {
* *
* @see java.util.Collections#unmodifiableMap(java.util.Map) * @see java.util.Collections#unmodifiableMap(java.util.Map)
*/ */
public static <K, V> Trie<K, V> unmodifiableTrie(final Trie<K, V> trie) { public static <K, V> Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) {
return UnmodifiableTrie.unmodifiableTrie(trie); return UnmodifiableTrie.unmodifiableTrie(trie);
} }

View File

@ -56,9 +56,11 @@ public final class UnmodifiableBag<E>
* @throws IllegalArgumentException if bag is null * @throws IllegalArgumentException if bag is null
* @since 4.0 * @since 4.0
*/ */
public static <E> Bag<E> unmodifiableBag(final Bag<E> bag) { public static <E> Bag<E> unmodifiableBag(final Bag<? extends E> bag) {
if (bag instanceof Unmodifiable) { if (bag instanceof Unmodifiable) {
return bag; @SuppressWarnings("unchecked") // safe to upcast
final Bag<E> tmpBag = (Bag<E>) bag;
return tmpBag;
} }
return new UnmodifiableBag<E>(bag); return new UnmodifiableBag<E>(bag);
} }
@ -70,8 +72,9 @@ public final class UnmodifiableBag<E>
* @param bag the bag to decorate, must not be null * @param bag the bag to decorate, must not be null
* @throws IllegalArgumentException if bag is null * @throws IllegalArgumentException if bag is null
*/ */
private UnmodifiableBag(final Bag<E> bag) { @SuppressWarnings("unchecked") // safe to upcast
super(bag); private UnmodifiableBag(final Bag<? extends E> bag) {
super((Bag<E>) bag);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -57,7 +57,7 @@ public class DualHashBidiMap<K, V> extends AbstractDualBidiMap<K, V> implements
* *
* @param map the map whose mappings are to be placed in this map * @param map the map whose mappings are to be placed in this map
*/ */
public DualHashBidiMap(final Map<K, V> map) { public DualHashBidiMap(final Map<? extends K, ? extends V> map) {
super(new HashMap<K, V>(), new HashMap<V, K>()); super(new HashMap<K, V>(), new HashMap<V, K>());
putAll(map); putAll(map);
} }

View File

@ -16,8 +16,6 @@
*/ */
package org.apache.commons.collections4.bidimap; package org.apache.commons.collections4.bidimap;
import org.apache.commons.collections4.BidiMap;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
@ -25,6 +23,8 @@ import java.io.Serializable;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import org.apache.commons.collections4.BidiMap;
/** /**
* Implementation of <code>BidiMap</code> that uses two <code>LinkedHashMap</code> instances. * Implementation of <code>BidiMap</code> that uses two <code>LinkedHashMap</code> instances.
* <p> * <p>
@ -52,7 +52,7 @@ public class DualLinkedHashBidiMap<K, V> extends AbstractDualBidiMap<K, V> imple
* *
* @param map the map whose mappings are to be placed in this map * @param map the map whose mappings are to be placed in this map
*/ */
public DualLinkedHashBidiMap(final Map<K, V> map) { public DualLinkedHashBidiMap(final Map<? extends K, ? extends V> map) {
super(new LinkedHashMap<K, V>(), new LinkedHashMap<V, K>()); super(new LinkedHashMap<K, V>(), new LinkedHashMap<V, K>());
putAll(map); putAll(map);
} }

View File

@ -79,7 +79,7 @@ public class DualTreeBidiMap<K, V> extends AbstractDualBidiMap<K, V>
* *
* @param map the map whose mappings are to be placed in this map * @param map the map whose mappings are to be placed in this map
*/ */
public DualTreeBidiMap(final Map<K, V> map) { public DualTreeBidiMap(final Map<? extends K, ? extends V> map) {
super(new TreeMap<K, V>(), new TreeMap<V, K>()); super(new TreeMap<K, V>(), new TreeMap<V, K>());
putAll(map); putAll(map);
this.comparator = null; this.comparator = null;

View File

@ -126,7 +126,7 @@ public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>>
* not Comparable or are not mutually comparable * not Comparable or are not mutually comparable
* @throws NullPointerException if any key or value in the map is null * @throws NullPointerException if any key or value in the map is null
*/ */
public TreeBidiMap(final Map<K, V> map) { public TreeBidiMap(final Map<? extends K, ? extends V> map) {
this(); this();
putAll(map); putAll(map);
} }

View File

@ -52,9 +52,11 @@ public final class UnmodifiableBidiMap<K, V>
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
* @since 4.0 * @since 4.0
*/ */
public static <K, V> BidiMap<K, V> unmodifiableBidiMap(final BidiMap<K, V> map) { public static <K, V> BidiMap<K, V> unmodifiableBidiMap(final BidiMap<? extends K, ? extends V> map) {
if (map instanceof Unmodifiable) { if (map instanceof Unmodifiable) {
return map; @SuppressWarnings("unchecked") // safe to upcast
final BidiMap<K, V> tmpMap = (BidiMap<K, V>) map;
return tmpMap;
} }
return new UnmodifiableBidiMap<K, V>(map); return new UnmodifiableBidiMap<K, V>(map);
} }
@ -66,8 +68,9 @@ public final class UnmodifiableBidiMap<K, V>
* @param map the map to decorate, must not be null * @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
*/ */
private UnmodifiableBidiMap(final BidiMap<K, V> map) { @SuppressWarnings("unchecked") // safe to upcast
super(map); private UnmodifiableBidiMap(final BidiMap<? extends K, ? extends V> map) {
super((BidiMap<K, V>) map);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -52,9 +52,12 @@ public final class UnmodifiableOrderedBidiMap<K, V>
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
* @since 4.0 * @since 4.0
*/ */
public static <K, V> OrderedBidiMap<K, V> unmodifiableOrderedBidiMap(final OrderedBidiMap<K, V> map) { public static <K, V> OrderedBidiMap<K, V> unmodifiableOrderedBidiMap(
final OrderedBidiMap<? extends K, ? extends V> map) {
if (map instanceof Unmodifiable) { if (map instanceof Unmodifiable) {
return map; @SuppressWarnings("unchecked") // safe to upcast
final OrderedBidiMap<K, V> tmpMap = (OrderedBidiMap<K, V>) map;
return tmpMap;
} }
return new UnmodifiableOrderedBidiMap<K, V>(map); return new UnmodifiableOrderedBidiMap<K, V>(map);
} }
@ -66,8 +69,9 @@ public final class UnmodifiableOrderedBidiMap<K, V>
* @param map the map to decorate, must not be null * @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
*/ */
private UnmodifiableOrderedBidiMap(final OrderedBidiMap<K, V> map) { @SuppressWarnings("unchecked") // safe to upcast
super(map); private UnmodifiableOrderedBidiMap(final OrderedBidiMap<? extends K, ? extends V> map) {
super((OrderedBidiMap<K, V>) map);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -54,9 +54,11 @@ public final class UnmodifiableSortedBidiMap<K, V>
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
* @since 4.0 * @since 4.0
*/ */
public static <K, V> SortedBidiMap<K, V> unmodifiableSortedBidiMap(final SortedBidiMap<K, V> map) { public static <K, V> SortedBidiMap<K, V> unmodifiableSortedBidiMap(final SortedBidiMap<K, ? extends V> map) {
if (map instanceof Unmodifiable) { if (map instanceof Unmodifiable) {
return map; @SuppressWarnings("unchecked") // safe to upcast
final SortedBidiMap<K, V> tmpMap = (SortedBidiMap<K, V>) map;
return tmpMap;
} }
return new UnmodifiableSortedBidiMap<K, V>(map); return new UnmodifiableSortedBidiMap<K, V>(map);
} }
@ -68,8 +70,9 @@ public final class UnmodifiableSortedBidiMap<K, V>
* @param map the map to decorate, must not be null * @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
*/ */
private UnmodifiableSortedBidiMap(final SortedBidiMap<K, V> map) { @SuppressWarnings("unchecked") // safe to upcast
super(map); private UnmodifiableSortedBidiMap(final SortedBidiMap<K, ? extends V> map) {
super((SortedBidiMap<K, V>) map);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -54,7 +54,7 @@ public final class UnmodifiableBoundedCollection<E> extends AbstractCollectionDe
* @throws IllegalArgumentException if {@code coll} is {@code null} * @throws IllegalArgumentException if {@code coll} is {@code null}
* @since 4.0 * @since 4.0
*/ */
public static <E> BoundedCollection<E> unmodifiableBoundedCollection(final BoundedCollection<E> coll) { public static <E> BoundedCollection<E> unmodifiableBoundedCollection(final BoundedCollection<? extends E> coll) {
return new UnmodifiableBoundedCollection<E>(coll); return new UnmodifiableBoundedCollection<E>(coll);
} }
@ -100,8 +100,9 @@ public final class UnmodifiableBoundedCollection<E> extends AbstractCollectionDe
* @param coll the collection to decorate, must not be null * @param coll the collection to decorate, must not be null
* @throws IllegalArgumentException if coll is null * @throws IllegalArgumentException if coll is null
*/ */
private UnmodifiableBoundedCollection(final BoundedCollection<E> coll) { @SuppressWarnings("unchecked") // safe to upcast
super(coll); private UnmodifiableBoundedCollection(final BoundedCollection<? extends E> coll) {
super((BoundedCollection<E>) coll);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -51,9 +51,11 @@ public final class UnmodifiableCollection<E>
* @throws IllegalArgumentException if collection is null * @throws IllegalArgumentException if collection is null
* @since 4.0 * @since 4.0
*/ */
public static <T> Collection<T> unmodifiableCollection(final Collection<T> coll) { public static <T> Collection<T> unmodifiableCollection(final Collection<? extends T> coll) {
if (coll instanceof Unmodifiable) { if (coll instanceof Unmodifiable) {
return coll; @SuppressWarnings("unchecked") // safe to upcast
final Collection<T> tmpColl = (Collection<T>) coll;
return tmpColl;
} }
return new UnmodifiableCollection<T>(coll); return new UnmodifiableCollection<T>(coll);
} }
@ -65,8 +67,9 @@ public final class UnmodifiableCollection<E>
* @param coll the collection to decorate, must not be null * @param coll the collection to decorate, must not be null
* @throws IllegalArgumentException if collection is null * @throws IllegalArgumentException if collection is null
*/ */
private UnmodifiableCollection(final Collection<E> coll) { @SuppressWarnings("unchecked") // safe to upcast
super(coll); private UnmodifiableCollection(final Collection<? extends E> coll) {
super((Collection<E>) coll);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -273,7 +273,7 @@ public class ComparatorChain<E> implements Comparator<E>, Serializable {
final Iterator<Comparator<E>> comparators = comparatorChain.iterator(); final Iterator<Comparator<E>> comparators = comparatorChain.iterator();
for (int comparatorIndex = 0; comparators.hasNext(); ++comparatorIndex) { for (int comparatorIndex = 0; comparators.hasNext(); ++comparatorIndex) {
final Comparator<E> comparator = comparators.next(); final Comparator<? super E> comparator = comparators.next();
int retval = comparator.compare(o1,o2); int retval = comparator.compare(o1,o2);
if (retval != 0) { if (retval != 0) {
// invert the order if it is a reverse sort // invert the order if it is a reverse sort

View File

@ -36,7 +36,7 @@ public class NullComparator<E> implements Comparator<E>, Serializable {
/** /**
* The comparator to use when comparing two non-<code>null</code> objects. * The comparator to use when comparing two non-<code>null</code> objects.
**/ **/
private final Comparator<E> nonNullComparator; private final Comparator<? super E> nonNullComparator;
/** /**
* Specifies whether a <code>null</code> are compared as higher than * Specifies whether a <code>null</code> are compared as higher than
@ -69,7 +69,7 @@ public class NullComparator<E> implements Comparator<E>, Serializable {
* @exception NullPointerException if <code>nonNullComparator</code> is * @exception NullPointerException if <code>nonNullComparator</code> is
* <code>null</code> * <code>null</code>
**/ **/
public NullComparator(final Comparator<E> nonNullComparator) { public NullComparator(final Comparator<? super E> nonNullComparator) {
this(nonNullComparator, true); this(nonNullComparator, true);
} }
@ -109,7 +109,7 @@ public class NullComparator<E> implements Comparator<E>, Serializable {
* @exception NullPointerException if <code>nonNullComparator</code> is * @exception NullPointerException if <code>nonNullComparator</code> is
* <code>null</code> * <code>null</code>
**/ **/
public NullComparator(final Comparator<E> nonNullComparator, final boolean nullsAreHigh) { public NullComparator(final Comparator<? super E> nonNullComparator, final boolean nullsAreHigh) {
this.nonNullComparator = nonNullComparator; this.nonNullComparator = nonNullComparator;
this.nullsAreHigh = nullsAreHigh; this.nullsAreHigh = nullsAreHigh;

View File

@ -60,7 +60,7 @@ public class ReverseComparator<E> implements Comparator<E>, Serializable {
* @param comparator Comparator to reverse * @param comparator Comparator to reverse
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public ReverseComparator(final Comparator<E> comparator) { public ReverseComparator(final Comparator<? super E> comparator) {
this.comparator = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator; this.comparator = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator;
} }

View File

@ -73,7 +73,7 @@ public class PermutationIterator<E> implements Iterator<List<E>> {
* @param coll the collection to generate permutations for * @param coll the collection to generate permutations for
* @throws NullPointerException if coll is null * @throws NullPointerException if coll is null
*/ */
public PermutationIterator(final Collection<E> coll) { public PermutationIterator(final Collection<? extends E> coll) {
if (coll == null) { if (coll == null) {
throw new NullPointerException("The collection must not be null"); throw new NullPointerException("The collection must not be null");
} }

View File

@ -31,7 +31,7 @@ import org.apache.commons.collections4.Unmodifiable;
public final class UnmodifiableIterator<E> implements Iterator<E>, Unmodifiable { public final class UnmodifiableIterator<E> implements Iterator<E>, Unmodifiable {
/** The iterator being decorated */ /** The iterator being decorated */
private final Iterator<E> iterator; private final Iterator<? extends E> iterator;
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
@ -44,12 +44,14 @@ public final class UnmodifiableIterator<E> implements Iterator<E>, Unmodifiable
* @return a new unmodifiable iterator * @return a new unmodifiable iterator
* @throws IllegalArgumentException if the iterator is null * @throws IllegalArgumentException if the iterator is null
*/ */
public static <E> Iterator<E> unmodifiableIterator(final Iterator<E> iterator) { public static <E> Iterator<E> unmodifiableIterator(final Iterator<? extends E> iterator) {
if (iterator == null) { if (iterator == null) {
throw new IllegalArgumentException("Iterator must not be null"); throw new IllegalArgumentException("Iterator must not be null");
} }
if (iterator instanceof Unmodifiable) { if (iterator instanceof Unmodifiable) {
return iterator; @SuppressWarnings("unchecked") // safe to upcast
final Iterator<E> tmpIterator = (Iterator<E>) iterator;
return tmpIterator;
} }
return new UnmodifiableIterator<E>(iterator); return new UnmodifiableIterator<E>(iterator);
} }
@ -60,7 +62,7 @@ public final class UnmodifiableIterator<E> implements Iterator<E>, Unmodifiable
* *
* @param iterator the iterator to decorate * @param iterator the iterator to decorate
*/ */
private UnmodifiableIterator(final Iterator<E> iterator) { private UnmodifiableIterator(final Iterator<? extends E> iterator) {
super(); super();
this.iterator = iterator; this.iterator = iterator;
} }

View File

@ -31,7 +31,7 @@ import org.apache.commons.collections4.Unmodifiable;
public final class UnmodifiableListIterator<E> implements ListIterator<E>, Unmodifiable { public final class UnmodifiableListIterator<E> implements ListIterator<E>, Unmodifiable {
/** The iterator being decorated */ /** The iterator being decorated */
private final ListIterator<E> iterator; private final ListIterator<? extends E> iterator;
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
@ -42,12 +42,14 @@ public final class UnmodifiableListIterator<E> implements ListIterator<E>, Unmod
* @return a new unmodifiable list iterator * @return a new unmodifiable list iterator
* @throws IllegalArgumentException if the iterator is null * @throws IllegalArgumentException if the iterator is null
*/ */
public static <E> ListIterator<E> umodifiableListIterator(final ListIterator<E> iterator) { public static <E> ListIterator<E> umodifiableListIterator(final ListIterator<? extends E> iterator) {
if (iterator == null) { if (iterator == null) {
throw new IllegalArgumentException("ListIterator must not be null"); throw new IllegalArgumentException("ListIterator must not be null");
} }
if (iterator instanceof Unmodifiable) { if (iterator instanceof Unmodifiable) {
return iterator; @SuppressWarnings("unchecked") // safe to upcast
final ListIterator<E> tmpIterator = (ListIterator<E>) iterator;
return tmpIterator;
} }
return new UnmodifiableListIterator<E>(iterator); return new UnmodifiableListIterator<E>(iterator);
} }
@ -58,7 +60,7 @@ public final class UnmodifiableListIterator<E> implements ListIterator<E>, Unmod
* *
* @param iterator the iterator to decorate * @param iterator the iterator to decorate
*/ */
private UnmodifiableListIterator(final ListIterator<E> iterator) { private UnmodifiableListIterator(final ListIterator<? extends E> iterator) {
super(); super();
this.iterator = iterator; this.iterator = iterator;
} }

View File

@ -30,7 +30,7 @@ import org.apache.commons.collections4.Unmodifiable;
public final class UnmodifiableMapIterator<K, V> implements MapIterator<K, V>, Unmodifiable { public final class UnmodifiableMapIterator<K, V> implements MapIterator<K, V>, Unmodifiable {
/** The iterator being decorated */ /** The iterator being decorated */
private final MapIterator<K, V> iterator; private final MapIterator<? extends K, ? extends V> iterator;
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
@ -42,12 +42,15 @@ public final class UnmodifiableMapIterator<K, V> implements MapIterator<K, V>, U
* @return a new unmodifiable map iterator * @return a new unmodifiable map iterator
* @throws IllegalArgumentException if the iterator is null * @throws IllegalArgumentException if the iterator is null
*/ */
public static <K, V> MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> iterator) { public static <K, V> MapIterator<K, V> unmodifiableMapIterator(
final MapIterator<? extends K, ? extends V> iterator) {
if (iterator == null) { if (iterator == null) {
throw new IllegalArgumentException("MapIterator must not be null"); throw new IllegalArgumentException("MapIterator must not be null");
} }
if (iterator instanceof Unmodifiable) { if (iterator instanceof Unmodifiable) {
return iterator; @SuppressWarnings("unchecked") // safe to upcast
final MapIterator<K, V> tmpIterator = (MapIterator<K, V>) iterator;
return tmpIterator;
} }
return new UnmodifiableMapIterator<K, V>(iterator); return new UnmodifiableMapIterator<K, V>(iterator);
} }
@ -58,7 +61,7 @@ public final class UnmodifiableMapIterator<K, V> implements MapIterator<K, V>, U
* *
* @param iterator the iterator to decorate * @param iterator the iterator to decorate
*/ */
private UnmodifiableMapIterator(final MapIterator<K, V> iterator) { private UnmodifiableMapIterator(final MapIterator<? extends K, ? extends V> iterator) {
super(); super();
this.iterator = iterator; this.iterator = iterator;
} }

View File

@ -31,7 +31,7 @@ public final class UnmodifiableOrderedMapIterator<K, V> implements OrderedMapIte
Unmodifiable { Unmodifiable {
/** The iterator being decorated */ /** The iterator being decorated */
private final OrderedMapIterator<K, V> iterator; private final OrderedMapIterator<? extends K, ? extends V> iterator;
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
@ -44,13 +44,15 @@ public final class UnmodifiableOrderedMapIterator<K, V> implements OrderedMapIte
* @throws IllegalArgumentException if the iterator is null * @throws IllegalArgumentException if the iterator is null
*/ */
public static <K, V> OrderedMapIterator<K, V> unmodifiableOrderedMapIterator( public static <K, V> OrderedMapIterator<K, V> unmodifiableOrderedMapIterator(
final OrderedMapIterator<K, V> iterator) { final OrderedMapIterator<K, ? extends V> iterator) {
if (iterator == null) { if (iterator == null) {
throw new IllegalArgumentException("OrderedMapIterator must not be null"); throw new IllegalArgumentException("OrderedMapIterator must not be null");
} }
if (iterator instanceof Unmodifiable) { if (iterator instanceof Unmodifiable) {
return iterator; @SuppressWarnings("unchecked") // safe to upcast
final OrderedMapIterator<K, V> tmpIterator = (OrderedMapIterator<K, V>) iterator;
return tmpIterator;
} }
return new UnmodifiableOrderedMapIterator<K, V>(iterator); return new UnmodifiableOrderedMapIterator<K, V>(iterator);
} }
@ -61,7 +63,7 @@ public final class UnmodifiableOrderedMapIterator<K, V> implements OrderedMapIte
* *
* @param iterator the iterator to decorate * @param iterator the iterator to decorate
*/ */
private UnmodifiableOrderedMapIterator(final OrderedMapIterator<K, V> iterator) { private UnmodifiableOrderedMapIterator(final OrderedMapIterator<K, ? extends V> iterator) {
super(); super();
this.iterator = iterator; this.iterator = iterator;
} }

View File

@ -55,7 +55,7 @@ public class DefaultKeyValue<K, V> extends AbstractKeyValue<K, V> {
* @param pair the pair to copy, must not be null * @param pair the pair to copy, must not be null
* @throws NullPointerException if the entry is null * @throws NullPointerException if the entry is null
*/ */
public DefaultKeyValue(final KeyValue<K, V> pair) { public DefaultKeyValue(final KeyValue<? extends K, ? extends V> pair) {
super(pair.getKey(), pair.getValue()); super(pair.getKey(), pair.getValue());
} }
@ -65,7 +65,7 @@ public class DefaultKeyValue<K, V> extends AbstractKeyValue<K, V> {
* @param entry the entry to copy, must not be null * @param entry the entry to copy, must not be null
* @throws NullPointerException if the entry is null * @throws NullPointerException if the entry is null
*/ */
public DefaultKeyValue(final Map.Entry<K, V> entry) { public DefaultKeyValue(final Map.Entry<? extends K, ? extends V> entry) {
super(entry.getKey(), entry.getValue()); super(entry.getKey(), entry.getValue());
} }

View File

@ -45,7 +45,7 @@ public final class DefaultMapEntry<K, V> extends AbstractMapEntry<K, V> {
* @param pair the pair to copy, must not be null * @param pair the pair to copy, must not be null
* @throws NullPointerException if the entry is null * @throws NullPointerException if the entry is null
*/ */
public DefaultMapEntry(final KeyValue<K, V> pair) { public DefaultMapEntry(final KeyValue<? extends K, ? extends V> pair) {
super(pair.getKey(), pair.getValue()); super(pair.getKey(), pair.getValue());
} }
@ -55,7 +55,7 @@ public final class DefaultMapEntry<K, V> extends AbstractMapEntry<K, V> {
* @param entry the entry to copy, must not be null * @param entry the entry to copy, must not be null
* @throws NullPointerException if the entry is null * @throws NullPointerException if the entry is null
*/ */
public DefaultMapEntry(final Map.Entry<K, V> entry) { public DefaultMapEntry(final Map.Entry<? extends K, ? extends V> entry) {
super(entry.getKey(), entry.getValue()); super(entry.getKey(), entry.getValue());
} }

View File

@ -46,7 +46,7 @@ public final class UnmodifiableMapEntry<K, V> extends AbstractMapEntry<K, V> imp
* @param pair the pair to copy, must not be null * @param pair the pair to copy, must not be null
* @throws NullPointerException if the entry is null * @throws NullPointerException if the entry is null
*/ */
public UnmodifiableMapEntry(final KeyValue<K, V> pair) { public UnmodifiableMapEntry(final KeyValue<? extends K, ? extends V> pair) {
super(pair.getKey(), pair.getValue()); super(pair.getKey(), pair.getValue());
} }
@ -56,7 +56,7 @@ public final class UnmodifiableMapEntry<K, V> extends AbstractMapEntry<K, V> imp
* @param entry the entry to copy, must not be null * @param entry the entry to copy, must not be null
* @throws NullPointerException if the entry is null * @throws NullPointerException if the entry is null
*/ */
public UnmodifiableMapEntry(final Map.Entry<K, V> entry) { public UnmodifiableMapEntry(final Map.Entry<? extends K, ? extends V> entry) {
super(entry.getKey(), entry.getValue()); super(entry.getKey(), entry.getValue());
} }

View File

@ -77,7 +77,7 @@ public class CursorableLinkedList<E> extends AbstractLinkedList<E> implements Se
* *
* @param coll the collection to copy * @param coll the collection to copy
*/ */
public CursorableLinkedList(final Collection<E> coll) { public CursorableLinkedList(final Collection<? extends E> coll) {
super(coll); super(coll);
} }

View File

@ -79,7 +79,7 @@ public class NodeCachingLinkedList<E> extends AbstractLinkedList<E> implements S
* *
* @param coll the collection to copy * @param coll the collection to copy
*/ */
public NodeCachingLinkedList(final Collection<E> coll) { public NodeCachingLinkedList(final Collection<? extends E> coll) {
super(coll); super(coll);
this.maximumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE; this.maximumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE;
} }

View File

@ -81,7 +81,7 @@ public class TreeList<E> extends AbstractList<E> {
* @param coll the collection to copy * @param coll the collection to copy
* @throws NullPointerException if the collection is null * @throws NullPointerException if the collection is null
*/ */
public TreeList(final Collection<E> coll) { public TreeList(final Collection<? extends E> coll) {
super(); super();
if (!coll.isEmpty()) { if (!coll.isEmpty()) {
root = new AVLNode<E>(coll); root = new AVLNode<E>(coll);

View File

@ -51,9 +51,11 @@ public final class UnmodifiableList<E>
* @throws IllegalArgumentException if list is null * @throws IllegalArgumentException if list is null
* @since 4.0 * @since 4.0
*/ */
public static <E> List<E> unmodifiableList(final List<E> list) { public static <E> List<E> unmodifiableList(final List<? extends E> list) {
if (list instanceof Unmodifiable) { if (list instanceof Unmodifiable) {
return list; @SuppressWarnings("unchecked") // safe to upcast
final List<E> tmpList = (List<E>) list;
return tmpList;
} }
return new UnmodifiableList<E>(list); return new UnmodifiableList<E>(list);
} }
@ -64,10 +66,10 @@ public final class UnmodifiableList<E>
* *
* @param list the list to decorate, must not be null * @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null * @throws IllegalArgumentException if list is null
* @since Commons Collection 5
*/ */
public UnmodifiableList(final List<E> list) { @SuppressWarnings("unchecked") // safe to upcast
super(list); public UnmodifiableList(final List<? extends E> list) {
super((List<E>) list);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -157,7 +157,7 @@ public class AbstractHashedMap<K, V> extends AbstractMap<K, V> implements Iterab
* @param map the map to copy * @param map the map to copy
* @throws NullPointerException if the map is null * @throws NullPointerException if the map is null
*/ */
protected AbstractHashedMap(final Map<K, V> map) { protected AbstractHashedMap(final Map<? extends K, ? extends V> map) {
this(Math.max(2 * map.size(), DEFAULT_CAPACITY), DEFAULT_LOAD_FACTOR); this(Math.max(2 * map.size(), DEFAULT_CAPACITY), DEFAULT_LOAD_FACTOR);
_putAll(map); _putAll(map);
} }

View File

@ -111,7 +111,7 @@ public abstract class AbstractLinkedMap<K, V> extends AbstractHashedMap<K, V> im
* @param map the map to copy * @param map the map to copy
* @throws NullPointerException if the map is null * @throws NullPointerException if the map is null
*/ */
protected AbstractLinkedMap(final Map<K, V> map) { protected AbstractLinkedMap(final Map<? extends K, ? extends V> map) {
super(map); super(map);
} }

View File

@ -105,7 +105,7 @@ public class CaseInsensitiveMap<K, V> extends AbstractHashedMap<K, V> implements
* @param map the map to copy * @param map the map to copy
* @throws NullPointerException if the map is null * @throws NullPointerException if the map is null
*/ */
public CaseInsensitiveMap(final Map<K, V> map) { public CaseInsensitiveMap(final Map<? extends K, ? extends V> map) {
super(map); super(map);
} }

View File

@ -81,7 +81,7 @@ public class HashedMap<K, V>
* @param map the map to copy * @param map the map to copy
* @throws NullPointerException if the map is null * @throws NullPointerException if the map is null
*/ */
public HashedMap(final Map<K, V> map) { public HashedMap(final Map<? extends K, ? extends V> map) {
super(map); super(map);
} }

View File

@ -141,7 +141,7 @@ public class LRUMap<K, V>
* @throws NullPointerException if the map is null * @throws NullPointerException if the map is null
* @throws IllegalArgumentException if the map is empty * @throws IllegalArgumentException if the map is empty
*/ */
public LRUMap(final Map<K, V> map) { public LRUMap(final Map<? extends K, ? extends V> map) {
this(map, false); this(map, false);
} }
@ -156,7 +156,7 @@ public class LRUMap<K, V>
* @throws IllegalArgumentException if the map is empty * @throws IllegalArgumentException if the map is empty
* @since 3.1 * @since 3.1
*/ */
public LRUMap(final Map<K, V> map, final boolean scanUntilRemovable) { public LRUMap(final Map<? extends K, ? extends V> map, final boolean scanUntilRemovable) {
this(map.size(), DEFAULT_LOAD_FACTOR, scanUntilRemovable); this(map.size(), DEFAULT_LOAD_FACTOR, scanUntilRemovable);
putAll(map); putAll(map);
} }

View File

@ -101,7 +101,7 @@ public class LinkedMap<K, V> extends AbstractLinkedMap<K, V> implements Serializ
* @param map the map to copy * @param map the map to copy
* @throws NullPointerException if the map is null * @throws NullPointerException if the map is null
*/ */
public LinkedMap(final Map<K, V> map) { public LinkedMap(final Map<? extends K, ? extends V> map) {
super(map); super(map);
} }

View File

@ -103,7 +103,7 @@ public class SingletonMap<K, V>
* *
* @param mapEntry the mapEntry to use * @param mapEntry the mapEntry to use
*/ */
public SingletonMap(final Map.Entry<K, V> mapEntry) { public SingletonMap(final Map.Entry<? extends K, ? extends V> mapEntry) {
super(); super();
this.key = mapEntry.getKey(); this.key = mapEntry.getKey();
this.value = mapEntry.getValue(); this.value = mapEntry.getValue();
@ -116,12 +116,12 @@ public class SingletonMap<K, V>
* @throws NullPointerException if the map is null * @throws NullPointerException if the map is null
* @throws IllegalArgumentException if the size is not 1 * @throws IllegalArgumentException if the size is not 1
*/ */
public SingletonMap(final Map<K, V> map) { public SingletonMap(final Map<? extends K, ? extends V> map) {
super(); super();
if (map.size() != 1) { if (map.size() != 1) {
throw new IllegalArgumentException("The map size must be 1"); throw new IllegalArgumentException("The map size must be 1");
} }
final Map.Entry<K, V> entry = map.entrySet().iterator().next(); final Map.Entry<? extends K, ? extends V> entry = map.entrySet().iterator().next();
this.key = entry.getKey(); this.key = entry.getKey();
this.value = entry.getValue(); this.value = entry.getValue();
} }

View File

@ -59,9 +59,11 @@ public final class UnmodifiableMap<K, V>
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
* @since 4.0 * @since 4.0
*/ */
public static <K, V> Map<K, V> unmodifiableMap(final Map<K, V> map) { public static <K, V> Map<K, V> unmodifiableMap(final Map<? extends K, ? extends V> map) {
if (map instanceof Unmodifiable) { if (map instanceof Unmodifiable) {
return map; @SuppressWarnings("unchecked") // safe to upcast
final Map<K, V> tmpMap = (Map<K, V>) map;
return tmpMap;
} }
return new UnmodifiableMap<K, V>(map); return new UnmodifiableMap<K, V>(map);
} }
@ -73,8 +75,9 @@ public final class UnmodifiableMap<K, V>
* @param map the map to decorate, must not be null * @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
*/ */
private UnmodifiableMap(final Map<K, V> map) { @SuppressWarnings("unchecked") // safe to upcast
super(map); private UnmodifiableMap(final Map<? extends K, ? extends V> map) {
super((Map<K, V>) map);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -57,9 +57,11 @@ public final class UnmodifiableOrderedMap<K, V> extends AbstractOrderedMapDecora
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
* @since 4.0 * @since 4.0
*/ */
public static <K, V> OrderedMap<K, V> unmodifiableOrderedMap(final OrderedMap<K, V> map) { public static <K, V> OrderedMap<K, V> unmodifiableOrderedMap(final OrderedMap<? extends K, ? extends V> map) {
if (map instanceof Unmodifiable) { if (map instanceof Unmodifiable) {
return map; @SuppressWarnings("unchecked") // safe to upcast
final OrderedMap<K, V> tmpMap = (OrderedMap<K, V>) map;
return tmpMap;
} }
return new UnmodifiableOrderedMap<K, V>(map); return new UnmodifiableOrderedMap<K, V>(map);
} }
@ -71,8 +73,9 @@ public final class UnmodifiableOrderedMap<K, V> extends AbstractOrderedMapDecora
* @param map the map to decorate, must not be null * @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
*/ */
private UnmodifiableOrderedMap(final OrderedMap<K, V> map) { @SuppressWarnings("unchecked") // safe to upcast
super(map); private UnmodifiableOrderedMap(final OrderedMap<? extends K, ? extends V> map) {
super((OrderedMap<K, V>) map);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -57,9 +57,11 @@ public final class UnmodifiableSortedMap<K, V>
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
* @since 4.0 * @since 4.0
*/ */
public static <K, V> SortedMap<K, V> unmodifiableSortedMap(final SortedMap<K, V> map) { public static <K, V> SortedMap<K, V> unmodifiableSortedMap(final SortedMap<K, ? extends V> map) {
if (map instanceof Unmodifiable) { if (map instanceof Unmodifiable) {
return map; @SuppressWarnings("unchecked") // safe to upcast
final SortedMap<K, V> tmpMap = (SortedMap<K, V>) map;
return tmpMap;
} }
return new UnmodifiableSortedMap<K, V>(map); return new UnmodifiableSortedMap<K, V>(map);
} }
@ -71,8 +73,9 @@ public final class UnmodifiableSortedMap<K, V>
* @param map the map to decorate, must not be null * @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null * @throws IllegalArgumentException if map is null
*/ */
private UnmodifiableSortedMap(final SortedMap<K, V> map) { @SuppressWarnings("unchecked") // safe to upcast
super(map); private UnmodifiableSortedMap(final SortedMap<K, ? extends V> map) {
super((SortedMap<K, V>) map);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -102,7 +102,7 @@ public class CircularFifoQueue<E> extends AbstractCollection<E>
* @param coll the collection to copy into the queue, may not be null * @param coll the collection to copy into the queue, may not be null
* @throws NullPointerException if the collection is null * @throws NullPointerException if the collection is null
*/ */
public CircularFifoQueue(final Collection<E> coll) { public CircularFifoQueue(final Collection<? extends E> coll) {
this(coll.size()); this(coll.size());
addAll(coll); addAll(coll);
} }

View File

@ -52,9 +52,11 @@ public final class UnmodifiableQueue<E>
* @return an unmodifiable Queue * @return an unmodifiable Queue
* @throws IllegalArgumentException if queue is null * @throws IllegalArgumentException if queue is null
*/ */
public static <E> Queue<E> unmodifiableQueue(final Queue<E> queue) { public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) {
if (queue instanceof Unmodifiable) { if (queue instanceof Unmodifiable) {
return queue; @SuppressWarnings("unchecked") // safe to upcast
final Queue<E> tmpQueue = (Queue<E>) queue;
return tmpQueue;
} }
return new UnmodifiableQueue<E>(queue); return new UnmodifiableQueue<E>(queue);
} }
@ -66,8 +68,9 @@ public final class UnmodifiableQueue<E>
* @param queue the queue to decorate, must not be null * @param queue the queue to decorate, must not be null
* @throws IllegalArgumentException if queue is null * @throws IllegalArgumentException if queue is null
*/ */
private UnmodifiableQueue(final Queue<E> queue) { @SuppressWarnings("unchecked") // safe to upcast
super(queue); private UnmodifiableQueue(final Queue<? extends E> queue) {
super((Queue<E>) queue);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -49,9 +49,11 @@ public final class UnmodifiableSet<E>
* @throws IllegalArgumentException if set is null * @throws IllegalArgumentException if set is null
* @since 4.0 * @since 4.0
*/ */
public static <E> Set<E> unmodifiableSet(final Set<E> set) { public static <E> Set<E> unmodifiableSet(final Set<? extends E> set) {
if (set instanceof Unmodifiable) { if (set instanceof Unmodifiable) {
return set; @SuppressWarnings("unchecked") // safe to upcast
final Set<E> tmpSet = (Set<E>) set;
return tmpSet;
} }
return new UnmodifiableSet<E>(set); return new UnmodifiableSet<E>(set);
} }
@ -63,14 +65,15 @@ public final class UnmodifiableSet<E>
* @param set the set to decorate, must not be null * @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null * @throws IllegalArgumentException if set is null
*/ */
private UnmodifiableSet(final Set<E> set) { @SuppressWarnings("unchecked") // safe to upcast
super(set); private UnmodifiableSet(final Set<? extends E> set) {
super((Set<E>) set);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@Override @Override
public Iterator<E> iterator() { public Iterator<E> iterator() {
return UnmodifiableIterator.<E>unmodifiableIterator(decorated().iterator()); return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
} }
@Override @Override

View File

@ -51,7 +51,7 @@ public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodif
* @return a new unmodifiable trie * @return a new unmodifiable trie
* @throws IllegalArgumentException if trie is null * @throws IllegalArgumentException if trie is null
*/ */
public static <K, V> UnmodifiableTrie<K, V> unmodifiableTrie(final Trie<K, V> trie) { public static <K, V> UnmodifiableTrie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) {
return new UnmodifiableTrie<K, V>(trie); return new UnmodifiableTrie<K, V>(trie);
} }
@ -62,11 +62,13 @@ public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodif
* @param trie the trie to decorate, must not be null * @param trie the trie to decorate, must not be null
* @throws IllegalArgumentException if trie is null * @throws IllegalArgumentException if trie is null
*/ */
public UnmodifiableTrie(final Trie<K, V> trie) { public UnmodifiableTrie(final Trie<K, ? extends V> trie) {
if (trie == null) { if (trie == null) {
throw new IllegalArgumentException("Trie must not be null"); throw new IllegalArgumentException("Trie must not be null");
} }
this.delegate = trie; @SuppressWarnings("unchecked") // safe to upcast
final Trie<K, V> tmpTrie = (Trie<K, V>) trie;
this.delegate = tmpTrie;
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------