Add FailableSupplieSuppliersr.nul()

This commit is contained in:
Gary Gregory 2023-10-16 09:14:44 -04:00
parent 9aad0f421f
commit 13839dae6e
4 changed files with 78 additions and 2 deletions

View File

@ -72,6 +72,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add LazyInitializer.isInitialized().</action>
<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>
<!-- 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>

View File

@ -29,6 +29,33 @@ import java.util.function.Supplier;
@FunctionalInterface
public interface FailableSupplier<R, E extends Throwable> {
/**
* Returns the singleton supplier that always returns null.
* <p>
* This supplier never throws an exception.
* </p>
*
* @since 3.14.0
*/
@SuppressWarnings("rawtypes")
FailableSupplier NUL = () -> null;
/**
* Returns the singleton supplier that always returns null.
* <p>
* This supplier never throws an exception.
* </p>
*
* @param <T> Supplied type.
* @param <E> The kind of thrown exception or error.
* @return The NUL singleton.
* @since 3.14.0
*/
@SuppressWarnings("unchecked")
static <T, E extends Exception> FailableSupplier<T, E> nul() {
return NUL;
}
/**
* Supplies an object
*

View File

@ -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.<Object, Exception>nul().get());
}
@Test
public void testNullSupplierRuntimeException() {
assertNull(FailableSupplier.<Object, RuntimeException>nul().get());
}
}

View File

@ -81,7 +81,7 @@ public class ObjectsTest extends AbstractLangTest {
@Test
void testRequireNonNullObjectFailableSupplierString() {
final TestableFailableSupplier<String, ?> supplier = new TestableFailableSupplier<>(() -> null);
final TestableFailableSupplier<String, ?> 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<String, ?> supplier2 = new TestableFailableSupplier<>(() -> null);
final TestableFailableSupplier<String, ?> supplier2 = new TestableFailableSupplier<>(FailableSupplier.nul());
try {
Objects.requireNonNull(null, supplier2);
fail("Expected Exception");