Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@
<contributor>
<name>Arturo Bernal</name>
</contributor>
<contributor>
<name>Edilson Alexandre Cuamba</name>
</contributor>
</contributors>

<dependencyManagement>
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,59 @@ public static String defaultString(final String str, final String nullDefault) {
return Objects.toString(str, nullDefault);
}

/**
* <p>Returns either the passed in String,
* or if the String is {@code null}, an empty String ("").</p>
*
* <pre>
* StringUtils.defaultString(null) = ""
* StringUtils.defaultString("") = ""
* StringUtils.defaultString("bat") = "bat"
* StringUtils.defaultString(1) = "1"
* StringUtils.defaultString(Integer.MIN_VALUE) = "-2147483648"
* </pre>
*
* @see Objects#toString(Object)
* @see String#valueOf(Object)
* @param str the Object to check, may be null
* @return the passed in String, or the empty String if it
* was {@code null}
*/
public static String defaultString(final Object object) {
if(object == null)
return "";

return Objects.toString(object);
}

/**
* Returns either the given String, or if the String is
* {@code null}, {@code nullDefault}.
*
* <pre>
* StringUtils.defaultString(null, "NULL") = "NULL"
* StringUtils.defaultString("", "NULL") = ""
* StringUtils.defaultString("bat", "NULL") = "bat"
* StringUtils.defaultString(1, "20") = "1"
* StringUtils.defaultString(Integer.MIN_VALUE, "200") = "-2147483648"
* </pre>
*
* @see Objects#toString(Object, String)
* @see String#valueOf(Object)
* @param str the String to check, may be null
* @param nullDefault the default String to return
* if the input is {@code null}, may be null
* @return the passed in String, or the default if it was {@code null}
* @deprecated Use {@link Objects#toString(Object, String)}
*/
@Deprecated
public static String defaultString(final Object object, final String nullDefault) {
if(object == null)
return defaultString(nullDefault);

return Objects.toString(object);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @EACUAMBA ,

I think instead of this new method, users could just use the Java 8+ Objects.toString(Object o, String nullDefault) - https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#toString-java.lang.Object-java.lang.String-

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kinow,

You are right, the toString method in Objects can work too. Thanks.

}

/**
* <p>Deletes all whitespaces from a String as defined by
* {@link Character#isWhitespace(char)}.</p>
Expand Down