@Test void testDistanceOfNullAndEmpty() { String a = null; String b = null; assertEquals(0, LevenshteinDistance.getDistance(a, b)); a = ""; b = ""; assertEquals(0, LevenshteinDistance.getDistance(a, b)); a = null; b = ""; assertEquals(0, LevenshteinDistance.getDistance(a, b)); a = ""; b = null; assertEquals(0, LevenshteinDistance.getDistance(a, b)); a = null; b = "x"; assertEquals(1, LevenshteinDistance.getDistance(a, b)); a = "x"; b = null; assertEquals(1, LevenshteinDistance.getDistance(a, b)); a = ""; b = "x"; assertEquals(1, LevenshteinDistance.getDistance(a, b)); a = "x"; b = ""; assertEquals(1, LevenshteinDistance.getDistance(a, b)); }
public static int getDistance(CharSequence a, CharSequence b) { if (a == null && b == null) { return 0; } if (a == null && b != null) { return b.length() * INSERTION_COST; } if (a != null && b == null) { return a.length() * INSERTION_COST; } final int lengthA = a.length(); final int lengthB = b.length(); int[][] distance = new int[lengthA + 1][lengthB + 1]; for (int i = 0; i < lengthA + 1; i++) { distance[i][0] = i * DELETION_COST; } for (int j = 0; j < lengthB + 1; j++) { distance[0][j] = j * INSERTION_COST; } for (int i = 1; i < lengthA + 1; i++) { for (int j = 1; j < lengthB + 1; j++) { if (a.charAt(i - 1) == b.charAt(j - 1)) { distance[i][j] = distance[i - 1][j - 1]; } else { distance[i][j] = Math.min(Math.min( distance[i - 1][j] + DELETION_COST, distance[i][j - 1] + INSERTION_COST), distance[i - 1][j - 1] + SUBSTITUTION_COST); } } } return distance[lengthA][lengthB]; }
LevenshteinDistance { public static int getDistance(CharSequence a, CharSequence b) { if (a == null && b == null) { return 0; } if (a == null && b != null) { return b.length() * INSERTION_COST; } if (a != null && b == null) { return a.length() * INSERTION_COST; } final int lengthA = a.length(); final int lengthB = b.length(); int[][] distance = new int[lengthA + 1][lengthB + 1]; for (int i = 0; i < lengthA + 1; i++) { distance[i][0] = i * DELETION_COST; } for (int j = 0; j < lengthB + 1; j++) { distance[0][j] = j * INSERTION_COST; } for (int i = 1; i < lengthA + 1; i++) { for (int j = 1; j < lengthB + 1; j++) { if (a.charAt(i - 1) == b.charAt(j - 1)) { distance[i][j] = distance[i - 1][j - 1]; } else { distance[i][j] = Math.min(Math.min( distance[i - 1][j] + DELETION_COST, distance[i][j - 1] + INSERTION_COST), distance[i - 1][j - 1] + SUBSTITUTION_COST); } } } return distance[lengthA][lengthB]; } }
LevenshteinDistance { public static int getDistance(CharSequence a, CharSequence b) { if (a == null && b == null) { return 0; } if (a == null && b != null) { return b.length() * INSERTION_COST; } if (a != null && b == null) { return a.length() * INSERTION_COST; } final int lengthA = a.length(); final int lengthB = b.length(); int[][] distance = new int[lengthA + 1][lengthB + 1]; for (int i = 0; i < lengthA + 1; i++) { distance[i][0] = i * DELETION_COST; } for (int j = 0; j < lengthB + 1; j++) { distance[0][j] = j * INSERTION_COST; } for (int i = 1; i < lengthA + 1; i++) { for (int j = 1; j < lengthB + 1; j++) { if (a.charAt(i - 1) == b.charAt(j - 1)) { distance[i][j] = distance[i - 1][j - 1]; } else { distance[i][j] = Math.min(Math.min( distance[i - 1][j] + DELETION_COST, distance[i][j - 1] + INSERTION_COST), distance[i - 1][j - 1] + SUBSTITUTION_COST); } } } return distance[lengthA][lengthB]; } private  LevenshteinDistance(); }
LevenshteinDistance { public static int getDistance(CharSequence a, CharSequence b) { if (a == null && b == null) { return 0; } if (a == null && b != null) { return b.length() * INSERTION_COST; } if (a != null && b == null) { return a.length() * INSERTION_COST; } final int lengthA = a.length(); final int lengthB = b.length(); int[][] distance = new int[lengthA + 1][lengthB + 1]; for (int i = 0; i < lengthA + 1; i++) { distance[i][0] = i * DELETION_COST; } for (int j = 0; j < lengthB + 1; j++) { distance[0][j] = j * INSERTION_COST; } for (int i = 1; i < lengthA + 1; i++) { for (int j = 1; j < lengthB + 1; j++) { if (a.charAt(i - 1) == b.charAt(j - 1)) { distance[i][j] = distance[i - 1][j - 1]; } else { distance[i][j] = Math.min(Math.min( distance[i - 1][j] + DELETION_COST, distance[i][j - 1] + INSERTION_COST), distance[i - 1][j - 1] + SUBSTITUTION_COST); } } } return distance[lengthA][lengthB]; } private  LevenshteinDistance(); static int getInsertionCost(); static synchronized void setInsertionCost(int cost); static int getDeletionCost(); static synchronized void setDeletionCost(int cost); static int getSubstitutionCost(); static synchronized void setSubstitutionCost(int cost); static int getDistance(CharSequence a, CharSequence b); }
LevenshteinDistance { public static int getDistance(CharSequence a, CharSequence b) { if (a == null && b == null) { return 0; } if (a == null && b != null) { return b.length() * INSERTION_COST; } if (a != null && b == null) { return a.length() * INSERTION_COST; } final int lengthA = a.length(); final int lengthB = b.length(); int[][] distance = new int[lengthA + 1][lengthB + 1]; for (int i = 0; i < lengthA + 1; i++) { distance[i][0] = i * DELETION_COST; } for (int j = 0; j < lengthB + 1; j++) { distance[0][j] = j * INSERTION_COST; } for (int i = 1; i < lengthA + 1; i++) { for (int j = 1; j < lengthB + 1; j++) { if (a.charAt(i - 1) == b.charAt(j - 1)) { distance[i][j] = distance[i - 1][j - 1]; } else { distance[i][j] = Math.min(Math.min( distance[i - 1][j] + DELETION_COST, distance[i][j - 1] + INSERTION_COST), distance[i - 1][j - 1] + SUBSTITUTION_COST); } } } return distance[lengthA][lengthB]; } private  LevenshteinDistance(); static int getInsertionCost(); static synchronized void setInsertionCost(int cost); static int getDeletionCost(); static synchronized void setDeletionCost(int cost); static int getSubstitutionCost(); static synchronized void setSubstitutionCost(int cost); static int getDistance(CharSequence a, CharSequence b); static final int DEFAULT_INSERTION_COST; static final int DEFAULT_DELETION_COST; static final int DEFAULT_SUBSTITUTION_COST; }