changeset 6007:cd4e5bc1ccb1

Further simplifications in MatchOp
author briangoetz
date Thu, 13 Sep 2012 19:20:08 -0400
parents 36439336d1e3
children 5e95936891d2
files src/share/classes/java/util/streams/ops/MatchOp.java test-ng/tests/org/openjdk/tests/java/util/streams/MapStreamTestDataProvider.java
diffstat 2 files changed, 20 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/streams/ops/MatchOp.java	Thu Sep 13 16:20:41 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/MatchOp.java	Thu Sep 13 19:20:08 2012 -0400
@@ -89,7 +89,7 @@
         @Override
         protected Boolean doLeaf() {
             boolean b = op.matchKind.match(helper.wrapIterator(spliterator.iterator()), op.predicate);
-            if (b == op.matchKind.shortCircuitOn) {
+            if (b == op.matchKind.shortCircuitResult) {
                 answer.compareAndSet(null, b);
                 completeRoot(b);
             }
@@ -99,55 +99,32 @@
         @Override
         public void onCompletion(CountedCompleter caller) {
             if (getParent() == null)
-                answer.compareAndSet(null, !op.matchKind.shortCircuitOn);
+                answer.compareAndSet(null, !op.matchKind.shortCircuitResult);
         }
     }
 
     public enum MatchKind {
-        ANY(true) {
-            @Override
-            <T> boolean match(Iterator<? extends T> iterator, Predicate<? super T> predicate) {
-                while (iterator.hasNext()) {
-                    if(predicate.test(iterator.next())) {
-                        return true;
-                    }
-                }
+        ANY(true, true),
+        ALL(false, false),
+        NONE(true, false);
 
-                return false;
-            }
-        },
-        ALL(false) {
-            @Override
-            <T> boolean match(Iterator<? extends T> iterator, Predicate<? super T> predicate) {
-                while (iterator.hasNext()) {
-                    if(!predicate.test(iterator.next())) {
-                        return false;
-                    }
-                }
+        private final boolean stopOnPredicateMatches;
+        private final boolean shortCircuitResult;
 
-                return true;
-            }
-        },
-        NONE(false) {
-            @Override
-            <T> boolean match(Iterator<? extends T> iterator, Predicate<? super T> predicate) {
-                while (iterator.hasNext()) {
-                    if(predicate.test(iterator.next())) {
-                        return false;
-                    }
-                }
-
-                return true;
-            }
-        };
-
-        private final boolean shortCircuitOn;
-
-        MatchKind(boolean shortCircuitOn) {
-            this.shortCircuitOn = shortCircuitOn;
+        MatchKind(boolean stopOnPredicateMatches, boolean shortCircuitResult) {
+            this.stopOnPredicateMatches = stopOnPredicateMatches;
+            this.shortCircuitResult = shortCircuitResult;
         }
 
-        abstract<T> boolean match(Iterator<? extends T> iterator, Predicate<? super T> predicate);
+        <T> boolean match(Iterator<? extends T> iterator, Predicate<? super T> predicate) {
+            while (iterator.hasNext()) {
+                if(predicate.test(iterator.next()) == stopOnPredicateMatches) {
+                    return shortCircuitResult;
+                }
+            }
+
+            return !shortCircuitResult;
+        }
     }
 
 }
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/MapStreamTestDataProvider.java	Thu Sep 13 16:20:41 2012 -0400
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/MapStreamTestDataProvider.java	Thu Sep 13 19:20:08 2012 -0400
@@ -91,7 +91,7 @@
 
                 list.add(new Object[]{
                             String.format("%s%s%s", name, "<Integer,Integer>", range),
-                            new StreamOpTestCase.MapTestData(makeIntegerIntegerMap((Class<? extends Map<Integer,Integer>>) type, size))
+                            new StreamOpTestCase.MapTestData<>(makeIntegerIntegerMap((Class<? extends Map<Integer,Integer>>) type, size))
                       });
                 list.add(new Object[]{
                             String.format("%s%s%s", name, "<Integer,String>", range),