Class MapContainer<K,V>

java.lang.Object
com.marcusslover.plus.lib.container.AbstractContainer<K>
com.marcusslover.plus.lib.container.type.MapContainer<K,V>
Type Parameters:
K - Key type.
V - Value type.

public abstract class MapContainer<K,V> extends AbstractContainer<K>
Container that represents a map of objects. Key is the name of the file. Files are dynamic. Value is the object. Each value is serialized to its own json file.
  • Field Details

  • Constructor Details

    • MapContainer

      public MapContainer()
  • Method Details

    • emptyValue

      @NotNull protected abstract V emptyValue(@NotNull K key)
      Creates a new instance of an object.

      This function is called when the object with the given key does not exist and has to be created for the first time.

      Parameters:
      key - The key of the object.
      Returns:
      The object.
    • onValueLoaded

      protected void onValueLoaded(@NotNull V value)
      Called when a new object gets loaded.

      This function is mainly called when storeLocally(K, V) is called.

      Parameters:
      value - The object that was most recently loaded.
    • onValueUnloaded

      protected void onValueUnloaded(@NotNull V value)
      Called when an object gets unloaded.

      This function is mainly called when cleanLocally(K) is called.

      Parameters:
      value - The object that was most recently unloaded.
    • update

      public void update(@NotNull K key)
      Description copied from class: AbstractContainer
      Push the updated object to the cache and save it to the file.
      Specified by:
      update in class AbstractContainer<K>
      Parameters:
      key - Key of the object.
    • loadData

      @NotNull public V loadData(@NotNull K key)
      Loads an object from the file. The key is the name of the file.
      Parameters:
      key - Key to the object.
      Returns:
      The object.
    • loadAllData

      public void loadAllData()
      Loads all the objects from the files. Called during start of the plugin, only when the container is annotated with InitialLoading.
      Specified by:
      loadAllData in class AbstractContainer<K>
    • saveData

      public void saveData(@NotNull K key)
      Unloads an object from the cache and saves it to the file.
      Parameters:
      key - Key to the object.
    • saveData

      public void saveData()
      Saves all the objects from the cache to the file. Should be called when the plugin is disabled.
    • containsKeyLocally

      public boolean containsKeyLocally(@NotNull K key)
      Checks if object with the given key is loaded in the cache.
      Parameters:
      key - Key to the object.
      Returns:
      True if the object is loaded, false otherwise.
    • containsValueLocally

      public boolean containsValueLocally(@NotNull V value)
      Checks if the object is loaded in the cache.

      The check does not include any files, only the cache!

      Parameters:
      value - Object to check.
      Returns:
      True if the object is loaded, false otherwise.
    • cleanLocally

      public void cleanLocally(@NotNull K key)
      Cleans the object from the cache.

      This function does not write the object to the file.

      Parameters:
      key - Key to the object.
    • storeLocally

      public void storeLocally(@NotNull K key, @Nullable V value)
      Puts the object in the cache.

      This function only puts the object in the cache. It doesn't save the object to the file -> Use update(K) for that or writeData(K, V).

      Parameters:
      key - Key to the object.
      value - Object to put.
    • retrieveLocally

      @Nullable public V retrieveLocally(@NotNull K key)
      Retrieves the object from the cache.

      This function will not attempt to load the object from the file, if it is not loaded -> Use loadData(K) instead.

      Parameters:
      key - Key to the object.
      Returns:
      The object or null if it is not loaded.
    • readData

      @NotNull public V readData(@NotNull K key)
      Reads the object from the file.

      If the file doesn't exist, it will create a new one. Default value created by emptyValue(Object) will be used.

      Parameters:
      key - Key to the object.
      Returns:
      The object.
    • read

      @Nullable public V read(@NotNull @NotNull String fileName)
      Reads the object from the file.

      This is an internal (raw) function -> Use readData(Object) instead.

      Parameters:
      fileName - Name of the file.
      Returns:
      The object.
    • writeData

      public void writeData(@NotNull K key, @Nullable V value)
      Writes the object to the file.

      This function does not unload anything from the cache. The cache is not affected by this function. If you want to delete the file, set the value to null!

      Parameters:
      key - Key to the object.
      value - Object to write.
    • getValues

      @NotNull public @NotNull Collection<V> getValues()
      Gets all the loaded objects from the cache.
      Returns:
      All the loaded objects.
    • getCache

      @NotNull public @NotNull Map<K,V> getCache()
      Gets all the loaded keys from the cache.
      Returns:
      All the loaded keys and their objects.