Skip to content

Commit 594928c

Browse files
committed
Either is now a specialization of CoProduct2; Either#orThrow can throw any Throwable
1 parent 0326a87 commit 594928c

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/main/java/com/jnape/palatable/lambda/adt/Either.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.jnape.palatable.lambda.adt;
22

3+
import com.jnape.palatable.lambda.adt.coproduct.CoProduct2;
4+
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedFn1;
35
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedSupplier;
4-
import com.jnape.palatable.lambda.functor.Bifunctor;
5-
import com.jnape.palatable.lambda.functor.Functor;
66

77
import java.util.Objects;
88
import java.util.Optional;
@@ -23,7 +23,7 @@
2323
* @param <L> The left parameter type
2424
* @param <R> The right parameter type
2525
*/
26-
public abstract class Either<L, R> implements Functor<R>, Bifunctor<L, R> {
26+
public abstract class Either<L, R> implements CoProduct2<L, R> {
2727

2828
private Either() {
2929
}
@@ -61,16 +61,16 @@ public final L forfeit(Function<? super R, ? extends L> forfeitFn) {
6161
}
6262

6363
/**
64-
* Return the wrapped value if this is a right; otherwise, map the wrapped left value to an <code>E</code> and throw
64+
* Return the wrapped value if this is a right; otherwise, map the wrapped left value to a <code>T</code> and throw
6565
* it.
6666
*
67-
* @param throwableFn a function from L to E
68-
* @param <E> the left parameter type (the throwable exception type)
67+
* @param throwableFn a function from L to T
68+
* @param <T> the left parameter type (the throwable type)
6969
* @return the wrapped value if this is a right
70-
* @throws E the result of applying the wrapped left value to throwableFn, if this is a left
70+
* @throws T the result of applying the wrapped left value to throwableFn, if this is a left
7171
*/
72-
public final <E extends RuntimeException> R orThrow(Function<? super L, ? extends E> throwableFn) throws E {
73-
return match(l -> {
72+
public final <T extends Throwable> R orThrow(Function<? super L, ? extends T> throwableFn) throws T {
73+
return match((CheckedFn1<T, L, R>) l -> {
7474
throw throwableFn.apply(l);
7575
}, id());
7676
}
@@ -200,13 +200,13 @@ public final <R2> Either<L, R2> fmap(Function<? super R, ? extends R2> fn) {
200200
@Override
201201
@SuppressWarnings("unchecked")
202202
public final <L2> Either<L2, R> biMapL(Function<? super L, ? extends L2> fn) {
203-
return (Either<L2, R>) Bifunctor.super.biMapL(fn);
203+
return (Either<L2, R>) CoProduct2.super.biMapL(fn);
204204
}
205205

206206
@Override
207207
@SuppressWarnings("unchecked")
208208
public final <R2> Either<L, R2> biMapR(Function<? super R, ? extends R2> fn) {
209-
return (Either<L, R2>) Bifunctor.super.biMapR(fn);
209+
return (Either<L, R2>) CoProduct2.super.biMapR(fn);
210210
}
211211

212212
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @see Fn1
1414
*/
1515
@FunctionalInterface
16-
public interface CheckedFn1<A, B> extends Fn1<A, B> {
16+
public interface CheckedFn1<T extends Throwable, A, B> extends Fn1<A, B> {
1717

1818
@Override
1919
default B apply(A a) {
@@ -29,7 +29,7 @@ default B apply(A a) {
2929
*
3030
* @param a the argument
3131
* @return the result of the function application
32-
* @throws Exception any exception thrown by the function application
32+
* @throws T any Throwable thrown by the function application
3333
*/
34-
B checkedApply(A a) throws Exception;
34+
B checkedApply(A a) throws T;
3535
}

0 commit comments

Comments
 (0)