Interface ObjectHolder<C extends Serializable,​V>

  • Type Parameters:
    C - the type of the configuration
    V - the type of the instance
    All Known Implementing Classes:
    ObjectHolder.SingletonHolder

    public interface ObjectHolder<C extends Serializable,​V>
    Represents a scoped storage for instances of type V that can be created from configuration of type C. The concrete implementations can use various strategies for storing the instances. For example a singleton can manage a single instance per process.
    Author:
    Piotr Szul
    • Method Detail

      • getOrCreate

        V getOrCreate​(@Nonnull
                      C config)
        Retrieves the instance associated with given configuration and if necessary creates ones.
        Parameters:
        config - the configuration for the instance.
        Returns:
        the instance associated with the holder.
      • reset

        void reset()
        Destroys all the instances stored the underlying scope.
      • singleton

        static <C extends Serializable,​V> ObjectHolder<C,​V> singleton​(@Nonnull
                                                                                  Function<C,​V> constructor,
                                                                                  boolean closeOnShutdown,
                                                                                  int shutdownHookPriority)
        Creates the holder allowing a single instance to be created per process. Attempts to create another instance with different configuration will fail.
        Type Parameters:
        C - the type of the configuration.
        V - the type of the instance.
        Parameters:
        constructor - the function that creates new instance from the configuration.
        closeOnShutdown - whether to close the instance on JVM shutdown.
        shutdownHookPriority - the priority of the shutdown hook.
        Returns:
        the singleton holder.
      • singleton

        static <C extends Serializable,​V> ObjectHolder<C,​V> singleton​(@Nonnull
                                                                                  Function<C,​V> constructor)
        Creates the holder allowing a single instance to be created per process. Attempts to create another instance with different configuration will fail. The instance will be closed on JVM shutdown.
        Type Parameters:
        C - the type of the configuration.
        V - the type of the instance.
        Parameters:
        constructor - the function that creates new instance from the configuration.
        Returns:
        the singleton holder.