Class ReferenceCounted
java.lang.Object
com.illumon.util.referencecounting.ReferenceCounted
- All Implemented Interfaces:
com.fishlib.base.log.LogOutputAppendable
,Serializable
- Direct Known Subclasses:
LivenessArtifact
,LivenessScope
,ProceduralReferenceCounted
public abstract class ReferenceCounted extends Object implements com.fishlib.base.log.LogOutputAppendable, Serializable
Implements a recurring reference counting pattern - a concurrent reference count that should refuse to go below zero,
and invokes
onReferenceCountAtZero()
exactly once when the count returns to zero.- See Also:
- Serialized Form
-
Constructor Summary
Constructors Modifier Constructor Description protected
ReferenceCounted()
protected
ReferenceCounted(int initialValue)
-
Method Summary
Modifier and Type Method Description com.fishlib.base.log.LogOutput
append(com.fishlib.base.log.LogOutput logOutput)
void
decrementReferenceCount()
Decrement the reference count by one, if it has ever been increased and has not already been decreased to zero.void
incrementReferenceCount()
Increment the reference count by one, if it has not already been decreased to zero.protected abstract void
onReferenceCountAtZero()
Callback method that will be invoked when the reference count returns to zero.void
resetReferenceCount()
Reset this reference count to its initial state for reuse.String
toString()
boolean
tryDecrementReferenceCount()
Decrement the reference count by one, if it has ever been increased and has not already been decreased to zero.boolean
tryIncrementReferenceCount()
Increment the reference count by 1, if it has not already been decreased to 0.
-
Constructor Details
-
ReferenceCounted
protected ReferenceCounted() -
ReferenceCounted
protected ReferenceCounted(int initialValue)
-
-
Method Details
-
toString
-
append
public com.fishlib.base.log.LogOutput append(@NotNull com.fishlib.base.log.LogOutput logOutput)- Specified by:
append
in interfacecom.fishlib.base.log.LogOutputAppendable
-
resetReferenceCount
public final void resetReferenceCount()Reset this reference count to its initial state for reuse. -
tryIncrementReferenceCount
public final boolean tryIncrementReferenceCount()Increment the reference count by 1, if it has not already been decreased to 0.- Returns:
- Whether the reference count was successfully incremented
- Throws:
IllegalStateException
- If the reference count is already at its maximum referenceCount
-
incrementReferenceCount
public final void incrementReferenceCount()Increment the reference count by one, if it has not already been decreased to zero.- Throws:
IllegalStateException
- If the reference count was not successfully incremented
-
tryDecrementReferenceCount
public final boolean tryDecrementReferenceCount()Decrement the reference count by one, if it has ever been increased and has not already been decreased to zero. Invokes the implementation'sonReferenceCountAtZero()
method if decrementing to zero.- Returns:
- Whether the reference count was successfully decremented
-
decrementReferenceCount
public final void decrementReferenceCount()Decrement the reference count by one, if it has ever been increased and has not already been decreased to zero. Invokes the implementation'sonReferenceCountAtZero()
method if decrementing to zero.- Throws:
IllegalStateException
- If the reference count was not successfully decremented
-
onReferenceCountAtZero
protected abstract void onReferenceCountAtZero()Callback method that will be invoked when the reference count returns to zero.
-