An open API service indexing awesome lists of open source software.

https://github.com/powerunit/powerunit-extensions-exceptions

Objects to support checked exception in lambda
https://github.com/powerunit/powerunit-extensions-exceptions

exception-handling exceptions functional-programming java lambda unchecked-exception

Last synced: 3 months ago
JSON representation

Objects to support checked exception in lambda

Awesome Lists containing this project

README

          

# Powerunit-extensions-exceptions

[![Build Status](https://travis-ci.com/powerunit/powerunit-extensions-exceptions.svg?branch=master)](https://travis-ci.com/powerunit/powerunit-extensions-exceptions)[![Known Vulnerabilities](https://snyk.io/test/github/powerunit/powerunit-extensions-exceptions/badge.svg?targetFile=pom.xml)](https://snyk.io/test/github/powerunit/powerunit-extensions-exceptions?targetFile=pom.xml) [![DepShield Badge](https://depshield.sonatype.org/badges/powerunit/powerunit-extensions-exceptions/depshield.svg)](https://depshield.github.io) [![Total alerts](https://img.shields.io/lgtm/alerts/g/powerunit/powerunit-extensions-exceptions.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/powerunit/powerunit-extensions-exceptions/alerts/)[![Coverage Status](https://coveralls.io/repos/github/powerunit/powerunit-extensions-exceptions/badge.svg?branch=master)](https://coveralls.io/github/powerunit/powerunit-extensions-exceptions?branch=master)[![codecov](https://codecov.io/gh/powerunit/powerunit-extensions-exceptions/branch/master/graph/badge.svg)](https://codecov.io/gh/powerunit/powerunit-extensions-exceptions)[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/54e6f34a650147e48b1864a420695a1c)](https://www.codacy.com/app/mathieu.boretti/powerunit-extensions-exceptions?utm_source=github.com&utm_medium=referral&utm_content=powerunit/powerunit-extensions-exceptions&utm_campaign=Badge_Coverage)[![Codacy Badge](https://api.codacy.com/project/badge/Grade/54e6f34a650147e48b1864a420695a1c)](https://www.codacy.com/app/mathieu.boretti/powerunit-extensions-exceptions?utm_source=github.com&utm_medium=referral&utm_content=powerunit/powerunit-extensions-exceptions&utm_campaign=Badge_Grade)[![CodeFactor](https://www.codefactor.io/repository/github/powerunit/powerunit-extensions-exceptions/badge)](https://www.codefactor.io/repository/github/powerunit/powerunit-extensions-exceptions)[![BCH compliance](https://bettercodehub.com/edge/badge/powerunit/powerunit-extensions-exceptions?branch=master)](https://bettercodehub.com/results/powerunit/powerunit-extensions-exceptions)[![codebeat badge](https://codebeat.co/badges/cdebf167-fee0-46b4-b33d-c613f1586a9d)](https://codebeat.co/projects/github-com-powerunit-powerunit-extensions-exceptions-master)[![Maven Central](https://maven-badges.herokuapp.com/maven-central/ch.powerunit.extensions/powerunit-extensions-exceptions/badge.svg)](https://maven-badges.herokuapp.com/maven-central/ch.powerunit.extensions/powerunit-extensions-exceptions)![mergify-status](https://gh.mergify.io/badges/powerunit/powerunit-extensions-exceptions.png?style=cut)[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=powerunit_powerunit-extensions-exceptions&metric=alert_status)](https://sonarcloud.io/dashboard?id=powerunit_powerunit-extensions-exceptions)[![badge](https://report.ci/status/powerunit/powerunit-extensions-exceptions/badge.svg?branch=master)](https://report.ci/status/powerunit/powerunit-extensions-exceptions?branch=master)[![javadoc](http://javadoc.io/badge/ch.powerunit.extensions/powerunit-extensions-exceptions.svg?color=yellow)](http://javadoc.io/doc/ch.powerunit.extensions/powerunit-extensions-exceptions)

**This version doesn't support anymore Java 9. [Please use an older version, for instance the version 2.2.0](https://github.com/powerunit/powerunit-extensions-exceptions/tree/powerunit-extensions-exceptions-2.2.0)**

This library provides support to wraps _checked exception_, to be used as target functional interface (which by default only support `RuntimeException`).

The library exposes several functional interface, similar to the one from `java.util.function`, but that may throw exception. Then several methods are provided to convert these exception to `RuntimeException` or lift the function.

For example:

```java
FunctionWithException fonctionThrowingException = ...;

Function functionThrowingRuntimeException = FunctionWithException
.unchecked(fonctionThrowingException);
```

wraps the exception from `IOException` into a `RuntimeException` (which cause is the original one).

## Usage

Add the following dependency to your maven project :

```xml

ch.powerunit.extensions
powerunit-extensions-exceptions
3.0.0

```

And then just use the interface from the package `ch.powerunit.extensions.exceptions`. Each available interface has a name similar with the one from the `java.util.function` package, but ending with `WithException`. Three essential static entry methods are available :

| Method | Description | Example |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------- |
| `unchecked` | Converts the functional interface to the one without exception, by wrapping the exception to a `RuntimeException` | `FunctionWithException` to `Function` |
| `lifted` | Converts the functional interface to the one without exception, by returning an `Optional` or a default value in case of exception (or ignore exception for interface without return value) | `FunctionWithException` to `Function>` |
| `ignored` | Converts the functional interface to the one without exception, by returning a default value in case of exception (or ignore exception for interface without return value) | `FunctionWithException` to `Function` |

Also, non static version (`uncheck`, `lift`, `ignore`) of the methods are available.

The method `unchecked` also support an additional parameter to define how to wrap the exception by passing a `Function` to do it.

An additional method `stage(d)` is also available on several interface.

### `uncheck(ed)`

This method converts the functional interface to the one without exception, by wrapping the exception to a `RuntimeException`.

Three versions of this methods exists :

* `uncheck()` converting the functional interface directly.
```java
FunctionWithException myFunction = ...;

Function myUncheckedFunction = myFunction.uncheck();
```
* `unchecked(myInterface)` converting the received parameter.
```java
Function myUncheckedFunction = FunctionWithException.unchecked(x->x);
```
* `unchecked(myInterface,myExceptionMapper)` converting the received parameter, and using the received exception mapper to wrap the exception.
```java
Function myUncheckedFunction =
FunctionWithException.unchecked(
x->x,
IllegalArgumentException::new
);
```
The resulting exceptions are :

| Original With Exception Interface| Uncheck(ed) without exception mapper | Unchecked with exception mapper |
| --------------------------------- | ------------------------------------ | ----------------------------------- |
| _The original exception_ | An instance of `ch.powerunit.extensions.exceptions.WrappedException` having the original exception as cause | A instance of `RuntimeException` returned by the exception mapper |

A more concrete example may be :
```java
Function mapper =
ExceptionMapper.forException(
SQLException.class,
s ->
new WrappedException(
String.format(
"%s ; ErrorCode=%s ; SQLState=%s",
s.getMessage(),
s.getErrorCode(),
s.getSQLState()
),
s
)
);
```
In this example, it extracts the SQL information from the `SQLException`. The `exceptionMapperFor` method ensures that in case the exception is not a `SQLException`, a standard wrapping is done. Please see [the dedicated section](#exception-mapper) for more information.

### `lift(ed)`

This method converts the functional interface to the one without exception, by returning an `Optional` or a default value in case of exception or ignore the exception for interface without return value.

Two versions of this methods exists :

* `lift()` converting the functional interface directly.
```java
FunctionWithException myFunction = ...;

Function> myLiftedFunction = myFunction.lift();
```
* `lifted(myInterface)` converting the received parameter.
```java
Function> myLiftedFunction = FunctionWithException.lifted(x->x);
```

### `ignore(d)`

This method converts the functional interface to the one without exception, by returning a default value in case of exception or ignore the exception for interface without return value.

Two versions of this methods exists :

* `ignore()` converting the functional interface directly.
```java
FunctionWithException myFunction = ...;

Function myLiftedFunction = myFunction.ignore();
```
* `ignored(myInterface)` converting the received parameter.
```java
Function myLiftedFunction = FunctionWithException.ignored(x->x);
```

### `stage(d)`

This method converts the functional interface to the one without exception, by returning an `CompletionStage`. _This is not available on interface returning primitive type_.

For functional interface without result, a `CompletionStage` is returned.

Two versions of this methods exists :

* `stage()` converting the functional interface directly.
```java
FunctionWithException myFunction = ...;

Function> myStagedFunction = myFunction.stage();
```
* `staged(myInterface)` converting the received parameter.
```java
Function> myStagedFunction = FunctionWithException.staged(x->x);
```

### Exception Mapper

The various methods `forExceptions` from `ExceptionMapper` provides a way to chain several Exception Mapper.

Also, some dedicated, _ready to used_, Exception Mapper are provided :

* `sqlExceptionMapper()` - Return an exception mapper that adds to the message of the `RuntimeException` the SQL Error from the underlying exception. **This is only usable when the module java.sql is available**.
* `jaxbExceptionMapper()` - Return an exception mapper that adds to the message of the `RuntimeException` the JAXB Error from the underlying exception. **This is only usable when JAXB is available**.
* `saxExceptionMapper()` - Return an exception mapper that adds to the message of the `RuntimeException` the SAX Error from the underlying exception. **This is only usable when the module java.xml is available**.
* `transformerExceptionMapper()` - Return an exception mapper that adds to the message of the `RuntimeException` the Transformer Error from the underlying exception. **This is only usable when the module java.xml is available**.

#### Define global ExceptionMapper

It is possible to define a default exception mappers by using service loader.

By default, the exception are wrapped in a `WrappedException`. This behaviour may be change by implementing the required `ExceptionMapper` and register them as _service implementation_.

To do so, create all the required implementation, for example :

```java
public class MyExceptionMapper implements ch.powerunit.extensions.exceptions.ExceptionMapper {
public RuntimeException apply(Exception e) {
//Add code here
}

public Class extends Exception> targetException() {
return //Add code here;
}

// Optional, to define the order between the ExceptionMapper
public int order() {
return 100;
}

}
```

Then this may be registered in the `module-info.java` file:

```java
module XXX {
requires powerunit.exceptions;

provides ch.powerunit.extensions.exceptions.ExceptionMapper
with ....MyExceptionMapper;
}
```

### CommonsCollections4Helper

_This helper is only available if the commons-collections4 library is available_

The class `CommonsCollections4Helper` provides several static methods to convert interface
from this library to the corresponding version in commons-collections4.

## Reference

The following classes are provided:

| Standard functional interface | Exception functional interface | Unchecked version | Lifted version | Ignored version | Stage version |
| ----------------------------- | -------------------------------------- | ----------------------- | ------------------------------ | -------------- |--------------------|
| `BiFunction` | `BiFunctionWithException` | `BiFunction` | `BiFunction>` | `BiFunction` | `BiFunction>` |
| `BiConsumer` | `BiConsumerWithException` | `BiConsumer` | `BiConsumer` | `BiConsumer` | `BiFunction>` |
| `BiPredicate` | `BiPredicateWithException` | `BiPredicate` | `BiPredicate` | `BiPredicate` | N/A |
| `BinaryOperator` | `BinaryOperatorWithException` | `BinaryOperator` | `BinaryFunction>` | `BinaryOperator` | `BinaryFunction>` |
| `BooleanSupplier` | `BooleanSupplierWithException` | `BooleanSupplier` | `BooleanSupplier` | `BooleanSupplier` | N/A |
| `Consumer` | `ConsumerWithException` | `Consumer` | `Consumer` | `Consumer` | `Function>` |
| `DoubleBinaryOperator` | `DoubleBinaryOperatorWithException` | `DoubleBinaryOperator` | `DoubleBinaryOperator` | `DoubleBinaryOperator` | N/A |
| `DoubleConsumer` | `DoubleConsumerWithException` | `DoubleConsumer` | `DoubleConsumer` | `DoubleConsumer` | `DoubleFunction>` |
| `DoubleFunction` | `DoubleFunctionWithException` | `DoubleFunction` | `DoubleFunction>` | `DoubleFunction` | `DoubleFunction>` |
| `DoublePredicate` | `DoublePredicateWithException` | `DoublePredicate` | `DoublePredicate` | `DoublePredicate` | N/A |
| `DoubleSupplier` | `DoubleSupplierWithException` | `DoubleSupplier` | `DoubleSupplier` | `DoubleSupplier` | N/A |
| `DoubleToIntFunction` | `DoubleToIntFunctionWithException` | `DoubleToIntFunction` | `DoubleToIntFunction` | `DoubleToIntFunction` | N/A |
| `DoubleToLongFunction` | `DoubleToLongFunctionWithException` | `DoubleToLongFunction` | `DoubleToLongFunction` | `DoubleToLongFunction` | N/A |
| `DoubleUnaryOperator` | `DoubleUnaryOperatorWithException` | `DoubleUnaryOperator` | `DoubleUnaryOperator` | `DoubleUnaryOperator` | N/A |
| `FileFilter` | `FileFilterWithException` | `FileFilter` | `FileFilter` | `FileFilter` | N/A |
| `FilenameFilter` | `FilenameFilterWithException` | `FilenameFilter` | `FilenameFilter` | `FilenameFilter` | N/A |
| `Function` | `FunctionWithException` | `Function` | `Function>` | `Function` | `Function>` |
| `IntBinaryOperator` | `IntBinaryOperatorWithException` | `IntBinaryOperator` | `IntBinaryOperator` | `IntBinaryOperator` | N/A |
| `IntConsumer` | `IntConsumerWithException` | `IntConsumer` | `IntConsumer` | `IntConsumer` | `IntFunction>` |
| `IntFunction` | `IntFunctionWithException` | `IntFunction` | `IntFunction>` | `IntFunction` | `IntFunction>` |
| `IntPredicate` | `IntPredicateWithException` | `IntPredicate` | `IntPredicate` | `IntPredicate` | N/A |
| `IntSupplier` | `IntSupplierWithException` | `IntSupplier` | `IntSupplier` | `IntSupplier` | N/A |
| `IntToDoubleFunction` | `IntToDoubleFunctionWithException` | `IntToDoubleFunction` | `IntToDoubleFunction` | `IntToDoubleFunction` |N/A |
| `IntToLongFunction` | `IntToLongFunctionWithException` | `IntToLongFunction` | `IntToLongFunction` | `IntToLongFunction` |N/A |
| `IntUnaryOperator` | `IntUnaryOperatorWithException` | `IntUnaryOperator` | `IntUnaryOperator` | `IntUnaryOperator` |N/A |
| `LongBinaryOperator` | `LongBinaryOperatorWithException` | `LongBinaryOperator` | `LongBinaryOperator` | `LongBinaryOperator` |N/A |
| `LongConsumer` | `LongConsumerWithException` | `LongConsumer` | `LongConsumer` | `LongConsumer` | `LongFunction>` |
| `LongFunction` | `LongFunctionWithException` | `LongFunction` | `LongFunction>` | `LongFunction` | `LongFunction>` |
| `LongPredicate` | `LongPredicateWithException` | `LongPredicate` | `LongPredicate` | `LongPredicate` | N/A |
| `LongSupplier` | `LongSupplierWithException` | `LongSupplier` | `LongSupplier` | `LongSupplier` | N/A |
| `LongToDoubleFunction` | `LongToDoubleFunctionWithException` | `LongToDoubleFunction` | `LongToDoubleFunction` | `LongToDoubleFunction` | N/A |
| `LongToIntFunction` | `LongToIntFunctionWithException` | `LongToIntFunction` | `LongToIntFunction` | `LongToIntFunction` |N/A |
| `LongUnaryOperator` | `LongUnaryOperatorWithException` | `LongUnaryOperator` | `LongUnaryOperator` | `LongUnaryOperator` |N/A |
| `ObjectInputFilter` | `ObjectInputFilterWithException` | `ObjectInputFilter` | `Function>` | `ObjectInputFilter` | `Function>` |
| `ObjDoubleConsumer` | `ObjDoubleConsumerWithException` | `ObjDoubleConsumer` | `ObjDoubleConsumer` | `ObjDoubleConsumer` | `Function>` |
| `ObjIntConsumer` | `ObjIntConsumerWithException` | `ObjIntConsumer` | `ObjIntConsumer` | `ObjIntConsumer` | `Function>` |
| `ObjLongConsumer` | `ObjLongConsumerWithException` | `ObjLongConsumer` | `ObjLongConsumer` | `ObjLongConsumer` | `Function>` |
| `PathMatcher` | `PathMatcherWithException` | `PathMatcher` | `PathMatcher` | `PathMatcher` | N/A
| `Predicate` | `PredicateWithException` | `Predicate` | `Predicate` | `Predicate` | N/A
| `Runnable` | `RunnableWithException` | `Runnable` | `Runnable` | `Runnable` | `Supplier>`|
| `Supplier` | `SupplierWithException` | `Supplier` | `Supplier>` | `Supplier` | `Supplier>` |
| `ToDoubleBiFunction` | `ToDoubleBiFunctionWithException` | `ToDoubleBiFunction` | `ToDoubleBiFunction` | `ToDoubleBiFunction`| N/A |
| `ToDoubleFunction` | `ToDoubleFunctionWithException` | `ToDoubleFunction` | `ToDoubleFunction` | `ToDoubleFunction` | N/A |
| `ToIntBiFunction` | `ToIntBiFunctionWithException` | `ToIntBiFunction` | `ToIntBiFunction` | `ToIntBiFunction` | N/A |
| `ToIntFunction` | `ToIntFunctionWithException` | `ToIntFunction` | `ToIntFunction` | `ToIntFunction` | N/A |
| `ToLongBiFunction` | `ToLongBiFunctionWithException` | `ToLongBiFunction` | `ToLongBiFunction` | `ToLongBiFunction` | N/A |
| `ToLongFunction` | `ToLongFunctionWithException` | `ToLongFunction` | `ToLongFunction` | `ToLongFunction` | N/A |
| `UnaryOperator` | `UnaryOperatorWithException` | `UnaryOperator` | `Function>` | `UnaryOperator` | `Function>` |