Interface RefCounted

All Known Implementing Classes:
AbstractRefCounted, SimpleRefCounted

public interface RefCounted
An interface for objects that need to be notified when all reference to itself are not in user anymore. This implements basic reference counting for instance if async operations holding on to services that are close concurrently but should be functional until all async operations have joined Classes implementing this interface should ref counted at any time ie. if an object is used it's reference count should be increased before using it by calling #incRef and a corresponding #decRef must be called in a try/finally block to release the object again ie.:
      inst.incRef();
      try {
        // use the inst...

      } finally {
          inst.decRef();
      }
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final RefCounted
    A noop implementation that always behaves as if it is referenced and cannot be released.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Decreases the refCount of this instance.
    boolean
    Returns true only if there was at least one active reference when the method was called; if it returns false then the object is closed; future attempts to acquire references will fail.
    void
    Increments the refCount of this instance.
    default void
    Similar to incRef() except that it also asserts that it managed to acquire the ref, for use in situations where it is a bug if all refs have been released.
    boolean
    Tries to increment the refCount of this instance.
  • Field Details

    • ALWAYS_REFERENCED

      static final RefCounted ALWAYS_REFERENCED
      A noop implementation that always behaves as if it is referenced and cannot be released.
  • Method Details

    • incRef

      void incRef()
      Increments the refCount of this instance.
      Throws:
      IllegalStateException - iff the reference counter can not be incremented.
      See Also:
    • tryIncRef

      boolean tryIncRef()
      Tries to increment the refCount of this instance. This method will return true iff the refCount was successfully incremented.
      See Also:
    • decRef

      boolean decRef()
      Decreases the refCount of this instance. If the refCount drops to 0, then this instance is considered as closed and should not be used anymore.
      Returns:
      returns true if the ref count dropped to 0 as a result of calling this method
      See Also:
    • hasReferences

      boolean hasReferences()
      Returns true only if there was at least one active reference when the method was called; if it returns false then the object is closed; future attempts to acquire references will fail.
      Returns:
      whether there are currently any active references to this object.
    • mustIncRef

      default void mustIncRef()
      Similar to incRef() except that it also asserts that it managed to acquire the ref, for use in situations where it is a bug if all refs have been released.