Package au.csiro.pathling.utilities
Interface ObjectHolder<C extends Serializable,V>
- Type Parameters:
C- the type of the configurationV- the type of the instance
- All Known Implementing Classes:
ObjectHolder.KeyedHolder,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, John Grimes
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classObjectHolder.KeyedHolder<C extends Serializable,V> An implementation ofObjectHolderthat memoises one instance per distinct configuration within the process.static classObjectHolder.SingletonHolder<C extends Serializable,V> A singleton implementation of ObjectHolder that holds a single instance of something. -
Method Summary
Modifier and TypeMethodDescriptiongetOrCreate(C config) Retrieves the instance associated with given configuration and if necessary creates ones.static <C extends Serializable,V>
ObjectHolder<C, V> perConfiguration(Function<C, V> constructor) Creates the holder memoising one instance per distinct configuration within the process.static <C extends Serializable,V>
ObjectHolder<C, V> perConfiguration(Function<C, V> constructor, boolean closeOnShutdown, int shutdownHookPriority) Creates the holder memoising one instance per distinct configuration within the process.voidreset()Destroys all the instances stored the underlying scope.static <C extends Serializable,V>
ObjectHolder<C, V> Creates the holder allowing a single instance to be created per process.static <C extends Serializable,V>
ObjectHolder<C, V> Creates the holder allowing a single instance to be created per process.
-
Method Details
-
getOrCreate
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
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.
-
perConfiguration
static <C extends Serializable,V> ObjectHolder<C,V> perConfiguration(@Nonnull Function<C, V> constructor, boolean closeOnShutdown, int shutdownHookPriority) Creates the holder memoising one instance per distinct configuration within the process. Several configurations may coexist, so lookups with different configurations each receive their own instance rather than failing.- Type Parameters:
C- the type of the configuration.V- the type of the instance.- Parameters:
constructor- the function that creates a new instance from the configuration.closeOnShutdown- whether to close the held instances on JVM shutdown.shutdownHookPriority- the priority of the shutdown hook.- Returns:
- the keyed holder.
-
perConfiguration
static <C extends Serializable,V> ObjectHolder<C,V> perConfiguration(@Nonnull Function<C, V> constructor) Creates the holder memoising one instance per distinct configuration within the process. Several configurations may coexist, so lookups with different configurations each receive their own instance rather than failing. The held instances 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 a new instance from the configuration.- Returns:
- the keyed holder.
-