Class Pair<F,S>

java.lang.Object
net.luis.utils.util.Pair<F,S>
Type Parameters:
F - The type of the first value
S - The type of the second value

public class Pair<F,S> extends Object
Tuple like class that holds two values of different types.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final F
    The first value of the pair.
    private final S
    The second value of the pair.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Pair(F first, S second)
    Constructs a new pair with the specified first and second value.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    Returns the first value of the pair.
    Returns the second value of the pair.
    int
     
    <T> @NotNull Pair<T,S>
    mapFirst(@NotNull Function<? super F,? extends T> mapper)
    Maps the first value of the pair to a new value using the specified mapper.
    <T> @NotNull Pair<F,T>
    mapSecond(@NotNull Function<? super S,? extends T> mapper)
    Maps the second value of the pair to a new value using the specified mapper.
    static <T, U> @NotNull Pair<T,U>
    of(T first, U second)
    Creates a new pair with the specified first and second value.
    @NotNull Pair<S,F>
    Swaps the first and second value of the pair.
     
    <T> @NotNull Pair<T,S>
    withFirst(T first)
    Creates a new pair with the specified first value.
    <T> @NotNull Pair<F,T>
    withSecond(T second)
    Creates a new pair with the specified second value.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • first

      private final F first
      The first value of the pair.
    • second

      private final S second
      The second value of the pair.
  • Constructor Details

    • Pair

      protected Pair(@Nullable F first, @Nullable S second)
      Constructs a new pair with the specified first and second value.
      Parameters:
      first - The first value
      second - The second value
      See Also:
  • Method Details

    • of

      @NotNull public static <T, U> @NotNull Pair<T,U> of(@Nullable T first, @Nullable U second)
      Creates a new pair with the specified first and second value.
      Type Parameters:
      T - The type of the first value
      U - The type of the second value
      Parameters:
      first - The first value
      second - The second value
      Returns:
      The created pair instance
    • getFirst

      public F getFirst()
      Returns the first value of the pair.
      Returns:
      The first value
    • getSecond

      public S getSecond()
      Returns the second value of the pair.
      Returns:
      The second value
    • swap

      @NotNull public @NotNull Pair<S,F> swap()
      Swaps the first and second value of the pair.
      Returns:
      A new pair with the swapped values
    • mapFirst

      @NotNull public <T> @NotNull Pair<T,S> mapFirst(@NotNull @NotNull Function<? super F,? extends T> mapper)
      Maps the first value of the pair to a new value using the specified mapper.
      Type Parameters:
      T - The new type of the first value
      Parameters:
      mapper - The mapper function
      Returns:
      A new pair with the mapped first value
    • mapSecond

      @NotNull public <T> @NotNull Pair<F,T> mapSecond(@NotNull @NotNull Function<? super S,? extends T> mapper)
      Maps the second value of the pair to a new value using the specified mapper.
      Type Parameters:
      T - The new type of the second value
      Parameters:
      mapper - The mapper function
      Returns:
      A new pair with the mapped second value
    • withFirst

      @NotNull public <T> @NotNull Pair<T,S> withFirst(@Nullable T first)
      Creates a new pair with the specified first value.
      Type Parameters:
      T - The new type of the first value
      Parameters:
      first - The new first value replacing the old one
      Returns:
      A new pair with the specified first value
    • withSecond

      @NotNull public <T> @NotNull Pair<F,T> withSecond(@Nullable T second)
      Creates a new pair with the specified second value.
      Type Parameters:
      T - The new type of the second value
      Parameters:
      second - The new second value replacing the old one
      Returns:
      A new pair with the specified second value
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object