Class MultiReferenceStorage

  • All Implemented Interfaces:
    Storage

    public class MultiReferenceStorage
    extends java.lang.Object
    implements Storage

    A wrapper around another Storage that also maintains a reference counter. The inner storage gets deleted only if the reference counter reaches zero.

    Reference counting is used to delete the storage when it is no longer needed. So, any users of this class should note:

    • Constructor Summary

      Constructors 
      Constructor Description
      MultiReferenceStorage​(Storage storage)
      Creates a new MultiReferenceStorage instance for the given back-end.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addReference()
      Increments the reference counter.
      private boolean decrementCounter()
      Synchronized decrement of reference count.
      void delete()
      Decrements the reference counter and deletes the inner Storage object if the reference counter reaches zero.
      java.io.InputStream getInputStream()
      Returns the input stream of the inner Storage object.
      private void incrementCounter()
      Synchronized increment of reference count.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • storage

        private final Storage storage
      • referenceCounter

        private int referenceCounter
    • Constructor Detail

      • MultiReferenceStorage

        public MultiReferenceStorage​(Storage storage)
        Creates a new MultiReferenceStorage instance for the given back-end. The reference counter is initially set to one so the caller does not have to call addReference() after this constructor.
        Parameters:
        storage - storage back-end that should be reference counted.
        Throws:
        java.lang.IllegalArgumentException - when storage is null
    • Method Detail

      • addReference

        public void addReference()
        Increments the reference counter.
        Throws:
        java.lang.IllegalStateException - if the reference counter is zero which implies that the backing storage has already been deleted.
      • delete

        public void delete()
        Decrements the reference counter and deletes the inner Storage object if the reference counter reaches zero.

        A client that holds a reference to this object must make sure not to invoke this method a second time.

        Specified by:
        delete in interface Storage
        Throws:
        java.lang.IllegalStateException - if the reference counter is zero which implies that the backing storage has already been deleted.
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        Returns the input stream of the inner Storage object.
        Specified by:
        getInputStream in interface Storage
        Returns:
        an input stream.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • incrementCounter

        private void incrementCounter()
        Synchronized increment of reference count.
        Throws:
        java.lang.IllegalStateException - when counter is already zero
      • decrementCounter

        private boolean decrementCounter()
        Synchronized decrement of reference count.
        Returns:
        true when counter has reached zero, false otherwise
        Throws:
        java.lang.IllegalStateException - when counter is already zero