DoubleMetaphoneMatcher extends AbstractAttributeMatcher { @Override public double getWeight(String str1, String str2) { String code1 = algorithm.encode(str1); String code2 = algorithm.encode(str2); if (code1 == null || code2 == null) { return 0d; } int maxLengh = Math.max(code1.length(), code2.length()); if (maxLengh == 0) { if (StringUtils.equalsIgnoreCase(str1, str2)) { return 1d; } else { return 0d; } } return StringComparisonUtil.difference(code1, code2) / (double) maxLengh; }  @Override AttributeMatcherType getMatchType(); @Override double getWeight(String str1, String str2);  }
@Test public void testGetWeight() { DoubleMetaphoneMatcher doubleMetaphoneMatcher = new DoubleMetaphoneMatcher(); String a = "John"; String b = "Jon"; double matchingWeight = doubleMetaphoneMatcher.getMatchingWeight(a, b); assertEquals(1.0d, matchingWeight, EPSILON); a = "23"; matchingWeight = doubleMetaphoneMatcher.getMatchingWeight(a, a); assertEquals("input strings are the same => result should be 1.", 1.0d, matchingWeight, EPSILON); b = "64"; matchingWeight = doubleMetaphoneMatcher.getMatchingWeight(a, b); assertNotSame("input strings are the same => result should NOT be 1.", 1.0d, matchingWeight); String c = " "; matchingWeight = doubleMetaphoneMatcher.getMatchingWeight(c, c); assertEquals("input strings are the same => result should be 1.", 0d, matchingWeight, EPSILON); a = "JohnFit"; b = "JohnFitzgeraldKennedy"; matchingWeight = doubleMetaphoneMatcher.getMatchingWeight(a, b); assertTrue("input strings are not the same but DoubleMetaphone should not be able to distinguish between " + a + " and " + b, 1.0d == matchingWeight); a = "\n"; b = "Hulme"; matchingWeight = doubleMetaphoneMatcher.getMatchingWeight(a, b); assertTrue("input strings are not the same " + a + " and " + b, 0.0d == matchingWeight); }