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 Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final com.google.gson.Gson
     
    protected final ExecutorService
     
    protected final boolean
    False disables info and warning logging (this option was added due to frequent json queries which cause log spam)
    protected final File
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsonStorage(@NotNull File storageFile, @NotNull ExecutorService jsonExecutorService, @org.jetbrains.annotations.NotNull boolean jsonQueryLog)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <KeyType, ValType, ColType extends Collection<ValType>>
    boolean
    clearCollection(@NotNull Map<KeyType,ColType> data, KeyType key, @NotNull String path)
    Clear Collection in data Map and in storageFile in given path.
    <KeyType, MapKeyType, ValType, MapType extends Map<MapKeyType, ValType>>
    boolean
    clearMap(@NotNull Map<KeyType,MapType> data, KeyType key, @NotNull Class<MapKeyType> mapKeyType)
    Clear inner Map in data Map and in storageFile.
    void
    Create storage file.
    void
    createStorageFolder(@NotNull File folderPath)
    Create folder for storage file.
    <KeyType, ValType>
    ValType
    get(@NotNull Map<KeyType,ValType> data, KeyType key)
    Get element from data Map.
    <KeyType, ValType>
    @NotNull Collection<ValType>
    getCollection(@NotNull Map<KeyType,? extends Collection<ValType>> data, KeyType key)
    Get Collection from data Map.
    com.google.gson.Gson
     
     
    void
    Load data from storage file.
    <KeyType, ValType>
    boolean
    remove(@Nullable Map<KeyType,ValType> data, KeyType key, @Nullable String path)
    Remove element from data Map and from storageFile from given path.
    <KeyType, ValType, ColType extends Collection<ValType>>
    boolean
    removeCollection(@NotNull Map<KeyType,ColType> data, KeyType key, ValType value, @NotNull String path)
    Remove element from Collection in data Map and from storageFile from given path.
    <KeyType, MapKeyType, ValType, MapType extends Map<MapKeyType, ValType>>
    boolean
    removeMap(@NotNull Map<KeyType,MapType> data, KeyType key, MapKeyType mapKey, @Nullable String path)
    Remove element from inner Map in data Map and from storageFile from given path.
    <KeyType, ValType>
    void
    save(@Nullable Map<KeyType,ValType> data, KeyType key, ValType value, @Nullable String path)
    Save element to data Map and to storageFile to given path.
    <KeyType, ValType, ColType extends Collection<ValType>>
    boolean
    saveCollection(@NotNull Map<KeyType,ColType> data, KeyType key, ValType value, @NotNull String path)
    Save element to Collection in data Map and to storageFile to given path.
    <KeyType, MapKeyType, ValType, MapType extends Map<MapKeyType, ValType>>
    void
    saveMap(@NotNull Map<KeyType,MapType> data, KeyType key, MapKeyType mapKey, ValType value, @Nullable String path)
    Save element to inner Map in data Map and to storageFile to given path.
     

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface me.wyne.wutils.storage.Storage

    loadDataImpl
  • Field Details

    • gson

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

      protected final boolean jsonQueryLog
      False disables info and warning logging (this option was added due to frequent json queries which cause log spam)
    • storageFile

      protected final File storageFile
    • jsonExecutorService

      protected final ExecutorService jsonExecutorService
  • Constructor Details

    • JsonStorage

      public JsonStorage(@NotNull @NotNull File storageFile, @NotNull @NotNull ExecutorService jsonExecutorService, @NotNull @org.jetbrains.annotations.NotNull boolean jsonQueryLog)
  • Method Details

    • gson

      public com.google.gson.Gson gson()
    • storageFile

      public File storageFile()
    • jsonExecutorService

      public ExecutorService jsonExecutorService()
    • createStorageFolder

      public void createStorageFolder(@NotNull @NotNull File folderPath)
      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
    • loadData

      public void loadData()
      Description copied from interface: Storage
      Load data from storage file. Uses Storage.loadDataImpl(), puts it in other thread and logs.
      Specified by:
      loadData in interface Storage
    • get

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

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

      public <KeyType, ValType> void save(@Nullable @Nullable Map<KeyType,ValType> data, @NotNull KeyType key, @NotNull ValType value, @Nullable @Nullable String path)
      Description copied from interface: Storage
      Save element to data Map 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 Map key type
      ValType - Data Map value type
      Parameters:
      data - Data Map to save element to
      key - Data Map 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 Map<KeyType,ColType> data, @NotNull KeyType key, @NotNull ValType value, @NotNull @NotNull String path)
      Description copied from interface: Storage
      Save element to Collection in data Map and to storageFile to given path.
      Specified by:
      saveCollection in interface Storage
      Type Parameters:
      KeyType - Data Map key type
      ValType - Collection value type
      ColType - Type of Collection
      Parameters:
      data - Data Map to save Collection to
      key - Data Map 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
    • saveMap

      public <KeyType, MapKeyType, ValType, MapType extends Map<MapKeyType, ValType>> void saveMap(@NotNull @NotNull Map<KeyType,MapType> data, @NotNull KeyType key, @NotNull MapKeyType mapKey, @NotNull ValType value, @Nullable @Nullable String path)
      Description copied from interface: Storage
      Save element to inner Map in data Map and to storageFile to given path.
      Specified by:
      saveMap in interface Storage
      Type Parameters:
      KeyType - Data Map key type
      MapKeyType - Inner Map key type
      ValType - Inner Map value type
      MapType - Type of inner Map
      Parameters:
      data - Data Map to save inner Map element to
      key - Data Map key to inner Map to save element to
      mapKey - Inner Map key to save element to
      value - Value to save
      path - Path to inner Map element in storageFile to save element to
    • remove

      public <KeyType, ValType> boolean remove(@Nullable @Nullable Map<KeyType,ValType> data, @NotNull KeyType key, @Nullable @Nullable String path)
      Description copied from interface: Storage
      Remove element from data Map 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 Map key type
      ValType - Data Map value type
      Parameters:
      data - Data Map to remove element from
      key - Data Map 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 Map<KeyType,ColType> data, @NotNull KeyType key, @NotNull ValType value, @NotNull @NotNull String path)
      Description copied from interface: Storage
      Remove element from Collection in data Map and from storageFile from given path.
      Specified by:
      removeCollection in interface Storage
      Type Parameters:
      KeyType - Data Map key type
      ValType - Collection value type
      ColType - Type of Collection
      Parameters:
      data - Data Map to remove Collection element from
      key - Data Map 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
    • removeMap

      public <KeyType, MapKeyType, ValType, MapType extends Map<MapKeyType, ValType>> boolean removeMap(@NotNull @NotNull Map<KeyType,MapType> data, @NotNull KeyType key, @NotNull MapKeyType mapKey, @Nullable @Nullable String path)
      Description copied from interface: Storage
      Remove element from inner Map in data Map and from storageFile from given path.
      Set path as null to remove element by mapKey
      Specified by:
      removeMap in interface Storage
      Type Parameters:
      KeyType - Data Map key type
      MapKeyType - Inner Map key type
      ValType - Inner Map value type
      MapType - Type of inner Map
      Parameters:
      data - Data Map to remove inner Map element from
      key - Data Map key to inner Map to remove element from
      mapKey - Inner Map key to remove
      path - Path to inner Map element in storageFile to remove
      Returns:
      True/False if remove succeed/failed
    • clearCollection

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

      public <KeyType, MapKeyType, ValType, MapType extends Map<MapKeyType, ValType>> boolean clearMap(@NotNull @NotNull Map<KeyType,MapType> data, @NotNull KeyType key, @NotNull @NotNull Class<MapKeyType> mapKeyType)
      Description copied from interface: Storage
      Clear inner Map in data Map and in storageFile.
      Specified by:
      clearMap in interface Storage
      Type Parameters:
      KeyType - Data Map key type
      MapKeyType - Inner Map key type
      ValType - Inner Map value type
      MapType - Type of inner Map
      Parameters:
      data - Data Map to clear inner Map from
      key - Data Map key to inner Map to clear
      mapKeyType - Inner Map key type
      Returns:
      True/False if clear succeed/failed