| Modifier and Type | Class and Description |
|---|---|
static class |
Try.ResourceHandler<R extends AutoCloseable>
Try with resource implementation.
|
static class |
Try.ResourceHandler2<R1 extends AutoCloseable,R2 extends AutoCloseable>
Try with resource implementation.
|
static class |
Try.ResourceHandler3<R1 extends AutoCloseable,R2 extends AutoCloseable,R3 extends AutoCloseable> |
static class |
Try.ResourceHandler4<R1 extends AutoCloseable,R2 extends AutoCloseable,R3 extends AutoCloseable,R4 extends AutoCloseable> |
static class |
Try.Value<V>
Try with a value.
|
| Constructor and Description |
|---|
Try() |
| Modifier and Type | Method and Description |
|---|---|
static <V> Try.Value<V> |
apply(Throwing.Supplier<? extends V> fn)
Creates a new try from given value provider.
|
static <V> Try.Value<V> |
call(Callable<? extends V> fn)
Creates a new try from given callable.
|
static Try.Value<Throwable> |
failure(Throwable x)
Get a new failure value.
|
abstract Optional<Throwable> |
getCause()
Cause for failure or empty optional for success result.
|
boolean |
isFailure()
True in case of failure.
|
boolean |
isSuccess()
True in case of success.
|
static <R extends AutoCloseable> |
of(R r1)
Functional try-with-resources:
|
static <R1 extends AutoCloseable,R2 extends AutoCloseable> |
of(R1 r1,
R2 r2)
Functional try-with-resources:
|
static <R1 extends AutoCloseable,R2 extends AutoCloseable,R3 extends AutoCloseable> |
of(R1 r1,
R2 r2,
R3 r3)
Functional try-with-resources with 3 inputs.
|
static <R1 extends AutoCloseable,R2 extends AutoCloseable,R3 extends AutoCloseable,R4 extends AutoCloseable> |
of(R1 r1,
R2 r2,
R3 r3,
R4 r4)
Functional try-with-resources with 4 inputs.
|
Try |
onComplete(Throwing.Consumer<Throwable> action)
Always run the given action, works like a finally clause.
|
Try |
onComplete(Throwing.Runnable action)
Always run the given action, works like a finally clause.
|
Try |
onFailure(Consumer<? super Throwable> action)
Run the given action if and only if this is a failure.
|
Try |
onSuccess(Runnable action)
Run the given action if and only if this is a success.
|
static Try |
run(Throwing.Runnable runnable)
Creates a side effect try from given runnable.
|
static <V> Try.Value<V> |
success(V value)
Get a new success value.
|
void |
throwException()
Propagate/throw the exception in case of failure.
|
<X extends Throwable> |
unwrap(Class<? extends X> type)
In case of failure unwrap the exception provided by calling
Throwable.getCause(). |
Try |
unwrap(Throwing.Predicate<Throwable> predicate)
In case of failure unwrap the exception provided by calling
Throwable.getCause(). |
static <R extends AutoCloseable> |
with(Throwing.Supplier<R> r1)
Functional try-with-resources:
|
static <R1 extends AutoCloseable,R2 extends AutoCloseable> |
with(Throwing.Supplier<R1> r1,
Throwing.Supplier<R2> r2)
Functional try-with-resources:
|
static <R1 extends AutoCloseable,R2 extends AutoCloseable,R3 extends AutoCloseable> |
with(Throwing.Supplier<R1> r1,
Throwing.Supplier<R2> r2,
Throwing.Supplier<R3> r3)
Functional try-with-resources with 3 inputs.
|
static <R1 extends AutoCloseable,R2 extends AutoCloseable,R3 extends AutoCloseable,R4 extends AutoCloseable> |
with(Throwing.Supplier<R1> r1,
Throwing.Supplier<R2> r2,
Throwing.Supplier<R3> r3,
Throwing.Supplier<R4> r4)
Functional try-with-resources with 4 inputs.
|
<X extends Throwable> |
wrap(Class<? extends X> predicate,
Throwing.Function<X,Throwable> wrapper)
In case of failure wrap an exception matching the given predicate to something else.
|
Try |
wrap(Throwing.Function<Throwable,Throwable> wrapper)
In case of failure wrap an exception matching the given predicate to something else.
|
<X extends Throwable> |
wrap(Throwing.Predicate<X> predicate,
Throwing.Function<X,Throwable> wrapper)
In case of failure wrap an exception matching the given predicate to something else.
|
public static final <R extends AutoCloseable> Try.ResourceHandler<R> of(R r1)
InputStream in = ...;
byte[] content = Try.of(in)
.apply(in -> read(in))
.get();
Jdbc example:
Connection connection = ...;
Try.of(connection)
.map(c -> c.preparedStatement("..."))
.map(stt -> stt.executeQuery())
.apply(rs-> {
return res.getString("column");
})
.get();
R - Resource type.r1 - Input resource.public static final <R1 extends AutoCloseable,R2 extends AutoCloseable> Try.ResourceHandler2<R1,R2> of(R1 r1, R2 r2)
InputStream in = ...;
OutputStream out = ...;
Try.of(in, out)
.run((from, to) -> copy(from, to))
.onFailure(Throwable::printStacktrace);
R1 - Resource type.R2 - Resource type.r1 - Input resource.r2 - Input resource.public static final <R1 extends AutoCloseable,R2 extends AutoCloseable,R3 extends AutoCloseable> Try.ResourceHandler3<R1,R2,R3> of(R1 r1, R2 r2, R3 r3)
R1 - Resource type.R2 - Resource type.R3 - Resource type.r1 - Input resource.r2 - Input resource.r3 - Input resource.public static final <R1 extends AutoCloseable,R2 extends AutoCloseable,R3 extends AutoCloseable,R4 extends AutoCloseable> Try.ResourceHandler4<R1,R2,R3,R4> of(R1 r1, R2 r2, R3 r3, R4 r4)
R1 - Resource type.R2 - Resource type.R3 - Resource type.R4 - Resource type.r1 - Input resource.r2 - Input resource.r3 - Input resource.r4 - Input resource.public static final <R extends AutoCloseable> Try.ResourceHandler<R> with(Throwing.Supplier<R> r1)
byte[] content = Try.with(() -> newInputStream())
.apply(in -> read(in))
.get();
Jdbc example:
Try.with(() -> newConnection())
.map(c -> c.preparedStatement("..."))
.map(stt -> stt.executeQuery())
.apply(rs-> {
return res.getString("column");
})
.get();
R - Resource type.r1 - Input resource.public static final <R1 extends AutoCloseable,R2 extends AutoCloseable> Try.ResourceHandler2<R1,R2> with(Throwing.Supplier<R1> r1, Throwing.Supplier<R2> r2)
Try.with(() -> newIn(), () -> newOut())
.run((from, to) -> copy(from, to))
.onFailure(Throwable::printStacktrace);
R1 - Resource type.R2 - Resource type.r1 - Input resource.r2 - Input resource.public static final <R1 extends AutoCloseable,R2 extends AutoCloseable,R3 extends AutoCloseable> Try.ResourceHandler3<R1,R2,R3> with(Throwing.Supplier<R1> r1, Throwing.Supplier<R2> r2, Throwing.Supplier<R3> r3)
R1 - Resource type.R2 - Resource type.R3 - Resource type.r1 - Input resource.r2 - Input resource.r3 - Input resource.public static final <R1 extends AutoCloseable,R2 extends AutoCloseable,R3 extends AutoCloseable,R4 extends AutoCloseable> Try.ResourceHandler4<R1,R2,R3,R4> with(Throwing.Supplier<R1> r1, Throwing.Supplier<R2> r2, Throwing.Supplier<R3> r3, Throwing.Supplier<R4> r4)
R1 - Resource type.R2 - Resource type.R3 - Resource type.R4 - Resource type.r1 - Input resource.r2 - Input resource.r3 - Input resource.r4 - Input resource.public static final <V> Try.Value<V> success(V value)
V - Value type.value - Value.public static final Try.Value<Throwable> failure(Throwable x)
x - Exception.public static <V> Try.Value<V> apply(Throwing.Supplier<? extends V> fn)
V - Value type.fn - Value provider.public static <V> Try.Value<V> call(Callable<? extends V> fn)
V - Value type.fn - Callable.public static Try run(Throwing.Runnable runnable)
Try.run(() -> ...)
.onFailure(x -> x.printStacktrace());
Throw the exception:
Try.run(() -> ...)
.throwException();
runnable - Runnable.public boolean isFailure()
public boolean isSuccess()
public Try onFailure(Consumer<? super Throwable> action)
action - Failure listener.public Try onSuccess(Runnable action)
action - Success listener.public <X extends Throwable> Try unwrap(Class<? extends X> type)
Throwable.getCause().
Useful for clean/shorter stackstrace.
Example for InvocationTargetException:
Try.run(() -> {
Method m = ...;
m.invoke(...); //might throw InvocationTargetException
}).unwrap(InvocationTargetException.class)
.throwException();
X - Exception type.type - Exception filter.public Try unwrap(Throwing.Predicate<Throwable> predicate)
Throwable.getCause().
Useful for clean/shorter stackstrace.
Example for InvocationTargetException:
Try.run(() -> {
Method m = ...;
m.invoke(...); //might throw InvocationTargetException
}).unwrap(InvocationTargetException.class::isInstance)
.throwException();
predicate - Exception filter.public Try wrap(Throwing.Function<Throwable,Throwable> wrapper)
wrapper - Exception mapper.public <X extends Throwable> Try wrap(Class<? extends X> predicate, Throwing.Function<X,Throwable> wrapper)
X - Exception type.predicate - Exception predicate.wrapper - Exception mapper.public <X extends Throwable> Try wrap(Throwing.Predicate<X> predicate, Throwing.Function<X,Throwable> wrapper)
X - Exception type.predicate - Exception predicate.wrapper - Exception mapper.public Try onComplete(Throwing.Runnable action)
action - Finally action.public Try onComplete(Throwing.Consumer<Throwable> action)
action - Finally action.public void throwException()
Copyright © 2017. All rights reserved.