Class Steps
- java.lang.Object
-
- io.atlassian.fugue.extensions.step.Steps
-
public final class Steps extends java.lang.ObjectThis provides an entry point for a "for-comprehension" styled syntax shortcut of fugueOption,Either,Tryand Java 8Optional.The intention of this comprehension is to provide access to flatMap, map and filter of these monad types in a way that is easier to read and reason about.
A short explanation is:
- Each step must preserve the shape of the original monad. For example if
you begin with an
Optionthen each step must return anOptionwith the final yield producing anOptionresult. - Each step retains access to the previously defined/success values
- On the first empty/failure step result, no further steps will be evaluated
- Filter provides a convenience method to break out of further evaluation
Simple example usage of Steps to produce an
Optionby chaining functions that all have access to the previous values.Option<Integer> result = Steps.begin(some(2)) .then(number -> some(number + 3)) .then((number1, number2) -> some(number1 * number2)) .yield((number1, number2, number3) -> number3 * 4);This yield will produce a
Option.some(A)containing the final value of 40.This is calculated by:
- The initial defined state of the option is 2
- This value of 2 is given parameter name of number for the first step, and is added to 3, producing 5
- The next step accepts both parameters (2 and 5) and multiplies them together, and returns this value of 10
- The final yield accepts all 3 parameters (2, 5, 10) but only cares about the 3rd parameter (10) and multiplies this by 4 to produce 40
- The result option is now a
someof 40
An example of a Steps causing it to break out early, and do no further evaluation is:
Option<Integer> result = Steps.begin(some(2)) .then(number -> none(Integer.class)) .then((number1, number2) -> some(number1 * number2)) .yield((number1, number2, number3) -> number3 * 4);In this example, the result will be
noneand the functions after the empty state is returned will not be evaluated.While each step provides access to the previous values, if these are not required, then it is possible to evaluate a new value using a Supplier at each step. For example, the following is possible:
Option<Integer> result = Steps.begin(some(2)) .then(() -> some(5)) .then((number1, number2) -> some(number1 * number2)) .yield((number1, number2, number3) -> number3 * 4);In this case we have decided that the first step will evaluate its result without requiring the previous state, and just return 5. This final result will still be a
someof 40.Filter is another method to break out of further evaulations and return the empty result. The following example, illustrates this:
Option<Integer> result = Steps.begin(some(2)) .then(number -> some(number + 3)) .filter((number1, number2) -> number2 == 0) .then((number1, number2) -> some(number1 * number2)) .yield((number1, number2, number3) -> number3 * 4);In this contrived example, the filter predicate will return false as number2 will always be 5, and never 0. This will result in that steps result being changed to the empty value, and prevent any further step evaluation.
NB: It is possible to chain together up to 6 steps
- Each step must preserve the shape of the original monad. For example if
you begin with an
-
-
Constructor Summary
Constructors Modifier Constructor Description privateSteps()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <A,LEFT>
EitherStep1<A,LEFT>begin(Either<LEFT,A> either)Begin a new Steps expresison, using theEithertype.static <A> OptionStep1<A>begin(Option<A> option)Begin a new Steps expresison, using theOptiontype.static <A> TryStep1<A>begin(Try<A> aTry)Begin a new Steps expresison, using theTrytype.static <A> OptionalStep1<A>begin(java.util.Optional<A> optional)Begin a new Steps expresison, using theOptionaltype.
-
-
-
Method Detail
-
begin
public static <A,LEFT> EitherStep1<A,LEFT> begin(Either<LEFT,A> either)
Begin a new Steps expresison, using theEithertype.This assumes a Right-Biased Either, and will therefore continue evaluating steps on
rightresults, and break out early on the firstleftresult.- Type Parameters:
A- The right hand side type for the initialEitherLEFT- The left hand side type for the initialEither- Parameters:
either- The start value for this steps.- Returns:
- A class allowing continuation of this first Step
- See Also:
Either,for description of Steps usage with examples
-
begin
public static <A> OptionStep1<A> begin(Option<A> option)
Begin a new Steps expresison, using theOptiontype.This will continue evaluating steps on
someresults, and break out early on the firstnoneresult.- Type Parameters:
A- The type for the initialOption- Parameters:
option- The start value for this steps.- Returns:
- A class allowing continuation of this first Step
- See Also:
Option,for description of Steps usage with examples
-
begin
public static <A> OptionalStep1<A> begin(java.util.Optional<A> optional)
Begin a new Steps expresison, using theOptionaltype.This will continue evaluating steps on
ofresults, and break out early on the firstemptyresult.- Type Parameters:
A- The type for the initialOptional- Parameters:
optional- The start value for this steps.- Returns:
- A class allowing continuation of this first Step
- See Also:
Optional,for description of Steps usage with examples
-
begin
public static <A> TryStep1<A> begin(Try<A> aTry)
Begin a new Steps expresison, using theTrytype.This will continue evaluating steps on
successresults, and break out early on the firstfailureresult.- Type Parameters:
A- The type for the initialTry- Parameters:
aTry- The start value for this steps.- Returns:
- A class allowing continuation of this first Step
- See Also:
Try,for description of Steps usage with examples
-
-