changeset 5843:2d1cea74173b it2-bootstrap

BiAllMatch and BiAnyMatch Ops. Better generics for Comparators (suggested by Georgiy Rakov <georgiy.rakov@oracle.com>)
author mduigou
date Mon, 06 Aug 2012 10:57:28 -0700
parents 4a7906022b7d
children 519630190ba5
files src/share/classes/java/util/Comparators.java src/share/classes/java/util/streams/SequentialMapPipeline.java src/share/classes/java/util/streams/ops/BiAllMatchOp.java src/share/classes/java/util/streams/ops/BiAnyMatchOp.java
diffstat 4 files changed, 103 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Comparators.java	Fri Aug 03 12:32:37 2012 -0700
+++ b/src/share/classes/java/util/Comparators.java	Mon Aug 06 10:57:28 2012 -0700
@@ -154,19 +154,17 @@
         }
     }
 
-    private static class ComposedComparator<T>
+    private static class ComposedComparator<T extends Comparable<? super T>>
         implements Comparator<T>, Serializable {
 
         private static final long serialVersionUID = -3088981872625314196L;
 
-        private final Comparator<T> first;
+        private final Comparator<? super T> first;
         private final Comparator<? super T> second;
 
-        private ComposedComparator(Comparator<T> first, Comparator<? super T> second) {
-            Objects.requireNonNull(first);
-            Objects.requireNonNull(second);
-            this.first = first;
-            this.second = second;
+        private ComposedComparator(Comparator<? super T> first, Comparator<? super T> second) {
+            this.first = Objects.requireNonNull(first);
+            this.second = Objects.requireNonNull(second);
         }
 
         @Override
@@ -177,13 +175,13 @@
     }
 
     // Included for convenience, duplicates Collections functionality
-    public static <T> Comparator<T> reverseOrder() {
-        return Collections.reverseOrder();
+    public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
+        return (Comparator<T>) (Comparator) Collections.reverseOrder();
     }
 
     // Included for convenience, duplicates Collections functionality
-    public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) {
-        return Collections.reverseOrder(cmp);
+    public static <T extends Comparable<? super T>> Comparator<T> reverseOrder(Comparator<? super T> cmp) {
+        return (Comparator<T>) Collections.reverseOrder(cmp);
     }
 
     public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
@@ -198,23 +196,23 @@
         return (Comparator<Mapping<K,V>>) MappingValueComparator.INSTANCE;
     }
 
-    public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Mapper<T, U> mapper) {
-        return new MapperComparator<>(mapper);
+    public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Mapper<? super T, U> mapper) {
+        return (Comparator<T>) new MapperComparator<>(mapper);
     }
 
-    public static <T> Comparator<T> comparing(IntMapper<T> mapper) {
-        return new IntMapperComparator<>(mapper);
+    public static <T> Comparator<T> comparing(IntMapper<? super T> mapper) {
+        return (Comparator<T>) new IntMapperComparator<>(mapper);
     }
 
-    public static <T> Comparator<T> comparing(LongMapper<T> mapper) {
-        return new LongMapperComparator<>(mapper);
+    public static <T> Comparator<T> comparing(LongMapper<? super T> mapper) {
+        return (Comparator<T>) new LongMapperComparator<>(mapper);
     }
 
-    public static<T> Comparator<T> comparing(DoubleMapper<T> mapper) {
-        return new DoubleMapperComparator<>(mapper);
+    public static<T> Comparator<T> comparing(DoubleMapper<? super T> mapper) {
+        return (Comparator<T>) new DoubleMapperComparator<>(mapper);
     }
 
-    public static<T> Comparator<T> compose(Comparator<T> first, Comparator<? super T> second) {
-        return new ComposedComparator<>(first, second);
+    public static<T> Comparator<T> compose(Comparator<? super T> first, Comparator<? super T> second) {
+        return (Comparator<T>) new ComposedComparator<>(first, second);
     }
 }
--- a/src/share/classes/java/util/streams/SequentialMapPipeline.java	Fri Aug 03 12:32:37 2012 -0700
+++ b/src/share/classes/java/util/streams/SequentialMapPipeline.java	Mon Aug 06 10:57:28 2012 -0700
@@ -143,6 +143,10 @@
         }
     }
 
+   public<U> U pipeline(ShortCircuitEagerOp<Mapping<K,V>, U> op) {
+        return op.evaluate(iterator());
+    }
+
     @Override
     public <A extends Map<K, V>> A into(A target) {
         forEach(target::put);
@@ -207,28 +211,12 @@
 
     @Override
     public boolean allMatch(BiPredicate<? super K, ? super V> predicate) {
-        MapIterator<K,V> each = iterator();
-
-        while(each.hasNext()) {
-            if(!predicate.eval(each.nextKey(), each.curValue())) {
-                return false;
-            }
-        }
-
-        return true;
+        return pipeline(new BiAllMatchOp<K,V>(predicate));
     }
 
     @Override
     public boolean anyMatch(BiPredicate<? super K, ? super V> predicate) {
-        MapIterator<K,V> each = iterator();
-
-        while(each.hasNext()) {
-            if(predicate.eval(each.nextKey(), each.curValue())) {
-                return true;
-            }
-        }
-
-        return false;
+        return pipeline(new BiAnyMatchOp<K,V>(predicate));
     }
 
     @Override
--- a/src/share/classes/java/util/streams/ops/BiAllMatchOp.java	Fri Aug 03 12:32:37 2012 -0700
+++ b/src/share/classes/java/util/streams/ops/BiAllMatchOp.java	Mon Aug 06 10:57:28 2012 -0700
@@ -29,33 +29,31 @@
 import java.util.Mapping;
 import java.util.Objects;
 import java.util.functions.BiPredicate;
-import java.util.functions.Predicate;
 import java.util.streams.ParallelStreamable;
 
 /**
- * AllMatchOp
+ * BiAllMatchOp
  *
- * @author Brian Goetz
  */
 public class BiAllMatchOp<K,V> implements ShortCircuitEagerOp<Mapping<K,V>, Boolean> {
-    private final BiPredicate<K,V> predicate;
+    private final BiPredicate<? super K, ? super V> predicate;
 
-    public BiAllMatchOp(Predicate<Mapping<K,V> predicate) {
+    public BiAllMatchOp(BiPredicate<? super K, ? super V> predicate) {
         this.predicate = Objects.requireNonNull(predicate);
     }
 
+    public Boolean evaluate(MapIterator<K,V> iterator) {
+        return allMatch(Objects.requireNonNull(iterator), predicate);
+    }
+
     @Override
     public Boolean evaluate(Iterator<Mapping<K,V>> iterator) {
-        return allMatch(Objects.requireNonNull(iterator), predicate);
-    }
-    
-    public Boolean evaluate(MapIterator<K,V> iterator) {
-        return allMatch(Objects.requireNonNull(iterator), predicate);
+        return allMatch(new MapIterator.IteratorAdapter(iterator), predicate);
     }
 
-    public static <K,V> boolean allMatch(Iterator<? extends Mapping<K,V>> iterator, BiPredicate<<K,V>> predicate) {
+    public static <K,V> boolean allMatch(MapIterator<K,V> iterator, BiPredicate<? super K, ? super V> predicate) {
         while (iterator.hasNext()) {
-            if(!predicate.test(iterator.next())) {
+            if(!predicate.eval(iterator.nextKey(), iterator.curValue())) {
                 return false;
             }
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/java/util/streams/ops/BiAnyMatchOp.java	Mon Aug 06 10:57:28 2012 -0700
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util.streams.ops;
+
+import java.util.Iterator;
+import java.util.MapIterator;
+import java.util.Mapping;
+import java.util.Objects;
+import java.util.functions.BiPredicate;
+import java.util.streams.ParallelStreamable;
+
+/**
+ * BiAnyMatchOp
+ *
+ */
+public class BiAnyMatchOp<K,V> implements ShortCircuitEagerOp<Mapping<K,V>, Boolean> {
+    private final BiPredicate<? super K, ? super V> predicate;
+
+    public BiAnyMatchOp(BiPredicate<? super K, ? super V> predicate) {
+        this.predicate = Objects.requireNonNull(predicate);
+    }
+
+    public Boolean evaluate(MapIterator<K,V> iterator) {
+        return anyMatch(Objects.requireNonNull(iterator), predicate);
+    }
+
+    @Override
+    public Boolean evaluate(Iterator<Mapping<K,V>> iterator) {
+        return anyMatch(new MapIterator.IteratorAdapter(iterator), predicate);
+    }
+
+    public static <K,V> boolean anyMatch(MapIterator<K,V> iterator, BiPredicate<? super K, ? super V> predicate) {
+        while (iterator.hasNext()) {
+            if(predicate.eval(iterator.nextKey(), iterator.curValue())) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+//
+//    @Override
+//    public <V> Boolean computeParallel(ParallelStreamable<V> source, ParallelOpHelper<T, V> helper) {
+//        throw new UnsupportedOperationException("nyi");
+//    }
+}