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 c641a27c72c5abdc3f9754cd813a1af43dcbd8c3 Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Tue Jan 28 08:19:26 2025 -0500 Add FailableIntToFloatFunction --- src/changes/changes.xml | 1 + .../lang3/function/FailableIntToFloatFunction.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 33d931172..f9e0f4ea2 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -95,6 +95,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="add" dev="ggregory" due-to="Gary Gregory">Add Predicates.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add RegExUtils methods typed to CharSequence input and deprecate old versions typed to String.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add IterableStringTokenizer.</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableIntToFloatFunction.</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 79 #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/FailableIntToFloatFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableIntToFloatFunction.java new file mode 100644 index 000000000..3952b5b54 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/function/FailableIntToFloatFunction.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; + +import java.util.function.IntToDoubleFunction; + +/** + * A functional interface like {@link IntToDoubleFunction} but for {@code float} that declares a {@link Throwable}. + * + * @param <E> The kind of thrown exception or error. + * @since 3.18.0 + */ +@FunctionalInterface +public interface FailableIntToFloatFunction<E extends Throwable> { + + /** NOP singleton */ + @SuppressWarnings("rawtypes") + FailableIntToFloatFunction NOP = t -> 0f; + + /** + * Returns The NOP singleton. + * + * @param <E> The kind of thrown exception or error. + * @return The NOP singleton. + */ + @SuppressWarnings("unchecked") + static <E extends Throwable> FailableIntToFloatFunction<E> nop() { + return NOP; + } + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + * @throws E Thrown when the function fails. + */ + float applyAsFloat(int value) 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 88c82e511..ca4f4b6d9 100644 --- a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java @@ -931,6 +931,11 @@ public void testFailableIntToDoubleFunctionNop() throws Throwable { assertEquals(0, FailableIntToDoubleFunction.nop().applyAsDouble(Integer.MAX_VALUE), "Expect NOP to return 0"); } + @Test + public void testFailableIntToFloatFunctionNop() throws Throwable { + assertEquals(0, FailableIntToFloatFunction.nop().applyAsFloat(Integer.MAX_VALUE), "Expect NOP to return 0"); + } + @Test public void testFailableIntToLongFunctionNop() throws Throwable { assertEquals(0, FailableIntToLongFunction.nop().applyAsLong(Integer.MAX_VALUE), "Expect NOP to return 0"); @@ -2041,6 +2046,36 @@ public double applyAsDouble(final int value) throws Throwable { }; } + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + public void testThrows_FailableIntToFloatFunction_IOException() { + new FailableIntToFloatFunction<IOException>() { + + @Override + public float applyAsFloat(final int value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * 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_FailableIntToFloatFunction_Throwable() { + new FailableIntToFloatFunction<Throwable>() { + + @Override + public float applyAsFloat(final int value) throws Throwable { + throw new IOException("test"); + } + }; + } + /** * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as * generic test types.