Interface Optional.OrElse

  • Enclosing interface:
    Optional<T>

    public static interface Optional.OrElse
    The OrElse interface is an extension to the Optional interface. This interface can be used to cascade behaviour when an entity is or is not present in the wrapper.

    For example if the Optional has no entity contained within then the OrElse can be used to throw an exception or run alternative logic.

    Example:

     API.Option(null)
          .ifPresent(System.out::println)
          .elseThrow(() -> new NullPointerException("No value present"))
     
    Since:
    0.0.1
    • Method Summary

      All Methods Instance Methods Default Methods 
      Modifier and Type Method Description
      default void elseRun​(java.lang.Runnable runner)
      This operation will run when the precondition of this OrElse is not met.
      default <X extends java.lang.Throwable>
      void
      elseThrow​(java.util.function.Supplier<X> exceptionSupplier)
      This operation will create an exception using the provided Supplier and throw it.
    • Method Detail

      • elseRun

        default void elseRun​(java.lang.Runnable runner)
        This operation will run when the precondition of this OrElse is not met.
        Parameters:
        runner - the code to be run when the else is running
      • elseThrow

        default <X extends java.lang.Throwable> void elseThrow​(java.util.function.Supplier<X> exceptionSupplier)
                                                        throws X extends java.lang.Throwable
        This operation will create an exception using the provided Supplier and throw it.
        Type Parameters:
        X - the type of exception
        Parameters:
        exceptionSupplier - the supplier to create the exception
        Throws:
        X - the exception in case of an OrElse run
        X extends java.lang.Throwable