Class SharedObjectFactory<T>

  • Type Parameters:
    T - Class whose objects need to release resources cleanly.

    public class SharedObjectFactory<T>
    extends java.lang.Object
    A factory class to create shared objects that need to release resources cleanly. This class uses reference counting to ensure that resources are released safely.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T get​(java.lang.String identifier, java.util.function.Supplier<T> constructor, java.util.function.Consumer<T> destroyer)
      Get a shared object that has the specified {@param identifier}.
      int getReferenceCount​(java.lang.String identifier)
      Return the current reference count of the object identified by the {@param identifier}.
      boolean release​(java.lang.String identifier)
      A method to notify to the factory that the user of the object no longer needs it.
      int size()  
      • Methods inherited from class java.lang.Object

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

      • SharedObjectFactory

        public SharedObjectFactory()
    • Method Detail

      • get

        public T get​(java.lang.String identifier,
                     java.util.function.Supplier<T> constructor,
                     java.util.function.Consumer<T> destroyer)
        Get a shared object that has the specified {@param identifier}. If an object with the {@param identifier} doesn't exist, a new one is created using the {@param constructor}. When this function is called, the reference count of the shared object is incremented by 1.
        Parameters:
        identifier - A string that uniquely identifies the object being shared
        constructor - A {@link Supplier> to construct a new instance of the object
        destroyer - A Consumer to clean-up any state when the object is no longer needed
        Returns:
        A shared object
      • release

        public boolean release​(java.lang.String identifier)
        A method to notify to the factory that the user of the object no longer needs it. This method decreases the reference count of the shared object by 1. If the reference count becomes 0, the destroyer is invoked and the object is removed from the shared objects.
        Parameters:
        identifier - A string that uniquely identifies the object being shared
        Returns:
        true if the shared object is no longer being used; false otherwise
      • size

        public int size()
        Returns:
        The number of shared objects that are being managed
      • getReferenceCount

        public int getReferenceCount​(java.lang.String identifier)
        Return the current reference count of the object identified by the {@param identifier}. If the factory isn't managing an object with the identifier, returns 0
        Parameters:
        identifier - A string that uniquely identifies the object being shared
        Returns:
        the current reference count of the object identified by the {@param identifier}. If the factory isn't managing an object with the identifier, returns 0