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));