com.atlassian.plugin.servlet.util
Class LazyLoadedReference<V>

java.lang.Object
  extended by com.atlassian.plugin.servlet.util.LazyLoadedReference<V>

public abstract class LazyLoadedReference<V>
extends java.lang.Object

Thread-safe lock-less (see note) 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. Therefore this reference behaves as though the final keyword has been used (you cannot reset it once it has been constructed).

When using this class you need to implement the create() method to return the object this reference will hold.

For instance:

  final LazyLoadedReference ref = new LazyLoadedReference()
  {
    protected Object create() throws Exception
    {
       // Do some useful object construction here
        return new MyObject();
    }
  };
 
Then call to get a reference to the object:
   MyObject myLazyLoadedObject = (MyObject) ref.get()
 

Note: Copied from JIRA com.atlassian.jira.util.concurrent.ThreadsafeLazyLoadedReference and modified to use generics and java.util.concurrent.

Since:
2.1.0

Nested Class Summary
static class LazyLoadedReference.InitializationException
          The factory create() method threw an exception.
 
Constructor Summary
LazyLoadedReference()
           
 
Method Summary
protected abstract  V create()
          The object factory method, guaranteed to be called once and only once.
 V get()
          Get the lazily loaded reference.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LazyLoadedReference

public LazyLoadedReference()
Method Detail

get

public final V get()
Get the lazily loaded reference. If your create() method throws an Exception, calls to get() will throw a RuntimeException which wraps the previously thrown exception.


create

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

protected abstract V create() throws Exception;

Throws:
java.lang.Exception


Copyright © 2009 Atlassian Pty Ltd. All Rights Reserved.