Class LazyReference<T>

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public abstract class LazyReference<T>
    extends java.lang.Object
    implements java.io.Closeable
    A class that keeps a reference to an object and initializes it in a lazy fashion. This class is meant to be used with anonymous class extensions to define initValue() much like ThreadLocal does. Any Exception thrown during initialization will be turned into a RuntimeException if it already is not one.

    This class is not thread-safe when use and closing of the reference occur in different threads. Closing of the reference will quietly close the underlying value. Exceptions during close are logged at the DEBUG level.

    • Constructor Summary

      Constructors 
      Constructor Description
      LazyReference()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      T get()
      The object will be atomically initialized unless it already has been initialized.
      protected abstract T initValue()  
      • Methods inherited from class java.lang.Object

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

      • LazyReference

        public LazyReference()
    • Method Detail

      • initValue

        protected abstract T initValue()
                                throws java.lang.Exception
        Throws:
        java.lang.Exception
      • get

        public T get()
        The object will be atomically initialized unless it already has been initialized. Note that this class performs no locking and there is a small possibility of the object being created multiple times with the extra objects being subsequently closed.
        Returns:
        the initialized object
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable