changeset 5993:973554ba6e73 it2-bootstrap

Combine XxxMatchOp classes into single MatchOp
author briangoetz
date Wed, 12 Sep 2012 22:08:57 -0400
parents c94321c10623
children 78d4439127f2
files make/java/java/FILES_java.gmk src/share/classes/java/util/streams/LinearPipeline.java src/share/classes/java/util/streams/ops/AllMatchOp.java src/share/classes/java/util/streams/ops/MatchOp.java src/share/classes/java/util/streams/ops/NoneMatchOp.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/MatchOpTest.java
diffstat 6 files changed, 111 insertions(+), 129 deletions(-) [+]
line wrap: on
line diff
--- a/make/java/java/FILES_java.gmk	Wed Sep 12 13:29:57 2012 -0700
+++ b/make/java/java/FILES_java.gmk	Wed Sep 12 22:08:57 2012 -0400
@@ -394,18 +394,16 @@
     java/util/streams/ops/GroupByOp.java \
     java/util/streams/ops/FindAnyOp.java \
     java/util/streams/ops/MapExtractKeysOp.java \
-    java/util/streams/ops/AllMatchOp.java \
+    java/util/streams/ops/MatchOp.java \
     java/util/streams/ops/StatelessOp.java \
     java/util/streams/ops/StatefulOp.java \
     java/util/streams/ops/CumulateOp.java \
-    java/util/streams/ops/AnyMatchOp.java \
     java/util/streams/ops/MapOp.java \
     java/util/streams/ops/FlatMapOp.java \
     java/util/streams/ops/FoldOp.java \
     java/util/streams/ops/MapFilterValuesOp.java \
     java/util/streams/ops/MappedOp.java \
     java/util/streams/ops/UniqOp.java \
-    java/util/streams/ops/NoneMatchOp.java \
     java/util/prefs/Preferences.java \
         java/util/prefs/AbstractPreferences.java \
     java/util/prefs/PreferenceChangeEvent.java \
--- a/src/share/classes/java/util/streams/LinearPipeline.java	Wed Sep 12 13:29:57 2012 -0700
+++ b/src/share/classes/java/util/streams/LinearPipeline.java	Wed Sep 12 22:08:57 2012 -0400
@@ -120,17 +120,17 @@
 
     @Override
     public boolean anyMatch(Predicate<? super U> predicate) {
-        return pipeline(new AnyMatchOp<>(predicate));
+        return pipeline(new MatchOp<>(predicate, MatchOp.MatchKind.ANY));
     }
 
     @Override
     public boolean allMatch(Predicate<? super U> predicate) {
-        return pipeline(new AllMatchOp<>(predicate));
+        return pipeline(new MatchOp<>(predicate, MatchOp.MatchKind.ALL));
     }
 
     @Override
     public boolean noneMatch(Predicate<? super U> predicate) {
-        return pipeline(new NoneMatchOp<>(predicate));
+        return pipeline(new MatchOp<>(predicate, MatchOp.MatchKind.NONE));
     }
 
     @Override
--- a/src/share/classes/java/util/streams/ops/AllMatchOp.java	Wed Sep 12 13:29:57 2012 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-/*
- * 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.Objects;
-import java.util.functions.Predicate;
-
-/**
- * AllMatchOp
- *
- * @author Brian Goetz
- */
-public class AllMatchOp<T> implements ShortCircuitTerminalOp<T, Boolean> {
-    private final Predicate<? super T> predicate;
-
-    public AllMatchOp(Predicate<? super T> predicate) {
-        this.predicate = Objects.requireNonNull(predicate);
-    }
-
-    @Override
-    public Boolean evaluate(Iterator<T> iterator) {
-        return allMatch(iterator, predicate);
-    }
-
-    public static <T> boolean allMatch(Iterator<? extends T> iterator, Predicate<? super T> predicate) {
-        Objects.requireNonNull(iterator);
-        Objects.requireNonNull(predicate);
-        while (iterator.hasNext()) {
-            if(!predicate.test(iterator.next())) {
-                return false;
-            }
-        }
-
-        return true;
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/java/util/streams/ops/MatchOp.java	Wed Sep 12 22:08:57 2012 -0400
@@ -0,0 +1,103 @@
+/*
+ * 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.functions.Predicate;
+
+/**
+ * MatchOp
+ *
+ * @author Brian Goetz
+ */
+public class MatchOp<T> implements ShortCircuitTerminalOp<T, Boolean> {
+    private final Predicate<? super T> predicate;
+    private final MatchKind matchKind;
+
+    public MatchOp(Predicate<? super T> predicate, MatchKind matchKind) {
+        this.predicate = predicate;
+        this.matchKind = matchKind;
+    }
+
+    @Override
+    public Boolean evaluate(Iterator<T> iterator) {
+        return matchKind.match(iterator, predicate);
+    }
+
+    // Code sketch for parallel implementation:
+    // - Decompose as per usual
+    // - run match on leaf chunks, returns b
+    // - if b == matchKind.shortCircuitOn, complete early and return b
+    // - else if we complete normally, return !shortCircuitOn
+
+
+    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;
+                    }
+                }
+
+                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;
+                    }
+                }
+
+                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;
+        }
+
+        abstract<T> boolean match(Iterator<? extends T> iterator, Predicate<? super T> predicate);
+    }
+
+}
--- a/src/share/classes/java/util/streams/ops/NoneMatchOp.java	Wed Sep 12 13:29:57 2012 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +0,0 @@
-/*
- * 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.Objects;
-import java.util.functions.Predicate;
-
-/**
- * NoneMatchOp
- *
- * @author Brian Goetz
- */
-public class NoneMatchOp<T> implements ShortCircuitTerminalOp<T, Boolean> {
-    private final Predicate<? super T> predicate;
-
-    public NoneMatchOp(Predicate<? super T> predicate) {
-        this.predicate = Objects.requireNonNull(predicate);
-    }
-
-    @Override
-    public Boolean evaluate(Iterator<T> iterator) {
-        Objects.requireNonNull(iterator);
-        return noneMatch(iterator, predicate);
-    }
-
-    public static <T> boolean noneMatch(Iterator<? extends T> iterator, Predicate<? super T> predicate) {
-        while (iterator.hasNext()) {
-            if(predicate.test(iterator.next())) {
-                return false;
-            }
-        }
-
-        return true;
-    }
-}
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/MatchOpTest.java	Wed Sep 12 13:29:57 2012 -0700
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/MatchOpTest.java	Wed Sep 12 22:08:57 2012 -0400
@@ -30,9 +30,7 @@
 import java.util.functions.Predicate;
 import java.util.streams.Stream;
 import java.util.streams.Streamable;
-import java.util.streams.ops.AllMatchOp;
-import java.util.streams.ops.AnyMatchOp;
-import java.util.streams.ops.NoneMatchOp;
+import java.util.streams.ops.MatchOp;
 import java.util.streams.ops.TerminalOp;
 
 import static org.openjdk.tests.java.util.LambdaTestHelpers.*;
@@ -57,7 +55,7 @@
 
             @Override
             <T> TerminalOp<T, Boolean> op(Predicate<T> predicate) {
-                return new AnyMatchOp<>(predicate);
+                return new MatchOp<>(predicate, MatchOp.MatchKind.ANY);
             }
         },
         ALL {
@@ -68,7 +66,7 @@
 
             @Override
             <T> TerminalOp<T, Boolean> op(Predicate<T> predicate) {
-                return new AllMatchOp<>(predicate);
+                return new MatchOp<>(predicate, MatchOp.MatchKind.ALL);
             }
         },
         NONE {
@@ -79,7 +77,7 @@
 
             @Override
             <T> TerminalOp<T, Boolean> op(Predicate<T> predicate) {
-                return new NoneMatchOp<>(predicate);
+                return new MatchOp<>(predicate, MatchOp.MatchKind.NONE);
             }
         };