changeset 6435:b5c2376af431

Add minBy(Comparator), maxBy(Comparator)
author briangoetz
date Sun, 18 Nov 2012 23:51:46 -0500
parents f7c32272e02f
children 1440e2129616
files src/share/classes/java/util/stream/ReferencePipeline.java src/share/classes/java/util/stream/Stream.java test-ng/tests/org/openjdk/tests/java/util/stream/op/MinMaxByTest.java
diffstat 3 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/ReferencePipeline.java	Sun Nov 18 13:56:58 2012 -0500
+++ b/src/share/classes/java/util/stream/ReferencePipeline.java	Sun Nov 18 23:51:46 2012 -0500
@@ -196,4 +196,14 @@
     public <V> V fold(Supplier<V> baseFactory, Combiner<V, U, V> reducer, BinaryOperator<V> combiner) {
         return pipeline(new FoldOp<>(baseFactory, reducer, combiner));
     }
+
+    @Override
+    public Optional<U> minBy(Comparator<? super U> comparator) {
+        return reduce((a, b) -> comparator.compare(a, b) <= 0 ? a : b);
+    }
+
+    @Override
+    public Optional<U> maxBy(Comparator<? super U> comparator) {
+        return reduce((a, b) -> comparator.compare(a, b) >= 0 ? a : b);
+    }
 }
--- a/src/share/classes/java/util/stream/Stream.java	Sun Nov 18 13:56:58 2012 -0500
+++ b/src/share/classes/java/util/stream/Stream.java	Sun Nov 18 23:51:46 2012 -0500
@@ -125,6 +125,10 @@
                Combiner<U, T, U> reducer,
                BinaryOperator<U> combiner);
 
+    Optional<T> maxBy(Comparator<? super T> comparator);
+
+    Optional<T> minBy(Comparator<? super T> comparator);
+
     boolean anyMatch(Predicate<? super T> predicate);
 
     boolean allMatch(Predicate<? super T> predicate);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-ng/tests/org/openjdk/tests/java/util/stream/op/MinMaxByTest.java	Sun Nov 18 23:51:46 2012 -0500
@@ -0,0 +1,53 @@
+/*
+ * 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 org.openjdk.tests.java.util.stream.op;
+
+import org.openjdk.tests.java.util.stream.OpTestCase;
+import org.openjdk.tests.java.util.stream.StreamTestData;
+import org.openjdk.tests.java.util.stream.StreamTestDataProvider;
+import org.testng.annotations.Test;
+
+import static org.openjdk.tests.java.util.LambdaTestHelpers.countTo;
+
+/**
+ * MinMaxByOpTest
+ *
+ * @author Brian Goetz
+ */
+@Test
+public class MinMaxByTest extends OpTestCase {
+    public void testMinBy() {
+        assertTrue(!countTo(0).stream().minBy(Integer::compare).isPresent());
+        assertTrue(!countTo(0).stream().maxBy(Integer::compare).isPresent());
+        assertEquals(1, (int) countTo(1000).stream().minBy(Integer::compare).get());
+        assertEquals(1000, (int) countTo(1000).stream().maxBy(Integer::compare).get());
+    }
+
+    @Test(dataProvider = "opArrays", dataProviderClass = StreamTestDataProvider.class)
+    public void testOps(String name, StreamTestData<Integer> data) {
+        exerciseTerminalOps(data, s -> s.minBy(Integer::compare));
+        exerciseTerminalOps(data, s -> s.maxBy(Integer::compare));
+    }
+}