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>>

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.

Which source values exist, and whether they are active, is decided by getSourceValueStream() and isSourceValueActivated(Object); use create(ValueSource, BiFunction) to take both from a ValueSource instead of overriding them.

  • Constructor Details

    • CachedValueGenerator

      public CachedValueGenerator()
  • Method Details

    • create

      public static <K, SV, FV> CachedValueGenerator<K,SV,FV> create(ValueSource<K,SV> source, BiFunction<SV,Function<String,Object>,FV> converter)
      Creates a generator from a source and a conversion, exposing every final value to every key.
      Type Parameters:
      K - type of the key
      SV - type of the source value
      FV - type of the final value
      Parameters:
      source - the source values
      converter - the conversion of a source value and its variable replacer to its final value
      Returns:
      the generator
    • create

      public static <K, SV, FV> CachedValueGenerator<K,SV,FV> create(ValueSource<K,SV> source, BiFunction<SV,Function<String,Object>,FV> converter, BiPredicate<FV,K> valueFilter)
      Creates a generator from a source and a conversion.
      Type Parameters:
      K - type of the key
      SV - type of the source value
      FV - type of the final value
      Parameters:
      source - the source values
      converter - the conversion of a source value and its variable replacer to its final value
      valueFilter - whether a final value is exposed to the key, evaluated on every apply(Object) call
      Returns:
      the generator
    • 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).
      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().
      Parameters:
      key - key whose source values are requested
      Returns:
      stream of source values for the key
    • 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().
      Parameters:
      value - source value to check
      Returns:
      true if the value may be exposed
    • 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
    • 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.