Class ReferenceCountedContext
- java.lang.Object
-
- io.helidon.integrations.cdi.referencecountedcontext.ReferenceCountedContext
-
- All Implemented Interfaces:
AlterableContext
,Context
public final class ReferenceCountedContext extends Object implements AlterableContext
A somewhat special-purposeAlterableContext
that destroys a contextual instance when its thread-specific reference count drops to zero or less than zero.A contextual instance's thread-specific reference count is incremented when either the
get(Contextual)
orget(Contextual, CreationalContext)
method is called. It is decremented when the obtained instance is passed to thedecrementReferenceCount(Contextual)
method, which is indirectly and solely responsible for that instance's ultimate removal from thisReferenceCountedContext
.Internally, the
destroy(Contextual)
method simply callsdecrementReferenceCount(Contextual)
.Thread Safety
Instances of this class are safe for concurrent use by multiple threads.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
decrementReferenceCount(Contextual<?> c)
Decrements the reference count of the contextual instance, if any, associated with the combination of the current thread and the suppliedContextual
, destroying the instance if and only if the reference count becomes less than or equal to zero, and returns the resulting reference count.int
decrementReferenceCount(Contextual<?> c, int amount)
Decrements the reference count of the contextual instance, if any, associated with the combination of the current thread and the suppliedContextual
, destroying the instance if and only if the reference count becomes less than or equal to zero, and returns the resulting reference count.void
destroy(Contextual<?> contextual)
Calls thedecrementReferenceCount(Contextual)
method with the suppliedContextual
, destroying it if and only if its thread-specific reference count becomes less than or equal to zero.<T> T
get(Contextual<T> contextual)
Returns the contextual instance associated with the current thread and the suppliedContextual
, ornull
if no such contextual instance exists.<T> T
get(Contextual<T> contextual, CreationalContext<T> cc)
Returns the contextual instance associated with the current thread and the suppliedContextual
, creating it if necessary.static ReferenceCountedContext
getInstanceFrom(BeanManager beanManager)
Returns the soleReferenceCountedContext
that is registered with the suppliedBeanManager
.int
getReferenceCount(Contextual<?> c)
Returns the reference count of the contextual instance, if any, associated with the combination of the current thread and the suppliedContextual
.Class<? extends Annotation>
getScope()
ReturnsReferenceCounted.class
when invoked.boolean
isActive()
Returnstrue
when invoked.
-
-
-
Method Detail
-
decrementReferenceCount
public int decrementReferenceCount(Contextual<?> c)
Decrements the reference count of the contextual instance, if any, associated with the combination of the current thread and the suppliedContextual
, destroying the instance if and only if the reference count becomes less than or equal to zero, and returns the resulting reference count.- Parameters:
c
- theContextual
whose instance's reference count should be decremented; may benull
in which case no action will be taken and0
will be returned- Returns:
- the resulting reference count
- See Also:
Contextual.destroy(Object, CreationalContext)
,get(Contextual, CreationalContext)
-
decrementReferenceCount
public int decrementReferenceCount(Contextual<?> c, int amount)
Decrements the reference count of the contextual instance, if any, associated with the combination of the current thread and the suppliedContextual
, destroying the instance if and only if the reference count becomes less than or equal to zero, and returns the resulting reference count.Most users will never have to call this method.
- Parameters:
c
- theContextual
whose instance's reference count should be decremented; may benull
in which case no action will be taken and0
will be returnedamount
- the amount by which to decrement; must be greater than or (trivially) equal to0
- Returns:
- the resulting reference count
- Throws:
IllegalArgumentException
- ifamount
is less than0
- See Also:
Contextual.destroy(Object, CreationalContext)
,get(Contextual, CreationalContext)
,decrementReferenceCount(Contextual)
-
getReferenceCount
public int getReferenceCount(Contextual<?> c)
Returns the reference count of the contextual instance, if any, associated with the combination of the current thread and the suppliedContextual
.Most users will never have to call this method.
This method never returns a negative number.
- Parameters:
c
- theContextual
whose instance's reference count should be returned; may benull
in which case0
will be returned- Returns:
- the reference count in question; never a negative number
-
destroy
public void destroy(Contextual<?> contextual)
Calls thedecrementReferenceCount(Contextual)
method with the suppliedContextual
, destroying it if and only if its thread-specific reference count becomes less than or equal to zero.- Specified by:
destroy
in interfaceAlterableContext
- Parameters:
contextual
- theContextual
to destroy; may benull
in which case no action will be taken- See Also:
decrementReferenceCount(Contextual)
,get(Contextual, CreationalContext)
-
get
public <T> T get(Contextual<T> contextual)
Returns the contextual instance associated with the current thread and the suppliedContextual
, ornull
if no such contextual instance exists.- Specified by:
get
in interfaceContext
- Parameters:
contextual
- theContextual
in question; may benull
in which casenull
wil be returned- Returns:
- the contextual instance associated with the current
thread and the supplied
Contextual
, ornull
-
get
public <T> T get(Contextual<T> contextual, CreationalContext<T> cc)
Returns the contextual instance associated with the current thread and the suppliedContextual
, creating it if necessary.- Specified by:
get
in interfaceContext
- Parameters:
contextual
- theContextual
in question; may benull
in which casenull
wil be returnedcc
- aCreationalContext
that will hold dependent instances; may benull
- Returns:
- the contextual instance associated with the current
thread and the supplied
Contextual
; may strictly speaking benull
but normally is not
-
getScope
public Class<? extends Annotation> getScope()
ReturnsReferenceCounted.class
when invoked.- Specified by:
getScope
in interfaceContext
- Returns:
ReferenceCounted.class
in all cases
-
isActive
public boolean isActive()
Returnstrue
when invoked.
-
getInstanceFrom
public static ReferenceCountedContext getInstanceFrom(BeanManager beanManager)
Returns the soleReferenceCountedContext
that is registered with the suppliedBeanManager
.Strictly speaking, this method may return
null
if theReferenceCountedExtension
has been deliberately disabled and hence has not had a chance to install aReferenceCountedContext
. In all normal usage this method will not returnnull
.- Parameters:
beanManager
- theBeanManager
whoseReferenceCountedContext
should be returned; must not benull
- Returns:
- a
ReferenceCountedContext
, ornull
in exceptionally rare situations - Throws:
NullPointerException
- ifbeanManager
isnull
-
-