Class SingleContainer<V>

java.lang.Object
com.marcusslover.plus.lib.container.AbstractContainer<V>
com.marcusslover.plus.lib.container.type.SingleContainer<V>
Type Parameters:
V - Value type.

public abstract class SingleContainer<V> extends AbstractContainer<V>
Container that represents a single object. The object is serialized to a single json file. The file name is final and can be changed in the constructor.
  • Field Details

    • valueType

      protected final Class<V> valueType
    • fileName

      protected final String fileName
    • cache

      @Nullable protected V cache
  • Constructor Details

    • SingleContainer

      public SingleContainer()
  • Method Details

    • emptyValue

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

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

      Returns:
      The object.
    • onValueLoaded

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

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

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

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

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

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

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

      @NotNull public V loadData()
      Loads an object from the file.
      Returns:
      The object.
    • loadAllData

      public void loadAllData()
      Description copied from class: AbstractContainer
      Attempts to load all the data from the container. Look at InitialLoading for more information.
      Specified by:
      loadAllData in class AbstractContainer<V>
    • saveData

      public void saveData()
      Unloads the data from the cache and saves it to the file.
    • cleanLocally

      public void cleanLocally()
      Cleans the local cache.

      This function does not delete the file.

    • storeLocally

      public void storeLocally(@Nullable V value)
      Stores locally the data.

      This function does not write the data to the file.

      Parameters:
      value - The value to store.
    • retrieveLocally

      @Nullable public V retrieveLocally()
      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() instead.

      Returns:
      The object or null if it is not loaded.
    • readData

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

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

      Returns:
      The object.
    • writeData

      public void writeData(@Nullable V value)
      Writes the data 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:
      value - Object to write.