Package de.eldoria.eldoutilities.pdc
Class PersistentDataWrapper
java.lang.Object
de.eldoria.eldoutilities.pdc.PersistentDataWrapper
A wrapper class for
PersistentDataContainer
.
Provides convenient methods for interacting with the persistent data of an object.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<T,
Z> Z compute
(NamespacedKey key, PersistentDataType<T, Z> type, Function<@Nullable Z, Z> map) Compute a value in aPersistentDataContainer
based on the current value<T,
Z> Z computeIfAbsent
(NamespacedKey key, PersistentDataType<T, Z> type, Z value) Compute a value in aPersistentDataContainer
if it is not set.<T,
Z> Z computeIfPresent
(NamespacedKey key, PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainer
if is set.static PersistentDataWrapper
create
(PersistentDataHolder holder) <T,
Z> Optional<Z> get
(NamespacedKey key, PersistentDataType<T, Z> type) Get a value from a persistent data holder.<T,
Z> Z getOrDefault
(NamespacedKey key, PersistentDataType<T, Z> type, Z defaultValue) Get a value from a persistent data holder.<T,
Z> boolean hasKey
(NamespacedKey key, PersistentDataType<T, Z> type) Check if a key is present in thePersistentDataContainer
.<T,
Z> void put
(NamespacedKey key, PersistentDataType<T, Z> type, Z value) Set a value in aPersistentDataContainer
.<T,
Z> boolean remove
(NamespacedKey key, PersistentDataType<T, Z> type) Remove a key from thePersistentDataContainer
if it is set.<T,
Z> void setIfAbsent
(NamespacedKey key, PersistentDataType<T, Z> type, Z value) Sets a value in a data container if it is absent.
-
Constructor Details
-
PersistentDataWrapper
-
-
Method Details
-
create
-
setIfAbsent
Sets a value in a data container if it is absent.- Type Parameters:
T
- type of keyZ
- type of value- Parameters:
key
- key to settype
- type of keyvalue
- value of key
-
compute
@Nullable public <T,Z> Z compute(NamespacedKey key, PersistentDataType<T, Z> type, Function<@Nullable Z, Z> map) Compute a value in aPersistentDataContainer
based on the current value- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
key
- key to computetype
- type of keymap
- map the current value to the new value. Current value is null if key is not set.- Returns:
- the mapped value. can be null if mapping function returns null or holder is null
-
computeIfAbsent
Compute a value in aPersistentDataContainer
if it is not set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
key
- key to computetype
- type of keyvalue
- value which should be set if the key is not present.- Returns:
- the value associated with this key. can be null if holder is null.
-
computeIfPresent
@Nullable public <T,Z> Z computeIfPresent(NamespacedKey key, PersistentDataType<T, Z> type, Function<Z, Z> mappingFunction) Compute a value in aPersistentDataContainer
if is set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
key
- key to computetype
- type of keymappingFunction
- function to map the current value to the new value.- Returns:
- the value associated with this key. can be null if holder is null.
-
get
Get a value from a persistent data holder.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
key
- key to gettype
- type of key- Returns:
- result wrapped in an optional if present.
-
getOrDefault
Get a value from a persistent data holder.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
key
- key to gettype
- type of keydefaultValue
- default value if key is absent- Returns:
- result wrapped in an optional if present.
-
put
Set a value in aPersistentDataContainer
.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
key
- key to settype
- type of keyvalue
- value to set
-
hasKey
Check if a key is present in thePersistentDataContainer
.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
key
- key to checktype
- type of key- Returns:
- true if the key is set
-
remove
Remove a key from thePersistentDataContainer
if it is set.- Type Parameters:
T
- type of valueZ
- type of value- Parameters:
key
- key to checktype
- type of key- Returns:
- true if the key was present and removed.
-