Class ReferenceCountedObject<T>

  • Type Parameters:
    T - The type of referenced object.

    public abstract class ReferenceCountedObject<T>
    extends Object
    An object which is lazily created when first referenced, and destroyed when the last reference is released.
    • Constructor Detail

      • ReferenceCountedObject

        protected ReferenceCountedObject()
        Creates a new referenced object whose reference count is initially zero.
    • Method Detail

      • acquire

        public final ReferenceCountedObject.Reference acquire()
        Returns a reference to the reference counted object.
        Returns:
        A reference to the reference counted object.
      • acquireIfNull

        public final ReferenceCountedObject.Reference acquireIfNull​(T value)
        Returns a reference to the provided object or, if it is null, a reference to the reference counted object.
        Parameters:
        value - The object to be referenced, or null if the reference counted object should be used.
        Returns:
        A reference to the provided object or, if it is null, a reference to the reference counted object.
      • destroyInstance

        protected abstract void destroyInstance​(T instance)
        Invoked when a reference is released and the reference count will become zero. Implementations should release any resources associated with the resource and should not return until the resources have been released.
        Parameters:
        instance - The instance to be destroyed.
      • newInstance

        protected abstract T newInstance()
        Invoked when a reference is acquired and the current reference count is zero. Implementations should create a new instance as fast as possible.
        Returns:
        The new instance.