From c2960c483f00424352e9336fca4a04736e1e5777 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 21 Aug 2022 11:05:38 -0400 Subject: [PATCH] Refactor internals and use Stream --- .../org/apache/commons/lang3/ObjectUtils.java | 35 +++---------------- 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index 920855ca3..0155da451 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -33,12 +33,14 @@ import java.util.Objects; import java.util.Optional; import java.util.TreeSet; import java.util.function.Supplier; +import java.util.stream.Stream; import org.apache.commons.lang3.exception.CloneFailedException; import org.apache.commons.lang3.function.Suppliers; import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.text.StrBuilder; import org.apache.commons.lang3.time.DurationUtils; +import org.apache.commons.lang3.stream.Streams; /** *

Operations on {@link Object}.

@@ -137,17 +139,7 @@ public class ObjectUtils { * @since 3.5 */ public static boolean allNotNull(final Object... values) { - if (values == null) { - return false; - } - - for (final Object val : values) { - if (val == null) { - return false; - } - } - - return true; + return values != null ? Stream.of(values).noneMatch(Objects::isNull) : false; } /** @@ -657,14 +649,7 @@ public class ObjectUtils { */ @SafeVarargs public static T firstNonNull(final T... values) { - if (values != null) { - for (final T val : values) { - if (val != null) { - return val; - } - } - } - return null; + return Streams.of(values).filter(Objects::nonNull).findFirst().orElse(null); } /** @@ -706,17 +691,7 @@ public class ObjectUtils { */ @SafeVarargs public static T getFirstNonNull(final Supplier... suppliers) { - if (suppliers != null) { - for (final Supplier supplier : suppliers) { - if (supplier != null) { - final T value = supplier.get(); - if (value != null) { - return value; - } - } - } - } - return null; + return Streams.of(suppliers).map(s -> s != null ? s.get() : null).filter(Objects::nonNull).findFirst().orElse(null); } /**