Interface AtMostOne<T>

    • Method Detail

      • discardRemaining

        default Optional<T> discardRemaining()
        This allows to just pick out the first element, and basically offers the same functionality as Stream.findFirst().
        Returns:
        The first element if it exists, or Optional.empty() if no elements exist.
      • orIfExcessiveThrow

        default <X extends ThrowableOptional<T> orIfExcessiveThrow​(Supplier<X> exceptionSupplier)
                                                              throws X extends Throwable
        Pick out the single contained element, or throw the given exception if there are excessive (more than one) elements available.
        Parameters:
        exceptionSupplier - the exception to throw if there are excessive elements available.
        Returns:
        The single element if it exists, or Optional.empty() if no elements exist.
        Throws:
        X - if there are excessive elements available.
        X extends Throwable
      • orElse

        <X extends ThrowableOptional<T> orElse​(ThrowingRunnable<X> handleUnexpectedMultipleElements)
                                          throws X extends Throwable
        How to handle if there actually are multiple elements available when only at most one is expected.
        Parameters:
        handleUnexpectedMultipleElements - the handling of the unexpected multiple elements.
        Returns:
        The first element if it exists, or Optional.empty() if no elements exist.
        Throws:
        X - if the function to handle more than one elements throws an exception.
        X extends Throwable