From 25f2f90ef0be5292145873181dbcb7700fb0aae4 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sat, 13 Aug 2022 00:55:44 +0200 Subject: [PATCH] New method in StringUtil --- .../fr/pandacube/lib/util/StringUtil.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java b/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java index 88fe99f..c6c91fd 100644 --- a/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java +++ b/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java @@ -2,6 +2,9 @@ package fr.pandacube.lib.util; import java.util.Arrays; import java.util.List; +import java.util.function.Predicate; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Provides various methods to manipulate Strings. @@ -78,4 +81,39 @@ public class StringUtil { Arrays.fill(chars, base); return String.valueOf(chars); } + + + + + + private static final Pattern endingNumber = Pattern.compile("(\\d+)$"); + + + /** + * Generate a name based on the original name, but that does not conflit with anouther one, according to the + * provided predicate. + * It can be used to to add an entry in a map when the key already exists, and it is ok to modify the added key to + * not erase the previous data. + * This situation can be compared to when a file is added to a directory but another file with the same name exists, + * so the new file have a suffix number to make the file name different. + *

+ * Be aware that this method may run an infinite loop if the predicate continuously returns true. + * @param originalName the original conflicting name. + * @param conflictTester a predicate that test if a generated name stills conflict with a set of name + * @return the original name, with a suffix number (ex: {@code "original1"}). + */ + public static String fixConflictName(String originalName, Predicate conflictTester) { + int suffix = 1; + Matcher endingMatcher = endingNumber.matcher(originalName); + if (endingMatcher.find()) { + suffix = Integer.parseInt(endingMatcher.group(1)) + 1; + originalName = originalName.substring(0, endingMatcher.start()); + } + + for (;; suffix++) { + String newStr = originalName + suffix; + if (!conflictTester.test(newStr)) + return newStr; + } + } }