java.lang.Object
de.s42.base.functional.Either<FirstType,SecondType>
- Typparameter:
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.
- Autor:
- Benjamin Schiller
-
Feldübersicht
Modifizierer und TypFeldBeschreibungprivate final FirstType
Stores the first final.private final SecondType
Stores the second final. -
Konstruktorübersicht
ModifiziererKonstruktorBeschreibungprivate
Either
(FirstType first, SecondType second) Internal constructor for an Either. -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungvoid
either
(Consumer<FirstType> actionFirst, Consumer<SecondType> actionSecond) Calls on of the actions.boolean
Either 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.int
hashCode()
The combined hashcode of first and second.void
Calls the action if first is not null with first as parameter.void
ifSecond
(Consumer<SecondType> action) Calls the action if second is not null with second as parameter.boolean
isFirst()
Returns true if first is not null.boolean
isSecond()
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.Stream
<?> stream()
Allows to get a stream either streaming first or secondtoString()
Returns the first.toString() if first not null otherwise second.toString().
-
Felddetails
-
first
Stores the first final. -
second
Stores the second final.
-
-
Konstruktordetails
-
Either
Internal constructor for an Either.- Parameter:
first
- First valuesecond
- Second value
-
-
Methodendetails
-
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.- Typparameter:
FirstType
- Type of the first valueSecondType
- Type of the second value- Parameter:
first
- First valuesecond
- Second value- Gibt zurück:
- Either of type <FirstType, SecondType>
- Löst aus:
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.- Typparameter:
FirstType
- Type of the first valueSecondType
- Type of the second value- Parameter:
first
- First value- Gibt zurück:
- An Either of first value
- Löst aus:
NullPointerException
- if first is null
-
ofSecond
public static <FirstType,SecondType> Either<FirstType,SecondType> ofSecond(SecondType second) throws NullPointerException Create an Either with second.- Typparameter:
FirstType
- Type of the first valueSecondType
- Type of the second value- Parameter:
second
- Second value- Gibt zurück:
- An Either of second value
- Löst aus:
NullPointerException
- if second is null
-
ifFirst
-
ifSecond
Calls the action if second is not null with second as parameter.- Parameter:
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.- Parameter:
actionFirst
- Called if first is not nullactionSecond
- Called if second is not null
-
isFirst
public boolean isFirst()Returns true if first is not null.- Gibt zurück:
- True if first is not null
-
isSecond
public boolean isSecond()Returns true if second is not null.- Gibt zurück:
- True if second is not null
-
first
-
firstOrThrow
Returns a Optional of first or throws a exception if first is null.- Gibt zurück:
- Optional of first or throws a exception if first is null
- Löst aus:
NullPointerException
- If first is null
-
firstOrElse
-
second
Returns a nullable Optional of second.- Gibt zurück:
- A nullable Optional of second
-
secondOrThrow
Returns a Optional of second or throws a exception if second is null.- Gibt zurück:
- Optional of second or throws a exception if second is null
- Löst aus:
NullPointerException
- If second is null
-
secondOrElse
Returns second if second is not null or other if second is null.- Parameter:
other
- Value that will be returned if second is null- Gibt zurück:
- 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.- Gibt zurück:
- The first if not null or second
-
stream
Allows to get a stream either streaming first or second- Gibt zurück:
- A stream that either streams first or second
-
toString
-
hashCode
-
equals
-