Class Pair<FirstType, SecondType>

java.lang.Object
de.s42.base.functional.Pair<FirstType, SecondType>
Type Parameters:
FirstType -
SecondType -

public final class Pair<FirstType, SecondType> extends Object
A pair of 2 values. Both types can be null. It provides handy access.
Author:
Benjamin Schiller
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final FirstType
    Stores the first final.
    private final SecondType
    Stores the second final.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Pair(FirstType first, SecondType second)
    Constructs a pair of first and second.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    each(Consumer<FirstType> actionFirst, Consumer<SecondType> actionSecond)
    Calls the action if first is not null with first as parameter.
    static <FirstType, SecondType>
    Pair<FirstType, SecondType>
    Create an empty Pair with both of the given values being null.
    boolean
    Either is equal if first and second are equal.
    Returns the optional of first value
    Returns first if first is not null or other if first is null.
    Returns a Optional of first or throws a exception if first is null.
    <ExceptionType extends Throwable>
    FirstType
    firstOrThrow(Supplier<ExceptionType> supplier)
    Returns a Optional of first or throws a customized exception if first is null.
    int
    The combined hashcode of first and second.
    void
    Calls the action if first is not null with first as parameter.
    void
    Calls the action if second is not null with second as parameter.
    boolean
    Returns true if first is not null.
    boolean
    Returns true if second is not null.
    static <FirstType, SecondType>
    Pair<FirstType, SecondType>
    of(FirstType first, SecondType second)
    Create a Pair with both of the given values being non null.
    static <FirstType, SecondType>
    Pair<FirstType, SecondType>
    ofFirst(FirstType first)
    Create a Pair with first set and second being null
    static <FirstType, SecondType>
    Pair<FirstType, SecondType>
    ofNullable(FirstType first, SecondType second)
    Create a Pair with both of the given values being null or non null.
    static <FirstType, SecondType>
    Pair<FirstType, SecondType>
    ofSecond(SecondType second)
    Create a Pair with second set and first being null
    Returns the optional of second value
    Returns second if second is not null or other if second is null.
    Returns a Optional of second or throws a exception if second is null.
    <ExceptionType extends Throwable>
    SecondType
    secondOrThrow(Supplier<ExceptionType> supplier)
    Returns a Optional of second or throws a customized exception if first is null.
    Allows to get a stream to streaming first then second (only contains them if non null)
    Returns a string in the form '[first, second]'

    Methods inherited from class java.lang.Object

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

    • first

      private final FirstType first
      Stores the first final.
    • second

      private final SecondType second
      Stores the second final.
  • Constructor Details

    • Pair

      private Pair(FirstType first, SecondType second)
      Constructs a pair of first and second. Both values may be null
      Parameters:
      first - First value
      second - Second value
  • Method Details

    • empty

      public static <FirstType, SecondType> Pair<FirstType, SecondType> empty()
      Create an empty Pair with both of the given values being null.
      Type Parameters:
      FirstType - Type of the first value
      SecondType - Type of the second value
      Returns:
      An empty Pair
    • of

      public static <FirstType, SecondType> Pair<FirstType, SecondType> of(FirstType first, SecondType second) throws NullPointerException
      Create a Pair with both of the given values being non null.
      Type Parameters:
      FirstType - Type of the first value
      SecondType - Type of the second value
      Parameters:
      first - First value
      second - Second value
      Returns:
      Pair of type <FirstType, SecondType>
      Throws:
      NullPointerException - if either of first or second is null
    • ofFirst

      public static <FirstType, SecondType> Pair<FirstType, SecondType> ofFirst(FirstType first) throws NullPointerException
      Create a Pair with first set and second being null
      Type Parameters:
      FirstType - Type of the first value
      SecondType - Type of the second value
      Parameters:
      first - First value
      Returns:
      Pair of type <FirstType, SecondType>
      Throws:
      NullPointerException - if first is null
    • ofSecond

      public static <FirstType, SecondType> Pair<FirstType, SecondType> ofSecond(SecondType second) throws NullPointerException
      Create a Pair with second set and first being null
      Type Parameters:
      FirstType - Type of the first value
      SecondType - Type of the second value
      Parameters:
      second - Second value
      Returns:
      Pair of type <FirstType, SecondType>
      Throws:
      NullPointerException - if second is null
    • ofNullable

      public static <FirstType, SecondType> Pair<FirstType, SecondType> ofNullable(FirstType first, SecondType second)
      Create a Pair with both of the given values being null or non null.
      Type Parameters:
      FirstType - Type of the first value
      SecondType - Type of the second value
      Parameters:
      first - First value
      second - Second value
      Returns:
      Either of type <FirstType, SecondType>
    • each

      public void each(Consumer<FirstType> actionFirst, Consumer<SecondType> actionSecond)
      Calls the action if first is not null with first as parameter.
      Parameters:
      actionFirst - Called if first is not null
      actionSecond - Called if second is not null
    • ifFirst

      public void ifFirst(Consumer<FirstType> action)
      Calls the action if first is not null with first as parameter.
      Parameters:
      action - Called if first is not null
    • ifSecond

      public void ifSecond(Consumer<SecondType> action)
      Calls the action if second is not null with second as parameter.
      Parameters:
      action - Called if second is not null
    • isFirst

      public boolean isFirst()
      Returns true if first is not null.
      Returns:
      True if first is not null
    • isSecond

      public boolean isSecond()
      Returns true if second is not null.
      Returns:
      True if second is not null
    • first

      public Optional<FirstType> first()
      Returns the optional of first value
      Returns:
      Optional of first value
    • firstOrThrow

      public FirstType firstOrThrow() throws NullPointerException
      Returns a Optional of first or throws a exception if first is null.
      Returns:
      Optional of first or throws a exception if first is null
      Throws:
      NullPointerException - If first is null
    • firstOrThrow

      public <ExceptionType extends Throwable> FirstType firstOrThrow(Supplier<ExceptionType> supplier) throws ExceptionType
      Returns a Optional of first or throws a customized exception if first is null.
      Type Parameters:
      ExceptionType -
      Parameters:
      supplier -
      Returns:
      Optional of first or throws a exception if first is null
      Throws:
      ExceptionType - generated if first is null
    • firstOrElse

      public FirstType firstOrElse(FirstType other)
      Returns first if first is not null or other if first is null.
      Parameters:
      other - Value that will be returned if first is null
      Returns:
      first if first is not null or other if first is null
    • second

      public Optional<SecondType> second()
      Returns the optional of second value
      Returns:
      Optional of second value
    • secondOrThrow

      public SecondType secondOrThrow() throws NullPointerException
      Returns a Optional of second or throws a exception if second is null.
      Returns:
      Optional of second or throws a exception if second is null
      Throws:
      NullPointerException - If second is null
    • secondOrThrow

      public <ExceptionType extends Throwable> SecondType secondOrThrow(Supplier<ExceptionType> supplier) throws ExceptionType
      Returns a Optional of second or throws a customized exception if first is null.
      Type Parameters:
      ExceptionType -
      Parameters:
      supplier -
      Returns:
      Optional of second or throws a exception if second is null
      Throws:
      ExceptionType - generated if second is null
    • secondOrElse

      public SecondType secondOrElse(SecondType other)
      Returns second if second is not null or other if second is null.
      Parameters:
      other - Value that will be returned if second is null
      Returns:
      Second if second is not null or other if second is null
    • stream

      public Stream<?> stream()
      Allows to get a stream to streaming first then second (only contains them if non null)
      Returns:
      A stream that streams first then second
    • toString

      public String toString()
      Returns a string in the form '[first, second]'
      Overrides:
      toString in class Object
      Returns:
      String of first then second
    • hashCode

      public int hashCode()
      The combined hashcode of first and second.
      Overrides:
      hashCode in class Object
      Returns:
      Combined hashcode of first and second
    • equals

      public boolean equals(Object obj)
      Either is equal if first and second are equal.
      Overrides:
      equals in class Object
      Parameters:
      obj - Other Either
      Returns:
      True if first and second are equal