LANG-901: StringUtils#startsWithAny/endsWithAny is case sensitive - documented as case insensitive (closes #149)

Improve the documentation and introduce more test cases that verify the documented behaviors.

Patch by Matthew Bartenschlag
This commit is contained in:
pascalschumacher 2016-05-21 17:41:15 +02:00
parent 2244ed9d63
commit 9bc992b133
3 changed files with 34 additions and 17 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
<action issue="LANG-901" type="fix" dev="pschumacher" due-to="Matthew Bartenschlag">StringUtils#startsWithAny/endsWithAny is case sensitive - documented as case insensitive</action>
<action issue="LANG-1223" type="add" dev="pschumacher" due-to="Nick Manley">Add StopWatch#getTime(TimeUnit)</action>
<action issue="LANG-781" type="add" dev="pschumacher" due-to="Krzysztof Wolny">Add methods to ObjectUtils class to check for null elements in the array</action>
<action issue="LANG-1228" type="add" dev="pschumacher" due-to="Brad Hess">Prefer Throwable.getCause() in ExceptionUtils.getCause()</action>

View File

@ -7971,30 +7971,33 @@ public class StringUtils {
}
/**
* <p>Check if a CharSequence starts with any of an array of specified strings.</p>
* <p>Check if a CharSequence starts with any of the provided case-sensitive prefixes.</p>
*
* <pre>
* StringUtils.startsWithAny(null, null) = false
* StringUtils.startsWithAny(null, new String[] {"abc"}) = false
* StringUtils.startsWithAny("abcxyz", null) = false
* StringUtils.startsWithAny("abcxyz", new String[] {""}) = false
* StringUtils.startsWithAny("abcxyz", new String[] {""}) = true
* StringUtils.startsWithAny("abcxyz", new String[] {"abc"}) = true
* StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
* StringUtils.startsWithAny("abcxyz", null, "xyz", "ABCX") = false
* StringUtils.startsWithAny("ABCXYZ", null, "xyz", "abc") = false
* </pre>
*
* @param string the CharSequence to check, may be null
* @param searchStrings the CharSequences to find, may be null or empty
* @return {@code true} if the CharSequence starts with any of the the prefixes, case sensitive, or
* both {@code null}
* @param sequence the CharSequence to check, may be null
* @param searchStrings the case-sensitive CharSequence prefixes, may be empty or contain {@code null}
* @see StringUtils#startsWith(CharSequence, CharSequence)
* @return {@code true} if the input {@code sequence} is {@code null} AND no {@code searchStrings} are provided, or
* the input {@code sequence} begins with any of the provided case-sensitive {@code searchStrings}.
* @since 2.5
* @since 3.0 Changed signature from startsWithAny(String, String[]) to startsWithAny(CharSequence, CharSequence...)
*/
public static boolean startsWithAny(final CharSequence string, final CharSequence... searchStrings) {
if (isEmpty(string) || ArrayUtils.isEmpty(searchStrings)) {
public static boolean startsWithAny(final CharSequence sequence, final CharSequence... searchStrings) {
if (isEmpty(sequence) || ArrayUtils.isEmpty(searchStrings)) {
return false;
}
for (final CharSequence searchString : searchStrings) {
if (startsWith(string, searchString)) {
if (startsWith(sequence, searchString)) {
return true;
}
}
@ -8017,6 +8020,7 @@ public class StringUtils {
* StringUtils.endsWith("abcdef", "def") = true
* StringUtils.endsWith("ABCDEF", "def") = false
* StringUtils.endsWith("ABCDEF", "cde") = false
* StringUtils.endsWith("ABCDEF", "") = true
* </pre>
*
* @see java.lang.String#endsWith(String)
@ -8153,7 +8157,7 @@ public class StringUtils {
}
/**
* <p>Check if a CharSequence ends with any of an array of specified strings.</p>
* <p>Check if a CharSequence ends with any of the provided case-sensitive suffixes.</p>
*
* <pre>
* StringUtils.endsWithAny(null, null) = false
@ -8162,20 +8166,23 @@ public class StringUtils {
* 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 string the CharSequence to check, may be null
* @param searchStrings the CharSequences to find, may be null or empty
* @return {@code true} if the CharSequence ends with any of the the prefixes, case insensitive, or
* both {@code null}
* @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}.
* @since 3.0
*/
public static boolean endsWithAny(final CharSequence string, final CharSequence... searchStrings) {
if (isEmpty(string) || ArrayUtils.isEmpty(searchStrings)) {
public static boolean endsWithAny(final CharSequence sequence, final CharSequence... searchStrings) {
if (isEmpty(sequence) || ArrayUtils.isEmpty(searchStrings)) {
return false;
}
for (final CharSequence searchString : searchStrings) {
if (endsWith(string, searchString)) {
if (endsWith(sequence, searchString)) {
return true;
}
}

View File

@ -90,6 +90,9 @@ public class StringUtilsStartsEndsWithTest {
assertTrue(StringUtils.startsWithAny("abcxyz", "abc"));
assertTrue(StringUtils.startsWithAny("abcxyz", null, "xyz", "abc"));
assertFalse(StringUtils.startsWithAny("abcxyz", null, "xyz", "abcd"));
assertTrue(StringUtils.startsWithAny("abcxyz", new String[]{""}));
assertFalse(StringUtils.startsWithAny("abcxyz", null, "xyz", "ABCX"));
assertFalse(StringUtils.startsWithAny("ABCXYZ", null, "xyz", "abc"));
assertTrue("StringUtils.startsWithAny(abcxyz, StringBuilder(xyz), StringBuffer(abc))", StringUtils.startsWithAny("abcxyz", new StringBuilder("xyz"), new StringBuffer("abc")));
assertTrue("StringUtils.startsWithAny( StrBuilder(abcxyz), StringBuilder(xyz), StringBuffer(abc))", StringUtils.startsWithAny( new StrBuilder("abcxyz"), new StringBuilder("xyz"), new StringBuffer("abc")));
@ -172,6 +175,12 @@ public class StringUtilsStartsEndsWithTest {
assertTrue("StringUtils.endsWithAny(abcxyz, new String[] {xyz})", StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}));
assertTrue("StringUtils.endsWithAny(abcxyz, new String[] {null, xyz, abc})", StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}));
assertFalse("StringUtils.endsWithAny(defg, new String[] {null, xyz, abc})", StringUtils.endsWithAny("defg", new String[] {null, "xyz", "abc"}));
assertTrue(StringUtils.endsWithAny("abcXYZ", "def", "XYZ"));
assertFalse(StringUtils.endsWithAny("abcXYZ", "def", "xyz"));
assertTrue(StringUtils.endsWithAny("abcXYZ", "def", "YZ"));
assertFalse(StringUtils.endsWithAny("abcXYZ", null));
assertTrue(StringUtils.endsWithAny("abcXYZ", ""));
assertTrue("StringUtils.endsWithAny(abcxyz, StringBuilder(abc), StringBuffer(xyz))", StringUtils.endsWithAny("abcxyz", new StringBuilder("abc"), new StringBuffer("xyz")));
assertTrue("StringUtils.endsWithAny( StrBuilder(abcxyz), StringBuilder(abc), StringBuffer(xyz))", StringUtils.endsWithAny( new StrBuilder("abcxyz"), new StringBuilder("abc"), new StringBuffer("xyz")));