Record Class Pair<F,S>
java.lang.Object
java.lang.Record
io.github.matyrobbrt.curseforgeapi.util.Pair<F,S>
- Type Parameters:
F
- the type of the first element.S
- the type of the second element.
A pair of elements.
This record gives access to a pair of elements F, S, where F is the first() element and S is the second() element. Pairs are immutable.
Mappers return a new pair instance, and such are chainable:
pair.mapFirst(0).mapSecond(12); // this pair now has the first value 0, and the second one 12
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaccept
(BiConsumer<F, S> consumer) Accepts the givenconsumer
on the values from the pairacceptFirst
(Consumer<F> consumer) Accepts the givenconsumer
on the first value of the pair.acceptSecond
(Consumer<S> consumer) Accepts the givenconsumer
on the second value of the pair.Clears the first value of the pair, by making itnull
.Clears the second value of the pair, by making itnull
.static <F,
S> Pair<F, S> empty()
Makes an empty pair, which has each valuenull
.final boolean
Indicates whether some other object is "equal to" this one.first()
Returns the value of thefirst
record component.static <T> Predicate<T>
getIsPresentPredicate
(Class<T> clazz) Gets the"Is Present"
predicate for the specified class.final int
hashCode()
Returns a hash code value for this object.<T> T
map
(BiFunction<F, S, T> mapper) Maps the pair to another value<NEW_FIRST,
NEW_SECOND>
Pair<NEW_FIRST,NEW_SECOND> Maps the pair's values to new ones.Maps the first value to another value.mapFirst
(NEW_FIRST newFirst) Maps the first value to another value.mapResponses
(Pair<Response<F>, Response<S>> pair) Maps a pair containing 2 responses into an optional that may have a pair containing the values of both responses, if both are present.Maps the second value to another value.mapSecond
(NEW_SECOND newSecond) Maps the second value to another value.static <F,
S> Pair<F, S> of
(F first, S second) Makes a pair from the given valuestatic <T> void
registerIsPresentPredicate
(Class<T> clazz, Predicate<T> predicate) Register an"Is Present"
predicate for the specified class.second()
Returns the value of thesecond
record component.swap()
Swaps the order of the values.Maps this pair to aAbstractMap.SimpleImmutableEntry
containing the pair's values in a First-Second order.Maps this pair to aAbstractMap.SimpleImmutableEntry
containing the pair's values in a Second-First order.final String
toString()
Returns a string representation of this record class.
-
Constructor Details
-
Pair
Creates an instance of aPair
record class.- Parameters:
first
- the value for thefirst
record componentsecond
- the value for thesecond
record component
-
-
Method Details
-
registerIsPresentPredicate
Register an"Is Present"
predicate for the specified class.- Type Parameters:
T
- the type to register a predicate for- Parameters:
clazz
- the class of the object to register the predicate forpredicate
- the predicate to register
-
getIsPresentPredicate
Gets the"Is Present"
predicate for the specified class.
The return value is never null, as if theIS_PRESENT_PREDICATES
doesn't contain an entry for the specified type, a predicate returningtrue
is returned.- Type Parameters:
T
- the type of the object whose predicate to get- Parameters:
clazz
- the class of the object whose predicate to get- Returns:
- the predicate
-
of
Makes a pair from the given value- Type Parameters:
F
- the type of the first valueS
- the type of the second value- Parameters:
first
- the first valuesecond
- the second value- Returns:
- the newly created pair
-
empty
Makes an empty pair, which has each valuenull
.- Type Parameters:
F
- the type of the first valueS
- the type of the second value- Returns:
- the empty pair.
-
mapResponses
Maps a pair containing 2 responses into an optional that may have a pair containing the values of both responses, if both are present.
This is especially useful when making multiple requests usingAsyncRequest.and(AsyncRequest)
because the values returned by the 2 async requests are merged into a pair. Example:final Response<String> firstResponse = Response.of("I exist", 200); final Response<Integer> secondResponse = Response.empty(400); final Pair<Response<String>, Response<Integer>> responsePair = Pair.of(firstResponse, secondResponse); Pair.mapResponses(responsePair).ifPresent(pair -> pair.accept((string, integerValue) -> { System.out.println(string + ": " + integerValue); }));
- Type Parameters:
F
- the first response typeS
- the second response type- Parameters:
pair
- the pair containing the responses to map- Returns:
- the optional
-
accept
Accepts the givenconsumer
on the values from the pair- Parameters:
consumer
- the consumer- Returns:
- the pair, for chaining
-
acceptFirst
Accepts the givenconsumer
on the first value of the pair.- Parameters:
consumer
- the consumer- Returns:
- the pair, for chaining
-
acceptSecond
Accepts the givenconsumer
on the second value of the pair.- Parameters:
consumer
- the consumer- Returns:
- the pair, for chaining
-
map
Maps the pair to another value- Type Parameters:
T
- the return type- Parameters:
mapper
- the mapper- Returns:
- the mapped value
-
mapBoth
public <NEW_FIRST,NEW_SECOND> Pair<NEW_FIRST,NEW_SECOND> mapBoth(Function<F, NEW_FIRST> firstMapper, Function<S, NEW_SECOND> secondMapper) Maps the pair's values to new ones.- Type Parameters:
NEW_FIRST
- the new first value typeNEW_SECOND
- the new first value type- Parameters:
firstMapper
- the mapper to use for mapping the first valuesecondMapper
- the mapper to use for mapping the second value- Returns:
- the mapped pair
-
mapFirst
Maps the first value to another value.- Type Parameters:
NEW_FIRST
- the new first value type- Parameters:
mapper
- the mapper- Returns:
- the mapped pair
-
mapFirst
Maps the first value to another value.- Type Parameters:
NEW_FIRST
- the new first value type- Parameters:
newFirst
- the new first value- Returns:
- the mapped pair
-
mapSecond
Maps the second value to another value.- Type Parameters:
NEW_SECOND
- the new first value type- Parameters:
mapper
- the mapper- Returns:
- the mapped pair
-
mapSecond
Maps the second value to another value.- Type Parameters:
NEW_SECOND
- the new first value type- Parameters:
newSecond
- the new second value- Returns:
- the mapped pair
-
toEntryFS
Maps this pair to aAbstractMap.SimpleImmutableEntry
containing the pair's values in a First-Second order.- Returns:
- the entry.
-
toEntrySF
Maps this pair to aAbstractMap.SimpleImmutableEntry
containing the pair's values in a Second-First order.- Returns:
- the entry.
-
clearFirst
Clears the first value of the pair, by making itnull
.- Returns:
- the new pair
-
clearSecond
Clears the second value of the pair, by making itnull
.- Returns:
- the new pair
-
swap
Swaps the order of the values.- Returns:
- the swapped pair
-
toOptional
Returns an optional which may contain this pair if the following conditions are met:- Returns:
- the optional
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object)
. -
first
Returns the value of thefirst
record component.- Returns:
- the value of the
first
record component
-
second
Returns the value of thesecond
record component.- Returns:
- the value of the
second
record component
-