Package com.forgerock.opendj.util
Class ReferenceCountedObject<T>
- java.lang.Object
-
- com.forgerock.opendj.util.ReferenceCountedObject<T>
-
- Type Parameters:
T- The type of referenced object.
public abstract class ReferenceCountedObject<T> extends Object
An object which is lazily created when first referenced, and destroyed when the last reference is released.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classReferenceCountedObject.ReferenceA reference to the reference counted object which will automatically be released during garbage collection.
-
Constructor Summary
Constructors Modifier Constructor Description protectedReferenceCountedObject()Creates a new referenced object whose reference count is initially zero.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description ReferenceCountedObject.Referenceacquire()Returns a reference to the reference counted object.ReferenceCountedObject.ReferenceacquireIfNull(T value)Returns a reference to the provided object or, if it isnull, a reference to the reference counted object.protected abstract voiddestroyInstance(T instance)Invoked when a reference is released and the reference count will become zero.protected abstract TnewInstance()Invoked when a reference is acquired and the current reference count is zero.
-
-
-
Method Detail
-
acquire
public final ReferenceCountedObject.Reference acquire()
Returns a reference to the reference counted object.- Returns:
- A reference to the reference counted object.
-
acquireIfNull
public final ReferenceCountedObject.Reference acquireIfNull(T value)
Returns a reference to the provided object or, if it isnull, a reference to the reference counted object.- Parameters:
value- The object to be referenced, ornullif the reference counted object should be used.- Returns:
- A reference to the provided object or, if it is
null, a reference to the reference counted object.
-
destroyInstance
protected abstract void destroyInstance(T instance)
Invoked when a reference is released and the reference count will become zero. Implementations should release any resources associated with the resource and should not return until the resources have been released.- Parameters:
instance- The instance to be destroyed.
-
newInstance
protected abstract T newInstance()
Invoked when a reference is acquired and the current reference count is zero. Implementations should create a new instance as fast as possible.- Returns:
- The new instance.
-
-