Package io.atlassian.fugue
Class Iterables.LazyReference<T>
- java.lang.Object
-
- java.lang.ref.Reference<T>
-
- java.lang.ref.WeakReference<T>
-
- io.atlassian.fugue.Iterables.LazyReference<T>
-
- Type Parameters:
T- the type of the contained element.
- All Implemented Interfaces:
java.util.function.Supplier<T>
- Direct Known Subclasses:
Iterables.Memoizer.Lazy
- Enclosing class:
- Iterables
abstract static class Iterables.LazyReference<T> extends java.lang.ref.WeakReference<T> implements java.util.function.Supplier<T>Class supports the implementation ofIterables.memoize(Iterable)and is not intended for general use. 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 thefinalkeyword has been used (you cannot reset it once it has been constructed). Object creation is guaranteed to be thread-safe and the first thread that callsget()will be the one that creates it.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 callget()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 callingget()callgetInterruptibly(). However, If yourcreate()method is interrupted and throws anInterruptedException, it is treated as an application exception and will be the causal exception inside the runtimeIterables.LazyReference.InitializationExceptionthatget()orgetInterruptibly()throws and yourcreate()will not be called again.This class is NOT
Serializable.Implementation note. This class extends
WeakReferenceasReferencedoes 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 overrideget(), 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
Nested Classes Modifier and Type Class Description static classIterables.LazyReference.InitializationExceptionIf the factorycreate()method threw an exception, this wraps it.(package private) static classIterables.LazyReference.Stateprivate classIterables.LazyReference.SyncSynchronization control for LazyReference.
-
Field Summary
Fields Modifier and Type Field Description private Iterables.LazyReference.Syncsync
-
Constructor Summary
Constructors Constructor Description LazyReference()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidcancel()Cancel the initializing operation if it has not already run.protected abstract Tcreate()The object factory method, guaranteed to be called once and only once.Tget()Get the lazily loaded reference in a non-cancellable manner.TgetInterruptibly()Get the lazily loaded reference in a cancellable manner.booleanisInitialized()Has thecreate()reference been initialized.
-
-
-
Field Detail
-
sync
private final Iterables.LazyReference.Sync sync
-
-
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()andgetInterruptibly()will return. - Throws:
java.lang.Exception- if anything goes wrong, rethrown as an InitializationException fromget()andgetInterruptibly()
-
get
public final T get()
Get the lazily loaded reference in a non-cancellable manner. If yourcreate()method throws an Exception calls toget()will throw an InitializationException which wraps the previously thrown exception.- Specified by:
getin interfacejava.util.function.Supplier<T>- Overrides:
getin classjava.lang.ref.Reference<T>- Returns:
- the object that
create()created. - Throws:
Iterables.LazyReference.InitializationException- if thecreate()method throws an exception. TheThrowable.getCause()will contain the exception thrown by thecreate()method
-
getInterruptibly
public final T getInterruptibly() throws java.lang.InterruptedException
Get the lazily loaded reference in a cancellable manner. If yourcreate()method throws an Exception, calls toget()will throw a RuntimeException which wraps the previously thrown exception.- Returns:
- the object that
create()created. - Throws:
Iterables.LazyReference.InitializationException- if thecreate()method throws an exception. TheThrowable.getCause()will contain the exception thrown by thecreate()methodjava.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, theInterruptedExceptionwill be thrown as the causal exception of theIterables.LazyReference.InitializationExceptionto everybody calling this method).
-
isInitialized
public final boolean isInitialized()
Has thecreate()reference been initialized.- Returns:
- true if the task is complete
-
cancel
public final void cancel()
Cancel the initializing operation if it has not already run. Will try and interrupt if it is currently running.
-
-