Package com.github.skopylov58.functional
Interface Either<L,R>
- Type Parameters:
L- left side typeR- right side type
- All Known Implementing Classes:
Either.Left,Either.Right
Minimal functional Either implementation.
It does not have left or right projections, map/flatMap/filter operations work only on
right side of Either and do not have effect for left side. Anyway you can use swap() for this purpose.
Either intentionally does not have any getX() methods, use fold(Function, Function),
optional() or stream() instead.
Either has a set of catching(CheckedConsumer) higher order functions which will be useful for exception handling.
var socket = catching(() -> new Socket("foo", 1234));
try (var c = socket.asCloseable()) {
socket.flatMap(catching(Socket::getOutputStream))
.flatMap(catching(o -> {
o.write(new byte[] {1,2,3});
}));
}
}));
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordLeft side of Either.static final recordRight side of Either. -
Method Summary
Modifier and TypeMethodDescriptionProduces side effects for Eitherdefault CloseableConverts this Either to Closeable which is handy to use in try-with-resources block.catching(Try.CheckedConsumer<? super R> consumer) Lifts throwing consumer to totalR->Either<Exception,R>function.catching(Try.CheckedFunction<? super R, ? extends T> mapper) Lifts throwingR->Tfunction to totalR->Either<Exception,R>function.catching(Try.CheckedSupplier<? extends R> supplier) Factory method to produceEither<Exception,R>from throwing supplierFilters right side of Either, does not have effect if it is left side.Flat maps right side of the Either.<T> TFolds Either to type Tdefault booleanisLeft()Checks if this is left side.default booleanisRight()Checks if this is right side.static <L,R> Either<L, R> left(L left) Factory method to produce left side of Either from L valueMaps right side of the Either.optional()Converts Either to Optionalstatic <L,R> Either<L, R> right(R right) Factory method to produce right side of Either from R valuestream()Converts Either to Streamswap()Swaps Either
-
Method Details
-
isLeft
default boolean isLeft()Checks if this is left side.- Returns:
- true is this is left side of Either.
-
isRight
default boolean isRight()Checks if this is right side.- Returns:
- true is this is right side of Either.
-
stream
Converts Either to Stream- Returns:
- one element stream for right, empty stream for left.
-
optional
Converts Either to Optional- Returns:
- Optional for right, Optional empty for left.
-
map
Maps right side of the Either.- Type Parameters:
T- new right type- Parameters:
mapper- mapper function that maps R type to T type- Returns:
- new Either
-
flatMap
default <T> Either<L,T> flatMap(Function<? super R, ? extends Either<? extends L, ? extends T>> mapper) Flat maps right side of the Either.- Type Parameters:
T- new right type- Parameters:
mapper- R toEither<L,T>- Returns:
- new
Either<L,T>
-
filter
default Either<L,R> filter(Predicate<? super R> predicate, Function<? super R, ? extends L> leftMapper) Filters right side of Either, does not have effect if it is left side.- Parameters:
predicate- condition to testleftMapper- will map R to L if predicate return false- Returns:
- Either
-
filter
-
swap
Swaps Either- Returns:
- swapped Either
-
fold
Folds Either to type T- Type Parameters:
T- folded type- Parameters:
leftMapper- maps left to TrightMapper- maps right to T- Returns:
- value of T type
-
accept
Produces side effects for Either- Parameters:
leftConsumer- left side effectrightConsumer- right side effect- Returns:
- this Either
-
right
Factory method to produce right side of Either from R value- Type Parameters:
R- type of right sideL- type of left side- Parameters:
right- right value- Returns:
Either.Right
-
left
Factory method to produce left side of Either from L value- Type Parameters:
R- type of right sideL- type of left side- Parameters:
left- left value- Returns:
Either.Left
-
catching
Factory method to produceEither<Exception,R>from throwing supplier- Type Parameters:
R- right side type- Parameters:
supplier- throwing supplier of R- Returns:
- Either
-
catching
static <R,T> Function<R,Either<Exception, catchingT>> (Try.CheckedFunction<? super R, ? extends T> mapper) Lifts throwingR->Tfunction to totalR->Either<Exception,R>function. May be useful with flatMap.- Type Parameters:
R- right side typeT- left side type- Parameters:
mapper- throwing function- Returns:
- lifted function
-
catching
Lifts throwing consumer to totalR->Either<Exception,R>function.- Type Parameters:
R- right side type- Parameters:
consumer-- Returns:
- lifted function
-
asCloseable
Converts this Either to Closeable which is handy to use in try-with-resources block.- Returns:
- Closeable
-