com.machinezoo.noexception.optional

Class OptionalBoolean

NoException » API Reference » com.machinezoo.noexception.optional



  • 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 and 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 consumer)
      Have the specified consumer accept the value if a value is present, otherwise do nothing.
      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)
      Return the value if present, otherwise return other.
      boolean orElseGet(BooleanSupplier other)
      Return the value if present, otherwise invoke other and return the result of that invocation.
      <X extends Throwable>
      boolean
      orElseThrow(Supplier<X> exceptionSupplier)
      Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.
      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)
        Return the value if present, otherwise return 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)
        Return the value if present, otherwise invoke other and return 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
        Return the contained value, if present, otherwise throw an exception to be 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
      • ifPresent

        public void ifPresent(IntConsumer consumer)
        Have the specified consumer accept the value if a value is present, otherwise do nothing. There is no BooleanConsumer in JDK. Value of this OptionalBoolean is therefore widened to int and passed to IntConsumer. Value of true is widened to 1, false to 0.
        Parameters:
        consumer - block to be executed if a value is present
        Throws:
        NullPointerException - if value is present and consumer is null
      • 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