Skip to content

Checked Exceptions-enabled Java 8+ functional interfaces + adapters

License

Notifications You must be signed in to change notification settings

pivovarit/throwing-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Checked-Exceptions-enabled Java 8+ Functional Interfaces

Build against JDKs License Maven Central

Stargazers over time

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

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

  • 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