Checked Exceptions-enabled Java 8+ functional interfaces + adapters
Branch: master
Clone or download
Latest commit e6e2c82 Jan 26, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Revise Readme (#40) Jan 26, 2019
.gitignore Readme + tweaks Nov 27, 2015
.travis.yml Update .travis.yml Nov 10, 2018
LICENSE Add license Feb 12, 2016
README.md Update README.md (#41) Jan 26, 2019
pom.xml Revise Readme (#40) Jan 26, 2019

README.md

Checked-Exceptions-enabled Java 8 Functional Interfaces

and adapters

Build Status License Maven Central

Rationale

Standard Functional Interfaces from java.util.function package are not checked-exception-friendly due to the lack of throws ... clause which results in tedious and verbose necessity of handling them by adding try-catch boilerplate.

This makes one-liners like this:

path -> new URI(path)

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 a custom ThrowingFunction#unchecked adapter:

...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);

Basic 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.5.0</version>
</dependency>
Gradle
compile 'com.pivovarit:throwing-function:1.5.0'

Dependencies

None - the library is implemented using core Java libraries.

Version history

1.5.0 (26-01-2019)

  • Introduced Semantic Versioning
  • Introduced ThrowingIntFunction
  • Moved interfaces to com.pivovarit.function
  • Removed controversial unwrap() functionality