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 booleananyIsNotNull()check if any of the two values is not nullbooleananyIsNull()check if any values are null<T> voidboth(Consumer<T> pairEntryConsumer)Apply consumer to both entries.booleanbothNotNull()check if both values are not nullbooleanbothNull()check both entries are nullPair<A,B>copy()shallow copy of this pairbooleandifferent()Check if entries are not equalstatic <A,B>
Pair<A,B>empty()Create empty pair of nullsbooleanequals(Object other)Check equality to another pairAfirst()Get first object in pairbooleanfirstNotNull()Check if first is non nullObjectgetEarliestNonNull()inthashCode()booleanisExactlyOneNonNull()static <A,B>
Pair<A,B>of(A valueA, B valueB)Factory methodBsecond()Get second object in pairbooleansecondNotNull()Check if second is non nullStringtoString()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:
hashCodein classObject- Returns:
- hashCode for this entity
- See Also:
Object.hashCode()
-
equals
public boolean equals(Object other)
Check equality to another pair- Overrides:
equalsin classObject- Parameters:
other- pair being compared to- See Also:
Object.equals(java.lang.Object)
-
toString
public String toString()
Convert to string- Overrides:
toStringin 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
-
-