6767 *
6868 * @author bayard@generationjava.com
6969 * @author Tim O'Brien
70- * @author ggregory@seagullsw.com
71- * @version $Id: Soundex.java,v 1.9 2003/10/12 19:48:15 tobrien Exp $
70+ * @author Gary Gregory
71+ * @version $Id: Soundex.java,v 1.10 2003/11/04 02:43:09 ggregory Exp $
7272 */
7373public class Soundex implements StringEncoder {
7474
75+ /**
76+ * This static variable contains an instance of the
77+ * Soundex using the US_ENGLISH mapping.
78+ */
79+ public static final Soundex US_ENGLISH = new Soundex ();
80+
7581 /**
7682 * This is a default mapping of the 26 letters used
7783 * in US english.
@@ -80,10 +86,10 @@ public class Soundex implements StringEncoder {
8086 "01230120022455012623010202" .toCharArray ();
8187
8288 /**
83- * This static variable contains an instance of the
84- * Soundex using the US_ENGLISH mapping .
89+ * The maximum length of a Soundex code - Soundex codes are
90+ * only four characters by definition .
8591 */
86- public static final Soundex US_ENGLISH = new Soundex () ;
92+ private int maxLength = 4 ;
8793
8894 /**
8995 * Every letter of the alphabet is "mapped" to a numerical
@@ -93,12 +99,6 @@ public class Soundex implements StringEncoder {
9399 */
94100 private char [] soundexMapping ;
95101
96- /**
97- * The maximum length of a Soundex code - Soundex codes are
98- * only four characters by definition.
99- */
100- private int maxLength = 4 ;
101-
102102 /**
103103 * Creates an instance of the Soundex object using the default
104104 * US_ENGLISH mapping.
@@ -117,30 +117,7 @@ public Soundex() {
117117 * code for a given character
118118 */
119119 public Soundex (char [] mapping ) {
120- this .soundexMapping = mapping ;
121- }
122-
123- /**
124- * Retreives the Soundex code for a given String object.
125- *
126- * @param str String to encode using the Soundex algorithm
127- * @return A soundex code for the String supplied
128- */
129- public String soundex (String str ) {
130- if (null == str || str .length () == 0 ) { return str ; }
131-
132- char out [] = { '0' , '0' , '0' , '0' };
133- char last , mapped ;
134- int incount = 1 , count = 1 ;
135- out [0 ] = Character .toUpperCase (str .charAt (0 ));
136- last = getMappingCode (str .charAt (0 ));
137- while ((incount < str .length ()) && (mapped = getMappingCode (str .charAt (incount ++))) != 0 && (count < maxLength )) {
138- if ((mapped != '0' ) && (mapped != last )) {
139- out [count ++] = mapped ;
140- }
141- last = mapped ;
142- }
143- return new String (out );
120+ this .setSoundexMapping (mapping );
144121 }
145122
146123 /**
@@ -174,10 +151,8 @@ public Object encode(Object pObject) throws EncoderException {
174151 *
175152 * @param pString A String object to encode
176153 * @return A Soundex code corresponding to the String supplied
177- * @throws EncoderException throws exception if there is an
178- * encoding-specific problem
179154 */
180- public String encode (String pString ) throws EncoderException {
155+ public String encode (String pString ) {
181156 return (soundex (pString ));
182157 }
183158
@@ -191,7 +166,7 @@ private char getMappingCode(char c) {
191166 if (!Character .isLetter (c )) {
192167 return 0 ;
193168 } else {
194- return soundexMapping [Character .toUpperCase (c ) - 'A' ];
169+ return this . getSoundexMapping () [Character .toUpperCase (c ) - 'A' ];
195170 }
196171 }
197172
@@ -200,7 +175,14 @@ private char getMappingCode(char c) {
200175 * @return int
201176 */
202177 public int getMaxLength () {
203- return maxLength ;
178+ return this .maxLength ;
179+ }
180+
181+ /**
182+ * @return Returns the soundexMapping.
183+ */
184+ private char [] getSoundexMapping () {
185+ return this .soundexMapping ;
204186 }
205187
206188 /**
@@ -211,4 +193,34 @@ public void setMaxLength(int maxLength) {
211193 this .maxLength = maxLength ;
212194 }
213195
196+ /**
197+ * @param soundexMapping The soundexMapping to set.
198+ */
199+ private void setSoundexMapping (char [] soundexMapping ) {
200+ this .soundexMapping = soundexMapping ;
201+ }
202+
203+ /**
204+ * Retreives the Soundex code for a given String object.
205+ *
206+ * @param str String to encode using the Soundex algorithm
207+ * @return A soundex code for the String supplied
208+ */
209+ public String soundex (String str ) {
210+ if (null == str || str .length () == 0 ) { return str ; }
211+
212+ char out [] = { '0' , '0' , '0' , '0' };
213+ char last , mapped ;
214+ int incount = 1 , count = 1 ;
215+ out [0 ] = Character .toUpperCase (str .charAt (0 ));
216+ last = getMappingCode (str .charAt (0 ));
217+ while ((incount < str .length ()) && (mapped = getMappingCode (str .charAt (incount ++))) != 0 && (count < this .getMaxLength ())) {
218+ if ((mapped != '0' ) && (mapped != last )) {
219+ out [count ++] = mapped ;
220+ }
221+ last = mapped ;
222+ }
223+ return new String (out );
224+ }
225+
214226}
0 commit comments