Package me.wyne.wutils.storage
Class JsonStorage
java.lang.Object
me.wyne.wutils.storage.JsonStorage
- All Implemented Interfaces:
Storage
Requires
com.google.gson dependency.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final com.google.gson.Gsonprotected final ExecutorServiceprotected final File -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<KeyType,ValType, ColType extends Collection<ValType>>
booleanclearCollection(@NotNull Map<KeyType, ColType> data, KeyType key, @NotNull String path) ClearCollectionin dataMapand in storageFile in given path.<KeyType,MapKeyType, ValType, MapType extends Map<MapKeyType, ValType>>
booleanvoidCreate storage file.voidcreateStorageFolder(@NotNull File folderPath) Create folder for storage file.<KeyType,ValType>
ValTypeGet element from dataMap.<KeyType,ValType>
@NotNull Collection<ValType>getCollection(@NotNull Map<KeyType, ? extends Collection<ValType>> data, KeyType key) GetCollectionfrom dataMap.voidloadData()Load data from storage file.<KeyType,ValType>
booleanRemove element from dataMapand from storageFile from given path.<KeyType,ValType, ColType extends Collection<ValType>>
booleanremoveCollection(@NotNull Map<KeyType, ColType> data, KeyType key, ValType value, @NotNull String path) Remove element fromCollectionin dataMapand from storageFile from given path.<KeyType,MapKeyType, ValType, MapType extends Map<MapKeyType, ValType>>
booleanremoveMap(@NotNull Map<KeyType, MapType> data, KeyType key, MapKeyType mapKey, @Nullable String path) <KeyType,ValType>
voidSave element to dataMapand to storageFile to given path.<KeyType,ValType, ColType extends Collection<ValType>>
booleansaveCollection(@NotNull Map<KeyType, ColType> data, KeyType key, ValType value, @NotNull String path) Save element toCollectionin dataMapand to storageFile to given path.<KeyType,MapKeyType, ValType, MapType extends Map<MapKeyType, ValType>>
voidMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface me.wyne.wutils.storage.Storage
loadDataImpl
-
Field Details
-
gson
protected final com.google.gson.Gson gson -
storageFile
-
jsonExecutorService
-
-
Constructor Details
-
JsonStorage
-
-
Method Details
-
createStorageFolder
Description copied from interface:StorageCreate folder for storage file.- Specified by:
createStorageFolderin interfaceStorage
-
createStorageFile
public void createStorageFile()Description copied from interface:StorageCreate storage file.- Specified by:
createStorageFilein interfaceStorage
-
loadData
public void loadData()Description copied from interface:StorageLoad data from storage file. UsesStorage.loadDataImpl(), puts it in other thread and logs. -
get
@Nullable public <KeyType,ValType> ValType get(@NotNull @NotNull Map<KeyType, ValType> data, @NotNull KeyType key) Get element from dataMap. If Map doesn't have given key it will return null. Map is used because data is often stored as key:value. -
getCollection
@NotNull public <KeyType,ValType> @NotNull Collection<ValType> getCollection(@NotNull @NotNull Map<KeyType, ? extends Collection<ValType>> data, @NotNull KeyType key) GetCollectionfrom dataMap. If dataMapdoesn't have given key or retrievedCollectionis empty it will return null. Map is used because data is often stored as key:value.- Specified by:
getCollectionin interfaceStorage- Type Parameters:
KeyType- DataMapkey typeValType-Collectionvalue type- Parameters:
data- DataMapto getCollectionfromkey- DataMapkey to getCollectionfrom- Returns:
Collectionfrom dataMapor 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:StorageSave element to dataMapand 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. -
saveCollection
public <KeyType,ValType, boolean saveCollectionColType extends Collection<ValType>> (@NotNull @NotNull Map<KeyType, ColType> data, @NotNull KeyType key, @NotNull ValType value, @NotNull @NotNull String path) Description copied from interface:StorageSave element toCollectionin dataMapand to storageFile to given path.- Specified by:
saveCollectionin interfaceStorage- Type Parameters:
KeyType- DataMapkey typeValType-Collectionvalue typeColType- Type ofCollection- Parameters:
data- DataMapto saveCollectiontokey- DataMapkey toCollectionto save element tovalue- Value to savepath- Path toJsonArrayin storageFile to save element to- Returns:
- True/False if save succeed/failed
-
saveMap
public <KeyType,MapKeyType, void saveMapValType, MapType extends Map<MapKeyType, ValType>> (@NotNull @NotNull Map<KeyType, MapType> data, @NotNull KeyType key, @NotNull MapKeyType mapKey, @NotNull ValType value, @Nullable @Nullable String path) Description copied from interface:Storage- Specified by:
saveMapin interfaceStorage- Type Parameters:
KeyType- DataMapkey typeMapKeyType- InnerMapkey typeValType- InnerMapvalue typeMapType- Type of innerMap- Parameters:
data- DataMapto save innerMapelement tokey- DataMapkey to innerMapto save element tomapKey- InnerMapkey to save element tovalue- Value to savepath- Path to innerMapelement 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:StorageRemove element from dataMapand 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:
removein interfaceStorage- Type Parameters:
KeyType- DataMapkey typeValType- DataMapvalue type- Parameters:
data- DataMapto remove element fromkey- DataMapkey to remove element frompath- Path in storageFile to remove element from- Returns:
- True/False if remove succeed/failed
-
removeCollection
public <KeyType,ValType, boolean removeCollectionColType extends Collection<ValType>> (@NotNull @NotNull Map<KeyType, ColType> data, @NotNull KeyType key, @NotNull ValType value, @NotNull @NotNull String path) Description copied from interface:StorageRemove element fromCollectionin dataMapand from storageFile from given path.- Specified by:
removeCollectionin interfaceStorage- Type Parameters:
KeyType- DataMapkey typeValType-Collectionvalue typeColType- Type ofCollection- Parameters:
data- DataMapto removeCollectionelement fromkey- DataMapkey toCollectionto remove element fromvalue- Value to removepath- Path toJsonArrayin storageFile to remove element from- Returns:
- True/False if remove succeed/failed
-
removeMap
public <KeyType,MapKeyType, boolean removeMapValType, MapType extends Map<MapKeyType, ValType>> (@NotNull @NotNull Map<KeyType, MapType> data, @NotNull KeyType key, @NotNull MapKeyType mapKey, @Nullable @Nullable String path) Description copied from interface:StorageRemove element from innerMapin dataMapand from storageFile from given path.
Set path as null to remove element by mapKey- Specified by:
removeMapin interfaceStorage- Type Parameters:
KeyType- DataMapkey typeMapKeyType- InnerMapkey typeValType- InnerMapvalue typeMapType- Type of innerMap- Parameters:
data- DataMapto remove innerMapelement fromkey- DataMapkey to innerMapto remove element frommapKey- InnerMapkey to removepath- Path to innerMapelement in storageFile to remove- Returns:
- True/False if remove succeed/failed
-
clearCollection
public <KeyType,ValType, boolean clearCollectionColType extends Collection<ValType>> (@NotNull @NotNull Map<KeyType, ColType> data, @NotNull KeyType key, @NotNull @NotNull String path) Description copied from interface:StorageClearCollectionin dataMapand in storageFile in given path.- Specified by:
clearCollectionin interfaceStorage- Type Parameters:
KeyType- DataMapkey typeValType-Collectionvalue typeColType- Type ofCollection- Parameters:
data- DataMapto clearCollectionfromkey- DataMapkey toCollectionto clearpath- Path toJsonArrayin storageFile to clear- Returns:
- True/False if clear succeed/failed
-
clearMap
public <KeyType,MapKeyType, boolean clearMapValType, MapType extends Map<MapKeyType, ValType>> (@NotNull @NotNull Map<KeyType, MapType> data, @NotNull KeyType key, @NotNull @NotNull Class<MapKeyType> mapKeyType) Description copied from interface:Storage- Specified by:
clearMapin interfaceStorage- Type Parameters:
KeyType- DataMapkey typeMapKeyType- InnerMapkey typeValType- InnerMapvalue typeMapType- Type of innerMap- Parameters:
data- DataMapto clear innerMapfromkey- DataMapkey to innerMapto clearmapKeyType- InnerMapkey type- Returns:
- True/False if clear succeed/failed
-