Class LazyReference<T>

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

    @ParametersAreNonnullByDefault
    public abstract class LazyReference<T>
    extends Object
    An object that automatically loads an object of type T by using a Supplier. The value is wrapped in a Reference that is automatically refreshed on-demand when it has been cleared, either by the program or by the garbage collector.
    See Also:
    Reference
    • Constructor Detail

      • LazyReference

        public LazyReference​(Supplier<T> refreshFunction)
        Constructs a new LazyReference.
        Parameters:
        refreshFunction - the function used to refresh the value
    • Method Detail

      • soft

        public static <T> LazyReference<T> soft​(Supplier<T> refreshFunction)
        Creates a new lazy reference that uses SoftReference.
        Type Parameters:
        T - the type of the embed value
        Parameters:
        refreshFunction - the function used to refresh the value
        Returns:
        a new lazy reference
      • weak

        public static <T> LazyReference<T> weak​(Supplier<T> refreshFunction)
        Creates a new lazy reference that uses WeakReference.
        Type Parameters:
        T - the type of the embed value
        Parameters:
        refreshFunction - the function used to refresh the value
        Returns:
        a new lazy reference
      • get

        public T get()
        Gets the value.
        Returns:
        the value
      • update

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

        @Nonnull
        protected abstract Reference<T> newReference​(@Nullable
                                                     T value)
        Creates a new Reference for the given value.
        Parameters:
        value - the value to embed
        Returns:
        a reference