TextUtils { public static Map<String, Set<String>> parseAssignment(String data) { Map<String, Set<String>> ret = new HashMap<>(); if (data != null) { StringTokenizer stringTokenizer = new StringTokenizer(data, String.format("%s%s%s", EQUALITY, COMMA, SEMICOLON), true); String key = null; Set<String> value = new HashSet<>(); int counter = 0; while (stringTokenizer.hasMoreTokens()) { String token = stringTokenizer.nextToken(); if (isValueDelimiter(token)) { continue; } else if (isKeyDelimiter(token) && key != null) { ret.put(key, value); counter = 0; key = null; value = new HashSet<>(); continue; } else if (counter == 0) { key = token.trim(); } else { String trimmedValue = token.trim(); if (!StringUtils.isEmpty(trimmedValue)) { value.add(trimmedValue); } } counter++; } if (key != null) { ret.put(key, value); } } return ret; }  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 testParseAssignment() { assertThat(parseAssignment(null).keySet(), empty()); Map<String, Set<String>> parsedEmptyValueAssignment = parseAssignment("key1 = "); assertThat(parsedEmptyValueAssignment.keySet(), contains("key1")); assertThat(parsedEmptyValueAssignment.get("key1"), empty()); String assignmentString = "key1 = value11, value12; key2=value21,value22"; Map<String, Set<String>> assignmentData = parseAssignment(assignmentString); assertThat(assignmentData.keySet(), hasSize(2)); assertThat(assignmentData.keySet(), containsInAnyOrder("key1", "key2")); assertThat(assignmentData.get("key1"), containsInAnyOrder("value11", "value12")); assertThat(assignmentData.get("key2"), containsInAnyOrder("value21", "value22")); }