This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTry.java
141 lines (112 loc) · 3.22 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
package cs2030s.fp;
/*
* CS2030S PE2 Question 1
* AY20/21 Semester 2
*
* @author zhongfu
*/
public abstract class Try<T> {
public static <T> Try<T> of(Producer<? extends T> p) {
try {
return Try.success(p.produce());
} catch (Throwable t) {
return Try.failure(t);
}
}
public static <T> Try<T> success(T val) {
return new Success<>(val);
}
public static <T> Try<T> failure(Throwable t) {
// this is ok because Failure implements every method that Try implements
// and the type means nothing in the context of Failure;
// we're not using the type var T in any way
@SuppressWarnings("unchecked")
Try<T> failure = (Try<T>) new Failure(t);
return failure;
}
public abstract T get() throws Throwable;
public abstract <U> Try<U> map(Transformer<? super T, ? extends U> t);
public abstract <U> Try<U> flatMap(
Transformer<? super T, ? extends Try<? extends U>> t);
public abstract Try<T> onFailure(Consumer<? super Throwable> c);
public abstract Try<T> recover(Transformer<? super Throwable, ? extends T> t);
private static class Success<T> extends Try<T> {
T val;
private Success(T val) {
this.val = val;
}
@Override
public T get() throws Throwable {
return this.val;
}
@Override
public <U> Try<U> map(Transformer<? super T, ? extends U> t) {
return Try.of(() -> t.transform(this.get()));
}
@Override
public <U> Try<U> flatMap(Transformer<? super T, ? extends Try<? extends U>> t) {
return Try.of(() -> t.transform(this.get()).get());
}
@Override
public Try<T> onFailure(Consumer<? super Throwable> c) {
return this;
}
@Override
public Try<T> recover(Transformer<? super Throwable, ? extends T> t) {
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Success<?>)) {
return false;
}
Success<?> s = (Success<?>) o;
return this.val == s.val || this.val != null && this.val.equals(s.val);
}
}
private static class Failure extends Try<Object> {
Throwable err;
private Failure(Throwable err) {
this.err = err;
}
@Override
public Object get() throws Throwable {
throw err;
}
@Override
public <U> Try<U> map(Transformer<? super Object, ? extends U> t) {
return Try.failure(this.err);
}
@Override
public <U> Try<U> flatMap(Transformer<? super Object, ? extends Try<? extends U>> t) {
return Try.failure(this.err);
}
@Override
public Try<Object> onFailure(Consumer<? super Throwable> c) {
try {
c.consume(this.err);
return this;
} catch (Throwable t) {
return Try.failure(t);
}
}
@Override
public Try<Object> recover(Transformer<? super Throwable, ? extends Object> t) {
return Try.of(() -> t.transform(this.err));
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Failure)) {
return false;
}
Failure f = (Failure) o;
return this.err == f.err || String.valueOf(this.err).equals(String.valueOf(f.err));
}
}
}