Interface DataManager
Implementations of this interface should provide persistent or in-memory storage functionality.
Created by Jummania on 20, November, 2024. Email: sharifuddinjumman@gmail.com Dhaka, Bangladesh.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
static interface
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addDataObserver
(DataManager.DataObserver observer) Registers aDataManager.DataObserver
to listen for data changes.default void
appendToList
(String key, int index, Object element) Appends an element to a list associated with the given key at the specified index.void
appendToList
(String key, int index, Object element, boolean removeDuplicate) Appends an element to a list associated with the given key at the specified index.default void
appendToList
(String key, Object element) Appends an element to a list associated with the given key.default void
appendToList
(String key, Object element, boolean removeDuplicate) Appends an element to a list associated with the given key.void
clear()
Clears all entries from the data structure.boolean
Checks if the data structure contains an entry associated with the given key.<T> T
Converts a JSON string into an object of the specified type.<T> T
fromReader
(Reader json, Type typeOfT) Converts a JSON input stream (Reader) into an object of the specified type.default boolean
getBoolean
(String key) Retrieves a boolean value associated with the given key.default boolean
getBoolean
(String key, boolean defValue) Retrieves a boolean value associated with the given key.default float
Retrieves a float value associated with the given key.default float
Retrieves a float value associated with the given key.<T> List<T>
getFullList
(String key, Type type) Retrieves a list of objects of the specified type associated with the given key.default int
Retrieves an integer value associated with the given key.default int
Retrieves an integer value associated with the given key.default long
Retrieves a long value associated with the given key.default long
Retrieves a long value associated with the given key.<T> T
Retrieves an object of the specified type associated with the given key.<T> PaginatedData<T>
getPagedList
(String key, Type type, int page) Retrieves a paginated list of objects of the specified type associated with the given key.getParameterized
(Type rawType, Type... typeArguments) Creates a parameterizedType
using the specified raw type and type arguments.getRawString
(String key) Retrieves the raw string value associated with the given key.default String
Retrieves a string value associated with the given key.default String
Retrieves a string value associated with the given key.void
Removes the entry associated with the given key.void
Unregisters the currently registeredDataManager.DataObserver
.void
removeFromList
(String key, int index) Removes an element from the list associated with the given key at the specified index.default void
saveBoolean
(String key, boolean value) Saves a boolean value associated with the given key by converting the boolean to a string.default void
Saves a float value associated with the given key by converting the float to a string.default void
Saves an integer value associated with the given key by converting the integer to a string.default <E> void
Saves a list of objects associated with the given key by converting the list to a JSON string.<E> void
Saves a list of objects associated with the given key by converting the list to a JSON string.default void
Saves a long value associated with the given key by converting the long to a string.default void
saveObject
(String key, Object value) Saves an object associated with the given key by converting the object to a JSON string.void
saveString
(String key, String value) Saves a string value associated with the given key.Converts an object into its JSON string representation.
-
Method Details
-
getString
Retrieves a string value associated with the given key. If no value is found or the value is null, returns the provided default value.- Parameters:
key
- the key to look up the valuedefValue
- the default value to return if no value is found or if the result is null- Returns:
- the retrieved string value or the default value if not found
-
getString
Retrieves a string value associated with the given key. If no value is found or the value is null, returns null.This is a shorthand method that calls
getString(String, String)
with a default value of null.- Parameters:
key
- the key to look up the value- Returns:
- the retrieved string value or null if not found
-
getInt
Retrieves an integer value associated with the given key. If no value is found or the value is null, returns the provided default value.- Parameters:
key
- the key to look up the valuedefValue
- the default value to return if no value is found or if the result is null- Returns:
- the retrieved integer value or the default value if not found
-
getInt
Retrieves an integer value associated with the given key. If no value is found or the value is null, returns 0.This is a shorthand method that calls
getInt(String, int)
with a default value of 0.- Parameters:
key
- the key to look up the value- Returns:
- the retrieved integer value or 0 if not found
-
getLong
Retrieves a long value associated with the given key. If no value is found or the value is null, returns the provided default value.- Parameters:
key
- the key to look up the valuedefValue
- the default value to return if no value is found or if the result is null- Returns:
- the retrieved long value or the default value if not found
-
getLong
Retrieves a long value associated with the given key. If no value is found or the value is null, returns 0.This is a shorthand method that calls
getLong(String, long)
with a default value of 0.- Parameters:
key
- the key to look up the value- Returns:
- the retrieved long value or 0 if not found
-
getFloat
Retrieves a float value associated with the given key. If no value is found or the value is null, returns the provided default value.- Parameters:
key
- the key to look up the valuedefValue
- the default value to return if no value is found or if the result is null- Returns:
- the retrieved float value or the default value if not found
-
getFloat
Retrieves a float value associated with the given key. If no value is found or the value is null, returns 0.This is a shorthand method that calls
getFloat(String, float)
with a default value of 0.- Parameters:
key
- the key to look up the value- Returns:
- the retrieved float value or 0 if not found
-
getBoolean
Retrieves a boolean value associated with the given key. If no value is found or the value is null, returns the provided default value.- Parameters:
key
- the key to look up the valuedefValue
- the default value to return if no value is found or if the result is null- Returns:
- the retrieved boolean value or the default value if not found
-
getBoolean
Retrieves a boolean value associated with the given key. If no value is found or the value is null, returns false.This is a shorthand method that calls
getBoolean(String, boolean)
with a default value of false.- Parameters:
key
- the key to look up the value- Returns:
- the retrieved boolean value or false if not found
-
getRawString
Retrieves the raw string value associated with the given key. The value is returned as-is, without any defaulting behavior.- Parameters:
key
- the key to look up the value- Returns:
- the raw string value associated with the key, or null if not found
-
getObject
Retrieves an object of the specified type associated with the given key. The object is deserialized from the underlying data source to match the specified type.- Type Parameters:
T
- the type of the object to be returned- Parameters:
key
- the key to look up the objecttype
- the type of the object to be returned- Returns:
- the object of the specified type associated with the key, or null if not found or if the type doesn't match
-
getFullList
Retrieves a list of objects of the specified type associated with the given key. The list is deserialized from the underlying data source to match the specified type.- Type Parameters:
T
- the type of the objects in the list- Parameters:
key
- the key to look up the list of objectstype
- the type of the objects in the list- Returns:
- the list of objects of the specified type associated with the key, or an empty list if not found
-
getPagedList
Retrieves a paginated list of objects of the specified type associated with the given key. The list is deserialized from the underlying data source to match the specified type, with pagination support.- Type Parameters:
T
- the type of the objects in the list- Parameters:
key
- the key to look up the paginated list of objectstype
- the type of the objects in the listpage
- the page number to retrieve- Returns:
- a
PaginatedData
object containing the paginated list of objects of the specified type, or an empty paginated data if no objects are found
-
saveString
Saves a string value associated with the given key. If the key already exists, the value will be updated.- Parameters:
key
- the key to associate with the string valuevalue
- the string value to save
-
saveInt
Saves an integer value associated with the given key by converting the integer to a string. If the key already exists, the value will be updated.This is a shorthand method that calls
saveString(String, String)
by converting the integer value to a string.- Parameters:
key
- the key to associate with the integer valuevalue
- the integer value to save
-
saveLong
Saves a long value associated with the given key by converting the long to a string. If the key already exists, the value will be updated.This is a shorthand method that calls
saveString(String, String)
by converting the long value to a string.- Parameters:
key
- the key to associate with the long valuevalue
- the long value to save
-
saveFloat
Saves a float value associated with the given key by converting the float to a string. If the key already exists, the value will be updated.This is a shorthand method that calls
saveString(String, String)
by converting the float value to a string.- Parameters:
key
- the key to associate with the float valuevalue
- the float value to save
-
saveBoolean
Saves a boolean value associated with the given key by converting the boolean to a string. If the key already exists, the value will be updated.This is a shorthand method that calls
saveString(String, String)
by converting the boolean value to a string.- Parameters:
key
- the key to associate with the boolean valuevalue
- the boolean value to save
-
saveObject
Saves an object associated with the given key by converting the object to a JSON string. If the key already exists, the value will be updated.This is a shorthand method that calls
saveString(String, String)
by converting the object to a JSON string usingtoJson(Object)
.- Parameters:
key
- the key to associate with the objectvalue
- the object to save
-
saveList
Saves a list of objects associated with the given key by converting the list to a JSON string. If the key already exists, the value will be updated. The size of the list is capped to the specified maximum array size.- Type Parameters:
E
- the type of elements in the list- Parameters:
key
- the key to associate with the list of objectsvalue
- the list of objects to savemaxArraySize
- the maximum number of elements in the list to be saved
-
saveList
Saves a list of objects associated with the given key by converting the list to a JSON string. If the key already exists, the value will be updated. The size of the list is capped to 25 elements.This is a shorthand method that calls
saveList(String, List, int)
with a default maximum array size of 25.- Type Parameters:
E
- the type of elements in the list- Parameters:
key
- the key to associate with the list of objectsvalue
- the list of objects to save
-
appendToList
Appends an element to a list associated with the given key at the specified index. If the list does not exist, a new list will be created. Optionally, duplicates can be removed before appending the element.- Parameters:
key
- the key to identify the listindex
- the position at which to insert the element in the listelement
- the element to append to the listremoveDuplicate
- whether to remove duplicate elements before appending the new element
-
appendToList
Appends an element to a list associated with the given key at the specified index. If the list does not exist, a new list will be created. Duplicates are not removed by default.This is a shorthand method that calls
appendToList(String, int, Object, boolean)
with the `removeDuplicate` flag set to false.- Parameters:
key
- the key to identify the listindex
- the position at which to insert the element in the listelement
- the element to append to the list
-
appendToList
Appends an element to a list associated with the given key. The element is added at the end of the list. If the list does not exist, a new list will be created. Optionally, duplicates can be removed before appending the element.This is a shorthand method that calls
appendToList(String, int, Object, boolean)
with the index set to -1, indicating the element should be appended to the end of the list.- Parameters:
key
- the key to identify the listelement
- the element to append to the listremoveDuplicate
- whether to remove duplicate elements before appending the new element
-
appendToList
Appends an element to a list associated with the given key. The element is added at the end of the list. If the list does not exist, a new list will be created. Duplicates are not removed by default.This is a shorthand method that calls
appendToList(String, Object, boolean)
with the index set to -1 and the `removeDuplicate` flag set to false, meaning the element will be added to the end of the list without removing duplicates.- Parameters:
key
- the key to identify the listelement
- the element to append to the list
-
removeFromList
Removes an element from the list associated with the given key at the specified index. If the list does not contain an element at the given index, no changes will be made.- Parameters:
key
- the key to identify the listindex
- the position of the element to remove from the list
-
fromJson
Converts a JSON string into an object of the specified type. This method deserializes the given JSON string into an instance of the provided type using the Gson library (or similar).- Type Parameters:
T
- the type of the object to return- Parameters:
value
- the JSON string to deserializetypeOfT
- the Type of the object to convert the JSON string into- Returns:
- the deserialized object of the specified type
-
fromReader
Converts a JSON input stream (Reader) into an object of the specified type. This method deserializes the provided JSON from the given reader into an instance of the provided type.- Type Parameters:
T
- the type of the object to return- Parameters:
json
- the Reader that contains the JSON data to deserializetypeOfT
- the Type of the object to convert the JSON data into- Returns:
- the deserialized object of the specified type
-
toJson
Converts an object into its JSON string representation. This method serializes the given object into a JSON string using a serialization library such as Gson.- Parameters:
object
- the object to serialize into JSON- Returns:
- the JSON string representation of the object
-
getParameterized
-
remove
Removes the entry associated with the given key. If the key does not exist, no changes will be made.- Parameters:
key
- the key identifying the entry to remove
-
clear
void clear()Clears all entries from the data structure. After calling this method, the data structure will be empty. -
contains
Checks if the data structure contains an entry associated with the given key.- Parameters:
key
- the key to check for existence in the data structure- Returns:
true
if the key exists in the data structure,false
otherwise
-
addDataObserver
Registers aDataManager.DataObserver
to listen for data changes.- Parameters:
observer
- The observer to be registered.
-
removeDataObserver
void removeDataObserver()Unregisters the currently registeredDataManager.DataObserver
.
-