com.atlassian.util.concurrent
Class LazyReference<T>

java.lang.Object
  extended by java.lang.ref.Reference<T>
      extended by java.lang.ref.WeakReference<T>
          extended by com.atlassian.util.concurrent.LazyReference<T>

@ThreadSafe
public abstract class LazyReference<T>
extends java.lang.ref.WeakReference<T>

Lazily loaded reference that is not constructed until required. This class is used to maintain a reference to an object that is expensive to create and must be constructed once and once only. This reference behaves as though the final keyword has been used (you cannot reset it once it has been constructed).

Usage: clients need to implement the create() method to return the object this reference will hold.

For instance:

 final LazyReference<MyObject> ref = new LazyReference() {
     protected MyObject create() throws Exception {
         // Do expensive object construction here
         return new MyObject();
     }
 };
 
Then call get() to get a reference to the referenced object:
 MyObject myLazyLoadedObject = ref.get()
 
NOTE: Interruption policy is that if you want to be cancellable while waiting for another thread to create the value, instead of calling get() call getInterruptibly(). However, If your create() method is interrupted and throws an InterruptedException, it is treated as an application exception and will be the causal exception inside the runtime LazyReference.InitializationException that get() or getInterruptibly() throws and your create() will not be called again.

Implementation note. This class extends WeakReference as Reference does not have a public constructor. WeakReference is preferable as it does not have any members and therefore doesn't increase the memory footprint. As we never pass a referent through to the super-class and override get(), the garbage collection semantics of WeakReference are irrelevant. The referenced object will not become eligible for GC unless the object holding the reference to this object is collectible.


Nested Class Summary
static class LazyReference.InitializationException
          If the factory create() method threw an exception, this wraps it.
 
Constructor Summary
LazyReference()
           
 
Method Summary
 void cancel()
          Has the create() reference been initialized.
protected abstract  T create()
          The object factory method, guaranteed to be called once and only once.
 T get()
          Get the lazily loaded reference in a non-cancellable manner.
 T getInterruptibly()
          Get the lazily loaded reference in a cancellable manner.
 boolean isInitialized()
          Has the create() reference been initialized.
 
Methods inherited from class java.lang.ref.Reference
clear, enqueue, isEnqueued
 
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

create

protected abstract T create()
                     throws java.lang.Exception
The object factory method, guaranteed to be called once and only once.

Returns:
the object that get() and getInterruptibly() will return.
Throws:
LazyReference.InitializationException - if anything goes wrong, rethrown as an InitializationException from get() and getInterruptibly()
java.lang.Exception

get

public final T get()
Get the lazily loaded reference in a non-cancellable manner. If your create() method throws an Exception calls to get() will throw an InitializationException which wraps the previously thrown exception.

Overrides:
get in class java.lang.ref.Reference<T>
Returns:
the object that create() created.
Throws:
LazyReference.InitializationException - if the create() method throws an exception. The Throwable.getCause() will contain the exception thrown by the create() method

getInterruptibly

public final T getInterruptibly()
                         throws java.lang.InterruptedException
Get the lazily loaded reference in a cancellable manner. If your create() method throws an Exception, calls to get() will throw a RuntimeException which wraps the previously thrown exception.

Returns:
the object that create() created.
Throws:
LazyReference.InitializationException - if the create() method throws an exception. The Throwable.getCause() will contain the exception thrown by the create() method
java.lang.InterruptedException - If the calling thread is Interrupted while waiting for another thread to create the value (if the creating thread is interrupted while blocking on something, the InterruptedException will be thrown as the causal exception of the LazyReference.InitializationException to everybody calling this method).

isInitialized

public boolean isInitialized()
Has the create() reference been initialized.

Returns:
true if the task is complete

cancel

public void cancel()
Has the create() reference been initialized.



Copyright © 2008 Atlassian Pty Ltd. All Rights Reserved.