Skip to content

Commit d3af237

Browse files
committed
Implemented isLeft, isRight, isMiddle in terms of either. Added swap methods.
1 parent d2f777d commit d3af237

File tree

1 file changed

+23
-52
lines changed

1 file changed

+23
-52
lines changed

core/src/main/java/fj/data/Either3.java

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import fj.Equal;
44
import fj.F;
5-
import fj.F0;
65
import fj.Hash;
76

87
import static fj.data.Option.none;
@@ -13,80 +12,38 @@ public abstract class Either3<A, B, C> {
1312
private Either3() {}
1413

1514
private static final class Left<A, B, C> extends Either3<A, B, C> {
16-
A a;
15+
private final A a;
16+
1717
Left(A a) {
1818
this.a = a;
1919
}
2020

21-
@Override
22-
public boolean isLeft() {
23-
return true;
24-
}
25-
26-
@Override
27-
public boolean isMiddle() {
28-
return false;
29-
}
30-
31-
@Override
32-
public boolean isRight() {
33-
return false;
34-
}
35-
3621
@Override
3722
public <D> D either(F<A, D> fa, F<B, D> fb, F<C, D> fc) {
3823
return fa.f(a);
3924
}
25+
4026
}
4127

4228
private static final class Middle<A, B, C> extends Either3<A, B, C> {
43-
B b;
29+
private final B b;
30+
4431
Middle(B b) {
4532
this.b = b;
4633
}
4734

48-
@Override
49-
public boolean isLeft() {
50-
return false;
51-
}
52-
53-
@Override
54-
public boolean isMiddle() {
55-
return true;
56-
}
57-
58-
@Override
59-
public boolean isRight() {
60-
return false;
61-
}
62-
6335
@Override
6436
public <D> D either(F<A, D> fa, F<B, D> fb, F<C, D> fc) {
6537
return fb.f(b);
6638
}
6739
}
6840

6941
private static final class Right<A, B, C> extends Either3<A, B, C> {
70-
C c;
42+
private final C c;
7143
Right(C c) {
7244
this.c = c;
7345
}
7446

75-
@Override
76-
public boolean isLeft() {
77-
return false;
78-
}
79-
80-
@Override
81-
public boolean isMiddle() {
82-
return false;
83-
}
84-
85-
@Override
86-
public boolean isRight() {
87-
return true;
88-
}
89-
9047
@Override
9148
public <D> D either(F<A, D> fa, F<B, D> fb, F<C, D> fc) {
9249
return fc.f(c);
@@ -161,11 +118,17 @@ public static <A, B, C> Either3<A, B, C> right(C c) {
161118
return new Right<>(c);
162119
}
163120

164-
public abstract boolean isLeft();
121+
public boolean isLeft() {
122+
return either(a -> true, b -> false, c -> false);
123+
}
165124

166-
public abstract boolean isMiddle();
125+
public boolean isMiddle() {
126+
return either(a -> false, b -> true, c -> false);
127+
}
167128

168-
public abstract boolean isRight();
129+
public boolean isRight() {
130+
return either(a -> false, b -> false, c -> true);
131+
}
169132

170133
public <X, Y, Z> Either3<X, Y, Z> map3(F<A, X> fl, F<B, Y> fm, F<C, Z> fr) {
171134
return either(
@@ -189,6 +152,14 @@ public Either3<C, B, A> swap() {
189152
return either(a -> right(a), b -> middle(b), c -> left(c));
190153
}
191154

155+
public Either3<B, A, C> swapLefts() {
156+
return either(a -> middle(a), b -> left(b), c -> right(c));
157+
}
158+
159+
public Either3<A, C, B> swapRights() {
160+
return either(a -> left(a), b -> right(b), c -> middle(c));
161+
}
162+
192163
public Option<A> leftOption() {
193164
return either(a -> some(a), b -> none(), c -> none());
194165
}

0 commit comments

Comments
 (0)