garydgregory commented on code in PR #903:
URL: https://github.com/apache/commons-lang/pull/903#discussion_r1067475328
##########
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:
Better like this IMO: `return StringUtils.isEmpty(str) ? defaultValue :
Character.valueOf(str.charAt(0));`
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]