Class Either<FirstType, SecondType>

java.lang.Object
de.s42.base.functional.Either<FirstType, SecondType>
Type Parameters:
FirstType - Type of the first value
SecondType - Type of the second value

public final class Either<FirstType, SecondType> extends Object
Either is an implementation of the either monad. You can encode either 1 of 2 values (first, second) to be contained. It provides many powerful ways to interact with it.
Author:
Benjamin Schiller
  • Field Details

    • first

      private final FirstType first
      Stores the first final.
    • second

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

    • Either

      private Either(FirstType first, SecondType second)
      Internal constructor for an Either.
      Parameters:
      first - First value
      second - Second value
  • Method Details

    • ofEither

      public static <FirstType, SecondType> Either<FirstType, SecondType> ofEither(FirstType first, SecondType second) throws IllegalArgumentException
      Create an Either with one 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:
      Either of type <FirstType, SecondType>
      Throws:
      IllegalArgumentException - if both or none is null (first != null ^ second != null)
    • ofFirst

      public static <FirstType, SecondType> Either<FirstType, SecondType> ofFirst(FirstType first) throws NullPointerException
      Create an Either with first.
      Type Parameters:
      FirstType - Type of the first value
      SecondType - Type of the second value
      Parameters:
      first - First value
      Returns:
      An Either of first value
      Throws:
      NullPointerException - if first is null
    • ofSecond

      public static <FirstType, SecondType> Either<FirstType, SecondType> ofSecond(SecondType second) throws NullPointerException
      Create an Either with second.
      Type Parameters:
      FirstType - Type of the first value
      SecondType - Type of the second value
      Parameters:
      second - Second value
      Returns:
      An Either of second value
      Throws:
      NullPointerException - if second is 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
    • either

      public void either(Consumer<FirstType> actionFirst, Consumer<SecondType> actionSecond)
      Calls on of the actions. Either actionFirst with first as parameter if first is not null or actionSecond with second as parameter.
      Parameters:
      actionFirst - Called if first is not null
      actionSecond - 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 a nullable Optional of first.
      Returns:
      A nullable Optional of first
    • 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 a nullable Optional of second.
      Returns:
      A nullable Optional of second
    • 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
    • firstOrSecond

      public Object firstOrSecond()
      Returns either the first or the second value. Allows to unwrap easily.
      Returns:
      The first if not null or second
    • stream

      public Stream<?> stream()
      Allows to get a stream either streaming first or second
      Returns:
      A stream that either streams first or second
    • toString

      public String toString()
      Returns the first.toString() if first not null otherwise second.toString().
      Overrides:
      toString in class Object
      Returns:
      String Of either first of 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