Class Lazy<T>

  • Type Parameters:
    T - the type of the embed value

    @ParametersAreNonnullByDefault
    public final class Lazy<T>
    extends Object
    An object that automatically loads an object of type T by using a Supplier during the first call to get().
    • Method Detail

      • of

        @Nonnull
        public static <T> Lazy<T> of​(T value)
        Creates a new lazy object with the embed value.
        Type Parameters:
        T - the type of the embed value
        Parameters:
        value - the embed value
        Returns:
        a new lazy object
      • with

        @Nonnull
        public static <T> Lazy<T> with​(Supplier<T> loadFunction)
        Creates a new lazy object with the Supplier to load the embed value.
        Type Parameters:
        T - the type of the embed value
        Parameters:
        loadFunction - the function used to load the value
        Returns:
        a new lazy object
      • get

        public T get()
        Gets the wrapped value. On the first call, the object is created.
        Returns:
        the value
      • update

        public void update​(@Nullable
                           T newValue)
        Updates the wrapped value.
        Parameters:
        newValue - the new value
      • update

        public void update​(UnaryOperator<T> updateOperator)
        Updates the wrapped value with a UnaryOperator.
        Parameters:
        updateOperator - the operator to update the wrapped value
      • isLoaded

        public boolean isLoaded()
        Returns true if the value has been loaded.
        Returns:
        true if the value has been loaded
      • ifLoaded

        public void ifLoaded​(Consumer<T> consumer)
        If a value is loaded, invoke the specified consumer with the value, otherwise do nothing.
        Parameters:
        consumer - action to be executed if a value is present
      • unload

        public void unload()
        Unloads the wrapped value. If the value is not loaded, then this method does nothing.