Add Suppliers.nul()
This commit is contained in:
parent
13839dae6e
commit
6c61b0916e
|
@ -73,6 +73,7 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
<action type="add" dev="ggregory" due-to="Benjamin Confino, Gary Gregory">Add ConcurrentInitializer#isInitialized() #1120.</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.failableStream(T...).</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableSupplier.nul().</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Suppliers.nul().</action>
|
||||
<!-- UPDATE -->
|
||||
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 58 to 64.</action>
|
||||
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.easymock:easymock from 5.1.0 to 5.2.0 #1104.</action>
|
||||
|
|
|
@ -22,6 +22,8 @@ import java.util.function.IntSupplier;
|
|||
import java.util.function.LongSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.apache.commons.lang3.function.Suppliers;
|
||||
|
||||
/**
|
||||
* Accesses current system property names and values.
|
||||
*
|
||||
|
@ -29,8 +31,6 @@ import java.util.function.Supplier;
|
|||
*/
|
||||
public final class SystemProperties {
|
||||
|
||||
private static final Supplier<String> NULL_SUPPLIER = () -> null;
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
|
@ -752,7 +752,7 @@ public final class SystemProperties {
|
|||
* @return the system property value or {@code null} if a security problem occurs
|
||||
*/
|
||||
public static String getProperty(final String property) {
|
||||
return getProperty(property, NULL_SUPPLIER);
|
||||
return getProperty(property, Suppliers.nul());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -26,6 +26,30 @@ import java.util.function.Supplier;
|
|||
*/
|
||||
public class Suppliers {
|
||||
|
||||
/**
|
||||
* Returns the singleton supplier that always returns null.
|
||||
* <p>
|
||||
* This supplier never throws an exception.
|
||||
* </p>
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static Supplier NUL = () -> null;
|
||||
|
||||
/**
|
||||
* Returns the singleton supplier that always returns null.
|
||||
* <p>
|
||||
* This supplier never throws an exception.
|
||||
* </p>
|
||||
*
|
||||
* @param <T> Supplied type.
|
||||
* @return The NUL singleton.
|
||||
* @since 3.14.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Supplier<T> nul() {
|
||||
return NUL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Null-safe call to {@link Supplier#get()}.
|
||||
*
|
||||
|
|
|
@ -47,6 +47,7 @@ import java.util.Set;
|
|||
import java.util.function.Supplier;
|
||||
|
||||
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.mutable.MutableObject;
|
||||
import org.apache.commons.lang3.text.StrBuilder;
|
||||
|
@ -479,14 +480,14 @@ public class ObjectUtilsTest extends AbstractLangTest {
|
|||
@Test
|
||||
public void testGetFirstNonNull() {
|
||||
// first non-null
|
||||
assertEquals("", ObjectUtils.getFirstNonNull(() -> null, () -> ""));
|
||||
assertEquals("", ObjectUtils.getFirstNonNull(Suppliers.nul(), () -> ""));
|
||||
// first encountered value is used
|
||||
assertEquals("1", ObjectUtils.getFirstNonNull(() -> null, () -> "1", () -> "2", () -> null));
|
||||
assertEquals("123", ObjectUtils.getFirstNonNull(() -> "123", () -> null, () -> "456"));
|
||||
assertEquals("1", ObjectUtils.getFirstNonNull(Suppliers.nul(), () -> "1", () -> "2", Suppliers.nul()));
|
||||
assertEquals("123", ObjectUtils.getFirstNonNull(() -> "123", Suppliers.nul(), () -> "456"));
|
||||
// don't evaluate suppliers after first value is found
|
||||
assertEquals("123", ObjectUtils.getFirstNonNull(() -> null, () -> "123", () -> fail("Supplier after first non-null value should not be evaluated")));
|
||||
assertEquals("123", ObjectUtils.getFirstNonNull(Suppliers.nul(), () -> "123", () -> fail("Supplier after first non-null value should not be evaluated")));
|
||||
// supplier returning null and null supplier both result in null
|
||||
assertNull(ObjectUtils.getFirstNonNull(null, () -> null));
|
||||
assertNull(ObjectUtils.getFirstNonNull(null, Suppliers.nul()));
|
||||
// Explicitly pass in an empty array of Object type to ensure compiler doesn't complain of unchecked generic array creation
|
||||
assertNull(ObjectUtils.getFirstNonNull());
|
||||
// supplier is null
|
||||
|
@ -494,8 +495,8 @@ public class ObjectUtilsTest extends AbstractLangTest {
|
|||
// varargs array itself is null
|
||||
assertNull(ObjectUtils.getFirstNonNull((Supplier<Object>[]) null));
|
||||
// test different types
|
||||
assertEquals(1, ObjectUtils.getFirstNonNull(() -> null, () -> 1));
|
||||
assertEquals(Boolean.TRUE, ObjectUtils.getFirstNonNull(() -> null, () -> Boolean.TRUE));
|
||||
assertEquals(1, ObjectUtils.getFirstNonNull(Suppliers.nul(), () -> 1));
|
||||
assertEquals(Boolean.TRUE, ObjectUtils.getFirstNonNull(Suppliers.nul(), () -> Boolean.TRUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -849,7 +850,7 @@ public class ObjectUtilsTest extends AbstractLangTest {
|
|||
@Test
|
||||
public void testToString_String_Supplier() {
|
||||
assertNull(ObjectUtils.toString(null, (Supplier<String>) null));
|
||||
assertNull(ObjectUtils.toString(null, () -> null));
|
||||
assertNull(ObjectUtils.toString(null, Suppliers.nul()));
|
||||
// Pretend computing BAR is expensive.
|
||||
assertEquals(BAR, ObjectUtils.toString(null, () -> BAR));
|
||||
assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, () -> BAR));
|
||||
|
@ -858,12 +859,12 @@ public class ObjectUtilsTest extends AbstractLangTest {
|
|||
@Test
|
||||
public void testToString_Supplier_Supplier() {
|
||||
assertNull(ObjectUtils.toString(NULL_SUPPLIER, (Supplier<String>) null));
|
||||
assertNull(ObjectUtils.toString(() -> null, (Supplier<String>) null));
|
||||
assertNull(ObjectUtils.toString(NULL_SUPPLIER, () -> null));
|
||||
assertNull(ObjectUtils.toString(() -> null, () -> null));
|
||||
assertNull(ObjectUtils.toString(Suppliers.nul(), (Supplier<String>) null));
|
||||
assertNull(ObjectUtils.toString(NULL_SUPPLIER, Suppliers.nul()));
|
||||
assertNull(ObjectUtils.toString(Suppliers.nul(), Suppliers.nul()));
|
||||
// Pretend computing BAR is expensive.
|
||||
assertEquals(BAR, ObjectUtils.toString(NULL_SUPPLIER, () -> BAR));
|
||||
assertEquals(BAR, ObjectUtils.toString(() -> null, () -> BAR));
|
||||
assertEquals(BAR, ObjectUtils.toString(Suppliers.nul(), () -> BAR));
|
||||
assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(() -> Boolean.TRUE, () -> BAR));
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,7 @@ import java.util.Objects;
|
|||
import java.util.function.Supplier;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import org.apache.commons.lang3.function.Suppliers;
|
||||
import org.apache.commons.lang3.mutable.MutableInt;
|
||||
import org.apache.commons.lang3.text.WordUtils;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
|
@ -682,7 +683,7 @@ public class StringUtilsTest extends AbstractLangTest {
|
|||
assertEquals("NULL", StringUtils.getIfBlank("", () -> "NULL"));
|
||||
assertEquals("NULL", StringUtils.getIfBlank(" ", () -> "NULL"));
|
||||
assertEquals("abc", StringUtils.getIfBlank("abc", () -> "NULL"));
|
||||
assertNull(StringUtils.getIfBlank("", () -> null));
|
||||
assertNull(StringUtils.getIfBlank("", Suppliers.nul()));
|
||||
assertNull(StringUtils.defaultIfBlank("", (String) null));
|
||||
// Tests compatibility for the API return type
|
||||
final String s = StringUtils.getIfBlank("abc", () -> "NULL");
|
||||
|
@ -750,7 +751,7 @@ public class StringUtilsTest extends AbstractLangTest {
|
|||
assertEquals("NULL", StringUtils.getIfEmpty((String) null, () -> "NULL"));
|
||||
assertEquals("NULL", StringUtils.getIfEmpty("", () -> "NULL"));
|
||||
assertEquals("abc", StringUtils.getIfEmpty("abc", () -> "NULL"));
|
||||
assertNull(StringUtils.getIfEmpty("", () -> null));
|
||||
assertNull(StringUtils.getIfEmpty("", Suppliers.nul()));
|
||||
assertNull(StringUtils.defaultIfEmpty("", (String) null));
|
||||
// Tests compatibility for the API return type
|
||||
final String s = StringUtils.getIfEmpty("abc", () -> "NULL");
|
||||
|
|
|
@ -37,6 +37,7 @@ public class SuppliersTest extends AbstractLangTest {
|
|||
public void testGet() {
|
||||
assertNull(Suppliers.get(null));
|
||||
assertNull(Suppliers.get(() -> null));
|
||||
assertNull(Suppliers.get(Suppliers.nul()));
|
||||
assertEquals("foo", Suppliers.get(() -> "foo"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue