Class Either<FirstType, SecondType>
java.lang.Object
de.s42.base.functional.Either<FirstType, SecondType>
- Type Parameters:
FirstType- Type of the first valueSecondType- Type of the second value
Either is an implementation of the either monad. You can encode either 1 of 2 values (first, second) to be contained.
It provides many powerful ways to interact with it.
- Author:
- Benjamin Schiller
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final FirstTypeStores the first final.private final SecondTypeStores the second final. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateEither(FirstType first, SecondType second) Internal constructor for an Either. -
Method Summary
Modifier and TypeMethodDescriptionvoideither(Consumer<FirstType> actionFirst, Consumer<SecondType> actionSecond) Calls on of the actions.booleanEither is equal if first and second are equal.first()Returns a nullable Optional of first.firstOrElse(FirstType other) Returns first if first is not null or other if first is null.Returns either the first or the second value.Returns a Optional of first or throws a exception if first is null.firstOrThrow(Supplier<ExceptionType> supplier) Returns a Optional of first or throws a customized exception if first is null.inthashCode()The combined hashcode of first and second.voidCalls the action if first is not null with first as parameter.voidifSecond(Consumer<SecondType> action) Calls the action if second is not null with second as parameter.booleanisFirst()Returns true if first is not null.booleanisSecond()Returns true if second is not null.static <FirstType, SecondType>
Either<FirstType, SecondType> ofEither(FirstType first, SecondType second) Create an Either with one of the given values being non null.static <FirstType, SecondType>
Either<FirstType, SecondType> ofFirst(FirstType first) Create an Either with first.static <FirstType, SecondType>
Either<FirstType, SecondType> ofSecond(SecondType second) Create an Either with second.second()Returns a nullable Optional of second.secondOrElse(SecondType other) Returns second if second is not null or other if second is null.Returns a Optional of second or throws a exception if second is null.<ExceptionType extends Throwable>
SecondTypesecondOrThrow(Supplier<ExceptionType> supplier) Returns a Optional of second or throws a customized exception if first is null.Stream<?> stream()Allows to get a stream either streaming first or secondtoString()Returns the first.toString() if first not null otherwise second.toString().
-
Field Details
-
first
Stores the first final. -
second
Stores the second final.
-
-
Constructor Details
-
Either
Internal constructor for an Either.- Parameters:
first- First valuesecond- Second value
-
-
Method Details
-
ofEither
public static <FirstType, SecondType> Either<FirstType, SecondType> ofEither(FirstType first, SecondType second) throws IllegalArgumentException Create an Either with one of the given values being non null.- Type Parameters:
FirstType- Type of the first valueSecondType- Type of the second value- Parameters:
first- First valuesecond- Second value- Returns:
- Either of type <FirstType, SecondType>
- Throws:
IllegalArgumentException- if both or none is null (first != null ^ second != null)
-
ofFirst
public static <FirstType, SecondType> Either<FirstType, SecondType> ofFirst(FirstType first) throws NullPointerException Create an Either with first.- Type Parameters:
FirstType- Type of the first valueSecondType- Type of the second value- Parameters:
first- First value- Returns:
- An Either of first value
- Throws:
NullPointerException- if first is null
-
ofSecond
public static <FirstType, SecondType> Either<FirstType, SecondType> ofSecond(SecondType second) throws NullPointerException Create an Either with second.- Type Parameters:
FirstType- Type of the first valueSecondType- Type of the second value- Parameters:
second- Second value- Returns:
- An Either of second value
- Throws:
NullPointerException- if second is null
-
ifFirst
-
ifSecond
Calls the action if second is not null with second as parameter.- Parameters:
action- Called if second is not null
-
either
Calls on of the actions. Either actionFirst with first as parameter if first is not null or actionSecond with second as parameter.- Parameters:
actionFirst- Called if first is not nullactionSecond- Called if second is not null
-
isFirst
public boolean isFirst()Returns true if first is not null.- Returns:
- True if first is not null
-
isSecond
public boolean isSecond()Returns true if second is not null.- Returns:
- True if second is not null
-
first
-
firstOrThrow
Returns a Optional of first or throws a exception if first is null.- Returns:
- Optional of first or throws a exception if first is null
- Throws:
NullPointerException- If first is null
-
firstOrThrow
public <ExceptionType extends Throwable> FirstType firstOrThrow(Supplier<ExceptionType> supplier) throws ExceptionType Returns a Optional of first or throws a customized exception if first is null.- Type Parameters:
ExceptionType-- Parameters:
supplier-- Returns:
- Optional of first or throws a exception if first is null
- Throws:
ExceptionType- generated if first is null
-
firstOrElse
-
second
Returns a nullable Optional of second.- Returns:
- A nullable Optional of second
-
secondOrThrow
Returns a Optional of second or throws a exception if second is null.- Returns:
- Optional of second or throws a exception if second is null
- Throws:
NullPointerException- If second is null
-
secondOrThrow
public <ExceptionType extends Throwable> SecondType secondOrThrow(Supplier<ExceptionType> supplier) throws ExceptionType Returns a Optional of second or throws a customized exception if first is null.- Type Parameters:
ExceptionType-- Parameters:
supplier-- Returns:
- Optional of second or throws a exception if second is null
- Throws:
ExceptionType- generated if second is null
-
secondOrElse
Returns second if second is not null or other if second is null.- Parameters:
other- Value that will be returned if second is null- Returns:
- Second if second is not null or other if second is null
-
firstOrSecond
Returns either the first or the second value. Allows to unwrap easily.- Returns:
- The first if not null or second
-
stream
Allows to get a stream either streaming first or second- Returns:
- A stream that either streams first or second
-
toString
-
hashCode
-
equals
-