Skip to content

Commit c641a27

Browse files
committed
Add FailableIntToFloatFunction
1 parent f02292f commit c641a27

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ The <action> type attribute can be add,update,fix,remove.
9595
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Predicates.</action>
9696
<action type="add" dev="ggregory" due-to="Gary Gregory">Add RegExUtils methods typed to CharSequence input and deprecate old versions typed to String.</action>
9797
<action type="add" dev="ggregory" due-to="Gary Gregory">Add IterableStringTokenizer.</action>
98+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableIntToFloatFunction.</action>
9899
<!-- UPDATE -->
99100
<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>
100101
<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>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.lang3.function;
19+
20+
import java.util.function.IntToDoubleFunction;
21+
22+
/**
23+
* A functional interface like {@link IntToDoubleFunction} but for {@code float} that declares a {@link Throwable}.
24+
*
25+
* @param <E> The kind of thrown exception or error.
26+
* @since 3.18.0
27+
*/
28+
@FunctionalInterface
29+
public interface FailableIntToFloatFunction<E extends Throwable> {
30+
31+
/** NOP singleton */
32+
@SuppressWarnings("rawtypes")
33+
FailableIntToFloatFunction NOP = t -> 0f;
34+
35+
/**
36+
* Returns The NOP singleton.
37+
*
38+
* @param <E> The kind of thrown exception or error.
39+
* @return The NOP singleton.
40+
*/
41+
@SuppressWarnings("unchecked")
42+
static <E extends Throwable> FailableIntToFloatFunction<E> nop() {
43+
return NOP;
44+
}
45+
46+
/**
47+
* Applies this function to the given argument.
48+
*
49+
* @param value the function argument
50+
* @return the function result
51+
* @throws E Thrown when the function fails.
52+
*/
53+
float applyAsFloat(int value) throws E;
54+
}

src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,11 @@ public void testFailableIntToDoubleFunctionNop() throws Throwable {
931931
assertEquals(0, FailableIntToDoubleFunction.nop().applyAsDouble(Integer.MAX_VALUE), "Expect NOP to return 0");
932932
}
933933

934+
@Test
935+
public void testFailableIntToFloatFunctionNop() throws Throwable {
936+
assertEquals(0, FailableIntToFloatFunction.nop().applyAsFloat(Integer.MAX_VALUE), "Expect NOP to return 0");
937+
}
938+
934939
@Test
935940
public void testFailableIntToLongFunctionNop() throws Throwable {
936941
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 {
20412046
};
20422047
}
20432048

2049+
/**
2050+
* Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as
2051+
* generic test types.
2052+
*/
2053+
@Test
2054+
public void testThrows_FailableIntToFloatFunction_IOException() {
2055+
new FailableIntToFloatFunction<IOException>() {
2056+
2057+
@Override
2058+
public float applyAsFloat(final int value) throws IOException {
2059+
throw new IOException("test");
2060+
}
2061+
};
2062+
}
2063+
2064+
/**
2065+
* Tests that our failable interface is properly defined to throw any exception using the top level generic types
2066+
* Object and Throwable.
2067+
*/
2068+
@Test
2069+
public void testThrows_FailableIntToFloatFunction_Throwable() {
2070+
new FailableIntToFloatFunction<Throwable>() {
2071+
2072+
@Override
2073+
public float applyAsFloat(final int value) throws Throwable {
2074+
throw new IOException("test");
2075+
}
2076+
};
2077+
}
2078+
20442079
/**
20452080
* Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as
20462081
* generic test types.

0 commit comments

Comments
 (0)