Skip to content

Commit dfa22e5

Browse files
committed
Adding CheckedEffect1 and CheckedFn1 static factory method
1 parent 32e084b commit dfa22e5

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1414
### Added
1515
- `Predicate#predicate` static factory method
1616
- `Product2-8` left/right rotation methods
17-
- `Tuple2-8` specializations of left/right product rotation
17+
- `Tuple2-8` specializations of left/right product rotation
18+
- `CheckedEffect`, an `Effect` variant that can throw checked exceptions
19+
- `CheckedFn1#checked`, convenience static factory method to aid inference
1820

1921
## [3.1.0] - 2018-07-16
2022
### Added
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.jnape.palatable.lambda.functions.specialized.checked;
2+
3+
import com.jnape.palatable.lambda.functions.Effect;
4+
5+
import static com.jnape.palatable.lambda.functions.specialized.checked.Runtime.throwChecked;
6+
7+
/**
8+
* Specialized {@link Effect} that can throw any {@link Throwable}.
9+
*
10+
* @param <T> The {@link Throwable} type
11+
* @param <A> The input type
12+
* @see CheckedRunnable
13+
* @see CheckedFn1
14+
* @see Effect
15+
*/
16+
@FunctionalInterface
17+
public interface CheckedEffect1<T extends Throwable, A> extends Effect<A> {
18+
19+
@Override
20+
default void accept(A a) {
21+
try {
22+
checkedAccept(a);
23+
} catch (Throwable t) {
24+
throw throwChecked(t);
25+
}
26+
}
27+
28+
/**
29+
* A version of {@link Effect#accept} that can throw checked exceptions.
30+
*
31+
* @param a the effect argument
32+
* @throws T any exception that can be thrown by this method
33+
*/
34+
void checkedAccept(A a) throws T;
35+
36+
/**
37+
* Convenience static factory method for constructing a {@link CheckedEffect1} without an explicit cast or type
38+
* attribution at the call site.
39+
*
40+
* @param checkedEffect the checked effect
41+
* @param <T> the inferred Throwable type
42+
* @param <A> the input type
43+
* @return the checked effect
44+
*/
45+
static <T extends Throwable, A> CheckedEffect1<T, A> checked(CheckedEffect1<T, A> checkedEffect) {
46+
return checkedEffect;
47+
}
48+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,18 @@ default B apply(A a) {
3434
* @throws T any exception that can be thrown by this method
3535
*/
3636
B checkedApply(A a) throws T;
37+
38+
/**
39+
* Convenience static factory method for constructing a {@link CheckedFn1} without an explicit cast or type
40+
* attribution at the call site.
41+
*
42+
* @param checkedFn1 the checked fn1
43+
* @param <T> the inferred Throwable type
44+
* @param <A> the input type
45+
* @param <B> the output type
46+
* @return the checked fn1
47+
*/
48+
static <T extends Throwable, A, B> CheckedFn1<T, A, B> checked(CheckedFn1<T, A, B> checkedFn1) {
49+
return checkedFn1;
50+
}
3751
}

0 commit comments

Comments
 (0)