Class OptionalBoolean


  • @Beta
    public final class OptionalBoolean
    extends Object
    Utility class for storing an optional boolean in a single byte value. This cuts down the memory requirement quite at very small computational cost.

    Note: fields do not have to be explicitly initialized, as default initialization value for 'byte', 0, is used to represent 'absent' condition.

    Author:
    Robert Varga
    • Method Detail

      • isPresent

        public static boolean isPresent​(byte value)
        Check if a field value has been set, just like Optional.isPresent().
        Parameters:
        value - field value
        Returns:
        True if the value is set.
        Throws:
        IllegalArgumentException - if value is invalid
      • of

        public static byte of​(boolean bool)
        Encode a boolean to a field value, just like Optional.of(Object).
        Parameters:
        bool - Boolean value.
        Returns:
        Field value.
      • ofNullable

        public static byte ofNullable​(@Nullable Boolean bool)
        Convert a nullable Boolean into a field value, just like Optional.ofNullable(Object).
        Parameters:
        bool - Boolean value.
        Returns:
        Field value.
      • toNullable

        public static @Nullable Boolean toNullable​(byte value)
        Convert a field value to a nullable Boolean. Similar to Optional.orElse(null).
        Parameters:
        value - Field value.
        Returns:
        Nullable Boolean.
      • toString

        public static @NonNull String toString​(byte value)
        Convert a field value into a String representation.
        Parameters:
        value - Field value.
        Returns:
        Boolean-compatible string, or "absent".
        Throws:
        IllegalArgumentException - if value is invalid.