You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/10/16 18:13:15 UTC

[commons-lang] 02/04: Add Suppliers.nul()

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

commit 6c61b0916ef77600d09c3e299498ce60058035a0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Oct 16 09:16:22 2023 -0400

    Add Suppliers.nul()
---
 src/changes/changes.xml                            |  1 +
 .../org/apache/commons/lang3/SystemProperties.java |  6 +++---
 .../apache/commons/lang3/function/Suppliers.java   | 24 +++++++++++++++++++++
 .../org/apache/commons/lang3/ObjectUtilsTest.java  | 25 +++++++++++-----------
 .../org/apache/commons/lang3/StringUtilsTest.java  |  5 +++--
 .../commons/lang3/function/SuppliersTest.java      |  1 +
 6 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1e48ea300..9ef8a801b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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>
diff --git a/src/main/java/org/apache/commons/lang3/SystemProperties.java b/src/main/java/org/apache/commons/lang3/SystemProperties.java
index 1e54f0fda..eaff144cb 100644
--- a/src/main/java/org/apache/commons/lang3/SystemProperties.java
+++ b/src/main/java/org/apache/commons/lang3/SystemProperties.java
@@ -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());
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/lang3/function/Suppliers.java b/src/main/java/org/apache/commons/lang3/function/Suppliers.java
index 6467a4780..a19a1598d 100644
--- a/src/main/java/org/apache/commons/lang3/function/Suppliers.java
+++ b/src/main/java/org/apache/commons/lang3/function/Suppliers.java
@@ -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()}.
      *
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index 15376767d..8f297918c 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -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));
     }
 
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index bc5aeef80..438dfe5ce 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -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");
diff --git a/src/test/java/org/apache/commons/lang3/function/SuppliersTest.java b/src/test/java/org/apache/commons/lang3/function/SuppliersTest.java
index 1c912757c..ec5a1faec 100644
--- a/src/test/java/org/apache/commons/lang3/function/SuppliersTest.java
+++ b/src/test/java/org/apache/commons/lang3/function/SuppliersTest.java
@@ -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"));
     }
 }