Skip to content

Commit

Permalink
Merge 3ec23f1 into 5ffe008
Browse files Browse the repository at this point in the history
  • Loading branch information
ThrawnCA committed Jun 22, 2020
2 parents 5ffe008 + 3ec23f1 commit 0903fb2
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,60 @@ public static String appendIfMissingIgnoreCase(final String str, final CharSeque
return appendIfMissing(str, suffix, true, suffixes);
}

/**
* Appends the suffix to the end of the string if the string is not
* empty ("") or {@code null}.
*
* <pre>
* StringUtils.appendIfNotEmpty(null, " ") = null
* StringUtils.appendIfNotEmpty(null, "-post") = null
* StringUtils.appendIfNotEmpty("", "-post") = ""
* StringUtils.appendIfNotEmpty(" ", " ") = " "
* StringUtils.appendIfNotEmpty(" ", "-post") = " -post"
* StringUtils.appendIfNotEmpty("abc", null) = "abc"
* StringUtils.appendIfNotEmpty("abc", "") = "abc"
* StringUtils.appendIfNotEmpty("abc", " ") = "abc "
* StringUtils.appendIfNotEmpty("abc", "-post") = "abc-post"
* </pre>
* @param str The string.
* @param suffix The suffix to append if 'str' is not empty. May be null.
* @return If 'str' is empty, then 'str', otherwise 'str' with suffix added.
*/
public static String appendIfNotEmpty(final String str, final CharSequence suffix) {
if (isEmpty(suffix) || isEmpty(str)) {
return str;
}
return str + suffix.toString();
}

/**
* Appends the suffix to the end of the string if the string is not
* empty (""), {@code null}, or whitespace only.
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
*
* <pre>
* StringUtils.appendIfNotBlank(null, " ") = null
* StringUtils.appendIfNotBlank(null, "-post") = null
* StringUtils.appendIfNotBlank("", "-post") = ""
* StringUtils.appendIfNotBlank(" ", " ") = " "
* StringUtils.appendIfNotBlank(" ", "-post") = " "
* StringUtils.appendIfNotBlank("abc", null) = "abc"
* StringUtils.appendIfNotBlank("abc", "") = "abc"
* StringUtils.appendIfNotBlank("abc", " ") = "abc "
* StringUtils.appendIfNotBlank("abc", "-post") = "abc-post"
* </pre>
* @param str The string.
* @param suffix The suffix to append if 'str' is not blank. May be null.
* @return If 'str' is blank, then 'str', otherwise 'str' with suffix added.
*/
public static String appendIfNotBlank(final String str, final CharSequence suffix) {
if (isEmpty(suffix) || isBlank(str)) {
return str;
}
return str + suffix;
}

/**
* <p>Capitalizes a String changing the first character to title case as
* per {@link Character#toTitleCase(int)}. No other characters are changed.</p>
Expand Down Expand Up @@ -5821,6 +5875,60 @@ public static String prependIfMissingIgnoreCase(final String str, final CharSequ
return prependIfMissing(str, prefix, true, prefixes);
}

/**
* Prepends the prefix to the start of the string if the string is not
* empty ("") or {@code null}.
*
* <pre>
* StringUtils.prependIfNotEmpty(null, " ") = null
* StringUtils.prependIfNotEmpty(null, "pre-") = null
* StringUtils.prependIfNotEmpty("", "pre-") = ""
* StringUtils.prependIfNotEmpty(" ", " ") = " "
* StringUtils.prependIfNotEmpty(" ", "pre-") = "pre- "
* StringUtils.prependIfNotEmpty("abc", null) = "abc"
* StringUtils.prependIfNotEmpty("abc", "") = "abc"
* StringUtils.prependIfNotEmpty("abc", " ") = " abc"
* StringUtils.prependIfNotEmpty("abc", "pre-") = "pre-abc"
* </pre>
* @param str The string.
* @param prefix The prefix to prepend if 'str' is not empty. May be null.
* @return If 'str' is empty, then 'str', otherwise 'str' with prefix added.
*/
public static String prependIfNotEmpty(final String str, final CharSequence prefix) {
if (isEmpty(prefix) || isEmpty(str)) {
return str;
}
return prefix.toString() + str;
}

/**
* Prepends the prefix to the start of the string if the string is not
* empty (""), {@code null}, or whitespace only.
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
*
* <pre>
* StringUtils.prependIfNotBlank(null, " ") = null
* StringUtils.prependIfNotBlank(null, "pre-") = null
* StringUtils.prependIfNotBlank("", "pre-") = ""
* StringUtils.prependIfNotBlank(" ", " ") = " "
* StringUtils.prependIfNotBlank(" ", "pre-") = " "
* StringUtils.prependIfNotBlank("abc", null) = "abc"
* StringUtils.prependIfNotBlank("abc", "") = "abc"
* StringUtils.prependIfNotBlank("abc", " ") = "abc"
* StringUtils.prependIfNotBlank("abc", "pre-") = "pre-abc"
* </pre>
* @param str The string.
* @param prefix The prefix to prepend if 'str' is not blank. May be null.
* @return If 'str' is blank, then 'str', otherwise 'str' with prefix added.
*/
public static String prependIfNotBlank(final String str, final CharSequence prefix) {
if (isEmpty(prefix) || isBlank(str)) {
return str;
}
return prefix.toString() + str;
}

/**
* <p>Removes all occurrences of a character from within the source string.</p>
*
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,36 @@ public void testAppendIfMissingIgnoreCase() {
assertEquals("abcMNO", StringUtils.appendIfMissingIgnoreCase("abcMNO", "xyz", "mno"), "appendIfMissingIgnoreCase(abcMNO,xyz,mno)");
}

@Test
public void testAppendIfNotEmpty() {
assertNull(StringUtils.appendIfNotEmpty(null, " "));
assertNull(StringUtils.appendIfNotEmpty(null, "-suffix"));
assertEquals("", StringUtils.appendIfNotEmpty("", "-suffix"));
assertEquals(" ", StringUtils.appendIfNotEmpty(" ", " "));
assertEquals(" -suffix", StringUtils.appendIfNotEmpty(" ", "-suffix"));
assertEquals("string", StringUtils.appendIfNotEmpty("string", null));
assertEquals("string", StringUtils.appendIfNotEmpty("string", ""));
assertEquals("string", StringUtils.appendIfNotEmpty("string", new StringBuilder()));
assertEquals("string ", StringUtils.appendIfNotEmpty("string", " "));
assertEquals("string-suffix", StringUtils.appendIfNotEmpty("string", "-suffix"));
assertEquals("string-suffix", StringUtils.appendIfNotEmpty("string", new StringBuilder("-suffix")));
}

@Test
public void testAppendIfNotBlank() {
assertNull(StringUtils.appendIfNotBlank(null, "-suffix"));
assertNull(StringUtils.appendIfNotBlank(null, " "));
assertEquals("", StringUtils.appendIfNotBlank("", "-suffix"));
assertEquals(" ", StringUtils.appendIfNotBlank(" ", " "));
assertEquals(" ", StringUtils.appendIfNotBlank(" ", "-suffix"));
assertEquals("string", StringUtils.appendIfNotBlank("string", null));
assertEquals("string", StringUtils.appendIfNotBlank("string", ""));
assertEquals("string", StringUtils.appendIfNotBlank("string", new StringBuilder()));
assertEquals("string ", StringUtils.appendIfNotBlank("string", " "));
assertEquals("string-suffix", StringUtils.appendIfNotBlank("string", "-suffix"));
assertEquals("string-suffix", StringUtils.appendIfNotBlank("string", new StringBuilder("-suffix")));
}

@Test
public void testCapitalize() {
assertNull(StringUtils.capitalize(null));
Expand Down Expand Up @@ -1565,6 +1595,36 @@ public void testPrependIfMissingIgnoreCase() {
assertEquals("MNOabc", StringUtils.prependIfMissingIgnoreCase("MNOabc", "xyz", "mno"), "prependIfMissingIgnoreCase(MNOabc,xyz,mno)");
}

@Test
public void testPrependIfNotEmpty() {
assertNull(StringUtils.prependIfNotEmpty(null, " "));
assertNull(StringUtils.prependIfNotEmpty(null, "prefix-"));
assertEquals("", StringUtils.prependIfNotEmpty("", "prefix-"));
assertEquals(" ", StringUtils.prependIfNotEmpty(" ", " "));
assertEquals("prefix- ", StringUtils.prependIfNotEmpty(" ", "prefix-"));
assertEquals("string", StringUtils.prependIfNotEmpty("string", null));
assertEquals("string", StringUtils.prependIfNotEmpty("string", ""));
assertEquals("string", StringUtils.prependIfNotEmpty("string", new StringBuilder()));
assertEquals(" string", StringUtils.prependIfNotEmpty("string", " "));
assertEquals("prefix-string", StringUtils.prependIfNotEmpty("string", "prefix-"));
assertEquals("prefix-string", StringUtils.prependIfNotEmpty("string", new StringBuilder("prefix-")));
}

@Test
public void testPrependIfNotBlank() {
assertNull(StringUtils.prependIfNotBlank(null, "prefix-"));
assertNull(StringUtils.prependIfNotBlank(null, " "));
assertEquals("", StringUtils.prependIfNotBlank("", "prefix-"));
assertEquals(" ", StringUtils.prependIfNotBlank(" ", " "));
assertEquals(" ", StringUtils.prependIfNotBlank(" ", "prefix-"));
assertEquals("string", StringUtils.prependIfNotBlank("string", null));
assertEquals("string", StringUtils.prependIfNotBlank("string", ""));
assertEquals("string", StringUtils.prependIfNotBlank("string", new StringBuilder()));
assertEquals(" string", StringUtils.prependIfNotBlank("string", " "));
assertEquals("prefix-string", StringUtils.prependIfNotBlank("string", "prefix-"));
assertEquals("prefix-string", StringUtils.prependIfNotBlank("string", new StringBuilder("prefix-")));
}

@Test
public void testReCapitalize() {
// reflection type of tests: Sentences.
Expand Down

0 comments on commit 0903fb2

Please sign in to comment.