--- /dev/null 2013-10-11 17:30:01.735334065 +0400
+++ new/src/share/demo/lambda/BulkDataOperations/src/WC.java 2013-10-11 17:36:53.767199300 +0400
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Oracle nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * This source code is provided to illustrate the usage of a given feature
+ * or technique and has been deliberately simplified. Additional steps
+ * required for a production-quality application, such as security checks,
+ * input validation and proper error handling, might not be present in
+ * this sample code.
+ */
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.function.Consumer;
+
+/**
+ * WC - print newline, word, and character counts for each file. See
+ * {@link #usage} method to run from command line. This sample shows usages of:
+ *
+ * - Lambda and bulk operations. Shows how to create custom collector to
+ * gather custom statistic. Also there is implementation of collecting
+ * statistics using built-in API.
+ * - Constructor reference.
+ * - Try-with-resources feature.
+ *
+ *
+ * @author Andrey Nazarov
+ */
+public class WC {
+
+ //Number of characters that may be read
+ private static final int READ_AHEAD_LIMIT = 100_000_000;
+
+ /**
+ * The main method for the WC program. Run program with empty argument list
+ * to see possible arguments.
+ *
+ * @param args the argument list for WC.
+ * @throws java.io.IOException If an input exception occurred.
+ */
+ public static void main(String[] args) throws IOException {
+
+ if (args.length != 1) {
+ usage();
+ return;
+ }
+
+ try (BufferedReader reader = new BufferedReader(
+ new FileReader(args[0]))) {
+ reader.mark(READ_AHEAD_LIMIT);
+
+ collectInFourPasses(reader);
+ collectInOnePass(reader);
+ } catch (FileNotFoundException e) {
+ usage();
+ System.out.println(e);
+ }
+ }
+
+ private static void collectInFourPasses(BufferedReader reader)
+ throws IOException {
+ /*
+ * Input is read as a stream of lines by lines().
+ * Every line in turned into a stream of chars by flatMap(...) method.
+ * Length of the stream is counted by count().
+ */
+ System.out.println("Character counts = "
+ + reader.lines().flatMap(str -> str.chars().boxed()).count());
+ reader.reset();
+ /*
+ * Input is read as a stream of lines by lines().
+ * Every line is split by white spaces into words by flatMap(...)
+ * method.
+ * Empty lines are removed by filter(...) method.
+ * Length of the stream is counted by count().
+ */
+ System.out.println("Word counts = "
+ + reader.lines()
+ .flatMap(str -> Arrays.stream(str.split("\\W")))
+ .filter(str -> !str.isEmpty()).count());
+ reader.reset();
+
+ System.out.println("Newline counts = " + reader.lines().count());
+ reader.reset();
+ /*
+ * Input is read as a stream of lines by lines()
+ * Every line is mapped to its length
+ * Maximum of the lengths is calculated
+ */
+ System.out.println("Max line length = "
+ + reader.lines().mapToInt(String::length).max().getAsInt());
+ reader.reset();
+ }
+
+ private static void collectInOnePass(BufferedReader reader) {
+ /*
+ * collect() method has 3 params:
+ * The first parameter is WCStatistic constructor reference.
+ * collect() will create WCStatistics instances, where statistics will
+ * be aggregated.
+ * The second parameter show how WCStatistics will process String value.
+ * The third parameter show how to merge two WCStatistic instances.
+ */
+ System.out.println(reader.lines()
+ .collect(WCStatistics::new, WCStatistics::accept,
+ WCStatistics::combine));
+ }
+
+ private static void usage() {
+ System.out.println("Usage: " + WC.class.getSimpleName() + " FILE");
+ System.out.println("Print newline, word,"
+ + " character counts and max line length for FILE.");
+ }
+
+ private static class WCStatistics implements Consumer {
+ /*
+ * @implNote This implementation is not thread safe. However, it is safe to use
+ * WCStatistics on a parallel stream, because the parallel
+ * implementation of {@link java.util.stream.Stream#collect Stream.collect()}
+ * provides the necessary partitioning, isolation, and merging of results for
+ * safe and efficient parallel execution.
+ */
+
+ private long characterCount;
+ private long lineCount;
+ private long wordCount;
+ private long maxLineLength;
+
+ /*
+ * Processes line.
+ */
+ @Override
+ public void accept(String line) {
+ characterCount += line.length();
+ lineCount++;
+ wordCount += Arrays.stream(line.split("\\W"))
+ .filter(str -> !str.isEmpty()).count();
+ maxLineLength = Math.max(maxLineLength, line.length());
+ }
+
+ /*
+ * Merges two WCStatistics.
+ */
+ public void combine(WCStatistics stat) {
+ wordCount += stat.wordCount;
+ lineCount += stat.lineCount;
+ characterCount += stat.characterCount;
+ maxLineLength = Math.max(maxLineLength, stat.maxLineLength);
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("#------WCStatistic------#\n");
+ sb.append("Character counts = ").append(characterCount).append("\n");
+ sb.append("Word counts = ").append(wordCount).append("\n");
+ sb.append("Newline counts = ").append(lineCount).append("\n");
+ sb.append("Max line length = ").append(maxLineLength).append("\n");
+ return sb.toString();
+ }
+ }
+}