View Javadoc

1   package io.atlassian.fugue.optic;
2   
3   import io.atlassian.fugue.Either;
4   import io.atlassian.fugue.Option;
5   import io.atlassian.fugue.Pair;
6   
7   import java.util.function.Function;
8   import java.util.function.Supplier;
9   import java.util.stream.Stream;
10  
11  /**
12   * {@link PLens} with a monomorphic set function
13   */
14  public final class Lens<S, A> extends PLens<S, S, A, A> {
15  
16    final PLens<S, S, A, A> pLens;
17  
18    public Lens(final PLens<S, S, A, A> pLens) {
19      this.pLens = pLens;
20    }
21  
22    @Override public A get(final S s) {
23      return pLens.get(s);
24    }
25  
26    @Override public Function<S, S> set(final A a) {
27      return pLens.set(a);
28    }
29  
30    @Override public <C> Function<S, Function<C, S>> modifyFunctionF(final Function<A, Function<C, A>> f) {
31      return pLens.modifyFunctionF(f);
32    }
33  
34    @Override public <L> Function<S, Either<L, S>> modifyEitherF(final Function<A, Either<L, A>> f) {
35      return pLens.modifyEitherF(f);
36    }
37  
38    @Override public Function<S, Option<S>> modifyOptionF(final Function<A, Option<A>> f) {
39      return pLens.modifyOptionF(f);
40    }
41  
42    @Override public Function<S, Iterable<S>> modifyIterableF(final Function<A, Iterable<A>> f) {
43      return pLens.modifyIterableF(f);
44    }
45  
46    @Override public Function<S, Supplier<S>> modifySupplierF(final Function<A, Supplier<A>> f) {
47      return pLens.modifySupplierF(f);
48    }
49  
50    @Override public Function<S, Pair<S, S>> modifyPairF(final Function<A, Pair<A, A>> f) {
51      return pLens.modifyPairF(f);
52    }
53  
54    @Override public Function<S, S> modify(final Function<A, A> f) {
55      return pLens.modify(f);
56    }
57  
58    /**
59     * join two {@link Lens} with the same target
60     */
61    public final <S1> Lens<Either<S, S1>, A> sum(final Lens<S1, A> other) {
62      return new Lens<>(pLens.sum(other.pLens));
63    }
64  
65    /**********************************************************/
66    /** Compose methods between a {@link Lens} and another Optics */
67    /**********************************************************/
68  
69    /**
70     * compose a {@link Lens} with a {@link Setter}
71     */
72    public final <C> Setter<S, C> composeSetter(final Setter<A, C> other) {
73      return new Setter<>(pLens.composeSetter(other.pSetter));
74    }
75  
76    /**
77     * compose a {@link Lens} with a {@link Traversal}
78     */
79    public final <C> Traversal<S, C> composeTraversal(final Traversal<A, C> other) {
80      return new Traversal<>(pLens.composeTraversal(other.pTraversal));
81    }
82  
83    /**
84     * compose a {@link Lens} with an {@link Optional}
85     */
86    public final <C> Optional<S, C> composeOptional(final Optional<A, C> other) {
87      return new Optional<>(pLens.composeOptional(other.pOptional));
88    }
89  
90    /**
91     * compose a {@link Lens} with a {@link Prism}
92     */
93    public final <C> Optional<S, C> composePrism(final Prism<A, C> other) {
94      return new Optional<>(pLens.composePrism(other.pPrism));
95    }
96  
97    /**
98     * compose a {@link Lens} with a {@link Lens}
99     */
100   public final <C> Lens<S, C> composeLens(final Lens<A, C> other) {
101     return new Lens<>(pLens.composeLens(other.pLens));
102   }
103 
104   /**
105    * compose a {@link Lens} with an {@link Iso}
106    */
107   public final <C> Lens<S, C> composeIso(final Iso<A, C> other) {
108     return new Lens<>(pLens.composeIso(other.pIso));
109   }
110 
111   /****************************************************************/
112   /** Transformation methods to view a {@link Lens} as another Optics */
113   /****************************************************************/
114 
115   /**
116    * view a {@link Lens} as a {@link Setter}
117    */
118   @Override public Setter<S, A> asSetter() {
119     return new Setter<>(pLens.asSetter());
120   }
121 
122   /**
123    * view a {@link Lens} as a {@link Traversal}
124    */
125   @Override public final Traversal<S, A> asTraversal() {
126     return new Traversal<>(pLens.asTraversal());
127   }
128 
129   /**
130    * view a {@link Lens} as an {@link Optional}
131    */
132   @Override public final Optional<S, A> asOptional() {
133     return new Optional<>(pLens.asOptional());
134   }
135 
136   public static <S> Lens<S, S> id() {
137     return new Lens<>(PLens.pId());
138   }
139 
140   /**
141    * create a {@link Lens} using a pair of functions: one to get the target, one
142    * to set the target.
143    */
144   public static <S, A> Lens<S, A> lens(final Function<S, A> get, final Function<A, Function<S, S>> set) {
145     return new Lens<>(PLens.pLens(get, set));
146   }
147 
148 }