This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-text.git
commit aac7e010f9dc31101018414d716ddfb5b439f06d Author: Gary D. Gregory <[email protected]> AuthorDate: Sun Jul 20 09:08:21 2025 -0400 org.apache.commons.text.translate.LookupTranslator.LookupTranslator(Map CharSequence>) now throws NullPointerException instead of java.security.InvalidParameterException - Javadoc - Reduce vertical whitespace --- src/changes/changes.xml | 2 ++ .../commons/text/translate/LookupTranslator.java | 23 +++++++--------------- .../text/translate/LookupTranslatorTest.java | 3 +-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 302829a0..bfd08972 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -56,6 +56,8 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD UnnecessaryFullyQualifiedName in AlphabetConverter.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD AvoidBranchingStatementAsLastInLoop in TextStringBuilder.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix PMD AvoidBranchingStatementAsLastInLoop in StrBuilder.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.text.translate.LookupTranslator.LookupTranslator(Map CharSequence>) now throws NullPointerException instead of java.security.InvalidParameterException.</action> + <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Interface StringLookup now extends UnaryOperator<String>.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Interface TextRandomProvider extends IntUnaryOperator.</action> diff --git a/src/main/java/org/apache/commons/text/translate/LookupTranslator.java b/src/main/java/org/apache/commons/text/translate/LookupTranslator.java index 400c8d38..005e36e4 100644 --- a/src/main/java/org/apache/commons/text/translate/LookupTranslator.java +++ b/src/main/java/org/apache/commons/text/translate/LookupTranslator.java @@ -14,14 +14,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.text.translate; import java.io.IOException; import java.io.Writer; -import java.security.InvalidParameterException; import java.util.BitSet; import java.util.HashMap; import java.util.Map; +import java.util.Objects; /** * Translates a value using a lookup table. @@ -32,36 +33,27 @@ public class LookupTranslator extends CharSequenceTranslator { /** The mapping to be used in translation. */ private final Map<String, String> lookupMap; - /** The first character of each key in the lookupMap. */ private final BitSet prefixSet; - /** The length of the shortest key in the lookupMap. */ private final int shortest; - /** The length of the longest key in the lookupMap. */ private final int longest; /** - * Constructs the lookup table to be used in translation + * Constructs the lookup table to be used in translation. * - * Note that, as of Lang 3.1 (the origin of this code), the key to the lookup - * table is converted to a java.lang.String. This is because we need the key - * to support hashCode and equals(Object), allowing it to be the key for a - * HashMap. See LANG-882. + * Note that, as of Lang 3.1 (the origin of this code), the key to the lookup table is converted to a java.lang.String. This is because we need the key to + * support hashCode and equals(Object), allowing it to be the key for a HashMap. See LANG-882. * - * @param lookupMap Map<CharSequence, CharSequence> table of translator - * mappings + * @param lookupMap Map<CharSequence, CharSequence> table of translator mappings, may not be null. */ public LookupTranslator(final Map<CharSequence, CharSequence> lookupMap) { - if (lookupMap == null) { - throw new InvalidParameterException("lookupMap cannot be null"); - } + Objects.requireNonNull(lookupMap, "lookupMap"); this.lookupMap = new HashMap<>(); this.prefixSet = new BitSet(); int currentShortest = Integer.MAX_VALUE; int currentLongest = 0; - for (final Map.Entry<CharSequence, CharSequence> pair : lookupMap.entrySet()) { this.lookupMap.put(pair.getKey().toString(), pair.getValue().toString()); this.prefixSet.set(pair.getKey().charAt(0)); @@ -92,7 +84,6 @@ public class LookupTranslator extends CharSequenceTranslator { for (int i = max; i >= shortest; i--) { final CharSequence subSeq = input.subSequence(index, index + i); final String result = lookupMap.get(subSeq.toString()); - if (result != null) { writer.write(result); return Character.codePointCount(subSeq, 0, subSeq.length()); diff --git a/src/test/java/org/apache/commons/text/translate/LookupTranslatorTest.java b/src/test/java/org/apache/commons/text/translate/LookupTranslatorTest.java index 49281e0c..11cc0bc2 100644 --- a/src/test/java/org/apache/commons/text/translate/LookupTranslatorTest.java +++ b/src/test/java/org/apache/commons/text/translate/LookupTranslatorTest.java @@ -22,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertThrowsExactly; import java.io.IOException; import java.io.StringWriter; -import java.security.InvalidParameterException; import java.util.HashMap; import java.util.Map; @@ -46,7 +45,7 @@ class LookupTranslatorTest { @Test void testFailsToCreateLookupTranslatorThrowsInvalidParameterException() { - assertThrowsExactly(InvalidParameterException.class, () -> new LookupTranslator(null)); + assertThrowsExactly(NullPointerException.class, () -> new LookupTranslator(null)); } // Tests: https://issues.apache.org/jira/browse/LANG-882
