Interface Retainable
- All Known Subinterfaces:
 Content.Chunk, RetainableByteBuffer, RetainableByteBuffer.Mutable
- All Known Implementing Classes:
 AbstractRetainableByteBuffer, ArrayByteBufferPool.Tracking.TrackedBuffer, Content.Chunk.Empty, Retainable.ReferenceCounter, Retainable.Wrapper, RetainableByteBuffer.Abstract, RetainableByteBuffer.DynamicCapacity, RetainableByteBuffer.EmptyRetainableByteBuffer, RetainableByteBuffer.FixedCapacity, RetainableByteBuffer.NonRetainableByteBuffer, RetainableByteBuffer.Pooled, RetainableByteBuffer.Wrapper, Stream.Data, Trailers
A reference counted resource, for example one that is borrowed from a pool, that may be retained an additional number of times, and released a correspondent number of times, over its lifecycle.
The resource is typically implicitly retained when it is first created. It may be retained more times (thus incrementing its reference count) and released (thus decrementing its reference count), until the reference count goes to zero.
Idiomatic usage
The general rules to use Retainable objects are the following:
- If the 
Retainablehas been obtained by calling a method, and the caller code consumes it, then the caller code must callrelease(). - If the 
Retainablehas been obtained bycaller2by calling a method, andcaller2returns it without consuming it tocaller1, thencaller2must not callrelease(), sincecaller1will. - If the 
Retainablehas been obtained as a method argument, the receiver code must either:- Consume the 
Retainablesynchronously within the method, in which caserelease()must not be called. - Pass the 
Retainableto some other method, in which caserelease()must not be called. - Store away the 
Retainablefor later or asynchronous processing, for example storing it in containers such asCollections, or capturing it in a lambda that is passed to another thread, etc., in which caseretain()must be called and a mechanism to callrelease()later or asynchronously for this additionalretain()must be arranged. 
 - Consume the 
 
- 
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classA reference count implementation for aRetainableresource.static classA wrapper ofRetainableinstances. - 
Field Summary
Fields - 
Method Summary
Modifier and TypeMethodDescriptiondefault booleandefault intGet the retained count.default booleandefault booleanrelease()Releases this resource, potentially decrementing a reference count (if any).static <R extends Retainable>
Rrelease(R retainable) Convenience method that replaces code like:default voidretain()Retains this resource, potentially incrementing a reference count if there are resources that will be released. 
- 
Field Details
- 
NON_RETAINABLE
 
 - 
 - 
Method Details
- 
canRetain
default boolean canRetain()Returns whether this resource is referenced counted by calls to
retain()andrelease().Implementations may decide that special resources are not not referenced counted (for example,
staticconstants) so callingretain()is a no-operation, and callingrelease()on those special resources is a no-operation that always returns true.- Returns:
 - true if calls to 
retain()are reference counted. 
 - 
isRetained
 - 
retain
default void retain()Retains this resource, potentially incrementing a reference count if there are resources that will be released.
 - 
release
default boolean release()Releases this resource, potentially decrementing a reference count (if any).
- Returns:
 truewhen the reference count goes to zero or if there was no reference count,falseotherwise.
 - 
getRetained
default int getRetained()Get the retained count. This value is volatile and should only be used for informational/debugging purposes.
- Returns:
 - the retained count
 
 - 
release
Convenience method that replaces code like:
with:if (buffer != null) { buffer.release(); buffer = null; }buffer = Retainable.release(buffer);- Type Parameters:
 R- The type of the retainable- Parameters:
 retainable- The retainable to release, if notnull.- Returns:
 - always returns 
null 
 
 -