Skip to content

Commit cb63f4a

Browse files
committed
Applying sebb's patch from CODEC-72 - fixing the char[] API of Soundex/RefinedSoundex, which shouldn't be used externally as they are the defaults. He's replaced them with Strings for the external use and copying of inputted char[]s
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@757676 13f79535-47bb-0310-9956-ffa450edef68
1 parent 981c000 commit cb63f4a

4 files changed

Lines changed: 58 additions & 8 deletions

File tree

src/java/org/apache/commons/codec/language/RefinedSoundex.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
*/
3131
public class RefinedSoundex implements StringEncoder {
3232

33+
public static final String US_ENGLISH_MAPPING_STRING = "01360240043788015936020505";
34+
3335
/**
3436
* RefinedSoundex is *refined* for a number of reasons one being that the
3537
* mappings have been altered. This implementation contains default
3638
* mappings for US English.
3739
*/
38-
public static final char[] US_ENGLISH_MAPPING = "01360240043788015936020505".toCharArray();
40+
private static final char[] US_ENGLISH_MAPPING = US_ENGLISH_MAPPING_STRING.toCharArray();
3941

4042
/**
4143
* Every letter of the alphabet is "mapped" to a numerical value. This char
@@ -55,7 +57,7 @@ public class RefinedSoundex implements StringEncoder {
5557
* English mapping.
5658
*/
5759
public RefinedSoundex() {
58-
this(US_ENGLISH_MAPPING);
60+
this.soundexMapping = US_ENGLISH_MAPPING;
5961
}
6062

6163
/**
@@ -68,7 +70,21 @@ public RefinedSoundex() {
6870
* a given character
6971
*/
7072
public RefinedSoundex(char[] mapping) {
71-
this.soundexMapping = mapping;
73+
this.soundexMapping = new char[mapping.length];
74+
System.arraycopy(mapping, 0, this.soundexMapping, 0, mapping.length);
75+
}
76+
77+
/**
78+
* Creates a refined soundex instance using a custom mapping. This
79+
* constructor can be used to customize the mapping, and/or possibly
80+
* provide an internationalized mapping for a non-Western character set.
81+
*
82+
* @param mapping
83+
* Mapping string to use when finding the corresponding code for
84+
* a given character
85+
*/
86+
public RefinedSoundex(String mapping) {
87+
this.soundexMapping = mapping.toCharArray();
7288
}
7389

7490
/**

src/java/org/apache/commons/codec/language/Soundex.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class Soundex implements StringEncoder {
4747
*
4848
* @see Soundex#Soundex(char[])
4949
*/
50-
public static final char[] US_ENGLISH_MAPPING = US_ENGLISH_MAPPING_STRING.toCharArray();
50+
private static final char[] US_ENGLISH_MAPPING = US_ENGLISH_MAPPING_STRING.toCharArray();
5151

5252
/**
5353
* An instance of Soundex using the US_ENGLISH_MAPPING mapping.
@@ -100,7 +100,7 @@ public int difference(String s1, String s2) throws EncoderException {
100100
* @see Soundex#US_ENGLISH_MAPPING
101101
*/
102102
public Soundex() {
103-
this(US_ENGLISH_MAPPING);
103+
this.soundexMapping = US_ENGLISH_MAPPING;
104104
}
105105

106106
/**
@@ -114,7 +114,21 @@ public Soundex() {
114114
* Mapping array to use when finding the corresponding code for a given character
115115
*/
116116
public Soundex(char[] mapping) {
117-
this.soundexMapping= mapping;
117+
this.soundexMapping = new char[mapping.length];
118+
System.arraycopy(mapping, 0, this.soundexMapping, 0, mapping.length);
119+
}
120+
121+
/**
122+
* Creates a refined soundex instance using a custom mapping. This
123+
* constructor can be used to customize the mapping, and/or possibly
124+
* provide an internationalized mapping for a non-Western character set.
125+
*
126+
* @param mapping
127+
* Mapping string to use when finding the corresponding code for
128+
* a given character
129+
*/
130+
public Soundex(String mapping) {
131+
this.soundexMapping = mapping.toCharArray();
118132
}
119133

120134
/**

src/test/org/apache/commons/codec/language/RefinedSoundexTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,16 @@ public void testGetMappingCodeNonLetter() {
113113
char code = this.getEncoder().getMappingCode('#');
114114
assertEquals("Code does not equals zero", 0, code);
115115
}
116+
117+
public void testNewInstance() {
118+
assertEquals("D6043", new RefinedSoundex().soundex("dogs"));
119+
}
120+
121+
public void testNewInstance2() {
122+
assertEquals("D6043", new RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING.toCharArray()).soundex("dogs"));
123+
}
124+
125+
public void testNewInstance3() {
126+
assertEquals("D6043", new RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING).soundex("dogs"));
127+
}
116128
}

src/test/org/apache/commons/codec/language/SoundexTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,22 @@ public void testUsMappingEWithAcute() {
382382
* https://issues.apache.org/jira/browse/CODEC-56
383383
*/
384384
public void testUsEnglishStatic() {
385-
assertEquals(Soundex.US_ENGLISH.soundex("Williams"), "W452");
385+
assertEquals("W452", Soundex.US_ENGLISH.soundex("Williams"));
386386
}
387387

388388
/**
389389
* https://issues.apache.org/jira/browse/CODEC-54
390390
* https://issues.apache.org/jira/browse/CODEC-56
391391
*/
392392
public void testNewInstance() {
393-
assertEquals(new Soundex().soundex("Williams"), "W452");
393+
assertEquals("W452", new Soundex().soundex("Williams"));
394+
}
395+
396+
public void testNewInstance2() {
397+
assertEquals("W452", new Soundex(Soundex.US_ENGLISH_MAPPING_STRING.toCharArray()).soundex("Williams"));
398+
}
399+
400+
public void testNewInstance3() {
401+
assertEquals("W452", new Soundex(Soundex.US_ENGLISH_MAPPING_STRING).soundex("Williams"));
394402
}
395403
}

0 commit comments

Comments
 (0)