Skip to content

Commit

Permalink
Improving the API by adding a new way to use throwable functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SuppieRK committed Oct 2, 2024
1 parent 37f0b77 commit 92a85fb
Show file tree
Hide file tree
Showing 12 changed files with 778 additions and 18 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,31 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 2.0.0

### Added

- New class `UnsafeFunctions` to allow for easier and more readable access to the functionality
- `ThrowableRunnable` variant of the `Runnable` interface

### Changed

- Corrected Javadocs
- **BREAKING**: `ThrowableBinaryOperator` now properly implements `BinaryOperator` interface instead of
`BinaryOperator`'s parent `BiFunction` interface
- **BREAKING**: `ThrowableUnaryOperator` now properly implements `UnaryOperator` interface instead of `UnaryOperator`'s
parent `Function` interface

## 1.0.3

### Changed

- Removed restrictions for Try.Success, allowing to save `null` as successful value

## 1.0.2

### Changed

- Restored Java 8 build compatibility
- Adjusted license year
- Improved examples in README
Expand All @@ -21,9 +38,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## 1.0.1

### Added

- Maven Central repository support

## 1.0.0

### Added

- Initial release with a set of Java functional interfaces and Try
50 changes: 42 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Java Throwable Utils

This dependency-less library serves for one simple purpose:
This dependency-less library serves for one simple purpose:
reduce boilerplate try-catch statements during work with Java Stream API.

[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils?ref=badge_shield)
Expand All @@ -10,18 +10,20 @@ reduce boilerplate try-catch statements during work with Java Stream API.
## How to add

- Maven

```xml
<dependency>
<groupId>io.github.suppierk</groupId>
<artifactId>java-throwable-utils</artifactId>
<version>1.0.3</version>
<version>2.0.0</version>
</dependency>
```

- Gradle

```groovy
dependencies {
implementation 'io.github.suppierk:java-throwable-utils:1.0.3'
implementation 'io.github.suppierk:java-throwable-utils:2.0.0'
}
```

Expand Down Expand Up @@ -133,16 +135,46 @@ public class Demo {
}
```

All exceptions will be propagated using neat trick similar to Apache Commons `ExceptionUtils.rethrow` by leveraging Java type erasure to make checked exceptions unchecked.
or with the help of `UnsafeFunctions` utility class you can shorten it even more without changing your logic too much:

> This is the recommended, the least intrusive and the least verbose way
```java
import static io.github.suppierk.java.UnsafeFunctions.*;

import java.util.ArrayList;
import java.util.List;

public class Demo {
public static String throwingMethod(String source) throws Exception {
throw new Exception(source);
}

public static void main(String[] args) {
List<String> test = new ArrayList<>();
test.add("sample");

try {
test.stream()
.map(unsafeFunction(Demo::throwingMethod))
.forEach(unsafeConsumer(Demo::throwingMethod));
} catch (Exception e) {
e.printStackTrace();
}
}
}
```

All exceptions will be propagated using neat trick similar to Apache Commons `ExceptionUtils.rethrow` by leveraging Java
type erasure to make checked exceptions unchecked.

## Try

This library has simple implementation of `Try`,
which benefits greatly from presence of these functions
This library has simple implementation of `Try`, which benefits greatly from presence of these functions
and allows us to handle exceptions in functional style much like you deal with nullable values using `Optional`.

```java
import io.github.suppierk.java.util.Try;
import io.github.suppierk.java.Try;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -164,7 +196,9 @@ public class Test {
}
```

Same as for `Optional`, `Try` in a case of failure will preserve only first exception happened in a call chain and skip further operations.
Same as for `Optional`, `Try` in a case of failure will preserve only first exception happened in a call chain and skip
further operations.

## License

[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils?ref=badge_large)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SONATYPE_AUTOMATIC_RELEASE=true

GROUP=io.github.suppierk
POM_ARTIFACT_ID=java-throwable-utils
VERSION_NAME=1.0.3
VERSION_NAME=2.0.0

POM_NAME=Java Throwable Utilities
POM_DESCRIPTION=Java 8 compatible extensions allowing to use checked exceptions in functional programming APIs + Try interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
* SOFTWARE.
*/

package io.github.suppierk.java.util;
package io.github.suppierk.java;

import io.github.suppierk.java.util.ExceptionSuppressor;
import io.github.suppierk.java.util.function.ThrowableConsumer;
import io.github.suppierk.java.util.function.ThrowableFunction;
import io.github.suppierk.java.util.function.ThrowablePredicate;
Expand Down
217 changes: 217 additions & 0 deletions src/main/java/io/github/suppierk/java/UnsafeFunctions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package io.github.suppierk.java;

import io.github.suppierk.java.lang.ThrowableRunnable;
import io.github.suppierk.java.util.ThrowableComparator;
import io.github.suppierk.java.util.function.*;
import java.util.Comparator;
import java.util.function.*;

/** A utility for nicer wrapping of the unsafe function calls. */
public final class UnsafeFunctions {
private UnsafeFunctions() {
// Utility class
}

public static Runnable unsafeRunnable(ThrowableRunnable runnable) {
return runnable;
}

public static <T, U> BiConsumer<T, U> unsafeBiConsumer(ThrowableBiConsumer<T, U> biConsumer) {
return biConsumer;
}

public static <T, U, R> BiFunction<T, U, R> unsafeBiFunction(
ThrowableBiFunction<T, U, R> biFunction) {
return biFunction;
}

public static <T> BinaryOperator<T> unsafeBinaryOperator(
ThrowableBinaryOperator<T> binaryOperator) {
return binaryOperator;
}

public static <T, U> BiPredicate<T, U> unsafeBiPredicate(ThrowableBiPredicate<T, U> biPredicate) {
return biPredicate;
}

public static BooleanSupplier unsafeBooleanSupplier(ThrowableBooleanSupplier booleanSupplier) {
return booleanSupplier;
}

public static <T> Consumer<T> unsafeConsumer(ThrowableConsumer<T> consumer) {
return consumer;
}

public static <T> Comparator<T> unsafeComparator(ThrowableComparator<T> comparator) {
return comparator;
}

public static DoubleBinaryOperator unsafeDoubleBinaryOperator(
ThrowableDoubleBinaryOperator doubleBinaryOperator) {
return doubleBinaryOperator;
}

public static DoubleConsumer unsafeDoubleConsumer(ThrowableDoubleConsumer doubleConsumer) {
return doubleConsumer;
}

public static <R> DoubleFunction<R> unsafeDoubleFunction(
ThrowableDoubleFunction<R> doubleFunction) {
return doubleFunction;
}

public static DoublePredicate unsafeDoublePredicate(ThrowableDoublePredicate doublePredicate) {
return doublePredicate;
}

public static DoubleSupplier unsafeDoubleSupplier(ThrowableDoubleSupplier doubleSupplier) {
return doubleSupplier;
}

public static DoubleToIntFunction unsafeDoubleToIntFunction(
ThrowableDoubleToIntFunction doubleToIntFunction) {
return doubleToIntFunction;
}

public static DoubleToLongFunction unsafeDoubleToLongFunction(
ThrowableDoubleToLongFunction doubleToLongFunction) {
return doubleToLongFunction;
}

public static DoubleUnaryOperator unsafeDoubleUnaryOperator(
ThrowableDoubleUnaryOperator doubleUnaryOperator) {
return doubleUnaryOperator;
}

public static <T, R> Function<T, R> unsafeFunction(ThrowableFunction<T, R> function) {
return function;
}

public static IntBinaryOperator unsafeIntBinaryOperator(
ThrowableIntBinaryOperator intBinaryOperator) {
return intBinaryOperator;
}

public static IntConsumer unsafeIntConsumer(ThrowableIntConsumer intConsumer) {
return intConsumer;
}

public static <R> IntFunction<R> unsafeIntFunction(ThrowableIntFunction<R> intFunction) {
return intFunction;
}

public static IntPredicate unsafeIntPredicate(ThrowableIntPredicate intPredicate) {
return intPredicate;
}

public static IntSupplier unsafeIntSupplier(ThrowableIntSupplier intSupplier) {
return intSupplier;
}

public static IntToDoubleFunction unsafeIntToDoubleFunction(
ThrowableIntToDoubleFunction intToDoubleFunction) {
return intToDoubleFunction;
}

public static IntToLongFunction unsafeIntToLongFunction(
ThrowableIntToLongFunction intToLongFunction) {
return intToLongFunction;
}

public static IntUnaryOperator unsafeIntUnaryOperator(
ThrowableIntUnaryOperator intUnaryOperator) {
return intUnaryOperator;
}

public static LongBinaryOperator unsafeLongBinaryOperator(
ThrowableLongBinaryOperator longBinaryOperator) {
return longBinaryOperator;
}

public static LongConsumer unsafeLongConsumer(ThrowableLongConsumer longConsumer) {
return longConsumer;
}

public static <R> LongFunction<R> unsafeLongFunction(ThrowableLongFunction<R> longFunction) {
return longFunction;
}

public static LongPredicate unsafeLongPredicate(ThrowableLongPredicate longPredicate) {
return longPredicate;
}

public static LongSupplier unsafeLongSupplier(ThrowableLongSupplier longSupplier) {
return longSupplier;
}

public static LongToDoubleFunction unsafeLongToDoubleFunction(
ThrowableLongToDoubleFunction longToDoubleFunction) {
return longToDoubleFunction;
}

public static LongToIntFunction unsafeLongToIntFunction(
ThrowableLongToIntFunction longToIntFunction) {
return longToIntFunction;
}

public static LongUnaryOperator unsafeLongUnaryOperator(
ThrowableLongUnaryOperator longUnaryOperator) {
return longUnaryOperator;
}

public static <T> ObjDoubleConsumer<T> unsafeObjDoubleConsumer(
ThrowableObjDoubleConsumer<T> objDoubleConsumer) {
return objDoubleConsumer;
}

public static <T> ObjIntConsumer<T> unsafeObjIntConsumer(
ThrowableObjIntConsumer<T> objIntConsumer) {
return objIntConsumer;
}

public static <T> ObjLongConsumer<T> unsafeObjLongConsumer(
ThrowableObjLongConsumer<T> objLongConsumer) {
return objLongConsumer;
}

public static <T> Predicate<T> unsafePredicate(ThrowablePredicate<T> predicate) {
return predicate;
}

public static <T> Supplier<T> unsafeSupplier(ThrowableSupplier<T> supplier) {
return supplier;
}

public static <T, U> ToDoubleBiFunction<T, U> unsafeToDoubleBiFunction(
ThrowableToDoubleBiFunction<T, U> toDoubleBiFunction) {
return toDoubleBiFunction;
}

public static <T> ToDoubleFunction<T> unsafeToDoubleFunction(
ThrowableToDoubleFunction<T> toDoubleFunction) {
return toDoubleFunction;
}

public static <T, U> ToIntBiFunction<T, U> unsafeToIntBiFunction(
ThrowableToIntBiFunction<T, U> toIntBiFunction) {
return toIntBiFunction;
}

public static <T> ToIntFunction<T> unsafeToIntFunction(ThrowableToIntFunction<T> toIntFunction) {
return toIntFunction;
}

public static <T, U> ToLongBiFunction<T, U> unsafeToLongBiFunction(
ThrowableToLongBiFunction<T, U> toLongBiFunction) {
return toLongBiFunction;
}

public static <T> ToLongFunction<T> unsafeToLongFunction(
ThrowableToLongFunction<T> toLongFunction) {
return toLongFunction;
}

public static <T> UnaryOperator<T> unsafeUnaryOperator(ThrowableUnaryOperator<T> unaryOperator) {
return unaryOperator;
}
}
Loading

0 comments on commit 92a85fb

Please sign in to comment.