Class CachedValueGenerator<K,SV,FV>
java.lang.Object
io.github.projectunified.valuevenue.core.CachedValueGenerator<K,SV,FV>
- Type Parameters:
K- type of the key used to select valuesSV- type of the source valueFV- type of the final value
- Direct Known Subclasses:
EnumValueGenerator
Generates the final values for a key from source values, mapping and caching each source value once.
The cache is shared by every key and lives as long as the generator, so mapToFinalValue(Object)
must be deterministic and must not depend on the key. Whether a cached value is exposed for a key is
decided per call by isValueActivated(Object, Object).
Instances are safe for concurrent apply(Object) and update() calls, but a cached
activation state is only recomputed by update(); callers must invoke it when the activation
of source values changes.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the active final values for the given key, in the order of its source value stream.Returns a snapshot of the cache as source value to final value, including entries whose source value is not active and entries created by a key.All source values the generator knows without a key, soupdate()can create their cache entries upfront.getSourceValueStream(K key) Source values to expose for the given key.booleanisSourceValueActivated(SV value) Whether a source value is currently active.booleanisValueActivated(FV value, K key) Whether a final value is exposed for the given key.abstract FVmapToFinalValue(SV sourceValue) Maps a source value to its final value.voidupdate()Creates the cache entries of allcommon source valuesand refreshes the activation state of every cached entry, including entries created lazily for a key.
-
Constructor Details
-
CachedValueGenerator
public CachedValueGenerator()
-
-
Method Details
-
getSourceValueStream
All source values the generator knows without a key, soupdate()can create their cache entries upfront. Empty by default: a key-only generator needs no override, while its entries are created lazily byapply(Object). Values that can only be reached through a key are returned bygetSourceValueStream(Object)instead.- Returns:
- stream of every key-independent source value
-
getSourceValueStream
Source values to expose for the given key. Defaults togetSourceValueStream(); override when the source values depend on the key. Entries for key-only source values are created lazily byapply(Object), not byupdate().- Parameters:
key- key whose source values are requested- Returns:
- stream of source values for the key
-
mapToFinalValue
Maps a source value to its final value. Called once per source value, then cached.- Parameters:
sourceValue- source value to map- Returns:
- final value of the source value
-
isSourceValueActivated
Whether a source value is currently active. The result is cached per source value; it is evaluated when the entry is created and refreshed byupdate(), so changes take effect after that call.- Parameters:
value- source value to check- Returns:
- true if the value may be exposed
-
isValueActivated
Whether a final value is exposed for the given key. Not cached: evaluated on everyapply(Object)call.- Parameters:
value- final value to checkkey- key the value would be exposed for- Returns:
- true if the value is visible to the key
-
apply
Returns the active final values for the given key, in the order of its source value stream. -
getCachedValues
Returns a snapshot of the cache as source value to final value, including entries whose source value is not active and entries created by a key.The map is read-only and detached: later
apply(Object)orupdate()calls do not change it.- Returns:
- read-only copy of the cached mapping
-
update
public void update()Creates the cache entries of allcommon source valuesand refreshes the activation state of every cached entry, including entries created lazily for a key.Entries are never evicted: a source value that disappears from the streams stays cached.
-