/** * Returns the latest name from a given list of names that matches the given prefix and suffix part. * <p> * E.g. * {@code getLatestName(["test-foo2", "test-foo10", "other", "test-foo1"], "test", "-foo") returns "test-foo10"} * </p> * @param names * @param prefix * @param suffix * @return */ public static Optional<String> getLatestName(Collection<String> names, String prefix, String suffix) { return names.stream() .filter(n -> n.startsWith(prefix)) .max((n1, n2) -> Utils.getNumPart(n1, suffix) - Utils.getNumPart(n2, suffix)); } /**
/** * Generates the next name for the given name and suffix. * <p> * E.g. {@code getNextName("test-foo", "-foo") returns "test-foo2", * getNextName("test-foo2", "-foo") returns "test-foo3"} * </p> * @param name the current name * @param suffix the suffix without the number part * @return the next name */ public static String getNextName(String name, String suffix) { Utils.requireNonEmpty(name, "Empty name"); String prefix; String suffixWithNum; int num = getNumPart(name, suffix); if (num == -1) { prefix = name; suffixWithNum = suffix; } else { prefix = getPrefix(name, suffix); suffixWithNum = suffix + (num + 1); } return prefix + suffixWithNum; }