[LANG-1498] Add support of lambda value evaluation for defaulting

methods #416.

Add org.apache.commons.lang3.ObjectUtils.defaultIfNull(T, Supplier<T>).
This commit is contained in:
Gary Gregory 2019-10-31 21:16:17 -04:00
parent 89d2edbf17
commit c47e5f95f6
3 changed files with 40 additions and 3 deletions

View File

@ -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.">

View File

@ -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>
*

View File

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