TextUtils { public static Collection<String> removeSuffixes(Collection<String> input, Collection<String> suffixes) { if (input == null) { return null; } if (input.isEmpty()) { return Collections.emptyList(); } if (suffixes == null || suffixes.isEmpty()) { return input; } return input.stream() .map(str -> { Optional<String> matchingSuffix = suffixes.stream() .filter(s -> str.endsWith(s) || isContainsMatch(str) && str.endsWith(s + "%") || isExactMatch(str) && str.endsWith(s + "$") ) .findFirst(); if (matchingSuffix.isPresent()) { int strLength = str.length(); int suffixLength = matchingSuffix.get().length(); char lastChar = str.charAt(str.length() - 1); boolean hasSpecialLastChar = lastChar == '$' || lastChar == '%'; int end = strLength - suffixLength; if (hasSpecialLastChar) { end--; } String substr = str.substring(0, end); return hasSpecialLastChar ? substr + lastChar : getAsExactMatch(substr); } return str; }) .collect(Collectors.toList()); }  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 testRemoveSuffixes() { assertThat(removeSuffixes(null, Collections.emptySet()), is(nullValue())); assertThat(removeSuffixes(Collections.emptySet(), null), is(empty())); assertThat(removeSuffixes(Collections.emptySet(), Collections.emptySet()), is(empty())); List<String> seed = Arrays.asList("%example.com%", "^test.net$", "touch", "click.net", "three", "%a%", "^b$"); List<String> suffixes = Arrays.asList(".com", ".net", "a", "b"); Collection<String> output = removeSuffixes(seed, suffixes); assertThat(output, hasSize(7)); assertThat(output, contains("%example%", "^test$", "touch", "^click$", "three", "%%", "^$")); }