public class WordCounter implements OutChannel<Integer> {

  private int numWords = 0;

  private boolean wasReadingWord;

  public void done() throws Exception {
    System.out.println("Number of words = " + numWords);
  }

  public void write(Integer data) throws Exception {
    if (Character.isWhitespace(data)) {
      if (wasReadingWord) {
        wasReadingWord = false;
      }
    } else if (!wasReadingWord) {
      numWords++;
      wasReadingWord = true;
    }
  }

  public int getNumWords() {
    return numWords;
  }

}
