Class JsonStorage

java.lang.Object
me.wyne.wutils.storage.JsonStorage
All Implemented Interfaces:
Storage

public abstract class JsonStorage extends Object implements Storage
Requires com.google.gson dependency.
  • Field Details

    • plugin

      protected final org.bukkit.plugin.Plugin plugin
    • gson

      protected final com.google.gson.Gson gson
    • storageFile

      protected final File storageFile
    • executorService

      protected final ExecutorService executorService
  • Constructor Details

    • JsonStorage

      public JsonStorage(@NotNull @NotNull org.bukkit.plugin.Plugin plugin, @NotNull @NotNull String filePath)
  • Method Details

    • createStorageFolder

      public void createStorageFolder()
      Description copied from interface: Storage
      Create folder for storage file.
      Specified by:
      createStorageFolder in interface Storage
    • createStorageFile

      public void createStorageFile()
      Description copied from interface: Storage
      Create storage file.
      Specified by:
      createStorageFile in interface Storage
    • get

      @Nullable public <KeyType, ValType> ValType get(@NotNull @NotNull HashMap<KeyType,ValType> data, @NotNull KeyType key)
      Get element from HashMap. If HashMap doesn't have given key it will return null. HashMap is used because data is often stored as key:value.
      Specified by:
      get in interface Storage
      Type Parameters:
      KeyType - Data HashMap key type
      ValType - Data HashMap value type
      Parameters:
      data - HashMap to get element from
      key - HashMap key to get element from
      Returns:
      Element of JsonStorage from data or null
    • getCollection

      @NotNull public <KeyType, ValType> @NotNull Collection<ValType> getCollection(@NotNull @NotNull HashMap<KeyType,? extends Collection<ValType>> data, @NotNull KeyType key)
      Get Collection from HashMap. If HashMap doesn't have given key or retrieved Collection is empty it will return null. HashMap is used because data is often stored as key:value.
      Specified by:
      getCollection in interface Storage
      Type Parameters:
      KeyType - Data HashMap key type
      ValType - Collection value type
      Parameters:
      data - HashMap to get Collection from
      key - HashMap key to get Collection from
      Returns:
      Collection from data or null
    • save

      public <KeyType, ValType> void save(@Nullable @Nullable HashMap<KeyType,ValType> data, @NotNull KeyType key, @NotNull ValType value, String path)
      Description copied from interface: Storage
      Save element to data HashMap and to storageFile to given path. Set path as null to save value directly to key in storageFile. Set data as null to save value only to storageFile.
      Specified by:
      save in interface Storage
      Type Parameters:
      KeyType - Data HashMap key type
      ValType - Data HashMap value type
      Parameters:
      data - HashMap to save element to
      key - HashMap key to save element to
      value - Value to save
      path - Path in storageFile to save element to
    • saveCollection

      public <KeyType, ValType, ColType extends Collection<ValType>> boolean saveCollection(@NotNull @NotNull HashMap<KeyType,ColType> data, @NotNull KeyType key, @NotNull ValType value, @NotNull @NotNull String path)
      Description copied from interface: Storage
      Save element to Collection in data HashMap and to storageFile to given path.
      Specified by:
      saveCollection in interface Storage
      Type Parameters:
      KeyType - Data HashMap key type
      ValType - Collection value type
      ColType - Type of Collection
      Parameters:
      data - HashMap to save Collection to
      key - HashMap key to Collection to save element to
      value - Value to save
      path - Path to JsonArray in storageFile to save element to
      Returns:
      True/False if save succeed/failed
    • remove

      public <KeyType, ValType> boolean remove(@Nullable @Nullable HashMap<KeyType,ValType> data, @NotNull KeyType key, String path)
      Description copied from interface: Storage
      Remove element from data HashMap and from storageFile from given path. Set path as null to remove whole key from storageFile. Set data as null to remove value only from storageFile.
      Specified by:
      remove in interface Storage
      Type Parameters:
      KeyType - Data HashMap key type
      ValType - Data HashMap value type
      Parameters:
      data - HashMap to remove element from
      key - HashMap key to remove element from
      path - Path in storageFile to remove element from
      Returns:
      True/False if remove succeed/failed
    • removeCollection

      public <KeyType, ValType, ColType extends Collection<ValType>> boolean removeCollection(@NotNull @NotNull HashMap<KeyType,ColType> data, @NotNull KeyType key, @NotNull ValType value, String path)
      Description copied from interface: Storage
      Remove element from Collection in data HashMap and from storageFile from given path.
      Specified by:
      removeCollection in interface Storage
      Type Parameters:
      KeyType - Data HashMap key type
      ValType - Collection value type
      ColType - Type of Collection
      Parameters:
      data - HashMap to remove Collection element from
      key - HashMap key to Collection to remove element from
      value - Value to remove
      path - Path to JsonArray in storageFile to remove element from
      Returns:
      True/False if remove succeed/failed
    • clearCollection

      public <KeyType, ValType, ColType extends Collection<ValType>> boolean clearCollection(@NotNull @NotNull HashMap<KeyType,ColType> data, @NotNull KeyType key, String path)
      Description copied from interface: Storage
      Clear Collection in data HashMap and in storageFile in given path.
      Specified by:
      clearCollection in interface Storage
      Type Parameters:
      KeyType - Data HashMap key type
      ValType - Collection value type
      ColType - Type of Collection
      Parameters:
      data - HashMap to clear Collection from
      key - HashMap key to Collection to clear
      path - Path to JsonArray in storageFile to clear
      Returns:
      True/False if clear succeed/failed