ThrowingFunction

Java 8 functional types on steroids supporting checked exceptions

License

License

GroupId

GroupId

pl.touk
ArtifactId

ArtifactId

throwing-function
Last Version

Last Version

1.3
Release Date

Release Date

Type

Type

jar
Description

Description

ThrowingFunction
Java 8 functional types on steroids supporting checked exceptions
Project URL

Project URL

https://github.com/TouK/ThrowingFunction
Source Code Management

Source Code Management

https://github.com/TouK/ThrowingFunction

Download throwing-function

How to add to project

<!-- https://jarcasting.com/artifacts/pl.touk/throwing-function/ -->
<dependency>
    <groupId>pl.touk</groupId>
    <artifactId>throwing-function</artifactId>
    <version>1.3</version>
</dependency>
// https://jarcasting.com/artifacts/pl.touk/throwing-function/
implementation 'pl.touk:throwing-function:1.3'
// https://jarcasting.com/artifacts/pl.touk/throwing-function/
implementation ("pl.touk:throwing-function:1.3")
'pl.touk:throwing-function:jar:1.3'
<dependency org="pl.touk" name="throwing-function" rev="1.3">
  <artifact name="throwing-function" type="jar" />
</dependency>
@Grapes(
@Grab(group='pl.touk', module='throwing-function', version='1.3')
)
libraryDependencies += "pl.touk" % "throwing-function" % "1.3"
[pl.touk/throwing-function "1.3"]

Dependencies

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.assertj : assertj-core jar 3.3.0

Project Modules

There are no modules declared in this project.

Checked-Exceptions-enabled Java 8+ Functional Interfaces

and adapters

Build Status License Maven Central

Rationale

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

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

Dependencies

None - the library is implemented using core Java libraries.

Version history

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
pl.touk

TouK

We’re a software house based in Warsaw. We deliver custom IT systems.

Versions

Version
1.3
1.2.1
1.2
1.1