package remonsinnema.blog.fizzbuzz;

import java.util.Arrays;
import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class FizzBuzzer implements Function<Integer, String> {

  private final Function<Integer, String> defaultReplacer
      = n -> Integer.toString(n);
  private final Collection<ReplaceMultipleWithFixedText> replacers = Arrays.asList(
      new ReplaceMultipleWithFixedText(3, "Fizz"),
      new ReplaceMultipleWithFixedText(5, "Buzz")
  );

  @Override
  public String apply(Integer n) {
    return numberReplacersFor(n)
        .map(function -> function.apply(n))
        .collect(Collectors.joining());
  }

  private Stream<Function<Integer, String>> numberReplacersFor(Integer n) {
    return Stream.of(replacers.stream()
        .filter(replacer -> replacer.test(n))
        .map(replacer -> (Function<Integer, String>) replacer)
        .findFirst()
        .orElse(defaultReplacer));
  }

}

@@ -80,24 +80,82 @@
 Collection;%0A
+import java.util.Iterator;%0Aimport java.util.Spliterators;%0A
 import java.
@@ -246,16 +246,55 @@
 Stream;%0A
+import java.util.stream.StreamSupport;%0A
 %0A%0Apublic
@@ -887,25 +887,53 @@
     
-return Stream.of(
+Iterator%3CFunction%3CInteger, String%3E%3E result = 
 repl
@@ -1069,35 +1069,153 @@
    .
-findFirst()%0A        .orElse
+iterator();%0A    return result.hasNext()%0A        ? StreamSupport.stream(Spliterators.spliteratorUnknownSize(result, 0), false)%0A        : Stream.of
 (def
@@ -1227,17 +1227,16 @@
 eplacer)
-)
 ;%0A  %7D%0A%0A%7D

Return all applicable replacers