Class CachedSupplier<T>

java.lang.Object
net.darkhax.pricklemc.common.api.util.CachedSupplier<T>
Type Parameters:
T - The type cached by the supplier.
All Implemented Interfaces:
Supplier<T>

public class CachedSupplier<T> extends Object implements Supplier<T>
An implementation of Supplier that will cache the result.
  • Constructor Details

    • CachedSupplier

      protected CachedSupplier(Supplier<T> delegate)
  • Method Details

    • get

      public T get()
      Specified by:
      get in interface Supplier<T>
    • invalidate

      public void invalidate()
      Invalidates the cached value. This will result in a new value being cached the next type get() is used.
    • isCached

      public boolean isCached()
      Checks if this supplier has a cached value. This is not a substitute for null checking.
      Returns:
      Has the supplier cached a value.
    • ifCached

      public void ifCached(Consumer<T> consumer)
      Safely attempts to invoke a consumer with the cached value. If a value has not been cached the consumer will not be invoked and a new value will not be cached. The consumer will still be invoked if the cached value is null.
      Parameters:
      consumer - The consumer to invoke if a value has been cached.
    • ifPresent

      public void ifPresent(Consumer<T> consumer)
      Safely attempts to invoke a consumer with the cached value. If a value has not been cached, or the cached value is null the consumer will not be invoked and a new value will not be cached.
      Parameters:
      consumer - The consumer to invoke if a cached value is present.
    • apply

      public void apply(Consumer<T> consumer)
      Invokes the consumer with the cached value. This will cause a value to be cached if one has not been cached already.
      Parameters:
      consumer - The consumer to invoke.
    • cast

      public <X> CachedSupplier<X> cast()
      Performs an unsafe cast to the expected type.
    • singleton

      public static <T> CachedSupplier<T> singleton(T singleton)
      Creates a cached supplier that can only produce a single value.
      Type Parameters:
      T - The type of value held by the cache.
      Parameters:
      singleton - The only value for the cache to use.
      Returns:
      A cached supplier that will only produce a single cached value.
    • cache

      public static <T> CachedSupplier<T> cache(Supplier<T> delegate)
      Creates a cached supplier that will cache a value from the supplied delegate when queried.
      Type Parameters:
      T - The type of value held by the cache.
      Parameters:
      delegate - The delegate supplier responsible for producing the cached value. This will only be accessed when the cached supplier is being accessed and has not been cached already.
      Returns:
      A supplier that will cache a value from the supplied delegate supplier.