bozzelliandrea commented on code in PR #1063:
URL: https://github.com/apache/commons-lang/pull/1063#discussion_r1224319063


##########
src/main/java/org/apache/commons/lang3/ObjectUtils.java:
##########
@@ -595,7 +595,11 @@ public static short CONST_SHORT(final int v) {
      * TODO Rename to getIfNull in 4.0
      */
     public static <T> T defaultIfNull(final T object, final T defaultValue) {
-        return object != null ? object : defaultValue;
+        try {

Review Comment:
   @aherbert thx for the review, yep you right, the supplier could be the only 
solution to check inside the defaultIfNull the exception.
   A possible solution could be overloaded the defaultIfNull method with a 
supplier as first parameter, like this:
   ```java
   public static <T> T defaultIfNull(final Supplier<T> supplier, final T 
defaultValue) {
           try {
               return defaultIfNull(supplier.get(), defaultValue);
           } catch (NullPointerException npe) {
               return defaultValue;
           }
       }
   
   ```
   and an usage like: `ObjectUtils.defaultIfNull(() -> list.get(0).getValue(), 
"helloDefault");`
   
   in this scenario, the exception will be checked when the get method of the 
provider is called.



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

Reply via email to