Skip to content

Commit 150d9e6

Browse files
committed
CheckedRunnable and CheckedSupplier convenience/conversion methods
1 parent 4c91720 commit 150d9e6

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2727
- `Both`, for dually applying two functions and producing a `Tuple2` of their results
2828
- `Lens#both`, for dually focusing with two lenses at once
2929
- `IfThenElse`, an expression form for `if` statements
30+
- `CheckedRunnable` and `CheckedSupplier` conversion and convenience methods
3031

3132
### Deprecated
3233
- `Either#trying` in favor of `Try#trying`

src/main/java/com/jnape/palatable/lambda/functions/specialized/checked/CheckedFn1.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,12 @@ default B apply(A a) {
2626
}
2727
}
2828

29+
/**
30+
* A version of {@link Fn1#apply} that can throw checked exceptions.
31+
*
32+
* @param a the function argument
33+
* @return the application of the argument to the function
34+
* @throws T any exception that can be thrown by this method
35+
*/
2936
B checkedApply(A a) throws T;
3037
}

src/main/java/com/jnape/palatable/lambda/functions/specialized/checked/CheckedRunnable.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.jnape.palatable.lambda.functions.specialized.checked;
22

3+
import com.jnape.palatable.lambda.adt.Unit;
4+
5+
import static com.jnape.palatable.lambda.adt.Unit.UNIT;
36
import static com.jnape.palatable.lambda.functions.specialized.checked.Runtime.throwChecked;
47

58
/**
@@ -21,5 +24,35 @@ default void run() {
2124
}
2225
}
2326

27+
/**
28+
* A version of {@link Runnable#run()} that can throw checked exceptions.
29+
*
30+
* @throws T any exception that can be thrown by this method
31+
*/
2432
void checkedRun() throws T;
33+
34+
/**
35+
* Convert this {@link CheckedRunnable} to a {@link CheckedSupplier} that returns {@link Unit}.
36+
*
37+
* @return the checked supplier
38+
*/
39+
default CheckedSupplier<T, Unit> toSupplier() {
40+
return () -> {
41+
run();
42+
return UNIT;
43+
};
44+
}
45+
46+
/**
47+
* Convenience static factory method for constructing a {@link CheckedRunnable} without an explicit cast or type
48+
* attribution at the call site.
49+
*
50+
* @param runnable the checked runnable
51+
* @param <T> the inferred Throwable type
52+
* @return the checked runnable
53+
*/
54+
static <T extends Throwable> CheckedRunnable<T> checked(CheckedRunnable<T> runnable) {
55+
return runnable::run;
56+
}
57+
2558
}

src/main/java/com/jnape/palatable/lambda/functions/specialized/checked/CheckedSupplier.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,26 @@ default A get() {
3131
* @throws T any exception that can be thrown by this method
3232
*/
3333
A checkedGet() throws T;
34+
35+
/**
36+
* Convert this {@link CheckedSupplier} to a {@link CheckedRunnable}.
37+
*
38+
* @return the checked runnable
39+
*/
40+
default CheckedRunnable<T> toRunnable() {
41+
return this::get;
42+
}
43+
44+
/**
45+
* Convenience static factory method for constructing a {@link CheckedSupplier} without an explicit cast or type
46+
* attribution at the call site.
47+
*
48+
* @param supplier the checked supplier
49+
* @param <T> the inferred Throwable type
50+
* @param <A> the supplier return type
51+
* @return the checked supplier
52+
*/
53+
static <T extends Throwable, A> CheckedSupplier<T, A> checked(CheckedSupplier<T, A> supplier) {
54+
return supplier::get;
55+
}
3456
}

0 commit comments

Comments
 (0)