This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-codec.git
commit 5647fe2a66b3e74f044f414ad10d92407ede9396 Author: Gary Gregory <[email protected]> AuthorDate: Sun Jul 26 16:14:01 2026 -0400 StringEncoderComparator.StringEncoderComparator(StringEncoder) now fails fast on null input. --- src/changes/changes.xml | 2 ++ .../commons/codec/StringEncoderComparator.java | 37 +++++++++------------- .../commons/codec/StringEncoderComparatorTest.java | 11 +++++++ 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 06f16bc0..6cce9e35 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -55,6 +55,8 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" issue="CODEC-337" dev="pkarwasz" due-to="Ruiqi Dong, Gary Gregory">Digest ALL reuses System.in, so only the first algorithm sees the real input (#431).</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Base64.toIntegerBytes(BigInteger) for zero edge case (#441).</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Add messages when throwing NullPointerException.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Add messages when throwing NullPointerException.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">StringEncoderComparator.StringEncoderComparator(StringEncoder) now fails fast on null input.</action> <!-- ADD --> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 98 to 103.</action> diff --git a/src/main/java/org/apache/commons/codec/StringEncoderComparator.java b/src/main/java/org/apache/commons/codec/StringEncoderComparator.java index 012119aa..0bd7a5e3 100644 --- a/src/main/java/org/apache/commons/codec/StringEncoderComparator.java +++ b/src/main/java/org/apache/commons/codec/StringEncoderComparator.java @@ -18,13 +18,14 @@ package org.apache.commons.codec; import java.util.Comparator; +import java.util.Objects; /** - * Compares Strings using a {@link StringEncoder}. This comparator is used to sort Strings by an encoding scheme such as - * Soundex, Metaphone, etc. This class can come in handy if one need to sort Strings by an encoded form of a name such - * as Soundex. - * - * <p>This class is immutable and thread-safe.</p> + * Compares Strings using a {@link StringEncoder}. This comparator is used to sort Strings by an encoding scheme such as Soundex, Metaphone, etc. This class can + * come in handy if one need to sort Strings by an encoded form of a name such as Soundex. + * <p> + * This class is immutable and thread-safe. + * </p> */ @SuppressWarnings("rawtypes") // TODO ought to implement Comparator<String> but that's not possible whilst maintaining binary compatibility. @@ -38,8 +39,7 @@ public class StringEncoderComparator implements Comparator { /** * Constructs a new instance. * - * @deprecated Creating an instance without a {@link StringEncoder} leads to a {@link NullPointerException}. Will be - * removed in 2.0. + * @deprecated Creating an instance without a {@link StringEncoder} leads to a {@link NullPointerException}. Will be removed in 2.0. */ @Deprecated public StringEncoderComparator() { @@ -49,41 +49,34 @@ public class StringEncoderComparator implements Comparator { /** * Constructs a new instance with the given algorithm. * - * @param stringEncoder - * the StringEncoder used for comparisons. + * @param stringEncoder the StringEncoder used for comparisons. + * @throws NullPointerException if the StringEncoder is null. */ public StringEncoderComparator(final StringEncoder stringEncoder) { - this.stringEncoder = stringEncoder; + this.stringEncoder = Objects.requireNonNull(stringEncoder, "stringEncoder"); } /** - * Compares two strings based not on the strings themselves, but on an encoding of the two strings using the - * StringEncoder this Comparator was created with. - * + * Compares two strings based not on the strings themselves, but on an encoding of the two strings using the StringEncoder this Comparator was created with. * If an {@link EncoderException} is encountered, return {@code 0}. * - * @param o1 - * the object to compare. - * @param o2 - * the object to compare to. + * @param o1 the object to compare. + * @param o2 the object to compare to. * @return The Comparable.compareTo() return code or 0 if an encoding error was caught. * @see Comparable */ @Override public int compare(final Object o1, final Object o2) { - int compareCode = 0; - try { @SuppressWarnings("unchecked") // May fail with CCE if encode returns something that is not Comparable // However this was always the case. - final Comparable<Comparable<?>> s1 = (Comparable<Comparable<?>>) this.stringEncoder.encode(o1); - final Comparable<?> s2 = (Comparable<?>) this.stringEncoder.encode(o2); + final Comparable<Comparable<?>> s1 = (Comparable<Comparable<?>>) stringEncoder.encode(o1); + final Comparable<?> s2 = (Comparable<?>) stringEncoder.encode(o2); compareCode = s1.compareTo(s2); } catch (final EncoderException ee) { compareCode = 0; } return compareCode; } - } diff --git a/src/test/java/org/apache/commons/codec/StringEncoderComparatorTest.java b/src/test/java/org/apache/commons/codec/StringEncoderComparatorTest.java index 9436b397..cc22818e 100644 --- a/src/test/java/org/apache/commons/codec/StringEncoderComparatorTest.java +++ b/src/test/java/org/apache/commons/codec/StringEncoderComparatorTest.java @@ -18,6 +18,7 @@ package org.apache.commons.codec; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Arrays; import java.util.List; @@ -58,4 +59,14 @@ class StringEncoderComparatorTest { final StringEncoderComparator sCompare = new StringEncoderComparator(new Soundex()); assertEquals(0, sCompare.compare("O'Brien", "O'Brian"), "O'Brien and O'Brian didn't come out with the same Soundex, something must be wrong here"); } + + @Test + void testConstructor() throws Exception { + assertThrows(NullPointerException.class, () -> new StringEncoderComparator(null)); + } + + @Test + void testDeprecatedConstructor() throws Exception { + assertThrows(NullPointerException.class, () -> new StringEncoderComparator().compare("test", "test")); + } }
