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-purposeAlterableContextthat 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 intdecrementReferenceCount(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.intdecrementReferenceCount(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.voiddestroy(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> Tget(Contextual<T> contextual)Returns the contextual instance associated with the current thread and the suppliedContextual, ornullif no such contextual instance exists.<T> Tget(Contextual<T> contextual, CreationalContext<T> cc)Returns the contextual instance associated with the current thread and the suppliedContextual, creating it if necessary.static ReferenceCountedContextgetInstanceFrom(BeanManager beanManager)Returns the soleReferenceCountedContextthat is registered with the suppliedBeanManager.intgetReferenceCount(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.classwhen invoked.booleanisActive()Returnstruewhen 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- theContextualwhose instance's reference count should be decremented; may benullin which case no action will be taken and0will 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- theContextualwhose instance's reference count should be decremented; may benullin which case no action will be taken and0will be returnedamount- the amount by which to decrement; must be greater than or (trivially) equal to0- Returns:
- the resulting reference count
- Throws:
IllegalArgumentException- ifamountis 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- theContextualwhose instance's reference count should be returned; may benullin which case0will 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:
destroyin interfaceAlterableContext- Parameters:
contextual- theContextualto destroy; may benullin 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, ornullif no such contextual instance exists.- Specified by:
getin interfaceContext- Parameters:
contextual- theContextualin question; may benullin which casenullwil 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:
getin interfaceContext- Parameters:
contextual- theContextualin question; may benullin which casenullwil be returnedcc- aCreationalContextthat will hold dependent instances; may benull- Returns:
- the contextual instance associated with the current
thread and the supplied
Contextual; may strictly speaking benullbut normally is not
-
getScope
public Class<? extends Annotation> getScope()
ReturnsReferenceCounted.classwhen invoked.- Specified by:
getScopein interfaceContext- Returns:
ReferenceCounted.classin all cases
-
isActive
public boolean isActive()
Returnstruewhen invoked.
-
getInstanceFrom
public static ReferenceCountedContext getInstanceFrom(BeanManager beanManager)
Returns the soleReferenceCountedContextthat is registered with the suppliedBeanManager.Strictly speaking, this method may return
nullif theReferenceCountedExtensionhas been deliberately disabled and hence has not had a chance to install aReferenceCountedContext. In all normal usage this method will not returnnull.- Parameters:
beanManager- theBeanManagerwhoseReferenceCountedContextshould be returned; must not benull- Returns:
- a
ReferenceCountedContext, ornullin exceptionally rare situations - Throws:
NullPointerException- ifbeanManagerisnull
-
-