You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "class101 (via GitHub)" <gi...@apache.org> on 2023/06/29 10:24:40 UTC

[GitHub] [commons-lang] class101 commented on a diff in pull request #903: Add toCharacterObject(String,String) with default value.

class101 commented on code in PR #903:
URL: https://github.com/apache/commons-lang/pull/903#discussion_r1246437527


##########
src/main/java/org/apache/commons/lang3/CharUtils.java:
##########
@@ -114,6 +114,33 @@ public static Character toCharacterObject(final String str) {
         return StringUtils.isEmpty(str) ? null : Character.valueOf(str.charAt(0));
     }
 
+    /**
+     * <p>Converts the String to a Character using the first character, returning a
+     *  default value if {@code null} or empty is passed in.</p>
+     *
+     * <p>For ASCII 7 bit characters, this uses a cache that will return the
+     * same Character object each time.</p>
+     *
+     * <pre>
+     *   CharUtils.toCharacterObject(null, 'A') = 'A'
+     *   CharUtils.toCharacterObject("", 'A')   = 'A'
+     *   CharUtils.toCharacterObject("A", 'D')  = 'A'
+     *   CharUtils.toCharacterObject("BA", 'D') = 'B'
+     * </pre>
+     *
+     * @param str  the character to convert
+     * @param defaultValue the value to use if the str is null or empty.
+     * @return the Character value of the first letter of the String or the default if null.
+     * @since 3.13.0
+     */
+    public static Character toCharacterObject(final String str, final Character defaultValue) {
+      final Character character = toCharacterObject(str);
+      if (character == null){
+          return defaultValue;
+      }
+      return character;
+    }

Review Comment:
   I think of you still use ternary in 2023, you are a really bad developer



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