View Javadoc
1   package io.atlassian.fugue.extensions.step;
2   
3   import io.atlassian.fugue.Either;
4   import io.atlassian.fugue.extensions.functions.Function6;
5   import io.atlassian.fugue.extensions.functions.Predicate6;
6   
7   import java.util.function.Supplier;
8   
9   /**
10   * The sixth step of the {@link Either} type.
11   * <p>
12   * This class is not intended to be contructed manually, and should only be used
13   * as part of a {@link Steps} chain, started by {@link Steps#begin(Either)}
14   *
15   * @param <A> The right hand side type of the first defined right value
16   * @param <B> The right hand side type of the second defined right value
17   * @param <C> The right hand side type of the third defined right value
18   * @param <D> The right hand side type of the fourth defined right value
19   * @param <E> The right hand side type of the fifth defined right value
20   * @param <F> The right hand side type of the sixth defined right value
21   * @param <LEFT> The left hand side type of the Either result
22   * @see Steps for usage examples
23   * @see Either
24   * @since 4.7.0
25   */
26  public final class EitherStep6<A, B, C, D, E, F, LEFT> {
27    private final Either<LEFT, A> either1;
28    private final Either<LEFT, B> either2;
29    private final Either<LEFT, C> either3;
30    private final Either<LEFT, D> either4;
31    private final Either<LEFT, E> either5;
32    private final Either<LEFT, F> either6;
33  
34    EitherStep6(Either<LEFT, A> either1, Either<LEFT, B> either2, Either<LEFT, C> either3, Either<LEFT, D> either4, Either<LEFT, E> either5,
35      Either<LEFT, F> either6) {
36      this.either1 = either1;
37      this.either2 = either2;
38      this.either3 = either3;
39      this.either4 = either4;
40      this.either5 = either5;
41      this.either6 = either6;
42    }
43  
44    /**
45     * Apply the provided predicate with the previous step results.
46     * <p>
47     * If the predicate is not satisfied then the unsatisfiedSupplier is used to
48     * populate the left value that will prevent any further steps evaluation.
49     *
50     * @param predicate The check that must be satisfied by contained values
51     * @param unsatisfiedSupplier Provide the value to populate the left if not
52     * satisfied
53     * @return This step class with either the same last step value, or changed to
54     * a left
55     */
56    public EitherStep6<A, B, C, D, E, F, LEFT> filter(Predicate6<? super A, ? super B, ? super C, ? super D, ? super E, ? super F> predicate,
57      Supplier<? extends LEFT> unsatisfiedSupplier) {
58      Either<LEFT, F> filterEither6 = either1.flatMap(value1 -> either2.flatMap(value2 -> either3.flatMap(value3 -> either4.flatMap(value4 -> either5
59        .flatMap(value5 -> either6.filterOrElse(value6 -> predicate.test(value1, value2, value3, value4, value5, value6), unsatisfiedSupplier))))));
60      return new EitherStep6<>(either1, either2, either3, either4, either5, filterEither6);
61    }
62  
63    /**
64     * Terminating step expression, that will provide the previous steps to this
65     * function and return the result as a <code>Right</code>
66     *
67     * @param functor The yield function to map on previous values
68     * @param <Z> The right hand side type for the returned result
69     * @return An Either with the result of this function on right, or the
70     * existing left
71     */
72    public <Z> Either<LEFT, Z> yield(Function6<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, Z> functor) {
73      return either1.flatMap(value1 -> either2.flatMap(value2 -> either3.flatMap(value3 -> either4.flatMap(value4 -> either5.flatMap(value5 -> either6
74        .map(value6 -> functor.apply(value1, value2, value3, value4, value5, value6)))))));
75    }
76  
77  }