Class CacheSupplier<T>

  • Type Parameters:
    T -
    All Implemented Interfaces:
    java.util.function.Supplier<T>

    public class CacheSupplier<T>
    extends java.lang.Object
    implements java.util.function.Supplier<T>
    Delegates the first get() call to the given supplier and stores its result in cache. Consecutive calls return the cached result, therefore computation is only done once. Does not maintain a reference to the delegate once its result is computed.
    Author:
    Mathias Hofer
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private T cache  
      private java.util.function.Supplier<T> delegate  
    • Constructor Summary

      Constructors 
      Constructor Description
      CacheSupplier​(java.util.function.Supplier<T> delegate)  
    • Method Summary

      Modifier and Type Method Description
      T get()  
      • Methods inherited from class java.lang.Object

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

      • cache

        private T cache
      • delegate

        private java.util.function.Supplier<T> delegate
    • Constructor Detail

      • CacheSupplier

        public CacheSupplier​(java.util.function.Supplier<T> delegate)
    • Method Detail

      • get

        public T get()
        Specified by:
        get in interface java.util.function.Supplier<T>