View Javadoc
1   package io.atlassian.fugue.extensions.step;
2   
3   import io.atlassian.fugue.extensions.functions.Function6;
4   import io.atlassian.fugue.extensions.functions.Predicate6;
5   
6   import java.util.Optional;
7   
8   /**
9    * The sixth step of the {@link Optional} type.
10   * <p>
11   * This class is not intended to be contructed manually, and should only be used
12   * as part of a {@link Steps} chain, started by {@link Steps#begin(Optional)}
13   *
14   * @param <A> The type of the first defined value
15   * @param <B> The type of the second defined value
16   * @param <C> The type of the third defined value
17   * @param <D> The type of the fourth defined value
18   * @param <E> The type of the fifth defined value
19   * @param <F> The type of the sixth defined value
20   * @see Steps for usage examples
21   * @see Optional
22   * @since 4.7.0
23   */
24  @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public final class OptionalStep6<A, B, C, D, E, F> {
25    private final Optional<A> optional1;
26    private final Optional<B> optional2;
27    private final Optional<C> optional3;
28    private final Optional<D> optional4;
29    private final Optional<E> optional5;
30    private final Optional<F> optional6;
31  
32    OptionalStep6(Optional<A> optional1, Optional<B> optional2, Optional<C> optional3, Optional<D> optional4, Optional<E> optional5,
33      Optional<F> optional6) {
34      this.optional1 = optional1;
35      this.optional2 = optional2;
36      this.optional3 = optional3;
37      this.optional4 = optional4;
38      this.optional5 = optional5;
39      this.optional6 = optional6;
40    }
41  
42    /**
43     * Apply the provided predicate with the previous step results.
44     *
45     * @param predicate The check that must be satisfied by contained values
46     * @return This step class with either the same last step value, or changed to
47     * empty
48     */
49    public OptionalStep6<A, B, C, D, E, F> filter(Predicate6<? super A, ? super B, ? super C, ? super D, ? super E, ? super F> predicate) {
50      Optional<F> filterOptional6 = optional1.flatMap(value1 -> optional2.flatMap(value2 -> optional3.flatMap(value3 -> optional4
51        .flatMap(value4 -> optional5.flatMap(value5 -> optional6.filter(value6 -> predicate.test(value1, value2, value3, value4, value5, value6)))))));
52      return new OptionalStep6<>(optional1, optional2, optional3, optional4, optional5, filterOptional6);
53    }
54  
55    /**
56     * Terminating step expression, that will provide the previous steps to this
57     * function and return the result as a <code>of</code>
58     *
59     * @param functor The yield function to map on previous values
60     * @param <Z> The type for the returned result
61     * @return An Optional containing this result or empty
62     */
63    public <Z> Optional<Z> yield(Function6<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, Z> functor) {
64      return optional1.flatMap(value1 -> optional2.flatMap(value2 -> optional3.flatMap(value3 -> optional4.flatMap(value4 -> optional5
65        .flatMap(value5 -> optional6.map(value6 -> functor.apply(value1, value2, value3, value4, value5, value6)))))));
66    }
67  }