diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d7f1ef721..1e48ea300 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -72,6 +72,7 @@ The type attribute can be add,update,fix,remove. Add LazyInitializer.isInitialized(). Add ConcurrentInitializer#isInitialized() #1120. Add Streams.failableStream(T...). + Add FailableSupplier.nul(). Bump commons-parent from 58 to 64. Bump org.easymock:easymock from 5.1.0 to 5.2.0 #1104. diff --git a/src/main/java/org/apache/commons/lang3/function/FailableSupplier.java b/src/main/java/org/apache/commons/lang3/function/FailableSupplier.java index 6f80af546..c4ad522f4 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableSupplier.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableSupplier.java @@ -29,6 +29,33 @@ import java.util.function.Supplier; @FunctionalInterface public interface FailableSupplier { + /** + * Returns the singleton supplier that always returns null. + *

+ * This supplier never throws an exception. + *

+ * + * @since 3.14.0 + */ + @SuppressWarnings("rawtypes") + FailableSupplier NUL = () -> null; + + /** + * Returns the singleton supplier that always returns null. + *

+ * This supplier never throws an exception. + *

+ * + * @param Supplied type. + * @param The kind of thrown exception or error. + * @return The NUL singleton. + * @since 3.14.0 + */ + @SuppressWarnings("unchecked") + static FailableSupplier nul() { + return NUL; + } + /** * Supplies an object * diff --git a/src/test/java/org/apache/commons/lang3/function/FailableSupplierTest.java b/src/test/java/org/apache/commons/lang3/function/FailableSupplierTest.java new file mode 100644 index 000000000..646205252 --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/function/FailableSupplierTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang3.function; + +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link FailableSupplier}. + */ +public class FailableSupplierTest { + + @Test + public void testNULL() throws Throwable { + assertNull(FailableSupplier.NUL.get()); + } + + @Test + public void testNullSupplierDefaultException() throws Exception { + assertNull(FailableSupplier.nul().get()); + } + + @Test + public void testNullSupplierException() throws Exception { + assertNull(FailableSupplier.nul().get()); + } + + @Test + public void testNullSupplierRuntimeException() { + assertNull(FailableSupplier.nul().get()); + } +} diff --git a/src/test/java/org/apache/commons/lang3/function/ObjectsTest.java b/src/test/java/org/apache/commons/lang3/function/ObjectsTest.java index 36788830e..19e623431 100755 --- a/src/test/java/org/apache/commons/lang3/function/ObjectsTest.java +++ b/src/test/java/org/apache/commons/lang3/function/ObjectsTest.java @@ -81,7 +81,7 @@ public class ObjectsTest extends AbstractLangTest { @Test void testRequireNonNullObjectFailableSupplierString() { - final TestableFailableSupplier supplier = new TestableFailableSupplier<>(() -> null); + final TestableFailableSupplier supplier = new TestableFailableSupplier<>(FailableSupplier.nul()); assertSame("foo", Objects.requireNonNull("foo", supplier)); assertFalse(supplier.isInvoked()); try { @@ -91,7 +91,7 @@ public class ObjectsTest extends AbstractLangTest { assertEquals("The supplier must not return null.", e.getMessage()); assertTrue(supplier.isInvoked()); } - final TestableFailableSupplier supplier2 = new TestableFailableSupplier<>(() -> null); + final TestableFailableSupplier supplier2 = new TestableFailableSupplier<>(FailableSupplier.nul()); try { Objects.requireNonNull(null, supplier2); fail("Expected Exception");