--- /dev/null 2019-10-02 10:12:24.000000000 +0100 +++ new/test/jdk/java/util/stream/test/org/openjdk/tests/java/util/stream/CollectorExample.java 2019-10-02 10:12:24.000000000 +0100 @@ -0,0 +1,109 @@ +/* + * 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.stream.Collector; +import java.util.function.Function; +import java.util.function.Supplier; +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); + } +} + + + +