Interface Lazy<T>

  • All Known Subinterfaces:
    LazyResettable<C>
    All Known Implementing Classes:
    LazyImpl, LazyResettableImpl, LazyResettableWithTearDown

    public interface Lazy<T>
    This utility provides lazy initialization for a wrapped object. API-wise, it can be considered similar to Optional with the main difference that since it will never be Optional.empty(), the get() function will never fail. Rather, get() will result in initializing the object if initialization has not happened yet. All other functions mimic the behavior of their counterpart in Optional and do NOT trigger initialization if it has not happened yet. Note that Lazy does allow wrapping a null if that is what the lazy initialization returns. For the use case of lazy initializing to something which may or may not exist, the implementer should consider combining this class with Optional, thus resulting in a Lazy. It is recommended to use instances of Lazy exclusively in conjunction with the final keyword, such that it is not allowed to swap the reference to a new Lazy instance. Following the recommended usage allows making the assumption that there will be no more than one initialization, and that the transition from uninitialized to initialized is one-way and irreversible. This interface promises to be threadsafe if multiple concurrent calls happen before the first initialization finishes, and furthermore guarantees that the initialization routine will happen at most once.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Lazy<java.lang.Boolean> FALSE  
      static Lazy<java.lang.Boolean> TRUE  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.util.Optional<T> filter​(java.util.function.Predicate<? super T> predicate)
      If a value is initialized, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.
      <U> java.util.Optional<U> flatMap​(java.util.function.Function<? super T,​java.util.Optional<U>> mapper)
      If a value is initialized, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional.
      T get()  
      void ifPresent​(java.util.function.Consumer<? super T> consumer)  
      boolean isPresent()  
      <U> java.util.Optional<U> map​(java.util.function.Function<? super T,​? extends U> mapper)
      If a value is initialized, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result.
      static <C> Lazy<C> of​(java.util.function.Supplier<C> supplier)  
      T orElse​(T other)
      Return the value if initialized, otherwise return other.
      T orElseGet​(java.util.function.Supplier<? extends T> other)
      Return the value if initialized, otherwise invoke other and return the result of that invocation.
      <X extends java.lang.Throwable>
      T
      orElseThrow​(java.util.function.Supplier<? extends X> exceptionSupplier)
      Return the contained value, if initialized, otherwise throw an exception to be created by the provided supplier.
    • Field Detail

      • FALSE

        static final Lazy<java.lang.Boolean> FALSE
      • TRUE

        static final Lazy<java.lang.Boolean> TRUE
    • Method Detail

      • of

        static <C> Lazy<C> of​(java.util.function.Supplier<C> supplier)
        Parameters:
        supplier - to initialize the wrapped value
        Returns:
        an instance of Lazy which will execute the {@param supplier} if needed
      • get

        T get()
        Returns:
        the wrapped value, with the side-effect of initializing it has not happened yet
      • ifPresent

        void ifPresent​(java.util.function.Consumer<? super T> consumer)
        Parameters:
        consumer - to pass the wrapped value to, only if it has already been initialized
      • isPresent

        boolean isPresent()
        Returns:
        true if there is a value present (i.e. initialized), otherwise false
      • filter

        java.util.Optional<T> filter​(java.util.function.Predicate<? super T> predicate)
        If a value is initialized, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.
        Parameters:
        predicate - a predicate to apply to the value, if initialized
        Returns:
        an Optional describing the value of this Optional if a value is present and the value matches the given predicate, otherwise an empty Optional
        Throws:
        java.lang.NullPointerException - if the predicate is null
      • map

        <U> java.util.Optional<U> map​(java.util.function.Function<? super T,​? extends U> mapper)
        If a value is initialized, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.
        Type Parameters:
        U - The type of the result of the mapping function
        Parameters:
        mapper - a mapping function to apply to the value, if initialized
        Returns:
        an Optional describing the result of applying a mapping function to the value of this Optional, if a value is initialized, otherwise an empty Optional
        Throws:
        java.lang.NullPointerException - if the mapping function is null
      • flatMap

        <U> java.util.Optional<U> flatMap​(java.util.function.Function<? super T,​java.util.Optional<U>> mapper)
        If a value is initialized, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional. This method is similar to map(Function), but the provided mapper is one whose result is already an Optional, and if invoked, flatMap does not wrap it with an additional Optional.
        Type Parameters:
        U - The type parameter to the Optional returned by the mapping function
        Parameters:
        mapper - a mapping function to apply to the value, if initialized
        Returns:
        the result of applying an Optional-bearing mapping function to the value of this Optional, if a value is initialized, otherwise an empty Optional
        Throws:
        java.lang.NullPointerException - if the mapping function is null or returns a null result
      • orElse

        T orElse​(T other)
        Return the value if initialized, otherwise return other.
        Parameters:
        other - the value to be returned if there is no value initialized, may be null
        Returns:
        the value, if initialized, otherwise other
      • orElseGet

        T orElseGet​(java.util.function.Supplier<? extends T> other)
        Return the value if initialized, otherwise invoke other and return the result of that invocation.
        Parameters:
        other - a Supplier whose result is returned if no value is initialized
        Returns:
        the value if initialized otherwise the result of other.get()
        Throws:
        java.lang.NullPointerException - if value is not initialized and other is null
      • orElseThrow

        <X extends java.lang.Throwable> T orElseThrow​(java.util.function.Supplier<? extends X> exceptionSupplier)
                                               throws X extends java.lang.Throwable
        Return the contained value, if initialized, otherwise throw an exception to be created by the provided supplier.
        Type Parameters:
        X - Type of the exception to be thrown
        Parameters:
        exceptionSupplier - The supplier which will return the exception to be thrown
        Returns:
        the initialized value
        Throws:
        X - if there is no value initialized
        java.lang.NullPointerException - if no value is initialized and exceptionSupplier is null
        X extends java.lang.Throwable