protected Function<Iterable<?>, ?> createReducerFunction(String reducerName, Map<String, ?> parameters){ if(Objects.equals(reducerName, "joiner")){ String separator = (String) parameters.get("separator"); return StringFunctions.joiner(separator == null ? ", " : separator); } if (Objects.equals(reducerName, "formatString")){ String format = Preconditions.checkNotNull((String)parameters.get("format"), "format"); return StringFunctions.formatterForIterable(format); } throw new IllegalStateException("unknown function: " + reducerName); }
@Test public static void testFormatterForIterable() { Assert.assertEquals(StringFunctions.formatterForIterable("Hello %s").apply(ImmutableList.of("World")), "Hello World"); Assert.assertEquals(StringFunctions.formatterForIterable("Hello").apply(ImmutableList.of()), "Hello"); Assert.assertEquals(StringFunctions.formatterForIterable("Hello").apply(null), "Hello"); }