|
| 1 | +package com.brianway.learning.java8.streamapi.parallel; |
| 2 | + |
| 3 | +import java.util.Spliterator; |
| 4 | +import java.util.function.Consumer; |
| 5 | +import java.util.stream.Stream; |
| 6 | +import java.util.stream.StreamSupport; |
| 7 | + |
| 8 | +/** |
| 9 | + * Spliterator: splitable iterator |
| 10 | + */ |
| 11 | +public class WordCount { |
| 12 | + |
| 13 | + public static final String SENTENCE = |
| 14 | + " Nel mezzo del cammin di nostra vita " + |
| 15 | + "mi ritrovai in una selva oscura" + |
| 16 | + " che la dritta via era smarrita "; |
| 17 | + |
| 18 | + public static void main(String[] args) { |
| 19 | + System.out.println("Found " + countWordsIteratively(SENTENCE) + " words"); |
| 20 | + System.out.println("Found " + countWords(SENTENCE) + " words"); |
| 21 | + } |
| 22 | + |
| 23 | + public static int countWordsIteratively(String s) { |
| 24 | + int counter = 0; |
| 25 | + boolean lastSpace = true; |
| 26 | + for (char c : s.toCharArray()) { |
| 27 | + if (Character.isWhitespace(c)) { |
| 28 | + lastSpace = true; |
| 29 | + } else { |
| 30 | + if (lastSpace) counter++; |
| 31 | + lastSpace = false; |
| 32 | + } |
| 33 | + } |
| 34 | + return counter; |
| 35 | + } |
| 36 | + |
| 37 | + public static int countWords(String s) { |
| 38 | +// Stream<Character> stream = IntStream.range(0, s.length()) |
| 39 | +// .mapToObj(SENTENCE::charAt).parallel(); |
| 40 | + Spliterator<Character> spliterator = new WordCounterSpliterator(s); |
| 41 | + Stream<Character> stream = StreamSupport.stream(spliterator, true); |
| 42 | + |
| 43 | + return countWords(stream); |
| 44 | + } |
| 45 | + |
| 46 | + private static int countWords(Stream<Character> stream) { |
| 47 | + WordCounter wordCounter = stream.reduce(new WordCounter(0, true), |
| 48 | + WordCounter::accumulate, |
| 49 | + WordCounter::combine); |
| 50 | + return wordCounter.getCounter(); |
| 51 | + } |
| 52 | + |
| 53 | + private static class WordCounter { |
| 54 | + private final int counter; |
| 55 | + private final boolean lastSpace; |
| 56 | + |
| 57 | + public WordCounter(int counter, boolean lastSpace) { |
| 58 | + this.counter = counter; |
| 59 | + this.lastSpace = lastSpace; |
| 60 | + } |
| 61 | + |
| 62 | + public WordCounter accumulate(Character c) { |
| 63 | + if (Character.isWhitespace(c)) { |
| 64 | + return lastSpace ? this : new WordCounter(counter, true); |
| 65 | + } else { |
| 66 | + return lastSpace ? new WordCounter(counter + 1, false) : this; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public WordCounter combine(WordCounter wordCounter) { |
| 71 | + return new WordCounter(counter + wordCounter.counter, wordCounter.lastSpace); |
| 72 | + } |
| 73 | + |
| 74 | + public int getCounter() { |
| 75 | + return counter; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static class WordCounterSpliterator implements Spliterator<Character> { |
| 80 | + |
| 81 | + private final String string; |
| 82 | + private int currentChar = 0; |
| 83 | + |
| 84 | + private WordCounterSpliterator(String string) { |
| 85 | + this.string = string; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean tryAdvance(Consumer<? super Character> action) { |
| 90 | + action.accept(string.charAt(currentChar++)); |
| 91 | + return currentChar < string.length(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Spliterator<Character> trySplit() { |
| 96 | + int currentSize = string.length() - currentChar; |
| 97 | + if (currentSize < 10) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + for (int splitPos = currentSize / 2 + currentChar; splitPos < string.length(); splitPos++) { |
| 101 | + if (Character.isWhitespace(string.charAt(splitPos))) { |
| 102 | + Spliterator<Character> spliterator = new WordCounterSpliterator(string.substring(currentChar, splitPos)); |
| 103 | + currentChar = splitPos; |
| 104 | + return spliterator; |
| 105 | + } |
| 106 | + } |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public long estimateSize() { |
| 112 | + return string.length() - currentChar; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public int characteristics() { |
| 117 | + return ORDERED + SIZED + SUBSIZED + NONNULL + IMMUTABLE; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments