Class DoubleOptional<T1,T2>

java.lang.Object
io.github.syst3ms.skriptparser.util.DoubleOptional<T1,T2>
Type Parameters:
T1 - the type of the first value
T2 - the type of the second value

public class DoubleOptional<T1,T2> extends Object
A double-valued version of an Optional, containing very similar methods. Note that the two values aren't really independent : either both are set, or both are empty.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T1,T2> DoubleOptional<T1,T2>
    Returns an empty DoubleOptional instance.
    filter(BiPredicate<? super T1, ? super T2> predicate)
    If both values are present and match the given predicate, returns a DoubleOptional describing these values, otherwise returns an empty DoubleOptional.
    firstOrElse(T1 other)
    If the first value is present, returns it, otherwise returns other.
    <X extends Throwable>
    T1
    firstOrElseThrow(Supplier<? extends X> exceptionSupplier)
    If the first value is present, returns it, otherwise throws an exception produced by the given exception-supplying function.
    <U,V> DoubleOptional<U,V>
    flatMap(BiFunction<? super T1, ? super T2, DoubleOptional<? extends U, ? extends V>> mapper)
    If both values are present, returns the result of applying the given DoubleOptional-returning mapping function to both values, and otherwise returns an empty DoubleOptional.
    <U> Optional<U>
    flatMapToOptional(BiFunction<? super T1, ? super T2, Optional<? extends U>> mapper)
    If both values are present, returns the result of applying the given Optional-returning mapping function to both values, and otherwise returns an empty Optional.
    If the first value is present, returns the value, otherwise throws a NoSuchElementException.
    If the second value is present, returns the value, otherwise throws a NoSuchElementException.
    void
    ifPresent(BiConsumer<? super T1, ? super T2> action)
    If both values are present, does the given action using both values, otherwise does nothing.
    void
    ifPresentOrElse(BiConsumer<? super T1, ? super T2> action, Runnable emptyAction)
    If both values are present, does the first action using both values, otherwise does the second action.
    boolean
    If either values are absent, returns true, otherwise false.
    boolean
    If both values are present, returns true, otherwise false.
    <U,V> DoubleOptional<U,V>
    map(Function<? super T1, ? extends U> firstMapper, Function<? super T2, ? extends V> secondMapper)
    If both values are present, returns a DoubleOptional describing the result (as if by ofNullable(Object, Object)) of applying each given mapping function to its respective value, and otherwise returns an empty DoubleOptional.
    <U> Optional<U>
    mapToOptional(BiFunction<? super T1, ? super T2, ? extends U> mapper)
    If both values are present, returns an Optional constructed from the result of the given mapping function applied to both values, otherwise returns an empty Optional.
    static <T1,T2> DoubleOptional<T1,T2>
    of(T1 first, T2 second)
    Returns an Optional describing the given non-null value.
    static <T1,T2> DoubleOptional<T1,T2>
    ofNullable(T1 first, T2 second)
    Returns a DoubleOptional describing the given values, if both non-null, otherwise returns an empty DoubleOptional.
    static <T1,T2> DoubleOptional<T1,T2>
    ofOptional(Optional<T1> first, Optional<T2> second)
    Returns a DoubleOptional describing the values represented by two Optionals if both are present, otherwise returns an empty DoubleOptional.
    or(Supplier<? extends DoubleOptional<? extends T1, ? extends T2>> supplier)
    If both values are present, returns a DoubleOptional describing them, otherwise returns a DoubleOptional obtained from the given supplier function.
    If the second value is present, returns it, otherwise returns other.
    <X extends Throwable>
    T2
    secondOrElseThrow(Supplier<? extends X> exceptionSupplier)
    If the second value is present, returns it, otherwise throws an exception produced by the given exception-supplying function.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • empty

      public static <T1,T2> DoubleOptional<T1,T2> empty()
      Returns an empty DoubleOptional instance.
      Type Parameters:
      T1 - the type of the first non-existent value
      T2 - the type of the second non-existent value
      Returns:
      an empty DoubleOptional
    • of

      public static <T1,T2> DoubleOptional<T1,T2> of(T1 first, T2 second)
      Returns an Optional describing the given non-null value.
      Type Parameters:
      T1 - the type of the first value
      T2 - the type fo the second value
      Parameters:
      first - the first value to describe, which must be non-null
      second - the second value to describe, which must be non-null
      Returns:
      a DoubleOptional with the value present
      Throws:
      NullPointerException - if either first or second are null
    • ofNullable

      public static <T1,T2> DoubleOptional<T1,T2> ofNullable(@Nullable T1 first, @Nullable T2 second)
      Returns a DoubleOptional describing the given values, if both non-null, otherwise returns an empty DoubleOptional.
      Type Parameters:
      T1 - the type of the first value
      T2 - the type of the second value
      Parameters:
      first - the first possibly null value to describe
      second - the secpond possibly null value to describe
      Returns:
      a DoubleOptional with both values present if the specified values are both non-null, otherwise an empty DoubleOptional
    • ofOptional

      public static <T1,T2> DoubleOptional<T1,T2> ofOptional(Optional<T1> first, Optional<T2> second)
      Returns a DoubleOptional describing the values represented by two Optionals if both are present, otherwise returns an empty DoubleOptional.
      Type Parameters:
      T1 - the type of the first value
      T2 - the type of the second value
      Parameters:
      first - a possibly empty Optional describing the first value
      second - a possibly empty Optional describing the second value
      Returns:
      a DoubleOptional describing the two values described by the two Optionals, if both are present, otherwise an empty DoubleOptional
    • getFirst

      public T1 getFirst()
      If the first value is present, returns the value, otherwise throws a NoSuchElementException.
      Returns:
      the non-null first value of this DoubleOptional
      Throws:
      NoSuchElementException - if no first value is present
    • getSecond

      public T2 getSecond()
      If the second value is present, returns the value, otherwise throws a NoSuchElementException.
      Returns:
      the non-null second value of this DoubleOptional
      Throws:
      NoSuchElementException - if no second value is present
    • isPresent

      public boolean isPresent()
      If both values are present, returns true, otherwise false.
      Returns:
      true if both values are present, otherwise false.
    • isEmpty

      public boolean isEmpty()
      If either values are absent, returns true, otherwise false.
      Returns:
      true if either values are absent, otherwise false
    • ifPresent

      public void ifPresent(BiConsumer<? super T1, ? super T2> action)
      If both values are present, does the given action using both values, otherwise does nothing.
      Parameters:
      action - the action to be performed if both values are present
    • ifPresentOrElse

      public void ifPresentOrElse(BiConsumer<? super T1, ? super T2> action, Runnable emptyAction)
      If both values are present, does the first action using both values, otherwise does the second action.
      Parameters:
      action - the action to be performed if both values are present
      emptyAction - the action to be performed if either values are absent
    • filter

      public DoubleOptional<T1,T2> filter(BiPredicate<? super T1, ? super T2> predicate)
      If both values are present and match the given predicate, returns a DoubleOptional describing these values, otherwise returns an empty DoubleOptional.
      Parameters:
      predicate - the predicate to test both values against, if present
      Returns:
      a DoubleOptional describing these values, if they are present and match the given predicate, otherwise an empty DoubleOptional
    • map

      public <U,V> DoubleOptional<U,V> map(Function<? super T1, ? extends U> firstMapper, Function<? super T2, ? extends V> secondMapper)
      If both values are present, returns a DoubleOptional describing the result (as if by ofNullable(Object, Object)) of applying each given mapping function to its respective value, and otherwise returns an empty DoubleOptional. If either mapping function returns null, the result will be an empty DoubleOptional.
      Type Parameters:
      U - the new type of the first value
      V - the new type of the second value
      Parameters:
      firstMapper - the mapping function to apply to the first value, if present
      secondMapper - the mapping function to apply to the second value, if present
      Returns:
      a DoubleOptional describing the result of applying mapping functions to each of the values if present, and an empty DoubleOptional otherwise.
    • mapToOptional

      public <U> Optional<U> mapToOptional(BiFunction<? super T1, ? super T2, ? extends U> mapper)
      If both values are present, returns an Optional constructed from the result of the given mapping function applied to both values, otherwise returns an empty Optional. If the mapping function returns null, an empty Optional is returned.
      Type Parameters:
      U - the type of the resulting Optional
      Parameters:
      mapper - the mapping function taking both values as inputs
      Returns:
      an Optional describing the result of applying the mapping function to both values if present, and an empty Optional otherwise
    • flatMap

      public <U,V> DoubleOptional<U,V> flatMap(BiFunction<? super T1, ? super T2, DoubleOptional<? extends U, ? extends V>> mapper)
      If both values are present, returns the result of applying the given DoubleOptional-returning mapping function to both values, and otherwise returns an empty DoubleOptional. This is similar to map(Function, Function), but it doesn't wrap the result in an additional DoubleOptional.
      Type Parameters:
      U - the type of the first value of the new DoubleOptional
      V - the type of the second value of the new DoubleOptional
      Parameters:
      mapper - the mapping function taking in both values (if present) and returning a DoubleOptional
      Returns:
      the result of applying the mapping function to both values if present, and an empty DoubleOptional otherwise
      Throws:
      NullPointerException - if the mapping function returns null
    • flatMapToOptional

      public <U> Optional<U> flatMapToOptional(BiFunction<? super T1, ? super T2, Optional<? extends U>> mapper)
      If both values are present, returns the result of applying the given Optional-returning mapping function to both values, and otherwise returns an empty Optional. This is similar to mapToOptional(BiFunction), but it doesn't wrap the result in an additional Optional.
      Type Parameters:
      U - the type of the new Optional
      Parameters:
      mapper - the mapping function taking in both values (if present) and returning an Optional
      Returns:
      the result of applying the mapping function to both values if present, and an empty Optional otherwise
      Throws:
      NullPointerException - if the mapping function returns null
    • or

      public DoubleOptional<T1,T2> or(Supplier<? extends DoubleOptional<? extends T1, ? extends T2>> supplier)
      If both values are present, returns a DoubleOptional describing them, otherwise returns a DoubleOptional obtained from the given supplier function.
      Parameters:
      supplier - the supplier producing the DoubleOptional returned if this one is empty
      Returns:
      a DoubleOptional describing the two values if present, and otherwise the DoubleOptional produced by the given supplier function
    • firstOrElse

      @Contract("!null -> !null") @Nullable public T1 firstOrElse(@Nullable T1 other)
      If the first value is present, returns it, otherwise returns other.
      Parameters:
      other - the value to be returned if the first value is not present (may be null)
      Returns:
      the first value if present, otherwise other
    • secondOrElse

      @Contract("!null -> !null") @Nullable public T2 secondOrElse(@Nullable T2 other)
      If the second value is present, returns it, otherwise returns other.
      Parameters:
      other - the value to be returned if the second value is not present (may be null)
      Returns:
      the second value if present, otherwise other
    • firstOrElseThrow

      public <X extends Throwable> T1 firstOrElseThrow(Supplier<? extends X> exceptionSupplier) throws X
      If the first value is present, returns it, otherwise throws an exception produced by the given exception-supplying function.
      Type Parameters:
      X - the type of exception that may be thrown
      Parameters:
      exceptionSupplier - the function producing the exception to be thrown if the first value is not present
      Returns:
      the first value if present
      Throws:
      X - if the first value is not present
    • secondOrElseThrow

      public <X extends Throwable> T2 secondOrElseThrow(Supplier<? extends X> exceptionSupplier) throws X
      If the second value is present, returns it, otherwise throws an exception produced by the given exception-supplying function.
      Type Parameters:
      X - the type of exception that may be thrown
      Parameters:
      exceptionSupplier - the function producing the exception to be thrown if the second value is not present
      Returns:
      the second value if present
      Throws:
      X - if the second value is not present