-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathResult.java
More file actions
39 lines (31 loc) · 909 Bytes
/
Result.java
File metadata and controls
39 lines (31 loc) · 909 Bytes
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
public class Result<T, E extends Exception> {
private final T value;
private final E error;
public Result(T value, E error) {
this.value = value;
this.error = error;
}
public static <T, E extends Exception> Result<T, E> create(Object input) {
return (input instanceof Exception)
? new Result<>(null, (E) input)
: new Result<>((T) input, null);
}
public static <T> Result<T, Exception> fromValue(T value) {
return new Result<>(value, null);
}
public static <T, E extends Exception> Result<T, E> fromError(E error) {
return new Result<>(null, error);
}
public boolean isSuccess() {
return error == null;
}
public boolean isError() {
return error != null;
}
public T getValue() {
return value;
}
public E getError() {
return error;
}
}