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 org.eclipse.jetty.io.Retainable
Retainable.ReferenceCounter, Retainable.Wrapper
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
acquire()
Updates the reference count from0
to1
.boolean
Returns whether this resource is referenced counted by calls toRetainable.retain()
andRetainable.release()
.int
get()
boolean
boolean
release()
Releases this resource, potentially decrementing a reference count (if any).void
retain()
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
0
to1
.This method should only be used when this resource is acquired from a pool.
-
canRetain
public boolean canRetain()Description copied from interface:Retainable
Returns 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,
static
constants) so callingRetainable.retain()
is a no-operation, and callingRetainable.release()
on those special resources is a no-operation that always returns true.- Specified by:
canRetain
in interfaceRetainable
- Returns:
- true if calls to
Retainable.retain()
are reference counted.
-
retain
public void retain()Description copied from interface:Retainable
Retains this resource, potentially incrementing a reference count if there are resources that will be released.
- Specified by:
retain
in interfaceRetainable
-
tryRetain
public boolean tryRetain() -
release
public boolean release()Description copied from interface:Retainable
Releases this resource, potentially decrementing a reference count (if any).
- Specified by:
release
in interfaceRetainable
- Returns:
true
when the reference count goes to zero or if there was no reference count,false
otherwise.
-
isRetained
public boolean isRetained()- Returns:
- whether this buffer is retained
-
toString
-