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 0612dce660cafc4c56f42cea1cff780d36b06d46 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Jan 14 09:03:06 2023 -0500 Add and use a package-private singleton for LongestCommonSubsequence --- src/changes/changes.xml | 1 + .../apache/commons/text/similarity/LongestCommonSubsequence.java | 5 +++++ .../commons/text/similarity/LongestCommonSubsequenceDistance.java | 7 +------ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b3d02cd0..c3b33d32 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -52,6 +52,7 @@ The <action> type attribute can be add,update,fix,remove. <action issue="TEXT-221" type="fix" dev="aherbert" due-to="Remco Riswick">Fix Bundle-SymbolicName to use the package name org.apache.commons.text</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Add and use a package-private singleton for RegexTokenizer.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Add and use a package-private singleton for CosineSimilarity.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Add and use a package-private singleton for LongestCommonSubsequence.</action> <!-- ADD --> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Dependabot">Bump actions/cache from 3.0.8 to 3.0.10 #361, #365.</action> 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 aaf05900..b91f23c0 100644 --- a/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequence.java +++ b/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequence.java @@ -51,6 +51,11 @@ package org.apache.commons.text.similarity; */ public class LongestCommonSubsequence implements SimilarityScore<Integer> { + /** + * Singleton instance. + */ + static final LongestCommonSubsequence INSTANCE = new LongestCommonSubsequence(); + /** * An implementation of "ALG B" from Hirschberg's CACM '71 paper. * Assuming the first input sequence is of size <code>m</code> and the second input sequence is of size diff --git a/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequenceDistance.java b/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequenceDistance.java index 29745461..f5f5bcf9 100644 --- a/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequenceDistance.java +++ b/src/main/java/org/apache/commons/text/similarity/LongestCommonSubsequenceDistance.java @@ -36,11 +36,6 @@ package org.apache.commons.text.similarity; */ public class LongestCommonSubsequenceDistance implements EditDistance<Integer> { - /** - * Object for calculating the longest common subsequence that we can then normalize in apply. - */ - private final LongestCommonSubsequence longestCommonSubsequence = new LongestCommonSubsequence(); - /** * Calculates an edit distance between two {@code CharSequence}'s {@code left} and * {@code right} as: {@code left.length() + right.length() - 2 * LCS(left, right)}, where @@ -58,7 +53,7 @@ public class LongestCommonSubsequenceDistance implements EditDistance<Integer> { if (left == null || right == null) { throw new IllegalArgumentException("Inputs must not be null"); } - return left.length() + right.length() - 2 * longestCommonSubsequence.apply(left, right); + return left.length() + right.length() - 2 * LongestCommonSubsequence.INSTANCE.apply(left, right); } }