Skip to content

Commit 19bac38

Browse files
committed
Add FailableByteSupplier
1 parent a4ec388 commit 19bac38

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ The <action> type attribute can be add,update,fix,remove.
8686
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.time.DateUtils.toZonedDateTime(Date[, TimeZone]).</action>
8787
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ByteConsumer.</action>
8888
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableByteConsumer.</action>
89+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableByteSupplier.</action>
8990
<!-- UPDATE -->
9091
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] Bump org.apache.commons:commons-text from 1.13.1 to 1.14.0.</action>
9192
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 85 to 87.</action>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
* https://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.IntSupplier;
21+
22+
/**
23+
* A functional interface like {@link IntSupplier}, but for a byte, that declares a {@link Throwable}.
24+
*
25+
* @param <E> The kind of thrown exception or error.
26+
* @since 3.19
27+
*/
28+
@FunctionalInterface
29+
public interface FailableByteSupplier<E extends Throwable> {
30+
31+
/**
32+
* Supplies a byte.
33+
*
34+
* @return a result
35+
* @throws E if the supplier fails
36+
*/
37+
byte getAsByte() throws E;
38+
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,36 @@ public void accept(final byte value) throws Throwable {
16781678
}.accept((byte) 0));
16791679
}
16801680

1681+
/**
1682+
* Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as
1683+
* generic test types.
1684+
*/
1685+
@Test
1686+
void testThrows_FailableByteSupplier_IOException() {
1687+
assertThrows(IOException.class, () -> new FailableByteSupplier<IOException>() {
1688+
1689+
@Override
1690+
public byte getAsByte() throws IOException {
1691+
throw new IOException("test");
1692+
}
1693+
}.getAsByte());
1694+
}
1695+
1696+
/**
1697+
* Tests that our failable interface is properly defined to throw any exception using the top level generic types
1698+
* Object and Throwable.
1699+
*/
1700+
@Test
1701+
void testThrows_FailableByteSupplier_Throwable() {
1702+
assertThrows(IOException.class, () -> new FailableByteSupplier<Throwable>() {
1703+
1704+
@Override
1705+
public byte getAsByte() throws Throwable {
1706+
throw new IOException("test");
1707+
}
1708+
}.getAsByte());
1709+
}
1710+
16811711
/**
16821712
* Tests that our failable interface is properly defined to throw any exception using the top level generic types
16831713
* Object and Throwable.

0 commit comments

Comments
 (0)