Class CachedConfigObject<K,V>

java.lang.Object
dev.crafty.core.config.CachedConfigObject<K,V>
Type Parameters:
K - the type of the key used to identify objects
V - the type of the value to be cached and serialized

public abstract class CachedConfigObject<K,V> extends Object
An abstract class that provides a cached, file-backed configuration object.

This class uses a Caffeine cache to store objects in memory and synchronizes them with a YAML configuration file on disk. Subclasses or instances (via the Builder) must provide the config file, serializer, and config section.

Since:
1.0.5
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Builder for creating instances of CachedConfigObject with custom configuration.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    get(K key)
    Gets a value from the cache, loading from config if not present.
    Gets all values currently stored in the cache.
    protected abstract File
    Returns the configuration file backing this cache.
    protected abstract String
    Returns the section name in the config file where objects are stored.
    protected abstract Optional<ConfigSerializer<V>>
    Returns the serializer used to (de)serialize objects to/from the config.
    protected abstract K
    Converts a String key from the config to the correct key type.
    protected abstract String
    Converts a key of type K to a String for config storage.
    void
    Loads all values from the config section into the cache, replacing any existing cache entries.
    void
    remove(K key)
    Removes a value from the cache and the config file.
    void
    set(K key, V value)
    Sets a value in the cache and saves it to the config file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CachedConfigObject

      public CachedConfigObject()
  • Method Details

    • getConfigFile

      protected abstract File getConfigFile()
      Returns the configuration file backing this cache.
      Returns:
      the config file
    • getSerializer

      protected abstract Optional<ConfigSerializer<V>> getSerializer()
      Returns the serializer used to (de)serialize objects to/from the config.
      Returns:
      an optional serializer
    • getConfigSection

      protected abstract String getConfigSection()
      Returns the section name in the config file where objects are stored.
      Returns:
      the config section name
    • keyFromString

      protected abstract K keyFromString(String key)
      Converts a String key from the config to the correct key type.
      Parameters:
      key - the string key from config
      Returns:
      the key of type K
    • keyToString

      protected abstract String keyToString(K key)
      Converts a key of type K to a String for config storage.
      Parameters:
      key - the key of type K
    • get

      public Optional<V> get(K key)
      Gets a value from the cache, loading from config if not present.
      Parameters:
      key - the key to look up
      Returns:
      an optional containing the value if present
    • loadAll

      public void loadAll()
      Loads all values from the config section into the cache, replacing any existing cache entries.
    • getAll

      public List<V> getAll()
      Gets all values currently stored in the cache.
      Returns:
      a list of all cached values
    • remove

      public void remove(K key)
      Removes a value from the cache and the config file.
      Parameters:
      key - the key to remove
    • set

      public void set(K key, V value)
      Sets a value in the cache and saves it to the config file.
      Parameters:
      key - the key to set
      value - the value to associate with the key