Java’s standard java.util.function
interfaces are not compatible with checked exceptions. This leads to verbose and cluttered code, requiring manual try-catch blocks for exception handling, 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);
}
}
This library introduces checked-exception-enabled functional interfaces, like ThrowingFunction
, allowing cleaner, more concise code. You can now handle exceptions in functional pipelines without sacrificing readability:
ThrowingFunction<String, URI, URISyntaxException> toUri = URI::new;
Using the ThrowingFunction#unchecked
adapter, this can be seamlessly integrated into standard streams:
...stream()
.map(unchecked(URI::new)) // static import of ThrowingFunction#unchecked
.forEach(System.out::println);
This eliminates the need for bulky try-catch blocks within stream operations:
...stream().map(path -> {
try {
return new URI(path);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}}).forEach(System.out::println);
- Functional Interfaces: Supports various functional types with checked exceptions.
- Adapters: Provides utility methods to convert
Throwing*
types into standard Java functional interfaces. - Lightweight: No external dependencies, implemented using core Java libraries.
- 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.6.1</version>
</dependency>
compile 'com.pivovarit:throwing-function:1.6.1'
None - the library is implemented using core Java libraries.
- Explicit module configuration via a multi-release jar
- Improved Javadoc
- Added
Automatic-Module-Name
to MANIFEST
- Fixed visibility issues with
ThrowingIntFunction
- Introduced proper Semantic Versioning
- Introduced
ThrowingIntFunction
- Moved interfaces to
com.pivovarit.function
- Removed controversial
unwrap()
functionality