TextUtil { public static int getLevenshteinDistance(String firstString, String secondString) { final String s0 = firstString == null ? "" : firstString; final String s1 = secondString == null ? "" : secondString; final int len0 = s0.length() + 1; final int len1 = s1.length() + 1; int[] cost = new int[len0]; int[] newcost = new int[len0]; for (int i = 0; i < len0; ++i) { cost[i] = i; } for (int j = 1; j < len1; ++j) { newcost[0] = j; for (int i = 1; i < len0; ++i) { final int match = (s0.charAt(i - 1) == s1.charAt(j - 1)) ? 0 : 1; final int costReplace = cost[i - 1] + match; final int costInsert = cost[i] + 1; final int costDelete = newcost[i - 1] + 1; newcost[i] = Math.min(Math.min(costInsert, costDelete), costReplace); } final int[] swap = cost; cost = newcost; newcost = swap; } return cost[len0 - 1]; } private  TextUtil(); @Pure static boolean equalsIgnoreCase(String firstText, String secondText, boolean isNullEmptyEquivalence); @Pure @SuppressWarnings("checkstyle:magicnumber") static String encodeBase26(int number); @Pure @SuppressWarnings("checkstyle:npathcomplexity") static Map<String, Integer> getHtmlToJavaTranslationTable(); @Pure @SuppressWarnings("checkstyle:npathcomplexity") static Map<Character, String> getJavaToHTMLTranslationTable(); @Pure @SuppressWarnings("checkstyle:magicnumber") static String parseHTML(String html); @Pure static String toHTML(String text); @Pure static String cutString(String text, int column); @Pure static String[] cutStringAsArray(String text, int column); static void cutStringAsArray(String text, CutStringCritera critera, List<String> output); @Pure static char getMnemonicChar(String text); @Pure static String removeMnemonicChar(String text); @Pure @SuppressWarnings("checkstyle:npathcomplexity") static Map<Character, String> getAccentTranslationTable(); @Pure static String removeAccents(String text); @Pure static String removeAccents(String text, Map<Character, String> map); @Pure @Inline(value = "textUtil.split('{', '}', $1)", imported = {TextUtil.class}) static String[] splitBrackets(String str); @Pure static String[] split(char leftSeparator, char rightSeparator, String str); @Pure @Inline(value = "textUtil.splitAsList('{', '}', $1)", imported = {TextUtil.class}) static List<String> splitBracketsAsList(String str); @Pure static List<String> splitAsList(char leftSeparator, char rightSeparator, String str); @Pure @Inline(value = "textUtil.splitAsUUIDs('{', '}', $1)", imported = {TextUtil.class}) static List<UUID> splitBracketsAsUUIDs(String str); @Pure static List<UUID> splitAsUUIDs(char leftSeparator, char rightSeparator, String str); @Pure @Inline(value = "TextUtil.join('{', '}', $1)", imported = {TextUtil.class}) static String mergeBrackets(@SuppressWarnings("unchecked") T... strs); @Pure @Inline(value = "TextUtil.join('{', '}', $1)", imported = {TextUtil.class}) static String mergeBrackets(Iterable<?> strs); @Pure static String join(char leftSeparator, char rightSeparator, @SuppressWarnings("unchecked") T... strs); @Pure static String join(char leftSeparator, char rightSeparator, Iterable<?> strs); @Pure @Inline(value = "TextUtil.join($1, null, null, Arrays.asList($2))", imported = {TextUtil.class, Arrays.class}) static String join(String joinText, @SuppressWarnings("unchecked") T... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, boolean... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, byte... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, char... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, short... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, int... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, long... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, float... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, double... elements); @Pure @Inline(value = "TextUtil.join($1, null, null, $2)", imported = {TextUtil.class}) static String join(String joinText, Iterable<?> elements); @Pure @Inline(value = "TextUtil.join($1, $2, $3, Arrays.asList($4))", imported = {TextUtil.class, Arrays.class}) static String join(String joinText, String prefix, String postfix, @SuppressWarnings("unchecked") T... elements); @Pure static String join(String joinText, String prefix, String postfix, Iterable<?> elements); @Pure static String join(String joinText, String prefix, String postfix, boolean... elements); @Pure static String join(String joinText, String prefix, String postfix, byte... elements); @Pure static String join(String joinText, String prefix, String postfix, char... elements); @Pure static String join(String joinText, String prefix, String postfix, short... elements); @Pure static String join(String joinText, String prefix, String postfix, int... elements); @Pure static String join(String joinText, String prefix, String postfix, long... elements); @Pure static String join(String joinText, String prefix, String postfix, float... elements); @Pure static String join(String joinText, String prefix, String postfix, double... elements); @Pure @Inline(value = "TextUtil.removeAccents($1, $3).equals(TextUtil.removeAccents($2, $3))", imported = {TextUtil.class}) static boolean equalsIgnoreAccents(String s1, String s2, Map<Character, String> map); @Pure @Inline(value = "TextUtil.removeAccents($1, $3).equalsIgnoreCase(TextUtil.removeAccents($2, $3))", imported = {TextUtil.class}) static boolean equalsIgnoreCaseAccents(String s1, String s2, Map<Character, String> map); @Pure static String toLowerCaseWithoutAccent(String text); @Pure static String toLowerCaseWithoutAccent(String text, Map<Character, String> map); @Pure static String toUpperCaseWithoutAccent(String text); @Pure static String toUpperCaseWithoutAccent(String text, Map<Character, String> map); @Pure @SuppressWarnings({"checkstyle:magicnumber", "checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity"}) static String formatTime(double amount, TimeUnit unit); @Pure @Inline(value = "$3.formatHex($1, $2)", imported = {StringEscaper.class}) static String formatHex(int amount, int digits); @Pure static String formatDouble(double amount, int decimalCount); @Pure static String formatFloat(float amount, int decimalCount); static int getLevenshteinDistance(String firstString, String secondString); @Pure static String toJavaString(String text); @Pure static String toJsonString(String text);  }
@Test public void getLevenshteinDistance() { assertEquals(0, TextUtil.getLevenshteinDistance(null, null)); assertEquals(0, TextUtil.getLevenshteinDistance(null, "")); assertEquals(0, TextUtil.getLevenshteinDistance("", null)); assertEquals(0, TextUtil.getLevenshteinDistance("", "")); assertEquals(1, TextUtil.getLevenshteinDistance("", "a")); assertEquals(3, TextUtil.getLevenshteinDistance("a", "abcd")); }