Skip to content

Commit

Permalink
Refactor StringUtils#endsWithAny()
Browse files Browse the repository at this point in the history
- Refactor to use Strings
- Gains a case-insensitive implementation
  • Loading branch information
garydgregory committed Sep 26, 2024
1 parent 317b364 commit e4c03c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1711,17 +1711,11 @@ public static boolean endsWith(final CharSequence str, final CharSequence suffix
* @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);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/apache/commons/lang3/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,42 @@ public boolean endsWith(final CharSequence str, final CharSequence suffix) {
return CharSequenceUtils.regionMatches(str, ignoreCase, str.length() - sufLen, suffix, 0, sufLen);
}

/**
* Tests if a CharSequence ends with any of the provided case-sensitive suffixes.
*
* <p>
* Case-sensitive examples
* </p>
*
* <pre>
* 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
* </pre>
*
* @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.
*
Expand Down

0 comments on commit e4c03c5

Please sign in to comment.