throwing-function

Java 8+ functional interfaces with checked exceptions support

License

License

GroupId

GroupId

com.pivovarit
ArtifactId

ArtifactId

throwing-function
Last Version

Last Version

1.5.1
Release Date

Release Date

Type

Type

jar
Description

Description

throwing-function
Java 8+ functional interfaces with checked exceptions support
Project URL

Project URL

https://github.com/pivovarit/throwing-function
Source Code Management

Source Code Management

https://github.com/pivovarit/throwing-function

Download throwing-function

How to add to project

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

Dependencies

test (3)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-engine jar 5.0.1
org.junit.jupiter : junit-jupiter-params jar 5.0.1
org.assertj : assertj-core jar 3.8.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

Versions

Version
1.5.1
1.5.0
1.4