Class Pair<L,R>

java.lang.Object
com.marcpg.libpg.storing.Pair<L,R>
Type Parameters:
L - The left field's type
R - The right field's type

public class Pair<L,R> extends Object
A pair is just two values that have a defined type. It's the same as a Map, but with only one value.
Since:
0.0.1
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    A side of a pair.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected L
    The left side of this pair.
    protected R
    The right side of this pair.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a pair.
    Pair(L left, R right)
    Construct a pair with both sides already initialized.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    both(@NotNull Consumer<Object> operation)
    Runs some operation on both the left and right side.
    void
    Clears both the left and right side's values.
    void
    Clears the value of a specific side.
    boolean
    Checks if this pair contains the specified value.
    get(Pair.Side side)
    Get the field of a specific side from the pair.
    This gets the heavier side of the pair based on the length when using Object.toString().
    This gets the heavier side of the pair based on the length when using Object.toString().
    getIf(@NotNull Predicate<Object> operation)
    Gets the side matching the predicate's requirements.
    boolean
    Check if the pair is empty.
    boolean
    Check if the pair is full.
    Get the left field of the pair.
    static <L, R> @NotNull Pair<L,R>
    of(L left, R right)
    Static method to create a new Pair instance.
    static <L, R> @NotNull Pair<L,R>
    ofEntry(Map.Entry<L,R> entry)
    Static method to convert a Map.Entry to a Pair, where the left side is the key and the right side is the value.
    Get the right field of the pair.
    void
    set(L left, R right)
    Set or change both fields of the pair.
    void
    Set or change the left field of the pair.
    void
    Set or change the right field of the pair.
    Convert the Pair to a readable String.

    Methods inherited from class java.lang.Object

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

    • left

      protected L left
      The left side of this pair.
  • Constructor Details

    • Pair

      public Pair(L left, R right)
      Construct a pair with both sides already initialized.
      Parameters:
      left - The value for the left field.
      right - The value for the right field.
      Since:
      0.0.4
    • Pair

      public Pair()
      Construct a pair.
      Since:
      0.0.8
  • Method Details

    • setLeft

      public void setLeft(L l)
      Set or change the left field of the pair.
      Parameters:
      l - The new object for the left field.
    • left

      public L left()
      Get the left field of the pair.
      Returns:
      The left field.
    • setRight

      public void setRight(R r)
      Set or change the right field of the pair.
      Parameters:
      r - The new object for the right field.
    • right

      public R right()
      Get the right field of the pair.
      Returns:
      The right field.
    • get

      public Object get(Pair.Side side)
      Get the field of a specific side from the pair. Not recommend, use left() and right() instead.
      Parameters:
      side - What side of the pair you want to get.
      Returns:
      The object that's on the side.
    • set

      public void set(L left, R right)
      Set or change both fields of the pair.
      Parameters:
      right - The new object for the left field.
      left - The new object for the right field.
    • clear

      public void clear()
      Clears both the left and right side's values.
    • clear

      public void clear(Pair.Side side)
      Clears the value of a specific side.
      Parameters:
      side - The side to clear the value at.
    • isEmpty

      public boolean isEmpty()
      Check if the pair is empty. This can also be checked by doing a null-check for the right and left side.
      Returns:
      If the pair is empty.
    • isFull

      public boolean isFull()
      Check if the pair is full. This can also be checked by doing a null-check for the right and left side.
      Returns:
      If the pair is full.
    • contains

      public boolean contains(Object o)
      Checks if this pair contains the specified value.
      Parameters:
      o - The object that's checked.
      Returns:
      true if this pair contains the value, false otherwise.
    • getHeavierSide

      public Pair.Side getHeavierSide()
      This gets the heavier side of the pair based on the length when using Object.toString().
      Returns:
      The heavier side of the pair.
      See Also:
    • getHeavierObject

      public Object getHeavierObject()
      This gets the heavier side of the pair based on the length when using Object.toString().
      Returns:
      The heavier object of the pair.
      See Also:
    • both

      public void both(@NotNull @NotNull Consumer<Object> operation)
      Runs some operation on both the left and right side.
      Parameters:
      operation - The operation to run.
      Since:
      0.0.5
    • getIf

      public Object getIf(@NotNull @NotNull Predicate<Object> operation)
      Gets the side matching the predicate's requirements.
      If both operations match the requirement, it will return the left side.
      Parameters:
      operation - What to check for.
      Returns:
      The side matching the requirements (prioritizing the left size).
      Since:
      0.0.5
    • toString

      public String toString()
      Convert the Pair to a readable String.
      It uses the simple scheme: `{"right":[right field], "left":[left field]}`.
      Overrides:
      toString in class Object
      Returns:
      The pair in a String format.
    • of

      @NotNull public static <L, R> @NotNull Pair<L,R> of(L left, R right)
      Static method to create a new Pair instance.
      Type Parameters:
      L - The left value's type.
      R - The right value's type.
      Parameters:
      left - The value for the left field.
      right - The value for the right field.
      Returns:
      A new Pair instance with the set values.
      Since:
      0.0.4
    • ofEntry

      @NotNull public static <L, R> @NotNull Pair<L,R> ofEntry(@NotNull Map.Entry<L,R> entry)
      Static method to convert a Map.Entry to a Pair, where the left side is the key and the right side is the value.
      Type Parameters:
      L - The left value's, so map-entry's key, type.
      R - The right value's, so map-entry's value, type.
      Parameters:
      entry - The Map.Entry to convert to a Pair.
      Returns:
      A new Pair instance with the same objects as the Entry.