@@ -580,7 +580,7 @@ public static short CONST_SHORT(final int v) {
580
580
* @param object the {@link Object} to test, may be {@code null}
581
581
* @param defaultValue the default value to return, may be {@code null}
582
582
* @return {@code object} if it is not {@code null}, defaultValue otherwise
583
- * TODO Rename to getIfNull in 4.0
583
+ * @deprecated use {@link ObjectUtils# getIfNull(Object, Object)}
584
584
*/
585
585
public static <T > T defaultIfNull (final T object , final T defaultValue ) {
586
586
return object != null ? object : defaultValue ;
@@ -640,6 +640,36 @@ public static <T> T firstNonNull(final T... values) {
640
640
return Streams .of (values ).filter (Objects ::nonNull ).findFirst ().orElse (null );
641
641
}
642
642
643
+ /**
644
+ * Returns the first non-null value in the collection. If all values are {@code null},
645
+ * or if the collection is {@code null} or empty, an empty {@link Optional} is returned.
646
+ *
647
+ * <p>Examples:
648
+ * <pre>
649
+ * ObjectUtils.firstNonNull(Arrays.asList(null, null)) = Optional.empty()
650
+ * ObjectUtils.firstNonNull(Arrays.asList(null, "")) = Optional.of("")
651
+ * ObjectUtils.firstNonNull(Arrays.asList(null, null, "")) = Optional.of("")
652
+ * ObjectUtils.firstNonNull(Arrays.asList(null, "zz")) = Optional.of("zz")
653
+ * ObjectUtils.firstNonNull(Arrays.asList("abc", *)) = Optional.of("abc")
654
+ * ObjectUtils.firstNonNull(Arrays.asList(null, "xyz", *)) = Optional.of("xyz")
655
+ * ObjectUtils.firstNonNull(Arrays.asList(Boolean.TRUE, *)) = Optional.of(Boolean.TRUE)
656
+ * ObjectUtils.firstNonNull(Collections.emptyList()) = Optional.empty()
657
+ * ObjectUtils.firstNonNull(null) = Optional.empty()
658
+ * </pre>
659
+ *
660
+ * @param <T> the type of elements in the collection
661
+ * @param values the collection to test, may be {@code null} or empty
662
+ * @return an {@link Optional} containing the first non-null value in the collection,
663
+ * or an empty {@link Optional} if no non-null value is found
664
+ * @since 3.18.0
665
+ */
666
+ public static <T > Optional <T > firstNonNull (final Collection <T > values ) {
667
+ if (Objects .isNull (values ) || values .isEmpty ()) {
668
+ return Optional .empty ();
669
+ }
670
+ return values .stream ().filter (Objects ::nonNull ).findFirst ();
671
+ }
672
+
643
673
/**
644
674
* Delegates to {@link Object#getClass()} using generics.
645
675
*
@@ -709,6 +739,26 @@ public static <T> T getIfNull(final T object, final Supplier<T> defaultSupplier)
709
739
return object != null ? object : Suppliers .get (defaultSupplier );
710
740
}
711
741
742
+ /**
743
+ * Returns a default value if the object passed is {@code null}.
744
+ *
745
+ * <pre>
746
+ * ObjectUtils.getIfNull(null, null) = null
747
+ * ObjectUtils.getIfNull(null, "") = ""
748
+ * ObjectUtils.getIfNull(null, "zz") = "zz"
749
+ * ObjectUtils.getIfNull("abc", *) = "abc"
750
+ * ObjectUtils.getIfNull(Boolean.TRUE, *) = Boolean.TRUE
751
+ * </pre>
752
+ *
753
+ * @param <T> the type of the object
754
+ * @param object the {@link Object} to test, may be {@code null}
755
+ * @param defaultValue the default value to return, may be {@code null}
756
+ * @return {@code object} if it is not {@code null}, defaultValue otherwise
757
+ */
758
+ public static <T > T getIfNull (final T object , final T defaultValue ) {
759
+ return object != null ? object : defaultValue ;
760
+ }
761
+
712
762
/**
713
763
* Gets the hash code of an object returning zero when the
714
764
* object is {@code null}.
@@ -1382,7 +1432,7 @@ public static void wait(final Object obj, final Duration duration) throws Interr
1382
1432
/**
1383
1433
* {@link ObjectUtils} instances should NOT be constructed in
1384
1434
* standard programming. Instead, the static methods on the class should
1385
- * be used, such as {@code ObjectUtils.defaultIfNull ("a","b");}.
1435
+ * be used, such as {@code ObjectUtils.getIfNull ("a","b");}.
1386
1436
*
1387
1437
* <p>This constructor is public to permit tools that require a JavaBean
1388
1438
* instance to operate.</p>
0 commit comments