Skip to content

Commit dda40c5

Browse files
author
Gary Gregory
committed
Add IOBiFunction#asBiFunction()
1 parent e9815c8 commit dda40c5

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/main/java/org/apache/commons/io/function/IOBiFunction.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
package org.apache.commons.io.function;
1919

2020
import java.io.IOException;
21+
import java.io.UncheckedIOException;
2122
import java.util.Objects;
23+
import java.util.function.BiFunction;
2224
import java.util.function.Function;
2325

2426
/**
@@ -75,4 +77,14 @@ default <V> IOBiFunction<T, U, V> andThen(final IOFunction<? super R, ? extends
7577
* @throws IOException if an I/O error occurs.
7678
*/
7779
R apply(T t, U u) throws IOException;
80+
81+
/**
82+
* Converts this instance to a {@link BiFunction} that throws {@link UncheckedIOException} instead of
83+
* {@link IOException}.
84+
*
85+
* @return an unchecked BiFunction.
86+
*/
87+
default BiFunction<T, U, R> asBiFunction() {
88+
return (t, u) -> Uncheck.apply(this, t, u);
89+
}
7890
}

src/test/java/org/apache/commons/io/function/IOBiFunctionTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
package org.apache.commons.io.function;
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2122
import static org.junit.jupiter.api.Assertions.assertNull;
2223
import static org.junit.jupiter.api.Assertions.assertThrows;
2324

2425
import java.io.IOException;
2526
import java.nio.file.Files;
2627
import java.nio.file.LinkOption;
2728
import java.nio.file.Path;
29+
import java.util.HashMap;
30+
import java.util.Map;
2831

2932
import org.apache.commons.io.file.PathUtils;
3033
import org.junit.jupiter.api.Test;
@@ -75,6 +78,15 @@ public void testApplyThrowsException() {
7578
assertThrows(IOException.class, () -> isDirectory.apply(PathUtils.current(), PathUtils.EMPTY_LINK_OPTION_ARRAY));
7679
}
7780

81+
@Test
82+
public void testAsBiFunction() throws IOException {
83+
final Map<String, Long> map = new HashMap<>();
84+
map.put("1", 0L);
85+
final IOBiFunction<String, Long, Long> f = (t, u) -> Files.size(PathUtils.current());
86+
map.computeIfPresent("1", f.asBiFunction());
87+
assertNotEquals(0L, map.get("1"));
88+
}
89+
7890
@Test
7991
public void testNoopIOConsumer() throws IOException {
8092
assertNull(IOBiFunction.noop().apply(null, null));

0 commit comments

Comments
 (0)