and adapters
Standard java.util.function
Functional Interfaces aren't checked-exception-friendly due to the absence of throws ...
clause which results in tedious and verbose necessity of handling them by adding try-catch
boilerplate.
Which makes one-liners like this:
path -> new URI(path)
become as verbose as:
path -> {
try {
return new URI(path);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
By applying com.pivovarit.function
functional interfaces, it's possible to regain clarity and readability:
ThrowingFunction<String, URI, URISyntaxException> toUri = URI::new;
and use them seamlessly with native java.util.function
classes by using custom ThrowingFunction#unchecked
adapters:
...stream()
.map(unchecked(URI::new)) // static import of ThrowingFunction#unchecked
.forEach(System.out::println);
which avoids ending up with:
...stream().map(path -> {
try {
return new URI(path);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}}).forEach(System.out::println);
- ThrowingFunction
- ThrowingIntFunction
- ThrowingToLongFunction
- ThrowingBiConsumer
- ThrowingBiFunction
- ThrowingBiPredicate
- ThrowingBinaryOperator
- ThrowingConsumer
- ThrowingPredicate
- ThrowingRunnable
- ThrowingSupplier
- ThrowingUnaryOperator
static Function<T, R> unchecked(ThrowingFunction<> f) {...}
Transforms a ThrowingFunction
instance into a standard java.util.function.Function
by wrapping checked exceptions in a RuntimeException
and rethrowing them.
static Function<T, Optional<R>> lifted() {...}
Transforms a ThrowingFunction
instance into a regular Function
returning result wrapped in an Optional
instance.
default ThrowingFunction<T, Void, E> asFunction() {...}
Returns Throwing(Predicate|Supplier|Consumer
) instance as a new ThrowingFunction
instance.
<dependency>
<groupId>com.pivovarit</groupId>
<artifactId>throwing-function</artifactId>
<version>1.5.1</version>
</dependency>
compile 'com.pivovarit:throwing-function:1.5.1'
None - the library is implemented using core Java libraries.
- Fixed visibility issues with
ThrowingIntFunction
- Introduced proper Semantic Versioning
- Introduced
ThrowingIntFunction
- Moved interfaces to
com.pivovarit.function
- Removed controversial
unwrap()
functionality