From e4c03c5e38b3e2550da7a1f9084d93dad32386ef Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 26 Sep 2024 10:33:26 -0400 Subject: [PATCH] Refactor StringUtils#endsWithAny() - Refactor to use Strings - Gains a case-insensitive implementation --- .../org/apache/commons/lang3/StringUtils.java | 12 ++----- .../org/apache/commons/lang3/Strings.java | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 6b359bcf2..6539fb605 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -1711,17 +1711,11 @@ public class StringUtils { * @return {@code true} if the input {@code sequence} is {@code null} AND no {@code searchStrings} are provided, or * the input {@code sequence} ends in any of the provided case-sensitive {@code searchStrings}. * @since 3.0 + * @deprecated Use {@link Strings#endsWithAny(CharSequence, CharSequence...) Strings.CS.endsWithAny(CharSequence, CharSequence...)} */ + @Deprecated public static boolean endsWithAny(final CharSequence sequence, final CharSequence... searchStrings) { - if (isEmpty(sequence) || ArrayUtils.isEmpty(searchStrings)) { - return false; - } - for (final CharSequence searchString : searchStrings) { - if (Strings.CS.endsWith(sequence, searchString)) { - return true; - } - } - return false; + return Strings.CS.endsWithAny(sequence, searchStrings); } /** diff --git a/src/main/java/org/apache/commons/lang3/Strings.java b/src/main/java/org/apache/commons/lang3/Strings.java index f775b3ae7..cf6c29369 100644 --- a/src/main/java/org/apache/commons/lang3/Strings.java +++ b/src/main/java/org/apache/commons/lang3/Strings.java @@ -597,6 +597,42 @@ public abstract class Strings { return CharSequenceUtils.regionMatches(str, ignoreCase, str.length() - sufLen, suffix, 0, sufLen); } + /** + * Tests if a CharSequence ends with any of the provided case-sensitive suffixes. + * + *

+ * Case-sensitive examples + *

+ * + *
+     * StringUtils.endsWithAny(null, null)                  = false
+     * StringUtils.endsWithAny(null, new String[] {"abc"})  = false
+     * StringUtils.endsWithAny("abcxyz", null)              = false
+     * StringUtils.endsWithAny("abcxyz", new String[] {""}) = true
+     * StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}) = true
+     * StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
+     * StringUtils.endsWithAny("abcXYZ", "def", "XYZ")      = true
+     * StringUtils.endsWithAny("abcXYZ", "def", "xyz")      = false
+     * 
+ * + * @param sequence the CharSequence to check, may be null + * @param searchStrings the case-sensitive CharSequences to find, may be empty or contain {@code null} + * @see StringUtils#endsWith(CharSequence, CharSequence) + * @return {@code true} if the input {@code sequence} is {@code null} AND no {@code searchStrings} are provided, or the input {@code sequence} ends in any + * of the provided case-sensitive {@code searchStrings}. + */ + public boolean endsWithAny(final CharSequence sequence, final CharSequence... searchStrings) { + if (StringUtils.isEmpty(sequence) || ArrayUtils.isEmpty(searchStrings)) { + return false; + } + for (final CharSequence searchString : searchStrings) { + if (endsWith(sequence, searchString)) { + return true; + } + } + return false; + } + /** * Compares two CharSequences, returning {@code true} if they represent equal sequences of characters. *