Class Optionals


  • public class Optionals
    extends java.lang.Object
    Utilities to use with Optional.
    Author:
    Garret Wilson
    • Constructor Summary

      Constructors 
      Constructor Description
      Optionals()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> void ifPresentOrElse​(java.util.Optional<T> optional, java.util.function.Consumer<? super T> action, java.lang.Runnable emptyAction)
      Performs an action with the value if a value is present, otherwise performs another action.
      static boolean isPresentAndEquals​(java.util.Optional<?> optional, java.lang.Object object)
      Determines whether an optional value is present and is equal to some other nullable object.
      static <T> java.util.Optional<T> or​(java.util.Optional<T> optional, java.util.function.Supplier<java.util.Optional<T>> supplier)
      Returns another another optional from a supplier if the given optional is not present.
      static <T> java.util.stream.Stream<T> stream​(java.util.Optional<T> optional)
      Converts an optional to a stream.
      static java.util.OptionalDouble toOptionalDouble​(java.util.Optional<java.lang.Double> optional)
      Converts an optional wrapper Double instance to a primitive containing OptionalDouble instance.
      static java.util.OptionalInt toOptionalInt​(java.util.Optional<java.lang.Integer> optional)
      Converts an optional wrapper Integer instance to a primitive containing OptionalInt instance.
      static java.util.OptionalLong toOptionalLong​(java.util.Optional<java.lang.Long> optional)
      Converts an optional wrapper Long instance to a primitive containing OptionalLong instance.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Optionals

        public Optionals()
    • Method Detail

      • or

        public static <T> java.util.Optional<T> or​(@Nonnull
                                                   java.util.Optional<T> optional,
                                                   @Nonnull
                                                   java.util.function.Supplier<java.util.Optional<T>> supplier)
        Returns another another optional from a supplier if the given optional is not present.
        API Note:
        This method duplicates functionality in Java 9.
        Type Parameters:
        T - The type of value contained in the optional.
        Parameters:
        optional - The optional to check
        supplier - The supplier of an alternative optional if the value of the given optional is not present.
        Returns:
        The given optional or, if the value is not present, an optional one returned by the given supplier.
        Throws:
        java.lang.NullPointerException - if the supplier is null or returns null.
        See Also:
        JDK-8080418, Optional.or()
      • ifPresentOrElse

        public static <T> void ifPresentOrElse​(@Nonnull
                                               java.util.Optional<T> optional,
                                               @Nonnull
                                               java.util.function.Consumer<? super T> action,
                                               @Nonnull
                                               java.lang.Runnable emptyAction)
        Performs an action with the value if a value is present, otherwise performs another action.
        API Note:
        This method duplicates functionality in Java 9.
        Type Parameters:
        T - The type of value contained in the optional.
        Parameters:
        optional - The optional to check
        action - The action to perform if the value is present.
        emptyAction - The action to perform if no value is present.
        Throws:
        java.lang.NullPointerException - if a value is present and the given action is null, or if no value is present and the given empty action is null.
        See Also:
        Optional.ifPresentOrElse()
      • isPresentAndEquals

        public static boolean isPresentAndEquals​(@Nonnull
                                                 java.util.Optional<?> optional,
                                                 @Nullable
                                                 java.lang.Object object)
        Determines whether an optional value is present and is equal to some other nullable object.
        API Note:
        Note that the given object with which to compare the optional object can be null, as per the parameter of Object.equals(Object).
        Implementation Specification:
        This method is equivalent to calling Optional.isPresent() and if the result is true, calling Object.equals(Object) on the contained object.
        Parameters:
        optional - The optional to check
        object - The object to compare for equality with the optional value.
        Returns:
        true if the given optional value is present and is equal to the given object.
        Throws:
        java.lang.NullPointerException - if the given optional is null.
        See Also:
        Optional.isPresent(), Optional.get(), Optional.equals(Object)
      • stream

        public static <T> java.util.stream.Stream<T> stream​(@Nonnull
                                                            java.util.Optional<T> optional)
        Converts an optional to a stream.
        API Note:
        This method duplicates functionality in Java 9.
        Type Parameters:
        T - The type of value contained in the optional.
        Parameters:
        optional - The optional to check
        Returns:
        A stream, either containing the optional value, or empty if the optional is empty.
        See Also:
        Optional.stream()
      • toOptionalDouble

        public static java.util.OptionalDouble toOptionalDouble​(@Nonnull
                                                                java.util.Optional<java.lang.Double> optional)
        Converts an optional wrapper Double instance to a primitive containing OptionalDouble instance.
        Parameters:
        optional - The Optional instance to convert.
        Returns:
        The equivalent primitive optional wrapper.
      • toOptionalInt

        public static java.util.OptionalInt toOptionalInt​(@Nonnull
                                                          java.util.Optional<java.lang.Integer> optional)
        Converts an optional wrapper Integer instance to a primitive containing OptionalInt instance.
        Parameters:
        optional - The Optional instance to convert.
        Returns:
        The equivalent primitive optional wrapper.
      • toOptionalLong

        public static java.util.OptionalLong toOptionalLong​(@Nonnull
                                                            java.util.Optional<java.lang.Long> optional)
        Converts an optional wrapper Long instance to a primitive containing OptionalLong instance.
        Parameters:
        optional - The Optional instance to convert.
        Returns:
        The equivalent primitive optional wrapper.