TextUtils { public static <T> Optional<T> routeByKey(Map<Predicate<String>, Function<String, T>> routes) { return routeByKey(null, routes, null, null); }  static String replacePlaceholders(final String template, Map<Placeholder, String> values); static String getPlaceholder(Placeholder placeholder); static String underscoreToUpperCamel(String input); static boolean containsPlaceholder(final String template, Placeholder placeholder); static String enumerateValues(Collection<String> values); static String enumerateValues(Collection<String> values, String delimiter); static String joinLines(Collection<String> values); static String humanizedDuration(ZonedDateTime zonedDateTime); static String createAssignment(Map<String, Set<String>> data); static Map<String, Set<String>> parseAssignment(String data); static Set<Integer> parseRange(String range); static boolean isRange(String value); static Set<String> parseEnumeration(String data); static Set<String> parseEnumeration(String data, String delimiter); static boolean isRangeDelimiter(String key); static boolean isFirstElementKey(String key); static boolean isLastElementKey(String key); static boolean isNextElementKey(String key); static boolean isPrevElementKey(String key); static boolean isExitKey(String key); static boolean isSkipKey(String key); static boolean isYesKey(String key); static boolean isNoKey(String key); static boolean isRepeatKey(String key); static boolean isNumericKey(String key); static boolean isPositiveInt(String key); static String quoteIfNeeded(String value); static String getAsExactMatch(String value); static Collection<String> removeSuffixes(Collection<String> input, Collection<String> suffixes); static Optional<T> routeByKey(Map<Predicate<String>, Function<String, T>> routes); static Optional<T> routeByKey(Map<Predicate<String>, Function<String, T>> routes, Consumer<String> onInvalidKey); static Optional<T> routeByKey(Map<Predicate<String>, Function<String, T>> routes, Consumer<String> onInvalidKey, Consumer<Exception> onException); static String getVersion(); static final String YES; static final String NO; static final String QUIT; static final String DASH; }
@Test public void testRouteByKey() throws IOException { List<Exception> exceptions = new ArrayList<>(); List<String> invalidKeys = new ArrayList<>(); Consumer<Exception> onException = exceptions::add; Consumer<String> onInvalidKey = invalidKeys::add; Map<Predicate<String>, Function<String, Boolean>> routes = new LinkedHashMap<Predicate<String>, Function<String, Boolean>>() { { put(k -> k.equals("y"), k -> true); put(k -> k.equals("n"), k -> false); put(k -> k.equals("e"), k -> Boolean.TRUE.compareTo(null) > 0); } }; Optional<Boolean> routeWithYes = routeByKey(mockConsoleReader("y"), routes, onInvalidKey, onException); assertTrue(routeWithYes.isPresent()); assertThat(routeWithYes.get(), equalTo(true)); Optional<Boolean> routeWithNo = routeByKey(mockConsoleReader("n"), routes, onInvalidKey, onException); assertTrue(routeWithNo.isPresent()); assertThat(routeWithNo.get(), equalTo(false)); routeByKey(mockConsoleReader("iy"), routes, onInvalidKey, onException); assertThat(invalidKeys, contains("i")); routeByKey(mockConsoleReader("e"), routes, onInvalidKey, onException); assertThat(exceptions, hasSize(1)); }