Class OptionalBoolean


  • public final class OptionalBoolean
    extends Object
    A container object which may or may not contain a boolean value. If value is present, isPresent() will return true and getAsBoolean() will return the value.

    Additional methods that depend on the presence or absence of a contained value are provided, such as orElse(boolean) (return a default value if value is not present) and ifPresent(IntConsumer) (execute a block of code if the value is present).

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static OptionalBoolean empty()
      Returns an empty OptionalBoolean instance.
      boolean equals​(Object obj)
      Indicates whether some other object is "equal to" this OptionalBoolean.
      boolean getAsBoolean()
      If a value is present in this OptionalBoolean, returns the value, otherwise throws NoSuchElementException.
      int hashCode()
      Returns the hash code value of the present value, if any, or zero if no value is present.
      void ifPresent​(IntConsumer action)
      If a value is present, passes the value to the provided consumer, otherwise does nothing.
      void ifPresentOrElse​(IntConsumer action, Runnable emptyAction)
      If a value is present, passes the value to the provided consumer, otherwise executes the provided action.
      boolean isPresent()
      Returns true if there is a value present, otherwise false.
      static OptionalBoolean of​(boolean value)
      Returns an OptionalBoolean with the specified value present.
      boolean orElse​(boolean other)
      Returns the value if present, otherwise returns other.
      boolean orElseGet​(BooleanSupplier other)
      Returns the value if present, otherwise invokes other and returns the result of that invocation.
      boolean orElseThrow()
      Returns the contained value if present, otherwise throws NoSuchElementException.
      <X extends Throwable>
      boolean
      orElseThrow​(Supplier<X> exceptionSupplier)
      Returns the contained value, if present, otherwise throws an exception created by the provided supplier.
      IntStream stream()
      If a value is present, returns the value in a stream, otherwise returns an empty stream.
      OptionalInt toInt()
      Widens the optional boolean value to OptionalInt.
      String toString()
      Returns string representation of this instance suitable for debugging.
    • Method Detail

      • empty

        public static OptionalBoolean empty()
        Returns an empty OptionalBoolean instance. No value is present for this OptionalBoolean. There is only one instance of empty OptionalBoolean.
        Returns:
        an empty OptionalBoolean
      • of

        public static OptionalBoolean of​(boolean value)
        Returns an OptionalBoolean with the specified value present. There are only two global instances of OptionalBoolean with value present. This method just returns one of them.
        Parameters:
        value - - the value to be present
        Returns:
        an OptionalBoolean with the value present
      • isPresent

        public boolean isPresent()
        Returns true if there is a value present, otherwise false.
        Returns:
        true if there is a value present, otherwise false
      • getAsBoolean

        public boolean getAsBoolean()
        If a value is present in this OptionalBoolean, returns the value, otherwise throws NoSuchElementException.
        Returns:
        the value held by this OptionalBoolean
        Throws:
        NoSuchElementException - if there is no value present
        See Also:
        isPresent()
      • orElse

        public boolean orElse​(boolean other)
        Returns the value if present, otherwise returns other.
        Parameters:
        other - the value to be returned if there is no value present
        Returns:
        the value, if present, otherwise other
      • orElseGet

        public boolean orElseGet​(BooleanSupplier other)
        Returns the value if present, otherwise invokes other and returns the result of that invocation.
        Parameters:
        other - a BooleanSupplier whose result is returned if no value is present
        Returns:
        the value if present, otherwise the result of other.getAsBoolean()
        Throws:
        NullPointerException - if value is not present and other is null
      • orElseThrow

        public <X extends Throwable> boolean orElseThrow​(Supplier<X> exceptionSupplier)
                                                  throws X extends Throwable
        Returns the contained value, if present, otherwise throws an exception created by the provided supplier. A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, IllegalStateException::new.
        Type Parameters:
        X - type of the exception to be thrown
        Parameters:
        exceptionSupplier - the supplier which will return the exception to be thrown
        Returns:
        the present value
        Throws:
        X - if there is no value present
        NullPointerException - if no value is present and exceptionSupplier is null
        X extends Throwable
      • stream

        public IntStream stream()
        If a value is present, returns the value in a stream, otherwise returns an empty stream. There is no BooleanStream in JDK. The boolean value is therefore widened to int and returned in an IntStream. Value of true is widened to 1, false to 0.
        Returns:
        the optional value as an IntStream
      • ifPresent

        public void ifPresent​(IntConsumer action)
        If a value is present, passes the value to the provided consumer, otherwise does nothing. There is no BooleanConsumer in JDK. The boolean value is therefore widened to int and passed to IntConsumer. Value of true is widened to 1, false to 0.
        Parameters:
        action - code to be executed if a value is present
        Throws:
        NullPointerException - if value is present and action is null
      • ifPresentOrElse

        public void ifPresentOrElse​(IntConsumer action,
                                    Runnable emptyAction)
        If a value is present, passes the value to the provided consumer, otherwise executes the provided action. There is no BooleanConsumer in JDK. The boolean value is therefore widened to int and passed to IntConsumer. Value of true is widened to 1, false to 0.
        Parameters:
        action - code to be executed if a value is present
        emptyAction - code to be executed if no value is present
        Throws:
        NullPointerException - if value is present and action is null
      • toInt

        public OptionalInt toInt()
        Widens the optional boolean value to OptionalInt. JDK consistently uses OptionalInt where OptionalBoolean would be semantically correct. This class can consequently cause compatibility problems with other code that expects OptionalInt. This method widens the boolean value to int as wrapped in OptionalInt to improve compatibility. Value of true is widened to 1, false to 0.
        Returns:
        the optional value converted to OptionalInt
      • equals

        public boolean equals​(Object obj)
        Indicates whether some other object is "equal to" this OptionalBoolean. The other object is considered equal if it is also an OptionalBoolean and both instances have no value present or the present values are the same.
        Overrides:
        equals in class Object
        Parameters:
        obj - an object to be tested for equality
        Returns:
        true if the other object is "equal to" this object, otherwise false
        See Also:
        hashCode()
      • hashCode

        public int hashCode()
        Returns the hash code value of the present value, if any, or zero if no value is present.
        Overrides:
        hashCode in class Object
        Returns:
        hash code value of the present value of 0 if no value is present
        See Also:
        equals(Object), Boolean.hashCode()
      • toString

        public String toString()
        Returns string representation of this instance suitable for debugging. In the current implementation, one of the strings empty, true, or false is returned.
        Overrides:
        toString in class Object
        Returns:
        the string representation of this instance