Package com.complexible.common.base
Interface AutoDisposable
-
- All Superinterfaces:
Disposable
- All Known Implementing Classes:
AbstractAutoDisposable
public interface AutoDisposable extends Disposable
A special kind ofDisposable
that keeps track of references to it and disposes the object automatically when there are no references left. At creation time anAutoDisposable
has zero reference count so whenever same number ofrelease()
calls followsacquire()
calls, it will be automatically closed. CallingDisposable.dispose()
directly will dispose the object immediately regardless of the reference count.- Since:
- 3.0
- Version:
- 3.0
- Author:
- Evren Sirin
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <T extends AutoDisposable>
Tacquire()
Acquires a reference to this object preventing it from being automatically disposed until the reference is released.int
getRefCount()
Return the current reference count for this object.void
release()
Releases one of these object's references.<T extends AutoDisposable>
TtryAcquire()
Tries to acquire a reference to this object.-
Methods inherited from interface com.complexible.common.base.Disposable
dispose
-
-
-
-
Method Detail
-
acquire
<T extends AutoDisposable> T acquire()
Acquires a reference to this object preventing it from being automatically disposed until the reference is released.- Type Parameters:
T
-- Returns:
- this object
-
tryAcquire
<T extends AutoDisposable> T tryAcquire()
Tries to acquire a reference to this object. Returns the reference if the number of references to the object is greater than zero at the time of the call. If not, it's possible that the object has already been disposed sonull
is returned instead. Note: this method should be used after the object has beenacquired
for the first time. Before that it always returnsnull
. The common use case is that the firstacquire()
happens before the object is published and becomes visible to users.- Type Parameters:
T
- object type- Returns:
- this object
-
release
void release()
-
getRefCount
int getRefCount()
Return the current reference count for this object. Provided only for informational purposes.
-
-