diff --git a/README.org b/README.org index b2ec557..694f6d1 100644 --- a/README.org +++ b/README.org @@ -722,3 +722,86 @@ #+END_SRC +** Either + + Allows handling a value that can be one of two types, a left value/type or a + right value/type. + + When an =Either= is returned from a method it will contain either a left or a + right. + + Where the =Either= is used to represent success/failure, the left case is, by + convention, used to indicate the error, and right the success. An alternative + is to use the =Result= which more clearly distinguishes success from failure. + + =Either= is not a Monad. + +*** Static Constructors + +**** =static Either left(final L l)= + + Create a new Either holding a left value. + + #+BEGIN_SRC java + final Either left = Either.left(getIntegerValue()); + #+END_SRC + +**** =static Either right(final R r)= + + Create a new Either holding a right value. + + #+BEGIN_SRC java + final Either right = Either.right(getStringValue()); + #+END_SRC + + +*** Instance Methods + +**** =boolean isLeft()= + + Checks if the Either holds a left value. + + #+BEGIN_SRC java + final boolean leftIsLeft = Either.left(getIntegerValue()).isLeft(); + final boolean rightIsLeft = Either.right(getStringValue()).isLeft(); + #+END_SRC + +**** =boolean isRight()= + + Checks if the Either holds a right value. + + #+BEGIN_SRC java + final boolean leftIsRight = Either.left(getIntegerValue()).isRight(); + final boolean rightIsRight = Either.right(getStringValue()).isRight(); + #+END_SRC + + +**** =void match(Consumer onLeft, Consumer onRight)= + + Matches the Either, invoking the correct Consumer. + + #+BEGIN_SRC java + Either.left(getIntegerValue()) + .match( + left -> handleIntegerValue(left), + right -> handleStringValue(right) + ); + #+END_SRC + +**** = Either mapLeft(Function f)= + + Map the function across the left value. + + #+BEGIN_SRC java + final Either either = Either.left(getIntegerValue()) + .mapLeft(i -> i.doubleValue()); + #+END_SRC + +**** = Either mapRight(Function f)= + + Map the function across the right value. + + #+BEGIN_SRC java + final Either either = Either.left(getIntegerValue()) + .mapRight(s -> s + "x"); + #+END_SRC