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 values
SV - type of the source value
FV - type of the final value
All Implemented Interfaces:
Function<K,List<FV>>
Direct Known Subclasses:
EnumValueGenerator

public abstract class CachedValueGenerator<K,SV,FV> extends Object implements Function<K,List<FV>>
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 Details

    • CachedValueGenerator

      public CachedValueGenerator()
  • Method Details

    • getSourceValueStream

      public Stream<SV> getSourceValueStream()
      All source values the generator knows without a key, so update() can create their cache entries upfront. Empty by default: a key-only generator needs no override, while its entries are created lazily by apply(Object). Values that can only be reached through a key are returned by getSourceValueStream(Object) instead.
      Returns:
      stream of every key-independent source value
    • getSourceValueStream

      public Stream<SV> getSourceValueStream(K key)
      Source values to expose for the given key. Defaults to getSourceValueStream(); override when the source values depend on the key. Entries for key-only source values are created lazily by apply(Object), not by update().
      Parameters:
      key - key whose source values are requested
      Returns:
      stream of source values for the key
    • mapToFinalValue

      public abstract FV mapToFinalValue(SV sourceValue)
      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

      public boolean isSourceValueActivated(SV value)
      Whether a source value is currently active. The result is cached per source value; it is evaluated when the entry is created and refreshed by update(), so changes take effect after that call.
      Parameters:
      value - source value to check
      Returns:
      true if the value may be exposed
    • isValueActivated

      public boolean isValueActivated(FV value, K key)
      Whether a final value is exposed for the given key. Not cached: evaluated on every apply(Object) call.
      Parameters:
      value - final value to check
      key - key the value would be exposed for
      Returns:
      true if the value is visible to the key
    • apply

      public List<FV> apply(K key)
      Returns the active final values for the given key, in the order of its source value stream.
      Specified by:
      apply in interface Function<K,SV>
      Parameters:
      key - key to generate values for
      Returns:
      list of the active final values, never null
    • getCachedValues

      public Map<SV,FV> 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) or update() calls do not change it.

      Returns:
      read-only copy of the cached mapping
    • update

      public void update()
      Creates the cache entries of all common source values and 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.