Class Retainable.ReferenceCounter
- All Implemented Interfaces:
 Retainable
- Enclosing interface:
 Retainable
A reference count implementation for a Retainable resource.
The reference count is initialized to 1 when the resource is created,
and therefore it is implicitly retained and needs a call to release().
Additional calls to retain() must be matched by correspondent
calls to release().
When the reference count goes to zero, the resource may be pooled.
When the resource is acquired from the pool, acquire() should be
called to set the reference count to 1.
- 
Nested Class Summary
Nested classes/interfaces inherited from interface Retainable
Retainable.ReferenceCounter, Retainable.Wrapper - 
Field Summary
Fields inherited from interface Retainable
NON_RETAINABLE - 
Constructor Summary
Constructors - 
Method Summary
Modifier and TypeMethodDescriptionvoidacquire()Updates the reference count from0to1.booleanReturns whether this resource is referenced counted by calls toRetainable.retain()andRetainable.release().intget()intGet the retained count.booleanReturns whetherRetainable.retain()has been called at least one more time thanRetainable.release().booleanrelease()Releases this resource, potentially decrementing a reference count (if any).voidretain()Retains this resource, potentially incrementing a reference count if there are resources that will be released.toString()boolean 
- 
Constructor Details
- 
ReferenceCounter
public ReferenceCounter() - 
ReferenceCounter
protected ReferenceCounter(int initialCount)  
 - 
 - 
Method Details
- 
get
public int get()- Returns:
 - the current reference count
 
 - 
acquire
public void acquire()Updates the reference count from
0to1.This method should only be used when this resource is acquired from a pool.
 - 
canRetain
public boolean canRetain()Description copied from interface:RetainableReturns whether this resource is referenced counted by calls to
Retainable.retain()andRetainable.release().Implementations may decide that special resources are not not referenced counted (for example,
staticconstants) so callingRetainable.retain()is a no-operation, and callingRetainable.release()on those special resources is a no-operation that always returns true.- Specified by:
 canRetainin interfaceRetainable- Returns:
 - true if calls to 
Retainable.retain()are reference counted. 
 - 
retain
public void retain()Description copied from interface:RetainableRetains this resource, potentially incrementing a reference count if there are resources that will be released.
- Specified by:
 retainin interfaceRetainable
 - 
tryRetain
public boolean tryRetain() - 
release
public boolean release()Description copied from interface:RetainableReleases this resource, potentially decrementing a reference count (if any).
- Specified by:
 releasein interfaceRetainable- Returns:
 truewhen the reference count goes to zero or if there was no reference count,falseotherwise.
 - 
isRetained
public boolean isRetained()Description copied from interface:RetainableReturns whether
Retainable.retain()has been called at least one more time thanRetainable.release().- Specified by:
 isRetainedin interfaceRetainable- Returns:
 - whether this buffer is retained
 
 - 
getRetained
public int getRetained()Description copied from interface:RetainableGet the retained count. This value is volatile and should only be used for informational/debugging purposes.
- Specified by:
 getRetainedin interfaceRetainable- Returns:
 - the retained count
 
 - 
toString
 
 -