Awesome
Checked-Exceptions-enabled Java 8+ Functional Interfaces
Overview
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);
Key Features
- 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.
Core API
Functional Interfaces
- ThrowingFunction
- ThrowingIntFunction
- ThrowingToLongFunction
- ThrowingBiConsumer
- ThrowingBiFunction
- ThrowingBiPredicate
- ThrowingBinaryOperator
- ThrowingConsumer
- ThrowingPredicate
- ThrowingRunnable
- ThrowingSupplier
- ThrowingUnaryOperator
Adapters
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.
Maven Central
<dependency>
<groupId>com.pivovarit</groupId>
<artifactId>throwing-function</artifactId>
<version>1.6.1</version>
</dependency>
Gradle
compile 'com.pivovarit:throwing-function:1.6.1'
Dependencies
None - the library is implemented using core Java libraries.
Version history
1.6.1 (25-09-2024)
- Explicit module configuration via a multi-release jar
- Improved Javadoc
1.6.0 (24-09-2024)
- Added
Automatic-Module-Name
to MANIFEST
1.5.1 (06-05-2020)
- Fixed visibility issues with
ThrowingIntFunction
1.5.0 (26-01-2019)
- Introduced proper Semantic Versioning
- Introduced
ThrowingIntFunction
- Moved interfaces to
com.pivovarit.function
- Removed controversial
unwrap()
functionality