Package org.goplanit.utils.misc
Class Pair<A,B>
- java.lang.Object
-
- org.goplanit.utils.misc.Pair<A,B>
-
- Type Parameters:
A
- first object in pairB
- second object in pair
- Direct Known Subclasses:
ComparablePair
public class Pair<A,B> extends Object
Custom pair class similar to C++. value- Author:
- markr
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
anyIsNotNull()
check if any of the two values is not nullboolean
anyIsNull()
check if any values are null<T> void
both(Consumer<T> pairEntryConsumer)
Apply consumer to both entries.boolean
bothNotNull()
check if both values are not nullboolean
bothNull()
check both entries are nullPair<A,B>
copy()
shallow copy of this pairboolean
different()
Check if entries are not equalstatic <A,B>
Pair<A,B>empty()
Create empty pair of nullsboolean
equals(Object other)
Check equality to another pairA
first()
Get first object in pairboolean
firstNotNull()
Check if first is non nullObject
getEarliestNonNull()
int
hashCode()
boolean
isExactlyOneNonNull()
static <A,B>
Pair<A,B>of(A valueA, B valueB)
Factory methodB
second()
Get second object in pairboolean
secondNotNull()
Check if second is non nullString
toString()
Convert to string
-
-
-
Method Detail
-
of
public static <A,B> Pair<A,B> of(A valueA, B valueB)
Factory method- Type Parameters:
A
- type of valueAB
- type of valueB- Parameters:
valueA
- firstvalueB
- second- Returns:
- new pair
-
empty
public static <A,B> Pair<A,B> empty()
Create empty pair of nulls- Type Parameters:
A
- typeAB
- typeB- Returns:
- null pair
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classObject
- Returns:
- hashCode for this entity
- See Also:
Object.hashCode()
-
equals
public boolean equals(Object other)
Check equality to another pair- Overrides:
equals
in classObject
- Parameters:
other
- pair being compared to- See Also:
Object.equals(java.lang.Object)
-
toString
public String toString()
Convert to string- Overrides:
toString
in classObject
- See Also:
Object.toString()
-
first
public A first()
Get first object in pair- Returns:
- first object in pair
-
second
public B second()
Get second object in pair- Returns:
- second object in pair
-
anyIsNotNull
public boolean anyIsNotNull()
check if any of the two values is not null- Returns:
- true when one of the entries is not null
-
bothNotNull
public boolean bothNotNull()
check if both values are not null- Returns:
- true when both entries are not null
-
anyIsNull
public boolean anyIsNull()
check if any values are null- Returns:
- true when any are null, false otherwise
-
isExactlyOneNonNull
public boolean isExactlyOneNonNull()
- Returns:
- true when exactly one of the two is not null
-
getEarliestNonNull
public Object getEarliestNonNull()
- Returns:
- earliest entry (first before second) that is nonNull, when both are null, null is returned
-
both
public <T> void both(Consumer<T> pairEntryConsumer)
Apply consumer to both entries. Throws ClassCastException when pair contains entries not compatible with type parameter of consumer- Type Parameters:
T
- consumer type assumed to be compatible with both pair entries- Parameters:
pairEntryConsumer
- to apply
-
firstNotNull
public boolean firstNotNull()
Check if first is non null- Returns:
- true when the case, false otherwise
-
secondNotNull
public boolean secondNotNull()
Check if second is non null- Returns:
- true when the case, false otherwise
-
different
public boolean different()
Check if entries are not equal- Returns:
- true when first does not equal second, false otherwise
-
bothNull
public boolean bothNull()
check both entries are null- Returns:
- true when both null, false otherwise
-
-