1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package org.openjdk.tests.java.util.stream;
  25 
  26 /*
  27  * THE CONTENTS OF THIS FILE HAVE TO BE IN SYNC WITH THE EXAMPLES USED
  28  * IN THE JAVADOC.
  29  *
  30  * @test
  31  * @bug 8231161
  32  * @compile CollectorExample.java
  33  * @summary Compilation test only. Compile code snippets from
  34  * java.util.stream.Collector class-level API documentation
  35  */
  36 
  37 import java.util.*;
  38 import java.util.function.BiConsumer;
  39 import java.util.function.BinaryOperator;
  40 import java.util.stream.Collector;
  41 import java.util.function.Function;
  42 import java.util.function.Supplier;
  43 import java.util.stream.Collectors;
  44 
  45 public class CollectorExample {
  46 
  47     // Empty helper classes
  48 
  49     class Widget {
  50     }
  51 
  52     class Employee {
  53         public int getSalary() {
  54             return 0;    // money isn't everything
  55         }
  56 
  57         public Department getDepartment() {
  58             return new Department();
  59         }
  60     }
  61 
  62     class Department {
  63     }
  64 
  65     <T, A, R> void testSnippet1(Collector<T, A, R> collector, T t1, T t2) {
  66 
  67         Supplier<A> supplier = collector.supplier();
  68         BiConsumer<A, T> accumulator = collector.accumulator();
  69         BinaryOperator<A> combiner = collector.combiner();
  70         Function<A, R> finisher = collector.finisher();
  71 
  72         // Example start
  73         A a1 = supplier.get();
  74         accumulator.accept(a1, t1);
  75         accumulator.accept(a1, t2);
  76         R r1 = finisher.apply(a1);
  77 
  78         A a2 = supplier.get();
  79         accumulator.accept(a2, t1);
  80         A a3 = supplier.get();
  81         accumulator.accept(a3, t2);
  82         R r2 = finisher.apply(combiner.apply(a2, a3));
  83     }
  84 
  85     void testSnippet2() {
  86         Collector<Widget, ?, TreeSet<Widget>> intoSet =
  87                 Collector.of(TreeSet::new, TreeSet::add,
  88                         (left, right) -> { left.addAll(right); return left; });
  89     }
  90 
  91     <T, A, R> void testSnippet3(Collector<T, A, R> collector, Collection<T> data) {
  92         A container = collector.supplier().get();
  93         for (T t : data)
  94             collector.accumulator().accept(container, t);
  95         collector.finisher().apply(container);
  96     }
  97 
  98     void testSnippet4and5() {
  99         Collector<Employee, ?, Integer> summingSalaries
 100                 = Collectors.summingInt(Employee::getSalary);
 101 
 102         Collector<Employee, ?, Map<Department, Integer>> summingSalariesByDept
 103                 = Collectors.groupingBy(Employee::getDepartment, summingSalaries);
 104     }
 105 }
 106 
 107 
 108 
 109