Class LazyLoaded<T>

  • Type Parameters:
    T - the type of the object to lazy load.
    All Implemented Interfaces:
    java.util.function.Supplier<T>

    public class LazyLoaded<T>
    extends java.lang.Object
    implements java.util.function.Supplier<T>
    Generic wrapper for thread safe lazy loading. For details see: https://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java
    • Field Summary

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

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T get()  
      boolean isLoaded()  
      • Methods inherited from class java.lang.Object

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

      • supplier

        private java.util.function.Supplier<T> supplier
      • instance

        private volatile T instance
    • Constructor Detail

      • LazyLoaded

        public LazyLoaded​(java.util.function.Supplier<T> supplier)
        Parameters:
        supplier - supplier to load the value lazy upon first request. Must not be null and should not return null.
    • Method Detail

      • get

        public T get()
        Specified by:
        get in interface java.util.function.Supplier<T>
        Returns:
        the value which is lazy loaded if necessary.
      • isLoaded

        public boolean isLoaded()
        Returns:
        true iff the data has been loaded