Author: bayard
Date: Tue Mar 24 07:18:36 2009
New Revision: 757676
URL: http://svn.apache.org/viewvc?rev=757676&view=rev
Log:
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
Modified:
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/RefinedSoundex.java
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Soundex.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/RefinedSoundexTest.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/SoundexTest.java
Modified:
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/RefinedSoundex.java
URL:
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/RefinedSoundex.java?rev=757676&r1=757675&r2=757676&view=diff
==============================================================================
---
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/RefinedSoundex.java
(original)
+++
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/RefinedSoundex.java
Tue Mar 24 07:18:36 2009
@@ -30,12 +30,14 @@
*/
public class RefinedSoundex implements StringEncoder {
+ public static final String US_ENGLISH_MAPPING_STRING =
"01360240043788015936020505";
+
/**
* RefinedSoundex is *refined* for a number of reasons one being that the
* mappings have been altered. This implementation contains default
* mappings for US English.
*/
- public static final char[] US_ENGLISH_MAPPING =
"01360240043788015936020505".toCharArray();
+ private static final char[] US_ENGLISH_MAPPING =
US_ENGLISH_MAPPING_STRING.toCharArray();
/**
* Every letter of the alphabet is "mapped" to a numerical value. This char
@@ -55,7 +57,7 @@
* English mapping.
*/
public RefinedSoundex() {
- this(US_ENGLISH_MAPPING);
+ this.soundexMapping = US_ENGLISH_MAPPING;
}
/**
@@ -68,7 +70,21 @@
* a given character
*/
public RefinedSoundex(char[] mapping) {
- this.soundexMapping = mapping;
+ this.soundexMapping = new char[mapping.length];
+ System.arraycopy(mapping, 0, this.soundexMapping, 0, mapping.length);
+ }
+
+ /**
+ * Creates a refined soundex instance using a custom mapping. This
+ * constructor can be used to customize the mapping, and/or possibly
+ * provide an internationalized mapping for a non-Western character set.
+ *
+ * @param mapping
+ * Mapping string to use when finding the corresponding
code for
+ * a given character
+ */
+ public RefinedSoundex(String mapping) {
+ this.soundexMapping = mapping.toCharArray();
}
/**
Modified:
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Soundex.java
URL:
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Soundex.java?rev=757676&r1=757675&r2=757676&view=diff
==============================================================================
---
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Soundex.java
(original)
+++
commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/Soundex.java
Tue Mar 24 07:18:36 2009
@@ -47,7 +47,7 @@
*
* @see Soundex#Soundex(char[])
*/
- public static final char[] US_ENGLISH_MAPPING =
US_ENGLISH_MAPPING_STRING.toCharArray();
+ private static final char[] US_ENGLISH_MAPPING =
US_ENGLISH_MAPPING_STRING.toCharArray();
/**
* An instance of Soundex using the US_ENGLISH_MAPPING mapping.
@@ -100,7 +100,7 @@
* @see Soundex#US_ENGLISH_MAPPING
*/
public Soundex() {
- this(US_ENGLISH_MAPPING);
+ this.soundexMapping = US_ENGLISH_MAPPING;
}
/**
@@ -114,7 +114,21 @@
* Mapping array to use when finding the corresponding
code for a given character
*/
public Soundex(char[] mapping) {
- this.soundexMapping= mapping;
+ this.soundexMapping = new char[mapping.length];
+ System.arraycopy(mapping, 0, this.soundexMapping, 0, mapping.length);
+ }
+
+ /**
+ * Creates a refined soundex instance using a custom mapping. This
+ * constructor can be used to customize the mapping, and/or possibly
+ * provide an internationalized mapping for a non-Western character set.
+ *
+ * @param mapping
+ * Mapping string to use when finding the corresponding
code for
+ * a given character
+ */
+ public Soundex(String mapping) {
+ this.soundexMapping = mapping.toCharArray();
}
/**
Modified:
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/RefinedSoundexTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/RefinedSoundexTest.java?rev=757676&r1=757675&r2=757676&view=diff
==============================================================================
---
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/RefinedSoundexTest.java
(original)
+++
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/RefinedSoundexTest.java
Tue Mar 24 07:18:36 2009
@@ -113,4 +113,16 @@
char code = this.getEncoder().getMappingCode('#');
assertEquals("Code does not equals zero", 0, code);
}
+
+ public void testNewInstance() {
+ assertEquals("D6043", new RefinedSoundex().soundex("dogs"));
+ }
+
+ public void testNewInstance2() {
+ assertEquals("D6043", new
RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING.toCharArray()).soundex("dogs"));
+ }
+
+ public void testNewInstance3() {
+ assertEquals("D6043", new
RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING).soundex("dogs"));
+ }
}
Modified:
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/SoundexTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/SoundexTest.java?rev=757676&r1=757675&r2=757676&view=diff
==============================================================================
---
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/SoundexTest.java
(original)
+++
commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/SoundexTest.java
Tue Mar 24 07:18:36 2009
@@ -382,7 +382,7 @@
* https://issues.apache.org/jira/browse/CODEC-56
*/
public void testUsEnglishStatic() {
- assertEquals(Soundex.US_ENGLISH.soundex("Williams"), "W452");
+ assertEquals("W452", Soundex.US_ENGLISH.soundex("Williams"));
}
/**
@@ -390,6 +390,14 @@
* https://issues.apache.org/jira/browse/CODEC-56
*/
public void testNewInstance() {
- assertEquals(new Soundex().soundex("Williams"), "W452");
+ assertEquals("W452", new Soundex().soundex("Williams"));
+ }
+
+ public void testNewInstance2() {
+ assertEquals("W452", new
Soundex(Soundex.US_ENGLISH_MAPPING_STRING.toCharArray()).soundex("Williams"));
+ }
+
+ public void testNewInstance3() {
+ assertEquals("W452", new
Soundex(Soundex.US_ENGLISH_MAPPING_STRING).soundex("Williams"));
}
}