-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEither.java
More file actions
171 lines (140 loc) · 4.62 KB
/
Copy pathEither.java
File metadata and controls
171 lines (140 loc) · 4.62 KB
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
package jtree.util;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import lombok.NonNull;
@SuppressWarnings("unchecked")
public final class Either<F,S> {
public static <F,S> Either<F,S> first(@NonNull F first) {
return new Either<>(true, first);
}
public static <F,S> Either<F,S> second(@NonNull S second) {
return new Either<>(false, second);
}
private final boolean isFirst;
private final Object value;
private Either(boolean isFirst, Object value) {
if(this.isFirst = isFirst) {
this.value = value;
} else {
this.value = value;
}
}
public boolean isFirst() { return isFirst; }
public boolean isSecond() { return !isFirst; }
public F first() {
if(isFirst) {
return (F)value;
} else {
throw new NoSuchElementException();
}
}
public F firstOrElse(F other) {
return isFirst? (F)value : other;
}
public F firstOrElseGet(Supplier<? extends F> supplier) {
return isFirst? (F)value : supplier.get();
}
public F firstOrElseThrow() {
if(isFirst) {
return (F)value;
} else {
throw new NoSuchElementException();
}
}
public <X extends Throwable> F firstOrElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if(isFirst) {
return (F)value;
} else {
throw exceptionSupplier.get();
}
}
public S second() {
if(isFirst) {
throw new NoSuchElementException();
} else {
return (S)value;
}
}
public S secondOrElse(S other) {
return isFirst? other : (S)value;
}
public S secondOrElseGet(Supplier<? extends S> supplier) {
return isFirst? supplier.get() : (S)value;
}
public S secondOrElseThrow() {
if(isFirst) {
throw new NoSuchElementException();
} else {
return (S)value;
}
}
public <X extends Throwable> S secondOrElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if(isFirst) {
throw exceptionSupplier.get();
} else {
return (S)value;
}
}
public Object getValue() {
return value;
}
public void ifFirst(Consumer<? super F> action) {
if(isFirst) {
action.accept((F)value);
}
}
public void ifSecond(Consumer<? super S> action) {
if(!isFirst) {
action.accept((S)value);
}
}
public void accept(Consumer<? super F> action, Consumer<? super S> otherAction) {
if(isFirst) {
action.accept((F)value);
} else {
otherAction.accept((S)value);
}
}
public <F2,S2> Either<F2,S2> flatMap(Function<? super F, ? extends Either<? extends F2, ? extends S2>> firstMapper, Function<? super S, ? extends Either<? extends F2, ? extends S2>> secondMapper) {
return isFirst? (Either<F2,S2>)firstMapper.apply((F)value) : (Either<F2,S2>)secondMapper.apply((S)value);
}
public <F2,S2> Either<F2,S2> flatMap(BiFunction<? super Boolean, ? super Object, ? extends Either<? extends F2, ? extends S2>> mapper) {
return (Either<F2,S2>)mapper.apply(isFirst, value);
}
public <F2,S2> Either<F2,S2> map(BiFunction<? super Boolean, ? super Object, ?> mapper) {
Object result = mapper.apply(isFirst, value);
return isFirst? Either.first((F2)result) : Either.second((S2)result);
}
public <F2,S2> Either<F2,S2> map(Function<? super F, ? extends F2> firstMapper, Function<? super S, ? extends S2> secondMapper) {
return isFirst? Either.first(firstMapper.apply((F)value)) : Either.second(secondMapper.apply((S)value));
}
public <T> T unravel(Function<? super F, ? extends T> firstMapper, Function<? super S, ? extends T> secondMapper) {
return isFirst? firstMapper.apply((F)value) : secondMapper.apply((S)value);
}
public <T> T unravel(BiFunction<? super Boolean, ? super Object, ? extends T> func) {
return func.apply(isFirst, value);
}
@Override
public int hashCode() {
return Objects.hash(isFirst, value);
}
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(obj instanceof Either) {
var either = (Either<?,?>)obj;
return isFirst == either.isFirst && Objects.equals(value, either.value);
}
return false;
}
@Override
public String toString() {
return String.format("Either.%s(%s)", isFirst? "first" : "second", value);
}
}