From 931f17e3435207ea97336bd230ac92f160a6073e Mon Sep 17 00:00:00 2001 From: uprem Date: Sat, 10 Sep 2016 13:10:54 +0530 Subject: [PATCH] Fix WordCounterSpliterator.tryAdvance fixes: 1. when initial string is empty, prevents throwing StringIndexOutOfBoundsException 2. the last valid iteration return true. old code returns false for the last character which breaks tryAdvance() specs --- src/main/java/lambdasinaction/chap7/WordCount.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/lambdasinaction/chap7/WordCount.java b/src/main/java/lambdasinaction/chap7/WordCount.java index 13ccce52..8a43a0f3 100644 --- a/src/main/java/lambdasinaction/chap7/WordCount.java +++ b/src/main/java/lambdasinaction/chap7/WordCount.java @@ -83,8 +83,12 @@ private WordCounterSpliterator(String string) { @Override public boolean tryAdvance(Consumer action) { - action.accept(string.charAt(currentChar++)); - return currentChar < string.length(); + if(currentChar < string.length()) { + action.accept(string.charAt(currentChar++)); + return true; + } else { + return false; + } } @Override