changeset 4717:085fb9c9164f

add wildcards for several cases
author mduigou
date Sun, 18 Dec 2011 15:47:06 -0800
parents 09a69a0111ba
children 9a0b17ea5292
files src/share/classes/java/util/Iterables.java src/share/classes/java/util/Iterators.java
diffstat 2 files changed, 33 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Iterables.java	Fri Dec 16 16:26:32 2011 -0800
+++ b/src/share/classes/java/util/Iterables.java	Sun Dec 18 15:47:06 2011 -0800
@@ -117,7 +117,7 @@
      * and type {@code U}.
      * @return An Iterable view of the filtered elements.
      */
-    public static <T, U> Iterable<U> filterMap(final Iterable<T> iterable,
+    public static <T, U> Iterable<U> filterMap(final Iterable<? extends T> iterable,
             final Predicate<? super T> predicate,
             final Mapper<? super T, ? extends U> mapper) {
         Objects.requireNonNull(iterable);
@@ -147,7 +147,7 @@
      * @param base Initial value for reducer.
      * @return reduced value of the elements.
      */
-    public static <T, U> U filterMapReduce(final Iterable<T> iterable,
+    public static <T, U> U filterMapReduce(final Iterable<? extends T> iterable,
             final Predicate<? super T> predicate,
             final Mapper<? super T, ? extends U> mapper,
             U base,
@@ -170,14 +170,14 @@
      * @param block The operation to be performed upon each each element.
      * @return The input Iterable, {@code iterable}.
      */
-    public static <T> Iterable<T> forEach(final Iterable<T> iterable, final Block<? super T> block) {
+    public static <T> Iterable<T> forEach(final Iterable<? extends T> iterable, final Block<? super T> block) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(block);
         for (T each : iterable) {
             block.apply(each);
         }
 
-        return iterable;
+        return (Iterable<T>) iterable;
     }
 
     /**
@@ -191,7 +191,7 @@
      * and type {@code U}.
      * @return An Iterable view consisting of the mapped elements.
      */
-    public static <T, U> Iterable<U> map(final Iterable<T> iterable, final Mapper<? super T, ? extends U> mapper) {
+    public static <T, U> Iterable<U> map(final Iterable<? extends T> iterable, final Mapper<? super T, ? extends U> mapper) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(mapper);
         return new BaseIterable<U>() {
@@ -213,34 +213,34 @@
      * @param base Initial value for reducer.
      * @return The reduced value of the elements.
      */
-    public static <T> T reduce(Iterable<T> iterable, T base, Operator<T> operator) {
+    public static <T> T reduce(Iterable<? extends T> iterable, T base, Operator<T> operator) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(operator);
         return Iterators.reduce(iterable.iterator(), base, operator);
     }
 
-    public static <T, U> U mapReduce(Iterable<T> iterable, Mapper<? super T, ? extends U> mapper, U base, Operator<U> operator) {
+    public static <T, U> U mapReduce(Iterable<? extends T> iterable, Mapper<? super T, ? extends U> mapper, U base, Operator<U> operator) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(mapper);
         Objects.requireNonNull(operator);
         return Iterators.mapReduce(iterable.iterator(), mapper, base, operator);
     }
 
-    public static<T> int mapReduce(Iterable<T> iterable, IntMapper<? super T> mapper, int base, IntOperator operator) {
+    public static<T> int mapReduce(Iterable<? extends T> iterable, IntMapper<? super T> mapper, int base, IntOperator operator) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(mapper);
         Objects.requireNonNull(operator);
         return Iterators.mapReduce(iterable.iterator(), mapper, base, operator);
     }
 
-    public static<T> long mapReduce(Iterable<T> iterable, LongMapper<? super T> mapper, long base, LongOperator operator) {
+    public static<T> long mapReduce(Iterable<? extends T> iterable, LongMapper<? super T> mapper, long base, LongOperator operator) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(mapper);
         Objects.requireNonNull(operator);
         return Iterators.mapReduce(iterable.iterator(), mapper, base, operator);
     }
 
-    public static<T> double mapReduce(Iterable<T> iterable, DoubleMapper<? super T> mapper, double base, DoubleOperator operator) {
+    public static<T> double mapReduce(Iterable<? extends T> iterable, DoubleMapper<? super T> mapper, double base, DoubleOperator operator) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(mapper);
         Objects.requireNonNull(operator);
@@ -256,32 +256,32 @@
      * @param target The collection other container into which the elements are added.
      * @return The provided container.
      */
-    public static <T, A extends Fillable<? super T>> A into(Iterable<T> iterable, A target) {
+    public static <T, A extends Fillable<? super T>> A into(Iterable<? extends T> iterable, A target) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(target);
         target.addAll(iterable);
         return target;
     }
 
-    public static <T> boolean anyMatch(Iterable<T> iterable, Predicate<? super T> filter) {
+    public static <T> boolean anyMatch(Iterable<? extends T> iterable, Predicate<? super T> filter) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(filter);
         return Iterators.anyMatch(iterable.iterator(), filter);
     }
 
-    public static <T> boolean noneMatch(Iterable<T> iterable, Predicate<? super T> filter) {
+    public static <T> boolean noneMatch(Iterable<? extends T> iterable, Predicate<? super T> filter) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(filter);
         return Iterators.noneMatch(iterable.iterator(), filter);
     }
 
-    public static <T> boolean allMatch(Iterable<T> iterable, Predicate<? super T> filter) {
+    public static <T> boolean allMatch(Iterable<? extends T> iterable, Predicate<? super T> filter) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(filter);
         return Iterators.allMatch(iterable.iterator(), filter);
     }
 
-    public static<T extends Comparable<? super T>> Iterable<T> sorted(final Iterable<T> iterable) {
+    public static<T extends Comparable<? super T>> Iterable<T> sorted(final Iterable<? extends T> iterable) {
         Objects.requireNonNull(iterable);
         return new BaseIterable<T>() {
             @Override
@@ -291,7 +291,7 @@
         };
     }
 
-    public static<T> Iterable<T> sorted(final Iterable<T> iterable, final Comparator<? super T> comparator) {
+    public static<T> Iterable<T> sorted(final Iterable<? extends T> iterable, final Comparator<? super T> comparator) {
         Objects.requireNonNull(iterable);
         Objects.requireNonNull(comparator);
         return new BaseIterable<T>() {
@@ -302,7 +302,7 @@
         };
     }
 
-    public static<T> Iterable<T> uniqueElements(Iterable<T> iterable) {
+    public static<T> Iterable<T> uniqueElements(Iterable<? extends T> iterable) {
         Objects.requireNonNull(iterable);
         int sizeEstimate = 0;
         if (iterable instanceof Collection)
--- a/src/share/classes/java/util/Iterators.java	Fri Dec 16 16:26:32 2011 -0800
+++ b/src/share/classes/java/util/Iterators.java	Sun Dec 18 15:47:06 2011 -0800
@@ -43,7 +43,7 @@
         throw new UnsupportedOperationException("remove");
     }
 
-    public static <T> long count(Iterator<T> iterator) {
+    public static <T> long count(Iterator<? extends T> iterator) {
         Objects.requireNonNull(iterator);
         long count = 0;
         while (iterator.hasNext()) {
@@ -53,7 +53,7 @@
         return count;
     }
 
-    public static <T> void forEach(Iterator<T> iterator, Block<? super T> block) {
+    public static <T> void forEach(Iterator<? extends T> iterator, Block<? super T> block) {
         Objects.requireNonNull(iterator);
         Objects.requireNonNull(block);
         while (iterator.hasNext()) {
@@ -61,12 +61,12 @@
         }
     }
 
-    public static <T> Iterator<T> filter(final Iterator<T> iterator, final Predicate<? super T> predicate) {
+    public static <T> Iterator<T> filter(final Iterator<? extends T> iterator, final Predicate<? super T> predicate) {
         Objects.requireNonNull(iterator);
         Objects.requireNonNull(predicate);
         return new Iterator<T>() {
 
-            final Iterator<T> source = iterator;
+            final Iterator<? extends T> source = iterator;
             boolean nextReady = false;
             T nextValue = null;
 
@@ -92,11 +92,11 @@
         };
     }
 
-    public static <T, U> Iterator<U> map(final Iterator<T> iterator, final Mapper<? super T, ? extends U> mapper) {
+    public static <T, U> Iterator<U> map(final Iterator<? extends T> iterator, final Mapper<? super T, ? extends U> mapper) {
         Objects.requireNonNull(iterator);
         Objects.requireNonNull(mapper);
         return new Iterator<U>() {
-            final Iterator<T> source = iterator;
+            final Iterator<? extends T> source = iterator;
 
             @Override
             public boolean hasNext() {
@@ -110,7 +110,7 @@
         };
     }
 
-    public static <T> T reduce(final Iterator<T> iterator, T base, final Operator<T> operator) {
+    public static <T> T reduce(final Iterator<? extends T> iterator, T base, final Operator<T> operator) {
         Objects.requireNonNull(iterator);
         Objects.requireNonNull(operator);
         while(iterator.hasNext()) {
@@ -120,7 +120,7 @@
         return base;
     }
 
-    public static <T, U> U mapReduce(Iterator<T> iterator, Mapper<? super T, ? extends U> mapper, U base, Operator<U> operator) {
+    public static <T, U> U mapReduce(Iterator<? extends T> iterator, Mapper<? super T, ? extends U> mapper, U base, Operator<U> operator) {
         Objects.requireNonNull(iterator);
         Objects.requireNonNull(mapper);
         Objects.requireNonNull(operator);
@@ -131,7 +131,7 @@
         return base;
     }
 
-    public static <T> int mapReduce(Iterator<T> iterator, IntMapper<? super T> mapper, int base, IntOperator operator) {
+    public static <T> int mapReduce(Iterator<? extends T> iterator, IntMapper<? super T> mapper, int base, IntOperator operator) {
         Objects.requireNonNull(iterator);
         Objects.requireNonNull(mapper);
         Objects.requireNonNull(operator);
@@ -142,7 +142,7 @@
         return base;
     }
 
-    public static <T> long mapReduce(Iterator<T> iterator, LongMapper<? super T> mapper, long base, LongOperator operator) {
+    public static <T> long mapReduce(Iterator<? extends T> iterator, LongMapper<? super T> mapper, long base, LongOperator operator) {
         Objects.requireNonNull(iterator);
         Objects.requireNonNull(mapper);
         Objects.requireNonNull(operator);
@@ -153,7 +153,7 @@
         return base;
      }
 
-    public static <T> double mapReduce(Iterator<T> iterator, DoubleMapper<? super T> mapper, double base, DoubleOperator operator) {
+    public static <T> double mapReduce(Iterator<? extends T> iterator, DoubleMapper<? super T> mapper, double base, DoubleOperator operator) {
         Objects.requireNonNull(iterator);
         Objects.requireNonNull(mapper);
         Objects.requireNonNull(operator);
@@ -164,7 +164,7 @@
         return base;
     }
 
-    public static <T> boolean anyMatch(Iterator<T> iterator, Predicate<? super T> filter) {
+    public static <T> boolean anyMatch(Iterator<? extends T> iterator, Predicate<? super T> filter) {
         Objects.requireNonNull(iterator);
         while (iterator.hasNext()) {
             if(filter.eval(iterator.next())) {
@@ -175,12 +175,12 @@
         return false;
     }
 
-    public static <T> boolean noneMatch(Iterator<T> iterator, Predicate<? super T> filter) {
+    public static <T> boolean noneMatch(Iterator<? extends T> iterator, Predicate<? super T> filter) {
         Objects.requireNonNull(iterator);
         return !anyMatch(iterator, filter);
     }
 
-    public static <T> boolean allMatch(Iterator<T> iterator, Predicate<? super T> filter) {
+    public static <T> boolean allMatch(Iterator<? extends T> iterator, Predicate<? super T> filter) {
         Objects.requireNonNull(iterator);
         while (iterator.hasNext()) {
             if(!filter.eval(iterator.next())) {
@@ -191,7 +191,7 @@
         return true;
     }
 
-    public static <T> Iterator<T> sorted(Iterator<T> iterator) {
+    public static <T> Iterator<T> sorted(Iterator<? extends T> iterator) {
         Objects.requireNonNull(iterator);
         final PriorityQueue<T> pq = new PriorityQueue<>();
         while (iterator.hasNext())
@@ -209,7 +209,7 @@
         };
     }
 
-    public static <T> Iterator<T> sorted(Iterator<T> iterator, Comparator<? super T> comp) {
+    public static <T> Iterator<T> sorted(Iterator<? extends T> iterator, Comparator<? super T> comp) {
         Objects.requireNonNull(iterator);
         Objects.requireNonNull(comp);
         final PriorityQueue<T> pq = new PriorityQueue<>(DEFAULT_PRIORITY_QUEUE_SIZE, comp);