From 359700e7641fdfbe737beaca36658c9395facd06 Mon Sep 17 00:00:00 2001 From: Javid Khan Date: Tue, 29 Sep 2026 16:43:39 +0530 Subject: [PATCH 1/2] reject null input in longestCommonSubstringLengthArray --- .../similarity/LongestCommonSubsequence.java | 4 ++++ .../LongestCommonSubsequenceTest.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequence.java b/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequence.java index 86bc097ea5..a200bf6cd1 100644 --- a/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequence.java +++ b/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequence.java @@ -284,12 +284,16 @@ public CharSequence longestCommonSubsequence(final CharSequence left, final Char * @param left first character sequence. * @param right second character sequence. * @return longest common substring length array. + * @throws IllegalArgumentException if either String input {@code null}. * @deprecated Deprecated as of 1.10. A more efficient implementation for calculating LCS is now available. * Use {@link #longestCommonSubsequence(CharSequence, CharSequence)} instead to directly calculate the LCS. * This method will be removed in 2.0. */ @Deprecated public int[][] longestCommonSubstringLengthArray(final CharSequence left, final CharSequence right) { + if (left == null || right == null) { + throw new IllegalArgumentException("Inputs must not be null"); + } final int[][] lcsLengthArray = new int[left.length() + 1][right.length() + 1]; for (int i = 0; i < left.length(); i++) { for (int j = 0; j < right.length(); j++) { diff --git a/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java index f7d64874de..839488caea 100644 --- a/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java +++ b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java @@ -68,6 +68,24 @@ void testGettingLongestCommonSubsequenceApplyStringNull() { assertThrows(IllegalArgumentException.class, () -> subject.apply(" ", null)); } + @Test + @SuppressWarnings("deprecation") + void testGettingLongestCommonSubstringLengthArrayNullNull() { + assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubstringLengthArray(null, null)); + } + + @Test + @SuppressWarnings("deprecation") + void testGettingLongestCommonSubstringLengthArrayNullString() { + assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubstringLengthArray(null, "right")); + } + + @Test + @SuppressWarnings("deprecation") + void testGettingLongestCommonSubstringLengthArrayStringNull() { + assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubstringLengthArray(" ", null)); + } + @Test void testGettingLongestCommonSubsequenceNullNull() { assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubsequence(null, null)); From be8da00bc8bc0dc4e67ae07c919d9355833347b5 Mon Sep 17 00:00:00 2001 From: Javid Khan Date: Sat, 24 Oct 2026 15:25:01 +0530 Subject: [PATCH 2/2] use Objects.requireNonNull in longestCommonSubstringLengthArray --- .../text/similarity/LongestCommonSubsequence.java | 9 +++++---- .../text/similarity/LongestCommonSubsequenceTest.java | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequence.java b/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequence.java index a200bf6cd1..d2b9b0c6fc 100644 --- a/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequence.java +++ b/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequence.java @@ -16,6 +16,8 @@ */ package org.apache.commons.text.similarity; +import java.util.Objects; + /** * A similarity algorithm indicating the length of the longest common subsequence between two strings. * @@ -284,16 +286,15 @@ public CharSequence longestCommonSubsequence(final CharSequence left, final Char * @param left first character sequence. * @param right second character sequence. * @return longest common substring length array. - * @throws IllegalArgumentException if either String input {@code null}. + * @throws NullPointerException if either input is {@code null}. * @deprecated Deprecated as of 1.10. A more efficient implementation for calculating LCS is now available. * Use {@link #longestCommonSubsequence(CharSequence, CharSequence)} instead to directly calculate the LCS. * This method will be removed in 2.0. */ @Deprecated public int[][] longestCommonSubstringLengthArray(final CharSequence left, final CharSequence right) { - if (left == null || right == null) { - throw new IllegalArgumentException("Inputs must not be null"); - } + Objects.requireNonNull(left, "left"); + Objects.requireNonNull(right, "right"); final int[][] lcsLengthArray = new int[left.length() + 1][right.length() + 1]; for (int i = 0; i < left.length(); i++) { for (int j = 0; j < right.length(); j++) { diff --git a/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java index 839488caea..655097ed6f 100644 --- a/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java +++ b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java @@ -71,19 +71,19 @@ void testGettingLongestCommonSubsequenceApplyStringNull() { @Test @SuppressWarnings("deprecation") void testGettingLongestCommonSubstringLengthArrayNullNull() { - assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubstringLengthArray(null, null)); + assertThrows(NullPointerException.class, () -> subject.longestCommonSubstringLengthArray(null, null)); } @Test @SuppressWarnings("deprecation") void testGettingLongestCommonSubstringLengthArrayNullString() { - assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubstringLengthArray(null, "right")); + assertThrows(NullPointerException.class, () -> subject.longestCommonSubstringLengthArray(null, "right")); } @Test @SuppressWarnings("deprecation") void testGettingLongestCommonSubstringLengthArrayStringNull() { - assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubstringLengthArray(" ", null)); + assertThrows(NullPointerException.class, () -> subject.longestCommonSubstringLengthArray(" ", null)); } @Test