Class SharedContext
- All Implemented Interfaces:
Context
,ResettableContext
,SafeCloseable
,AutoCloseable
ResettableContext
used as a holder for other ResettableContext
s that may be shared across components.
This serves as a place to cache re-usable computations or resources, but must be reset()
for every step of
an operation (usually a chunk of ordered keys).
For example, redirected data sources that share the same mappings may cache a chunk of redirections for the most recent chunk of row keys they have been handed.
It's important that "nested" usage follows the convention of creating a new instance and passing that instance to context creation methods. Said nested instance should be (or be attached to) an entry in the parent context, and reset/closed when said entry is. It should always be safe to skip nested SharedContext creation if all sources that may be using a given instance will be passed the same ordered keys.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
AbstractSharedContext.Key
implementation for use when a simple Object reference coupled with sub-class identity can determine equality for sharing purposes.static interface
SharedContext.Key<VALUE_TYPE extends ResettableContext>
Key marker interface. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close implementation which invokesSafeCloseable.close()
on all values registered viagetOrCreate(Key, Supplier)
, and then forgets all registered values.final <V extends ResettableContext,
K extends SharedContext.Key<V>>
VgetOrCreate
(K key, @NotNull Supplier<V> valueFactory) Get or create theResettableContext
value for aSharedContext.Key
key.static SharedContext
Construct a new, empty shared context.void
reset()
Reset implementation which invokesResettableContext.reset()
on all values registered viagetOrCreate(Key, Supplier)
.
-
Constructor Details
-
SharedContext
protected SharedContext()
-
-
Method Details
-
getOrCreate
public final <V extends ResettableContext,K extends SharedContext.Key<V>> V getOrCreate(K key, @NotNull @NotNull Supplier<V> valueFactory) Get or create theResettableContext
value for aSharedContext.Key
key. If the value is computed, the result value will be associated with thekey
until theSharedContext
isclose()
ed.- Parameters:
key
- The keyvalueFactory
- The value factory, to be invoked ifkey
is not found within thisSharedContext
- Returns:
- The value associated with
key
, possibly newly-created
-
reset
@OverridingMethodsMustInvokeSuper public void reset()Reset implementation which invokes
ResettableContext.reset()
on all values registered viagetOrCreate(Key, Supplier)
.Sub-classes should be sure to call
super.reset()
.- Specified by:
reset
in interfaceResettableContext
-
close
@OverridingMethodsMustInvokeSuper public void close()Close implementation which invokes
SafeCloseable.close()
on all values registered viagetOrCreate(Key, Supplier)
, and then forgets all registered values.Sub-classes should be sure to call
super.close()
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceContext
- Specified by:
close
in interfaceSafeCloseable
-