View Javadoc
1   package io.atlassian.fugue.extensions.step;
2   
3   import io.atlassian.fugue.Try;
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 Try} 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(Try)}
14   *
15   * @param <A> The type of the first defined value
16   * @param <B> The type of the second defined value
17   * @param <C> The type of the third defined value
18   * @param <D> The type of the fourth defined value
19   * @param <E> The type of the fifth defined value
20   * @param <E> The type of the sixth defined value
21   * @see Steps for usage examples
22   * @see Try
23   * @since 4.7.0
24   */
25  public final class TryStep6<A, B, C, D, E, F> {
26    private final Try<A> try1;
27    private final Try<B> try2;
28    private final Try<C> try3;
29    private final Try<D> try4;
30    private final Try<E> try5;
31    private final Try<F> try6;
32  
33    TryStep6(Try<A> try1, Try<B> try2, Try<C> try3, Try<D> try4, Try<E> try5, Try<F> try6) {
34      this.try1 = try1;
35      this.try2 = try2;
36      this.try3 = try3;
37      this.try4 = try4;
38      this.try5 = try5;
39      this.try6 = try6;
40    }
41  
42    /**
43     * Apply the provided predicate with the previous step results.
44     * <p>
45     * If the predicate is not satisfied then the unsatisfiedSupplier is used to
46     * populate the failure value that will prevent any further steps evaluation.
47     *
48     * @param predicate The check that must be satisfied by contained values
49     * @param unsatisfiedSupplier Provide the value to populate the failure if not
50     * satisfied
51     * @return This step class with either the same last step value, or changed to
52     * a failure
53     */
54    public TryStep6<A, B, C, D, E, F> filter(Predicate6<? super A, ? super B, ? super C, ? super D, ? super E, ? super F> predicate,
55      Supplier<Exception> unsatisfiedSupplier) {
56      Try<F> filterTry6 = try1.flatMap(value1 -> try2.flatMap(value2 -> try3.flatMap(value3 -> try4.flatMap(value4 -> try5.flatMap(value5 -> try6
57        .filterOrElse(value6 -> predicate.test(value1, value2, value3, value4, value5, value6), unsatisfiedSupplier))))));
58      return new TryStep6<>(try1, try2, try3, try4, try5, filterTry6);
59    }
60  
61    /**
62     * Terminating step expression, that will provide the previous steps to this
63     * function and return the result as a <code>Success</code>
64     *
65     * @param functor The yield function to map on previous values
66     * @param <Z> The type for the returned result
67     * @return A Try containing this result as success or failure
68     */
69    public <Z> Try<Z> yield(Function6<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, Z> functor) {
70      return try1.flatMap(value1 -> try2.flatMap(value2 -> try3.flatMap(value3 -> try4.flatMap(value4 -> try5.flatMap(value5 -> try6
71        .map(value6 -> functor.apply(value1, value2, value3, value4, value5, value6)))))));
72    }
73  }