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 Details

  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • append

      public com.fishlib.base.log.LogOutput append​(@NotNull com.fishlib.base.log.LogOutput logOutput)
      Specified by:
      append in interface com.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's onReferenceCountAtZero() 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's onReferenceCountAtZero() 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.