Class AlpineStore<K,D>

java.lang.Object
co.crystaldev.alpinecore.framework.storage.AlpineStore<K,D>
Type Parameters:
K - the key type
D - the data type
All Implemented Interfaces:
Activatable

@Experimental public abstract class AlpineStore<K,D> extends Object implements Activatable
Handles persistent key and data pairs which are backed by a configurable AlpineDriver.

Inheritors should never be manually instantiated.

Since:
0.1.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final AlpinePlugin
    The plugin that activated this store
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Simple constructor using the default caching strategy.
    protected
    AlpineStore(AlpinePlugin plugin, AlpineDriver<K,D> driver, CachingStrategy strategy)
    Constructor that allows the configuration of a caching strategy.
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    activate(@NotNull AlpinePlugin context)
    Activate this object.
    final void
    deactivate(@NotNull AlpinePlugin context)
    Deactivate this object.
    boolean
    Persists cached data entries to the underlying data storage.
    boolean
    flush(K key)
    Persists a cached data entry associated with the given key to the underlying data storage.
    final D
    get(K key)
    Get data stored at a given key.
    final D
    getOrCreate(K key, @NotNull Supplier<D> defaultDataSupplier)
    Get data stored at a given key, or create an entry if there is none.
    final D
    getOrCreate(K key, D defaultData)
    Get data stored at a given key, or create an entry if there is none.
    final boolean
    has(K key)
    Check if data exists for a given key.
    final boolean
    Is this object active?
    final @NotNull Collection<D>
    Retrieve all stored data entries from the underlying data storage.
    final @NotNull Collection<D>
    loadAllEntries(@Nullable Consumer<Exception> exceptionHandler)
    Retrieve all stored data entries from the underlying data storage.
    final void
    put(K key, D data)
    Store data at a given key.
    final boolean
    remove(K key)
    Delete data stored at a given key.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface co.crystaldev.alpinecore.framework.Activatable

    canDeactivate
  • Field Details

    • plugin

      protected final AlpinePlugin plugin
      The plugin that activated this store
  • Constructor Details

    • AlpineStore

      protected AlpineStore(AlpinePlugin plugin, AlpineDriver<K,D> driver)
      Simple constructor using the default caching strategy.

      Locked down to prevent improper instantiation.

      Stores are reflectively instantiated by the framework automatically.

      Parameters:
      driver - the storage driver
      Since:
      0.1.0
    • AlpineStore

      protected AlpineStore(AlpinePlugin plugin, AlpineDriver<K,D> driver, CachingStrategy strategy)
      Constructor that allows the configuration of a caching strategy.

      Locked down to prevent improper instantiation.

      Stores are reflectively instantiated by the framework automatically.

      Parameters:
      driver - the storage driver
      strategy - the strategy used by the caching layer
      Since:
      0.1.0
  • Method Details

    • get

      @Nullable public final D get(@NotNull K key)
      Get data stored at a given key.
      Parameters:
      key - the key
      Returns:
      the data
    • getOrCreate

      @NotNull public final D getOrCreate(@NotNull K key, @NotNull D defaultData)
      Get data stored at a given key, or create an entry if there is none.
      Parameters:
      key - the key
      defaultData - the data to create a new entry with
      Returns:
      the data
    • getOrCreate

      @NotNull public final D getOrCreate(@NotNull K key, @NotNull @NotNull Supplier<D> defaultDataSupplier)
      Get data stored at a given key, or create an entry if there is none.
      Parameters:
      key - the key
      defaultDataSupplier - the data to create a new entry with
      Returns:
      the data
    • loadAllEntries

      @NotNull public final @NotNull Collection<D> loadAllEntries() throws Exception
      Retrieve all stored data entries from the underlying data storage.

      It may be a blocking task, and the time it takes to complete depends on the size of the data storage.

      Returns:
      A collection containing all stored data entries.
      Throws:
      Exception - If an exception occurs while retrieving the data entries.
      See Also:
    • loadAllEntries

      @NotNull public final @NotNull Collection<D> loadAllEntries(@Nullable @Nullable Consumer<Exception> exceptionHandler)
      Retrieve all stored data entries from the underlying data storage.

      It may be a blocking task, and the time it takes to complete depends on the size of the data storage.

      Parameters:
      exceptionHandler - A function for handling errors.
      Returns:
      A collection containing all stored data entries.
      See Also:
    • has

      public final boolean has(@NotNull K key)
      Check if data exists for a given key.
      Parameters:
      key - the key
      Returns:
      whether an entry exists
    • remove

      public final boolean remove(@NotNull K key)
      Delete data stored at a given key.
      Parameters:
      key - the key
      Returns:
      whether the operation was successful
    • put

      public final void put(@NotNull K key, @NotNull D data)
      Store data at a given key.

      This method will silently overwrite any pre-existing data.

      Parameters:
      key - the key
      data - the data
    • flush

      public boolean flush()
      Persists cached data entries to the underlying data storage.

      This method is responsible for persisting data entries that have been cached but not yet saved to the underlying data storage. It attempts to persist the entries and clears the cache.

      Returns:
      whether the operation was successful
    • flush

      public boolean flush(@NotNull K key)
      Persists a cached data entry associated with the given key to the underlying data storage.

      This method is responsible for persisting a specific data entry that has been cached but not yet saved to the underlying data storage. It attempts to persist the entry and returns whether the operation was successful. If the persistence operation fails, it logs an error message.

      Parameters:
      key - The key associated with the data entry to be persisted.
      Returns:
      Whether the persistence operation was successful.
    • activate

      public final void activate(@NotNull @NotNull AlpinePlugin context)
      Description copied from interface: Activatable
      Activate this object.
      Specified by:
      activate in interface Activatable
      Parameters:
      context - The plugin which initiated the request
    • deactivate

      public final void deactivate(@NotNull @NotNull AlpinePlugin context)
      Description copied from interface: Activatable
      Deactivate this object.
      Specified by:
      deactivate in interface Activatable
      Parameters:
      context - The plugin which initiated the request
    • isActive

      public final boolean isActive()
      Description copied from interface: Activatable
      Is this object active?
      Specified by:
      isActive in interface Activatable
      Returns:
      Whether the object is active