@Test public void testGenerateIdExamples() { Map<String, String> examples = new HashMap<>(); examples.put("Le ciel est bleu", "Le-ciel-est-bleu"); examples.put("Le ciel est bleu ", "Le-ciel-est-bleu"); examples.put(" Le ciel est bleu ", "Le-ciel-est-bleu"); examples.put("open+source", "open-source"); examples.put("open + source", "open-source"); examples.put("open + source", "open-source"); examples.put("S. Fermigier first law of project management", "S-Fermigier-first-law-of"); for (Entry<String, String> example : examples.entrySet()) { assertEquals(example.getValue(), IdUtils.generateId(example.getKey(), "-", false, 24)); } }
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; }