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.

public record Pair<F,S>(F first, S second) extends Record
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 Details

    • Pair

      public Pair(F first, S second)
      Creates an instance of a Pair record class.
      Parameters:
      first - the value for the first record component
      second - the value for the second record component
  • Method Details

    • registerIsPresentPredicate

      public static <T> void registerIsPresentPredicate(Class<T> clazz, Predicate<T> predicate)
      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 for
      predicate - the predicate to register
    • getIsPresentPredicate

      public static <T> Predicate<T> getIsPresentPredicate(Class<T> clazz)
      Gets the "Is Present" predicate for the specified class.
      The return value is never null, as if the IS_PRESENT_PREDICATES doesn't contain an entry for the specified type, a predicate returning true 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

      public static <F, S> Pair<F,S> of(F first, S second)
      Makes a pair from the given value
      Type Parameters:
      F - the type of the first value
      S - the type of the second value
      Parameters:
      first - the first value
      second - the second value
      Returns:
      the newly created pair
    • empty

      public static <F, S> Pair<F,S> empty()
      Makes an empty pair, which has each value null.
      Type Parameters:
      F - the type of the first value
      S - the type of the second value
      Returns:
      the empty pair.
    • mapResponses

      public static <F, S> Optional<Pair<F,S>> 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.
      This is especially useful when making multiple requests using AsyncRequest.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);
           }));
       
       
      In the example above, the System.out call would not run, because one of the responses is empty.
      Type Parameters:
      F - the first response type
      S - the second response type
      Parameters:
      pair - the pair containing the responses to map
      Returns:
      the optional
    • accept

      public Pair<F,S> accept(BiConsumer<F,S> consumer)
      Accepts the given consumer on the values from the pair
      Parameters:
      consumer - the consumer
      Returns:
      the pair, for chaining
    • acceptFirst

      public Pair<F,S> acceptFirst(Consumer<F> consumer)
      Accepts the given consumer on the first value of the pair.
      Parameters:
      consumer - the consumer
      Returns:
      the pair, for chaining
    • acceptSecond

      public Pair<F,S> acceptSecond(Consumer<S> consumer)
      Accepts the given consumer on the second value of the pair.
      Parameters:
      consumer - the consumer
      Returns:
      the pair, for chaining
    • map

      public <T> T map(BiFunction<F,S,T> mapper)
      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 type
      NEW_SECOND - the new first value type
      Parameters:
      firstMapper - the mapper to use for mapping the first value
      secondMapper - the mapper to use for mapping the second value
      Returns:
      the mapped pair
    • mapFirst

      public <NEW_FIRST> Pair<NEW_FIRST,S> mapFirst(Function<F,NEW_FIRST> mapper)
      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

      public <NEW_FIRST> Pair<NEW_FIRST,S> mapFirst(NEW_FIRST newFirst)
      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

      public <NEW_SECOND> Pair<F,NEW_SECOND> mapSecond(Function<S,NEW_SECOND> mapper)
      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

      public <NEW_SECOND> Pair<F,NEW_SECOND> mapSecond(NEW_SECOND newSecond)
      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

      public Map.Entry<F,S> toEntryFS()
      Maps this pair to a AbstractMap.SimpleImmutableEntry containing the pair's values in a First-Second order.
      Returns:
      the entry.
    • toEntrySF

      public Map.Entry<S,F> toEntrySF()
      Maps this pair to a AbstractMap.SimpleImmutableEntry containing the pair's values in a Second-First order.
      Returns:
      the entry.
    • clearFirst

      public Pair<F,S> clearFirst()
      Clears the first value of the pair, by making it null.
      Returns:
      the new pair
    • clearSecond

      public Pair<F,S> clearSecond()
      Clears the second value of the pair, by making it null.
      Returns:
      the new pair
    • swap

      public Pair<S,F> swap()
      Swaps the order of the values.
      Returns:
      the swapped pair
    • toOptional

      public Optional<Pair<F,S>> toOptional()
      Returns an optional which may contain this pair if the following conditions are met:
      Returns:
      the optional
    • toString

      public final String 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.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • 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.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • first

      public F first()
      Returns the value of the first record component.
      Returns:
      the value of the first record component
    • second

      public S second()
      Returns the value of the second record component.
      Returns:
      the value of the second record component