You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2022/05/01 22:23:46 UTC

[GitHub] [commons-lang] EACUAMBA commented on a diff in pull request #889: Added method to convert Object to String that support null values.

EACUAMBA commented on code in PR #889:
URL: https://github.com/apache/commons-lang/pull/889#discussion_r862528440


##########
src/main/java/org/apache/commons/lang3/StringUtils.java:
##########
@@ -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);

Review Comment:
   Hi @kinow,
   
   You are right, the toString method  in Objects can work too.  Thanks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org