Created
June 28, 2020 23:04
-
-
Save diogoarm/67a0f0e9c483e93c62495daee62ecf50 to your computer and use it in GitHub Desktop.
Functional interface to use with streams, it changes checked exception to unchecked.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ... | |
import java.util.function.Function; | |
@FunctionalInterface | |
public interface ThrowingFunction<T, R, E extends Throwable> { | |
R apply(T t) throws E; | |
static <T, R, E extends Throwable> Function<T, R> unchecked(ThrowingFunction<T, R, E> f) { | |
return t -> { | |
try { | |
return f.apply(t); | |
} catch (Throwable e) { | |
throw new RuntimeException(e); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment