-
Notifications
You must be signed in to change notification settings - Fork 2
/
Try.java
202 lines (174 loc) · 4.09 KB
/
Try.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package de.tototec.utils.functional;
import java.io.Serializable;
import java.util.NoSuchElementException;
public final class Try<T> implements Serializable {
// BEGIN OF STATIC PART
private static final long serialVersionUID = 1L;
public static <T> Try<T> success(final T success) {
return new Try<T>(null, success, true);
}
public static <T> Try<T> failure(final Throwable failure) {
if (isFatal(failure)) {
Util.<RuntimeException, T> sneakyThrow(failure);
}
return new Try<T>(failure, null, false);
}
public static <T> Try<T> of(final CheckedF0<T> provider) {
try {
return success(provider.apply());
} catch (final Throwable e) {
return failure(e);
}
}
public static boolean isFatal(final Throwable throwable) {
return throwable instanceof InterruptedException
|| throwable instanceof LinkageError
|| throwable instanceof ThreadDeath
|| throwable instanceof VirtualMachineError;
}
private static final class Util {
@SuppressWarnings("unchecked")
private static final <T extends Throwable, R> R sneakyThrow(final Throwable t) throws T {
throw (T) t;
}
}
// END OF STATIC PART
private final Throwable failure;
private final T value;
private final boolean isSuccess;
private Try(final Throwable error, final T value, final boolean isOk) {
this.failure = error;
this.value = value;
this.isSuccess = isOk;
if (isOk && error != null) {
throw new IllegalArgumentException("Failure must be null");
} else if (!isOk && value != null) {
throw new IllegalArgumentException("Value value must be null");
}
}
public Throwable failure() {
if (!isSuccess) {
return failure;
} else {
throw new NoSuchElementException("Failure not defined.");
}
}
public Optional<Throwable> failureOption() {
if (!isSuccess) {
return Optional.some(failure);
} else {
return Optional.none();
}
}
/**
* Get the value or throw the exception.
*
* @return
*/
public T get() {
if (isSuccess) {
return value;
} else {
return Util.<RuntimeException, T> sneakyThrow(failure);
}
}
public Optional<T> toOption() {
if (isSuccess) {
return Optional.some(value);
} else {
return Optional.none();
}
}
public java.util.Optional<T> toJavaOption() {
return toOption().toJava();
}
public Either<Throwable, T> toEither() {
return new Either<Throwable, T>(failure, value, isSuccess);
}
public boolean isSuccess() {
return isSuccess;
}
public boolean isFailure() {
return !isSuccess;
}
@Override
public String toString() {
return isSuccess ? ("Success(" + value + ")") : ("Failure(" + failure + ")");
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (isSuccess ? 1231 : 1237);
result = prime * result + ((failure == null) ? 0 : failure.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Try<?> other = (Try<?>) obj;
if (isSuccess != other.isSuccess) {
return false;
}
if (isSuccess) {
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
} else {
if (failure == null) {
if (other.failure != null) {
return false;
}
} else if (!failure.equals(other.failure)) {
return false;
}
}
return true;
}
public T getOrElse(final T t) {
if (isSuccess) {
return get();
} else {
return t;
}
}
public T getOrElseF(final F0<T> f) {
if (isSuccess) {
return get();
} else {
return f.apply();
}
}
public Try<T> orElseF(final CheckedF0<Try<T>> f) {
if (isSuccess) {
return this;
} else {
try {
return f.apply();
} catch (final Throwable e) {
if (isFatal(e)) {
Util.<RuntimeException, T> sneakyThrow(e);
}
return new Try<T>(e, null, false);
}
}
}
public void foreach(final Procedure1<? super T> f) {
if (isSuccess()) {
f.apply(get());
}
}
}