Java 8+ functional types supporting checked exceptions + some handy utils.
ThrowingFunction<String, URI, URISyntaxException> toUri = URI::new;
...stream().map(ThrowingFunction.unchecked(URI::new)).forEach(System.out::println);
...stream().map(unchecked(URI::new)).forEach(System.out::println); //with a static import
...stream().map(path -> {
try {
return new URI(path);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}).forEach(System.out::println);
For Maven users:
<dependency>
<groupId>pl.touk</groupId>
<artifactId>throwing-function</artifactId>
<version>1.3</version>
</dependency>
- ThrowingBiConsumer
- ThrowingBiFunction
- ThrowingBiPredicate
- ThrowingBinaryOperator
- ThrowingConsumer
- ThrowingFunction
- ThrowingPredicate
- ThrowingRunnable
- ThrowingSupplier
- ThrowingUnaryOperator
default Function<T, R> uncheck() {...}
Transforms ThrowingFunction instance into a regular Function. Checked exception gets wrapped in a RuntimeException. Feature is available for all java.util.function types. Comes both as a static and as an instance method.
static Function<T, R> uncheck(ThrowingFunction<> f) {...}
Static version of the uncheck() method. Works nice when using with original java functional types. ...stream().map(unchecked(URI::new)).forEach(System.out::println);
static Function<T, Optional<R>> lifted() {...}
Transforms ThrowingFunction into a regular Function returning result wrapped into an Optional instance. If exception
is thrown, result will contain an empty Optional instance. Exception gets ignored. Comes as a static method. Equivalent instance method is called lift()
default ThrowingFunction<T, Void, E> asFunction() {...}
Returns ThrowingPredicate/ThrowingSupplier/ThrowingConsumer instance as a new ThrowingFunction instance.
Checker.checked()
Additional static function allowing to catch wrapped checked exceptions, unwrap and rethrow them. Comes in handy sometimes.
- Grzegorz Piwowarek
- Hubert Lipiński
- Tomasz Wielga
This project is licenced under Apache License.