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..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,12 +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 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) { + 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 f7d64874de..655097ed6f 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(NullPointerException.class, () -> subject.longestCommonSubstringLengthArray(null, null)); + } + + @Test + @SuppressWarnings("deprecation") + void testGettingLongestCommonSubstringLengthArrayNullString() { + assertThrows(NullPointerException.class, () -> subject.longestCommonSubstringLengthArray(null, "right")); + } + + @Test + @SuppressWarnings("deprecation") + void testGettingLongestCommonSubstringLengthArrayStringNull() { + assertThrows(NullPointerException.class, () -> subject.longestCommonSubstringLengthArray(" ", null)); + } + @Test void testGettingLongestCommonSubsequenceNullNull() { assertThrows(IllegalArgumentException.class, () -> subject.longestCommonSubsequence(null, null));