concept wordcounterspliterator in category java

appears as: WordCounterSpliterator
Modern Java in Action: Lambdas, streams, reactive and functional programming

This is an excerpt from Manning's book Modern Java in Action: Lambdas, streams, reactive and functional programming.

Listing 7.6. The WordCounterSpliterator
class WordCounterSpliterator implements Spliterator<Character> {
    private final String string;
    private int currentChar = 0;
    public WordCounterSpliterator(String string) {
        this.string = string;
    }
    @Override
    public boolean tryAdvance(Consumer<? super Character> action) {
        action.accept(string.charAt(currentChar++));                      #1
        return currentChar < string.length();                             #2
    }
    @Override
    public Spliterator<Character> trySplit() {
        int currentSize = string.length() - currentChar;
        if (currentSize < 10) {
            return null;                                                  #3
        }
        for (int splitPos = currentSize / 2 + currentChar;
                 splitPos < string.length(); splitPos++) {                #4
            if (Character.isWhitespace(string.charAt(splitPos))) {        #5
                Spliterator<Character> spliterator =                      #6
                   new WordCounterSpliterator(string.substring(currentChar,
                                                               splitPos));
                currentChar = splitPos;                                   #7
                return spliterator;                                       #8
            }
        }
        return null;
    }
    @Override
    public long estimateSize() {
        return string.length() - currentChar;
    }
    @Override
    public int characteristics() {
        return ORDERED + SIZED + SUBSIZED + NON-NULL + IMMUTABLE;
    }
}

This Spliterator is created from the String to be parsed and iterates over its Characters by holding the index of the one currently being traversed. Let’s quickly revisit the methods of the WordCounterSpliterator implementing the Spliterator interface:

Java 8 in Action: Lambdas, streams, and functional-style programming

This is an excerpt from Manning's book Java 8 in Action: Lambdas, streams, and functional-style programming.

Listing 7.6. The WordCounterSpliterator
class WordCounterSpliterator implements Spliterator<Character> { private final String string; private int currentChar = 0; public WordCounterSpliterator(String string) { this.string = string; } @Override public boolean tryAdvance(Consumer<? super Character> action) { action.accept(string.charAt(currentChar++)); #1 return currentChar < string.length(); #2 } @Override public Spliterator<Character> trySplit() { int currentSize = string.length() - currentChar; if (currentSize < 10) { return null; #3 } for (int splitPos = currentSize / 2 + currentChar; splitPos < string.length(); splitPos++) { #4 if (Character.isWhitespace(string.charAt(splitPos))) { #5 Spliterator<Character> spliterator = #6 new WordCounterSpliterator(string.substring(currentChar, splitPos)); currentChar = splitPos; #7 return spliterator; } } return null; } @Override public long estimateSize() { return string.length() - currentChar; } @Override public int characteristics() { return ORDERED + SIZED + SUBSIZED + NONNULL + IMMUTABLE; } } #1: Consume the current character. #2: Return true if there are further characters to be consumed. #3: Return null to signal that the String to be parsed is small enough to be processed sequentially. #4: Set the candidate’s split position to be half of the String to be parsed. #5: Advance the split position until the next space. #6: Create a new WordCounterSpliterator parsing the String from the start to the split position. #7: Set the start position of this WordCounterSpliterator to the split position.

This Spliterator is created from the String to be parsed and iterates over its Characters by holding the index of the one currently being traversed. Let’s quickly revisit the methods of the WordCounterSpliterator implementing the Spliterator interface: