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 5912b221337f8df0dfa75d6de0ff8a3611033bda Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Feb 28 16:13:36 2025 -0500 Add FailableToBooleanFunction --- src/changes/changes.xml | 1 + .../lang3/function/FailableToBooleanFunction.java | 54 ++++++++++++++++++++++ .../lang3/function/FailableFunctionsTest.java | 35 ++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 53300a6f4..266dec8e3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -102,6 +102,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableIntToFloatFunction.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add Validate.isTrue(boolean, Supplier<String>).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add EnumUtils.getFirstEnum(Class<E>, int, ToIntFunction<E>, E).</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableToBooleanFunction.</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 81 #1267, #1277, #1283, #1288, #1302.</action> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300.</action> diff --git a/src/main/java/org/apache/commons/lang3/function/FailableToBooleanFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableToBooleanFunction.java new file mode 100644 index 000000000..d406258e6 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/function/FailableToBooleanFunction.java @@ -0,0 +1,54 @@ +/* + * 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; + +/** + * A functional interface like a {@code ToBooleanFunction} that declares a {@link Throwable}. + * + * @param <T> the type of the argument to the function + * @param <E> The kind of thrown exception or error. + * @since 3.11 + */ +@FunctionalInterface +public interface FailableToBooleanFunction<T, E extends Throwable> { + + /** NOP singleton */ + @SuppressWarnings("rawtypes") + FailableToBooleanFunction NOP = t -> false; + + /** + * Returns the NOP singleton. + * + * @param <T> the type of the argument to the function + * @param <E> The kind of thrown exception or error. + * @return The NOP singleton. + */ + @SuppressWarnings("unchecked") + static <T, E extends Throwable> FailableToBooleanFunction<T, E> nop() { + return NOP; + } + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @return the function result + * @throws E Thrown when the function fails. + */ + boolean applyAsBoolean(T t) throws E; +} diff --git a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java index 8b50080f0..e38178145 100644 --- a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java @@ -975,6 +975,11 @@ public void testFailableObjLongConsumerNop() throws Throwable { FailableObjLongConsumer.nop().accept("Foo", Long.MAX_VALUE); } + @Test + public void testFailableToBooleanFunctionNop() throws Throwable { + assertEquals(false, FailableToBooleanFunction.nop().applyAsBoolean("Foo"), "Expect NOP to return false"); + } + @Test public void testFailableToDoubleBiFunctionNop() throws Throwable { assertEquals(0, FailableToDoubleBiFunction.nop().applyAsDouble("Foo", "Bar"), "Expect NOP to return 0"); @@ -2507,6 +2512,36 @@ public String get() throws IOException { }.get()); } + /** + * Tests that our failable interface is properly defined to throw any exception using the top level generic types + * Object and Throwable. + */ + @Test + public void testThrows_FailableToBooleanFunction_Object_Throwable() { + assertThrows(IOException.class, () -> new FailableToBooleanFunction<Object, Throwable>() { + + @Override + public boolean applyAsBoolean(final Object t) throws Throwable { + throw new IOException("test"); + } + }.applyAsBoolean(new Object())); + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + public void testThrows_FailableToBooleanFunction_String_IOException() { + assertThrows(IOException.class, () -> new FailableToBooleanFunction<String, IOException>() { + + @Override + public boolean applyAsBoolean(final String t) throws IOException { + throw new IOException("test"); + } + }.applyAsBoolean(StringUtils.EMPTY)); + } + /** * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable.