Class ReferenceCounted

java.lang.Object
io.deephaven.util.referencecounting.ReferenceCounted
All Implemented Interfaces:
LogOutputAppendable
Direct Known Subclasses:
BaseChunkInputStreamGenerator, JobScheduler.IterationManager, ProceduralReferenceCounted, ReferenceCountedLivenessReferent

public abstract class ReferenceCounted extends Object implements LogOutputAppendable
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.
  • Constructor Details

    • ReferenceCounted

      protected ReferenceCounted()
    • ReferenceCounted

      protected ReferenceCounted(int initialValue)
      Parameters:
      initialValue - The initial value for the reference count, taken as an unsigned integer. Must not be one of the reserved values -1 or -2.
  • Method Details

    • toString

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

      public LogOutput append(LogOutput logOutput)
      Specified by:
      append in interface LogOutputAppendable
    • getReferenceCountDebug

      public static String getReferenceCountDebug(Object maybeReferenceCounted)
    • 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
    • forceReferenceCountToZero

      public final boolean forceReferenceCountToZero()
      Force the reference count to zero. If it was non-zero, this will have the same side effects as returning to zero normally, but subsequent invocations of decrementReferenceCount() and tryDecrementReferenceCount() will act as if the reference count was successfully decremented until resetReferenceCount() is invoked.
      Returns:
      Whether this invocation actually forced the reference count to zero (and invoked onReferenceCountAtZero(). false means that this ReferenceCounted reached a zero through other means.
    • 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.