@Test public void testGenerateIdAccentsLower() { String s1 = "C'est l'\u00E9t\u00E9 !"; assertEquals("c-est-l-ete", IdUtils.generateId(s1, "-", true, 100)); assertEquals("C-est-l-ete", IdUtils.generateId(s1, "-", false, 100)); String s2 = "C'est !!! l'\u00E9t\u00E9 !!!!"; assertEquals("c-est-l-ete", IdUtils.generateId(s2, "-", true, 100)); assertEquals("C-est-l-ete", IdUtils.generateId(s2, "-", false, 100)); }
public static String generateId(String s, String wordSeparator, boolean lower, int maxChars) { s = StringUtils.toAscii(s); s = s.trim(); if (lower) { s = s.toLowerCase(); } String[] words = s.split(WORD_SPLITTING_REGEXP); List<String> wordsList = new ArrayList<>(); for (String word : words) { if (word != null && word.length() > 0) { wordsList.add(word); } } if (wordsList.isEmpty()) { return generateStringId(); } StringBuilder sb = new StringBuilder(); String id; if (maxChars > 0) { sb.append(wordsList.get(0)); for (int i = 1; i < wordsList.size(); i++) { String newWord = wordsList.get(i); if (sb.length() + newWord.length() > maxChars) { break; } else { sb.append(wordSeparator).append(newWord); } } id = sb.toString(); id = id.substring(0, Math.min(id.length(), maxChars)); } else { id = String.join(wordSeparator, wordsList); } return id; }
IdUtils { public static String generateId(String s, String wordSeparator, boolean lower, int maxChars) { s = StringUtils.toAscii(s); s = s.trim(); if (lower) { s = s.toLowerCase(); } String[] words = s.split(WORD_SPLITTING_REGEXP); List<String> wordsList = new ArrayList<>(); for (String word : words) { if (word != null && word.length() > 0) { wordsList.add(word); } } if (wordsList.isEmpty()) { return generateStringId(); } StringBuilder sb = new StringBuilder(); String id; if (maxChars > 0) { sb.append(wordsList.get(0)); for (int i = 1; i < wordsList.size(); i++) { String newWord = wordsList.get(i); if (sb.length() + newWord.length() > maxChars) { break; } else { sb.append(wordSeparator).append(newWord); } } id = sb.toString(); id = id.substring(0, Math.min(id.length(), maxChars)); } else { id = String.join(wordSeparator, wordsList); } return id; } }
IdUtils { public static String generateId(String s, String wordSeparator, boolean lower, int maxChars) { s = StringUtils.toAscii(s); s = s.trim(); if (lower) { s = s.toLowerCase(); } String[] words = s.split(WORD_SPLITTING_REGEXP); List<String> wordsList = new ArrayList<>(); for (String word : words) { if (word != null && word.length() > 0) { wordsList.add(word); } } if (wordsList.isEmpty()) { return generateStringId(); } StringBuilder sb = new StringBuilder(); String id; if (maxChars > 0) { sb.append(wordsList.get(0)); for (int i = 1; i < wordsList.size(); i++) { String newWord = wordsList.get(i); if (sb.length() + newWord.length() > maxChars) { break; } else { sb.append(wordSeparator).append(newWord); } } id = sb.toString(); id = id.substring(0, Math.min(id.length(), maxChars)); } else { id = String.join(wordSeparator, wordsList); } return id; } private  IdUtils(); }
IdUtils { public static String generateId(String s, String wordSeparator, boolean lower, int maxChars) { s = StringUtils.toAscii(s); s = s.trim(); if (lower) { s = s.toLowerCase(); } String[] words = s.split(WORD_SPLITTING_REGEXP); List<String> wordsList = new ArrayList<>(); for (String word : words) { if (word != null && word.length() > 0) { wordsList.add(word); } } if (wordsList.isEmpty()) { return generateStringId(); } StringBuilder sb = new StringBuilder(); String id; if (maxChars > 0) { sb.append(wordsList.get(0)); for (int i = 1; i < wordsList.size(); i++) { String newWord = wordsList.get(i); if (sb.length() + newWord.length() > maxChars) { break; } else { sb.append(wordSeparator).append(newWord); } } id = sb.toString(); id = id.substring(0, Math.min(id.length(), maxChars)); } else { id = String.join(wordSeparator, wordsList); } return id; } private  IdUtils(); static String generateStringId(); static long generateLongId(); static String generateId(String s, String wordSeparator, boolean lower, int maxChars); static boolean isValidUUID(String uuid); }
IdUtils { public static String generateId(String s, String wordSeparator, boolean lower, int maxChars) { s = StringUtils.toAscii(s); s = s.trim(); if (lower) { s = s.toLowerCase(); } String[] words = s.split(WORD_SPLITTING_REGEXP); List<String> wordsList = new ArrayList<>(); for (String word : words) { if (word != null && word.length() > 0) { wordsList.add(word); } } if (wordsList.isEmpty()) { return generateStringId(); } StringBuilder sb = new StringBuilder(); String id; if (maxChars > 0) { sb.append(wordsList.get(0)); for (int i = 1; i < wordsList.size(); i++) { String newWord = wordsList.get(i); if (sb.length() + newWord.length() > maxChars) { break; } else { sb.append(wordSeparator).append(newWord); } } id = sb.toString(); id = id.substring(0, Math.min(id.length(), maxChars)); } else { id = String.join(wordSeparator, wordsList); } return id; } private  IdUtils(); static String generateStringId(); static long generateLongId(); static String generateId(String s, String wordSeparator, boolean lower, int maxChars); static boolean isValidUUID(String uuid); static final String UUID_TYPE_4_REGEXP; }