1 package io.atlassian.fugue.optic.std;
2
3 import io.atlassian.fugue.Either;
4 import io.atlassian.fugue.optic.PPrism;
5 import io.atlassian.fugue.optic.Prism;
6
7 import java.util.function.Function;
8
9 import static io.atlassian.fugue.optic.PPrism.pPrism;
10
11
12
13
14 public final class EitherOptics {
15
16 private EitherOptics() {}
17
18 public static <A, B, C> PPrism<Either<A, B>, Either<C, B>, A, C> pLeft() {
19 return pPrism(ab -> ab.swap().bimap(Either::right, Function.identity()), Either::left);
20 }
21
22 public static <A, B> Prism<Either<A, B>, A> left() {
23 return Prism.prism(ab -> ab.left().toOption(), Either::left);
24 }
25
26 public static <A, B, C> PPrism<Either<A, B>, Either<A, C>, B, C> pRight() {
27 return pPrism(ab -> ab.bimap(Either::left, Function.identity()), Either::right);
28 }
29
30 public static <A, B> Prism<Either<A, B>, B> right() {
31 return Prism.prism(ab -> ab.right().toOption(), Either::right);
32 }
33 }