1 16 17 package org.apache.commons.codec.language; 18 19 import org.apache.commons.codec.EncoderException; 20 import org.apache.commons.codec.StringEncoder; 21 22 29 final class SoundexUtils { 30 31 39 static String clean(String str) { 40 if (str == null || str.length() == 0) { 41 return str; 42 } 43 int len = str.length(); 44 char[] chars = new char[len]; 45 int count = 0; 46 for (int i = 0; i < len; i++) { 47 if (Character.isLetter(str.charAt(i))) { 48 chars[count++] = str.charAt(i); 49 } 50 } 51 if (count == len) { 52 return str.toUpperCase(); 53 } 54 return new String (chars, 0, count).toUpperCase(); 55 } 56 57 83 static int difference(StringEncoder encoder, String s1, String s2) throws EncoderException { 84 return differenceEncoded(encoder.encode(s1), encoder.encode(s2)); 85 } 86 87 107 static int differenceEncoded(String es1, String es2) { 108 109 if (es1 == null || es2 == null) { 110 return 0; 111 } 112 int lengthToMatch = Math.min(es1.length(), es2.length()); 113 int diff = 0; 114 for (int i = 0; i < lengthToMatch; i++) { 115 if (es1.charAt(i) == es2.charAt(i)) { 116 diff++; 117 } 118 } 119 return diff; 120 } 121 122 } 123 | Popular Tags |