# HG changeset patch # User jboes # Date 1570125596 -3600 # Node ID e25b317d0350fe5933ffdba7ab09d154b687f4e1 # Parent 3ab9f0464a7d71d3a7edc3d5160d19869c2e78d9 8231161: Wrong return type in code sample in Collector API documentation Summary: Correct declaration of container from R to A and add compilation test Reviewed-by: smarks, lancea diff -r 3ab9f0464a7d -r e25b317d0350 src/java.base/share/classes/java/util/stream/Collector.java --- a/src/java.base/share/classes/java/util/stream/Collector.java Thu Oct 03 08:51:40 2019 -0700 +++ b/src/java.base/share/classes/java/util/stream/Collector.java Thu Oct 03 18:59:56 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2019, 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 @@ -33,6 +33,10 @@ import java.util.function.Function; import java.util.function.Supplier; +// A compilation test for the code snippets in this class-level javadoc can be found at: +// test/jdk/java/util/stream/test/org/openjdk/tests/java/util/stream/CollectorExample.java +// The test needs to be updated if the examples in this javadoc change or new examples are added. + /** * A mutable reduction operation that * accumulates input elements into a mutable result container, optionally transforming @@ -154,7 +158,7 @@ * Performing a reduction operation with a {@code Collector} should produce a * result equivalent to: *
{@code
- *     R container = collector.supplier().get();
+ *     A container = collector.supplier().get();
  *     for (T t : data)
  *         collector.accumulator().accept(container, t);
  *     return collector.finisher().apply(container);
diff -r 3ab9f0464a7d -r e25b317d0350 test/jdk/java/util/stream/test/org/openjdk/tests/java/util/stream/CollectorExample.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/util/stream/test/org/openjdk/tests/java/util/stream/CollectorExample.java	Thu Oct 03 18:59:56 2019 +0100
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2019, 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.
+ *
+ * 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;
+
+/*
+ * THE CONTENTS OF THIS FILE HAVE TO BE IN SYNC WITH THE EXAMPLES USED
+ * IN THE JAVADOC.
+ *
+ * @test
+ * @bug 8231161
+ * @compile CollectorExample.java
+ * @summary Compilation test only. Compile code snippets from
+ * java.util.stream.Collector class-level API documentation
+ */
+
+import java.util.*;
+import java.util.function.BiConsumer;
+import java.util.function.BinaryOperator;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
+
+public class CollectorExample {
+
+    // Empty helper classes
+
+    class Widget {
+    }
+
+    class Employee {
+        public int getSalary() {
+            return 0;    // money isn't everything
+        }
+
+        public Department getDepartment() {
+            return new Department();
+        }
+    }
+
+    class Department {
+    }
+
+     void testSnippet1(Collector collector, T t1, T t2) {
+
+        Supplier supplier = collector.supplier();
+        BiConsumer accumulator = collector.accumulator();
+        BinaryOperator combiner = collector.combiner();
+        Function finisher = collector.finisher();
+
+        // Example start
+        A a1 = supplier.get();
+        accumulator.accept(a1, t1);
+        accumulator.accept(a1, t2);
+        R r1 = finisher.apply(a1);
+
+        A a2 = supplier.get();
+        accumulator.accept(a2, t1);
+        A a3 = supplier.get();
+        accumulator.accept(a3, t2);
+        R r2 = finisher.apply(combiner.apply(a2, a3));
+    }
+
+    void testSnippet2() {
+        Collector> intoSet =
+                Collector.of(TreeSet::new, TreeSet::add,
+                        (left, right) -> { left.addAll(right); return left; });
+    }
+
+     void testSnippet3(Collector collector, Collection data) {
+        A container = collector.supplier().get();
+        for (T t : data)
+            collector.accumulator().accept(container, t);
+        collector.finisher().apply(container);
+    }
+
+    void testSnippet4and5() {
+        Collector summingSalaries
+                = Collectors.summingInt(Employee::getSalary);
+
+        Collector> summingSalariesByDept
+                = Collectors.groupingBy(Employee::getDepartment, summingSalaries);
+    }
+}