Class Retainable.ReferenceCounter

java.lang.Object
org.eclipse.jetty.io.Retainable.ReferenceCounter
All Implemented Interfaces:
Retainable
Enclosing interface:
Retainable

public static class Retainable.ReferenceCounter extends Object implements Retainable

A reference count implementation for a Retainable resource.

The reference count is initialized to 1 when the resource is created, and therefore it is implicitly retained and needs a call to release().

Additional calls to retain() must be matched by correspondent calls to release().

When the reference count goes to zero, the resource may be pooled. When the resource is acquired from the pool, acquire() should be called to set the reference count to 1.

  • Constructor Details

    • ReferenceCounter

      public ReferenceCounter()
    • ReferenceCounter

      protected ReferenceCounter(int initialCount)
  • Method Details

    • get

      public int get()
      Returns:
      the current reference count
    • acquire

      public void acquire()

      Updates the reference count from 0 to 1.

      This method should only be used when this resource is acquired from a pool.

    • canRetain

      public boolean canRetain()
      Description copied from interface: Retainable

      Returns whether this resource is referenced counted by calls to Retainable.retain() and Retainable.release().

      Implementations may decide that special resources are not not referenced counted (for example, static constants) so calling Retainable.retain() is a no-operation, and calling Retainable.release() on those special resources is a no-operation that always returns true.

      Specified by:
      canRetain in interface Retainable
      Returns:
      true if calls to Retainable.retain() are reference counted.
    • retain

      public void retain()
      Description copied from interface: Retainable

      Retains this resource, potentially incrementing a reference count if there are resources that will be released.

      Specified by:
      retain in interface Retainable
    • tryRetain

      public boolean tryRetain()
    • release

      public boolean release()
      Description copied from interface: Retainable

      Releases this resource, potentially decrementing a reference count (if any).

      Specified by:
      release in interface Retainable
      Returns:
      true when the reference count goes to zero or if there was no reference count, false otherwise.
    • isRetained

      public boolean isRetained()

      Returns whether retain() has been called at least one more time than release().

      Returns:
      whether this buffer is retained
    • toString

      public String toString()
      Overrides:
      toString in class Object