This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push: new c47e5f9 [LANG-1498] Add support of lambda value evaluation for defaulting methods #416. c47e5f9 is described below commit c47e5f95f6853f127c291ef2293edd9a28aceec6 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Thu Oct 31 21:16:17 2019 -0400 [LANG-1498] Add support of lambda value evaluation for defaulting methods #416. Add org.apache.commons.lang3.ObjectUtils.defaultIfNull(T, Supplier<T>). --- src/changes/changes.xml | 1 + .../java/org/apache/commons/lang3/ObjectUtils.java | 27 ++++++++++++++++++++-- .../org/apache/commons/lang3/ObjectUtilsTest.java | 15 +++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 89bd6e6..c24b139 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -79,6 +79,7 @@ The <action> type attribute can be add,update,fix,remove. <action issue="LANG-1494" type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.time.Calendars.</action> <action issue="LANG-1495" type="add" dev="ggregory" due-to="Cheong Voon Leong">Add EnumUtils getEnum() methods with default values #475.</action> <action issue="LANG-1177" type="add" dev="ggregory" due-to="Liel Fridman">Added indexesOf methods and simplified removeAllOccurences #471.</action> + <action issue="LANG-1498" type="add" dev="ggregory" due-to="Lysergid, Gary Gregory">Add support of lambda value evaluation for defaulting methods #416.</action> </release> <release version="3.9" date="2019-04-09" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11."> diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index 4a84c4f..58f0b19 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -27,6 +27,7 @@ import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeSet; +import java.util.function.Supplier; import org.apache.commons.lang3.exception.CloneFailedException; import org.apache.commons.lang3.mutable.MutableInt; @@ -153,8 +154,30 @@ public class ObjectUtils { return !isEmpty(object); } - // Defaulting - //----------------------------------------------------------------------- + /** + * <p>Returns the given {@code object} is it is non-null, otherwise returns the Supplier's get value.</p> + * + * <p>The caller responsible for thread-safety and exception handling of default value supplier.</p> + * + * <pre> + * ObjectUtils.getIfNull(null, () -> null) = null + * ObjectUtils.getIfNull(null, null) = null + * ObjectUtils.getIfNull(null, () -> "") = "" + * ObjectUtils.getIfNull(null, () -> "zz") = "zz" + * ObjectUtils.getIfNull("abc", *) = "abc" + * ObjectUtils.getIfNull(Boolean.TRUE, *) = Boolean.TRUE + * </pre> + * + * @param <T> the type of the object + * @param object the {@code Object} to test, may be {@code null} + * @param defaultValueSupplier the default value to return, may be {@code null} + * @return {@code object} if it is not {@code null}, {@code defaultValueSupplier.get()} otherwise + * @since 3.10 + */ + public static <T> T defaultIfNull(final T object, final Supplier<T> defaultValueSupplier) { + return object != null ? object : defaultValueSupplier == null ? null : defaultValueSupplier.get(); + } + /** * <p>Returns a default value if the object passed is {@code null}.</p> * diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index 45ad9b9..22f3e8e 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -40,8 +40,10 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Supplier; import org.apache.commons.lang3.exception.CloneFailedException; +import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.mutable.MutableObject; import org.apache.commons.lang3.text.StrBuilder; import org.junit.jupiter.api.Test; @@ -109,11 +111,22 @@ public class ObjectUtilsTest { //----------------------------------------------------------------------- @Test - public void testIsNull() { + public void testDefaultIfNull() { final Object o = FOO; final Object dflt = BAR; assertSame(dflt, ObjectUtils.defaultIfNull(null, dflt), "dflt was not returned when o was null"); assertSame(o, ObjectUtils.defaultIfNull(o, dflt), "dflt was returned when o was not null"); + assertSame(dflt, ObjectUtils.defaultIfNull(null, () -> dflt), "dflt was not returned when o was null"); + assertSame(o, ObjectUtils.defaultIfNull(o, () -> dflt), "dflt was returned when o was not null"); + MutableInt callsCounter = new MutableInt(0); + Supplier<Object> countingDefaultSupplier = () -> { + callsCounter.increment(); + return dflt; + }; + ObjectUtils.defaultIfNull(o, countingDefaultSupplier); + assertEquals(0, callsCounter.getValue()); + ObjectUtils.defaultIfNull(null, countingDefaultSupplier); + assertEquals(1, callsCounter.getValue()); } @Test