|
3 | 3 | import com.jnape.palatable.lambda.adt.coproduct.CoProduct2; |
4 | 4 | import com.jnape.palatable.lambda.functions.Fn0; |
5 | 5 | import com.jnape.palatable.lambda.functions.Fn1; |
| 6 | +import com.jnape.palatable.lambda.functions.builtin.fn1.Downcast; |
| 7 | +import com.jnape.palatable.lambda.functions.specialized.Pure; |
6 | 8 | import com.jnape.palatable.lambda.functions.specialized.SideEffect; |
7 | 9 | import com.jnape.palatable.lambda.functor.Applicative; |
8 | 10 | import com.jnape.palatable.lambda.functor.builtin.Lazy; |
@@ -44,10 +46,9 @@ private Try() { |
44 | 46 | * @param recoveryFn the function mapping the {@link Throwable} to the result |
45 | 47 | * @return a new {@link Try} instance around either the original successful result or the mapped result |
46 | 48 | */ |
47 | | - @SuppressWarnings("unchecked") |
48 | 49 | public final <S extends Throwable> Try<A> catching(Class<S> throwableType, |
49 | 50 | Fn1<? super S, ? extends A> recoveryFn) { |
50 | | - return catching(throwableType::isInstance, t -> recoveryFn.apply((S) t)); |
| 51 | + return catching(throwableType::isInstance, t -> recoveryFn.apply(Downcast.<S, Throwable>downcast(t))); |
51 | 52 | } |
52 | 53 |
|
53 | 54 | /** |
@@ -76,13 +77,13 @@ public final Try<A> catching(Fn1<? super Throwable, ? extends Boolean> predicate |
76 | 77 | * rules above |
77 | 78 | */ |
78 | 79 | public final Try<A> ensuring(SideEffect sideEffect) { |
79 | | - return match(t -> trying(sideEffect) |
80 | | - .<Try<A>>fmap(constantly(failure(t))) |
81 | | - .recover(t2 -> { |
82 | | - t.addSuppressed(t2); |
83 | | - return failure(t); |
84 | | - }), |
85 | | - a -> trying(sideEffect).fmap(constantly(a))); |
| 80 | + return this.<Try<A>>match(t -> trying(sideEffect) |
| 81 | + .<Try<A>>fmap(constantly(failure(t))) |
| 82 | + .recover(t2 -> { |
| 83 | + t.addSuppressed(t2); |
| 84 | + return failure(t); |
| 85 | + }), |
| 86 | + a -> trying(sideEffect).fmap(constantly(a))); |
86 | 87 | } |
87 | 88 |
|
88 | 89 | /** |
@@ -133,7 +134,6 @@ public final <T extends Throwable> A orThrow() throws T { |
133 | 134 | */ |
134 | 135 | public abstract <T extends Throwable> A orThrow(Fn1<? super Throwable, ? extends T> fn) throws T; |
135 | 136 |
|
136 | | - |
137 | 137 | /** |
138 | 138 | * If this is a success, wrap the value in a {@link Maybe#just} and return it. Otherwise, return {@link |
139 | 139 | * Maybe#nothing()}. |
@@ -381,6 +381,15 @@ public static <A extends AutoCloseable, B extends AutoCloseable, C extends AutoC |
381 | 381 | return withResources(fn0, bFn, b -> withResources(() -> cFn.apply(b), fn::apply)); |
382 | 382 | } |
383 | 383 |
|
| 384 | + /** |
| 385 | + * The canonical {@link Pure} instance for {@link Try}. |
| 386 | + * |
| 387 | + * @return the {@link Pure} instance |
| 388 | + */ |
| 389 | + public static Pure<Try<?>> pureTry() { |
| 390 | + return Try::success; |
| 391 | + } |
| 392 | + |
384 | 393 | private static final class Failure<A> extends Try<A> { |
385 | 394 | private final Throwable t; |
386 | 395 |
|
|
0 commit comments