blob: dec2e7e079b93bf0f61a84a7f0d6d8a97de994b1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package io.dico.dicore.exceptions.checkedfunctions;
import io.dico.dicore.exceptions.ExceptionHandler;
import java.util.function.Supplier;
/**
* checked mimic of {@link Supplier}
*
* @param <TResult>
* @param <TException>
*/
@FunctionalInterface
public interface CheckedSupplier<TResult, TException extends Throwable>
extends CheckedFunctionalObject<TResult, TException>, Supplier<TResult> {
/**
* The computation
*
* @return the result of this computation
* @throws TException if an error occurs
*/
TResult checkedGet() throws TException;
/**
* Unchecked version of {@link #checkedGet()}
* If a {@link TException} occurs, an unchecked one might be thrown by {@link #resultOnError(Throwable, Object...)}
*
* @return the result of this computation
* @see #checkedGet()
* @see #resultOnError(Throwable, Object...)
*/
@Override
default TResult get() {
try {
return checkedGet();
} catch (Throwable ex) {
return handleGenericException(ex);
}
}
/**
* {@inheritDoc}
*/
@Override
default CheckedSupplier<TResult, TException> handleExceptionsWith(ExceptionHandler handler) {
return new CheckedSupplier<TResult, TException>() {
@Override
public TResult checkedGet() throws TException {
return CheckedSupplier.this.checkedGet();
}
@Override
@SuppressWarnings("unchecked")
public TResult handleGenericException(Throwable thrown, Object... args) {
Object result = handler.handleGenericException(thrown, args);
try {
return (TResult) result;
} catch (Exception ex) {
return null;
}
}
@Override
public CheckedSupplier<TResult, TException> handleExceptionsWith(ExceptionHandler handler) {
return CheckedSupplier.this.handleExceptionsWith(handler);
}
};
}
}
|