Class Pair<FirstType, SecondType>
java.lang.Object
de.s42.base.functional.Pair<FirstType, SecondType>
- Type Parameters:
FirstType-SecondType-
A pair of 2 values. Both types can be null. It provides handy access.
- Author:
- Benjamin Schiller
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final FirstTypeStores the first final.private final SecondTypeStores the second final. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePair(FirstType first, SecondType second) Constructs a pair of first and second. -
Method Summary
Modifier and TypeMethodDescriptionvoideach(Consumer<FirstType> actionFirst, Consumer<SecondType> actionSecond) Calls the action if first is not null with first as parameter.static <FirstType, SecondType>
Pair<FirstType, SecondType> empty()Create an empty Pair with both of the given values being null.booleanEither is equal if first and second are equal.first()Returns the optional of first valuefirstOrElse(FirstType other) Returns first if first is not null or other if first is null.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>
Pair<FirstType, SecondType> of(FirstType first, SecondType second) Create a Pair with both of the given values being non null.static <FirstType, SecondType>
Pair<FirstType, SecondType> ofFirst(FirstType first) Create a Pair with first set and second being nullstatic <FirstType, SecondType>
Pair<FirstType, SecondType> ofNullable(FirstType first, SecondType second) Create a Pair with both of the given values being null or non null.static <FirstType, SecondType>
Pair<FirstType, SecondType> ofSecond(SecondType second) Create a Pair with second set and first being nullsecond()Returns the optional of second valuesecondOrElse(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 to streaming first then second (only contains them if non null)toString()Returns a string in the form '[first, second]'
-
Field Details
-
first
Stores the first final. -
second
Stores the second final.
-
-
Constructor Details
-
Pair
Constructs a pair of first and second. Both values may be null- Parameters:
first- First valuesecond- Second value
-
-
Method Details
-
empty
Create an empty Pair with both of the given values being null.- Type Parameters:
FirstType- Type of the first valueSecondType- Type of the second value- Returns:
- An empty Pair
-
of
public static <FirstType, SecondType> Pair<FirstType, SecondType> of(FirstType first, SecondType second) throws NullPointerException Create a Pair with both 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:
- Pair of type <FirstType, SecondType>
- Throws:
NullPointerException- if either of first or second is null
-
ofFirst
public static <FirstType, SecondType> Pair<FirstType, SecondType> ofFirst(FirstType first) throws NullPointerException Create a Pair with first set and second being null- Type Parameters:
FirstType- Type of the first valueSecondType- Type of the second value- Parameters:
first- First value- Returns:
- Pair of type <FirstType, SecondType>
- Throws:
NullPointerException- if first is null
-
ofSecond
public static <FirstType, SecondType> Pair<FirstType, SecondType> ofSecond(SecondType second) throws NullPointerException Create a Pair with second set and first being null- Type Parameters:
FirstType- Type of the first valueSecondType- Type of the second value- Parameters:
second- Second value- Returns:
- Pair of type <FirstType, SecondType>
- Throws:
NullPointerException- if second is null
-
ofNullable
public static <FirstType, SecondType> Pair<FirstType, SecondType> ofNullable(FirstType first, SecondType second) Create a Pair with both of the given values being null or 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>
-
each
Calls the action if first is not null with first as parameter.- Parameters:
actionFirst- Called if first is not nullactionSecond- Called if second is not null
-
ifFirst
-
ifSecond
Calls the action if second is not null with second as parameter.- Parameters:
action- 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 the optional of second value- Returns:
- Optional of second value
-
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
-
stream
Allows to get a stream to streaming first then second (only contains them if non null)- Returns:
- A stream that streams first then second
-
toString
-
hashCode
-
equals
-