1 package io.atlassian.fugue.optic.std;
2
3 import io.atlassian.fugue.Pair;
4 import io.atlassian.fugue.optic.Iso;
5 import io.atlassian.fugue.optic.Lens;
6 import io.atlassian.fugue.optic.PLens;
7
8 import java.util.AbstractMap;
9 import java.util.Map;
10
11 import static io.atlassian.fugue.Pair.pair;
12 import static io.atlassian.fugue.optic.PLens.pLens;
13
14 public final class PairOptics {
15
16 private PairOptics() {}
17
18 public static <A, B, C> PLens<Pair<A, B>, Pair<C, B>, A, C> pLeft() {
19 return pLens(Pair::left, c -> ab -> pair(c, ab.right()));
20 }
21
22 public static <A, B> Lens<Pair<A, B>, A> left() {
23 return new Lens<>(pLeft());
24 }
25
26 public static <A, B, C> PLens<Pair<A, B>, Pair<A, C>, B, C> pRight() {
27 return pLens(Pair::right, c -> ab -> pair(ab.left(), c));
28 }
29
30 public static <A, B> Lens<Pair<A, B>, B> _right() {
31 return new Lens<>(pRight());
32 }
33
34 public static <A, B> Iso<Pair<A, B>, Map.Entry<A, B>> pairToEntry() {
35 return Iso.iso(p -> new AbstractMap.SimpleImmutableEntry<>(p.left(), p.right()), e -> pair(e.getKey(), e.getValue()));
36 }
37 }