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 20:00:49 UTC

[GitHub] [commons-lang] EACUAMBA opened a new pull request, #889: Added method to convert Object to String that support null values.

EACUAMBA opened a new pull request, #889:
URL: https://github.com/apache/commons-lang/pull/889

   I added a method to help people convert any object to String or return an empty String.
   
   I added this methods because I was facing a problem with convert Wrap classes like Integer, Double to String.
   I needed something to return empty string or a default value.
   
   This is my contribute.


-- 
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


[GitHub] [commons-lang] EACUAMBA closed pull request #889: Added method to convert Object to String that support null values.

Posted by GitBox <gi...@apache.org>.
EACUAMBA closed pull request #889: Added method to convert Object to String that support null values.
URL: https://github.com/apache/commons-lang/pull/889


-- 
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


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

Posted by GitBox <gi...@apache.org>.
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


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

Posted by GitBox <gi...@apache.org>.
kinow commented on code in PR #889:
URL: https://github.com/apache/commons-lang/pull/889#discussion_r862522289


##########
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 @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-



-- 
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