<dependency>
<groupId>com.codepoetics</groupId>
<artifactId>ambivalence</artifactId>
<version>0.2</version>
</dependency>
An Either
type for Java 8, in case you needed one.
Features:
join
andforEither
to access values safely.- left and right projections, with
map
andflatMap
. equals
,hashCode
andtoString
implemented.Tryable
wraps an exception-throwing lambda so that it returnsEither<T, Exception>
.Eithers
providesStream
collectors which split a stream ofEither
values and optionally collect the left values.
Either<String, Integer> stringOrInt1 = Either.ofLeft("a string");
Either<String, Integer> stringOrInt2 = Either.ofRight(23);
System.out.println(stringOrInt1.join(String::toUpperCase, Object::toString)); // prints "A STRING"
System.out.println(stringOrInt2.join(String::toUpperCase, Object::toString)); // prints "23"