Package host.anzo.commons.collection
Class StatsSet
java.lang.Object
host.anzo.commons.collection.StatsSet
A wrapper over Map that allows you to safely work with different data types.
Supports automatic type conversion when retrieving values, as well as
adding values, primitive types, and Enums.
Also implements standard Map methods.
IMPORTANT: Even when using a thread-safe internal map, data consistency is not guaranteed when using custom methods. All manipulations are performed with the value retrieved at the time of access, and if another thread changes the data at this time, this will not be taken into account.
If standard Map methods are used, then thread safety will directly depend on the type of the internal map.
IMPORTANT: Even when using a thread-safe internal map, data consistency is not guaranteed when using custom methods. All manipulations are performed with the value retrieved at the time of access, and if another thread changes the data at this time, this will not be taken into account.
If standard Map methods are used, then thread safety will directly depend on the type of the internal map.
-
Nested Class Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears the map.compute(String key, @NotNull BiFunction<? super String, ? super @Nullable Object, ?> remappingFunction) Calculates a new value for the key using the given function.computeIfAbsent(String key, @NotNull Function<? super String, ?> mappingFunction) Calculates the value for the key if the key is missing, using the given function.computeIfPresent(String key, @NotNull BiFunction<? super String, ? super Object, ?> remappingFunction) Computes a new value for the key, if the key is present, using the given function.booleancontainsKey(Object key) Checks if the map contains the key.booleancontainsValue(Object value) Checks if the map contains a value.entrySet()Returns the map recordset.voidforEach(BiConsumer<? super String, ? super Object> action) Performs the given action for each entry in the map.@Nullable ObjectReturns the value by key.booleangetBooleanOrDefault(@NotNull String key, boolean defaultValue) Returns a boolean value for the key, or a default value if the key is not found.booleangetBooleanOrThrow(@NotNull String key) Returns a boolean value by key.bytegetByteOrDefault(@NotNull String key, byte defaultValue) Returns the byte value for the key, or the default value if the key is not found.bytegetByteOrThrow(@NotNull String key) Returns the byte value by key.doublegetDoubleOrDefault(@NotNull String key, double defaultValue) Returns a double value by key, or a default value if the key is not found.doublegetDoubleOrThrow(@NotNull String key) Returns a double value by key.<T extends Enum<T>>
TgetEnumOrDefault(@NotNull String key, @NotNull Class<T> enumClass, T defaultValue, @Nullable String start, @Nullable String end) Returns the enumeration value by key, or the default value if the key is not found.<T extends Enum<T>>
TgetEnumOrThrow(@NotNull String key, @NotNull Class<T> enumClass) Returns an enumeration value by key.floatgetFloatOrDefault(@NotNull String key, float defaultValue) Returns a float value by key, or a default value if the key is not found.floatgetFloatOrThrow(@NotNull String key) Returns a float value by key.intgetIntOrDefault(@NotNull String key, int defaultValue) Returns an integer value for the key, or a default value if the key is not found.intgetIntOrThrow(@NotNull String key) Returns an integer value by key.longgetLongOrDefault(@NotNull String key, long defaultValue) Returns a long value by key, or a default value if the key is not found.longgetLongOrThrow(@NotNull String key) Returns a long value by key.<A> AgetObjectOrDefault(@NotNull String key, @NotNull Class<A> type, A defaultValue) Returns the object by key, cast to the given type, or the default value if the key is not found.<A> AgetObjectOrThrow(@NotNull String key, @NotNull Class<A> type) Returns an object by key, cast to the given type.getOrDefault(Object key, Object defaultValue) Returns the value by key, or the default value if the key is not found.boolean @NotNull []getParsedBooleanArrayOrThrow(@NotNull String key, @NotNull String splitOn, @Nullable String start, @Nullable String end) Returns an array of booleans by key.byte @NotNull []getParsedByteArrayOrThrow(@NotNull String key, @NotNull String splitOn, @Nullable String start, @Nullable String end) Returns a byte array by key.char @NotNull []getParsedCharArrayOrThrow(@NotNull String key, @Nullable String start, @Nullable String end) Returns a char array by key.double @NotNull []getParsedDoubleArrayOrThrow(@NotNull String key, @NotNull String splitOn, @Nullable String start, @Nullable String end) Returns an array of doubles by key.float @NotNull []getParsedFloatArrayOrThrow(@NotNull String key, @NotNull String splitOn, @Nullable String start, @Nullable String end) Returns an array of floats by key.int @NotNull []getParsedIntArrayOrThrow(@NotNull String key, @NotNull String splitOn, @Nullable String start, @Nullable String end) Returns an array of integers by key.long @NotNull []getParsedLongArrayOrThrow(@NotNull String key, @NotNull String splitOn, @Nullable String start, @Nullable String end) Returns an array of long values by key.short @NotNull []getParsedShortArrayOrThrow(@NotNull String key, @NotNull String splitOn, @Nullable String start, @Nullable String end) Returns an array of short values by key.@Nullable ObjectgetRawObject(@NotNull String key) Returns a raw object by key.shortgetShortOrDefault(@NotNull String key, short defaultValue) Returns a short value by key, or a default value if the key is not found.shortgetShortOrThrow(@NotNull String key) Returns a short value by key.@Nullable StringgetStringOrDefault(@NotNull String key, @Nullable String defaultValue) Returns a string value for a key, or a default value if the key is not found.@NotNull StringgetStringOrThrow(@NotNull String key) Returns a string value by key.booleanisEmpty()Checks if the map is empty.keySet()Returns the set of map keys.merge(String key, @NotNull Object value, @NotNull BiFunction<? super Object, ? super Object, ?> remappingFunction) Merge a value by key with a given value using a given function.static StatsSetparseParams(@Nullable String params, @NotNull String splitter1, @NotNull String splitter2, @NotNull Supplier<Map<String, Object>> mapFactory) Parses the parameter string by the given separators and creates aStatsSet.@Nullable ObjectPuts a value into the map by key.voidCopies all records from another map to the current one.voidputBoolean(@NotNull String key, boolean value) Puts a boolean value into the map by key.voidPuts a byte value into the map by key.voidPuts a double value into the map by key.voidPuts a string value into the map by key if the value is not null.voidPuts a float value into the map by key.@Nullable ObjectputIfAbsent(String key, Object value) Puts a value into the map by key if the key is missing.voidPuts an integer value into the map by key.voidPuts a long value into the map by key.voidPuts an object into the map by key if the value is not null.voidPuts a short value into the map by key.voidPuts a string value into the map by key if the value is not null.Deletes a record by key.booleanDeletes a record by key and value.@Nullable ObjectReplaces the value by key.booleanReplaces a value by keyvoidreplaceAll(BiFunction<? super String, ? super Object, ?> function) Replaces all values in the map with the results of calling the given function.intsize()Returns the size of the map.toString()Returns a string representation of the map.@NotNull Collection<Object> values()Returns a collection of map values.static StatsSetCreates a newStatsSetinstance and wraps the given map.
-
Constructor Details
-
StatsSet
Creates a newStatsSetinstance using the factory to create the internal map.- Parameters:
mapFactory- factory to create the internal map
-
-
Method Details
-
wrap
Creates a newStatsSetinstance and wraps the given map.- Parameters:
map- the map to wrap- Returns:
- a new
StatsSetinstance
-
parseParams
public static StatsSet parseParams(@Nullable @Nullable String params, @NotNull @NotNull String splitter1, @NotNull @NotNull String splitter2, @NotNull @NotNull Supplier<Map<String, Object>> mapFactory) Parses the parameter string by the given separators and creates aStatsSet. For example: (key1)(spliter2)(value1)(spliter1)(key2)(spliter2)(value2)(spliter1)(keyN)(spliter2)(valueN)- Parameters:
params- parameter stringsplitter1- separator between key-value pairssplitter2- separator between key and valuemapFactory- factory for creating the internal map- Returns:
- a new
StatsSetinstance with the data from the parameter string
-
getBooleanOrThrow
Returns a boolean value by key.- Parameters:
key- key- Returns:
- boolean value
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to boolean
-
getBooleanOrDefault
Returns a boolean value for the key, or a default value if the key is not found.- Parameters:
key- keydefaultValue- default value- Returns:
- boolean or default value
-
getByteOrThrow
Returns the byte value by key.- Parameters:
key- key- Returns:
- byte value
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to byte type
-
getByteOrDefault
Returns the byte value for the key, or the default value if the key is not found.- Parameters:
key- the keydefaultValue- the default value- Returns:
- the byte value or default value
-
getShortOrThrow
Returns a short value by key.- Parameters:
key- key- Returns:
- short value
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to short
-
getShortOrDefault
Returns a short value by key, or a default value if the key is not found.- Parameters:
key- keydefaultValue- default value- Returns:
- short value or default value
-
getIntOrThrow
Returns an integer value by key.- Parameters:
key- key- Returns:
- integer value
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to integer
-
getIntOrDefault
Returns an integer value for the key, or a default value if the key is not found.- Parameters:
key- keydefaultValue- default value- Returns:
- integer value or default value
-
getLongOrThrow
Returns a long value by key.- Parameters:
key- key- Returns:
- long value
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to long
-
getLongOrDefault
Returns a long value by key, or a default value if the key is not found.- Parameters:
key- keydefaultValue- default value- Returns:
- long value or default value
-
getFloatOrThrow
Returns a float value by key.- Parameters:
key- key- Returns:
- float value
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to float
-
getFloatOrDefault
Returns a float value by key, or a default value if the key is not found.- Parameters:
key- keydefaultValue- default value- Returns:
- float value or default value
-
getDoubleOrThrow
Returns a double value by key.- Parameters:
key- key- Returns:
- double value
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to double
-
getDoubleOrDefault
Returns a double value by key, or a default value if the key is not found.- Parameters:
key- keydefaultValue- default value- Returns:
- double value or default value
-
getStringOrThrow
@NotNull public @NotNull String getStringOrThrow(@NotNull @NotNull String key) throws IllegalArgumentException Returns a string value by key.- Parameters:
key- key- Returns:
- string value
- Throws:
IllegalArgumentException- if value not found
-
getStringOrDefault
@Nullable public @Nullable String getStringOrDefault(@NotNull @NotNull String key, @Nullable @Nullable String defaultValue) Returns a string value for a key, or a default value if the key is not found.- Parameters:
key- keydefaultValue- default value- Returns:
- string value or default value
-
getEnumOrThrow
@NotNull public <T extends Enum<T>> T getEnumOrThrow(@NotNull @NotNull String key, @NotNull @NotNull Class<T> enumClass) throws IllegalArgumentException Returns an enumeration value by key.- Type Parameters:
T- the enumeration type- Parameters:
key- the keyenumClass- the enumeration class- Returns:
- the enumeration value
- Throws:
IllegalArgumentException- if the value is not found, cannot be converted to an enumeration type, or the class is not an enumeration
-
getEnumOrDefault
@Nullable public <T extends Enum<T>> T getEnumOrDefault(@NotNull @NotNull String key, @NotNull @NotNull Class<T> enumClass, @Nullable T defaultValue, @Nullable @Nullable String start, @Nullable @Nullable String end) Returns the enumeration value by key, or the default value if the key is not found.- Type Parameters:
T- the enumeration type- Parameters:
key- the keyenumClass- the enumeration classdefaultValue- the default value- Returns:
- the enumeration value or default value
- Throws:
IllegalArgumentException- if the class is not an enum
-
getRawObject
Returns a raw object by key.- Parameters:
key- key- Returns:
- raw object or null if key not found
-
getObjectOrThrow
@NotNull public <A> A getObjectOrThrow(@NotNull @NotNull String key, @NotNull @NotNull Class<A> type) throws IllegalArgumentException Returns an object by key, cast to the given type.- Type Parameters:
A- object type- Parameters:
key- keytype- class to cast the object to- Returns:
- object cast to the given type
- Throws:
IllegalArgumentException- if the value is not found or cannot be cast to the given type
-
getObjectOrDefault
@Nullable public <A> A getObjectOrDefault(@NotNull @NotNull String key, @NotNull @NotNull Class<A> type, @Nullable A defaultValue) Returns the object by key, cast to the given type, or the default value if the key is not found.- Type Parameters:
A- the type of the object- Parameters:
key- the keytype- the class to cast the object todefaultValue- the default value- Returns:
- the object cast to the given type, or the default value
-
getParsedBooleanArrayOrThrow
public boolean @NotNull [] getParsedBooleanArrayOrThrow(@NotNull @NotNull String key, @NotNull @NotNull String splitOn, @Nullable @Nullable String start, @Nullable @Nullable String end) throws IllegalArgumentException Returns an array of booleans by key.- Parameters:
key- keysplitOn- delimiter for parsing string into arraystart- start character to clear stringend- end character to clear string- Returns:
- array of booleans
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to boolean array
-
getParsedCharArrayOrThrow
public char @NotNull [] getParsedCharArrayOrThrow(@NotNull @NotNull String key, @Nullable @Nullable String start, @Nullable @Nullable String end) throws IllegalArgumentException Returns a char array by key.- Parameters:
key- keystart- start character to clear the stringend- end character to clear the string- Returns:
- char array
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to char array
-
getParsedByteArrayOrThrow
public byte @NotNull [] getParsedByteArrayOrThrow(@NotNull @NotNull String key, @NotNull @NotNull String splitOn, @Nullable @Nullable String start, @Nullable @Nullable String end) throws IllegalArgumentException Returns a byte array by key.- Parameters:
key- the keysplitOn- the separator for parsing the string into an arraystart- the starting character to clear the stringend- the ending character to clear the string- Returns:
- the byte array
- Throws:
IllegalArgumentException- if the value is not found or cannot be converted to a byte array
-
getParsedShortArrayOrThrow
public short @NotNull [] getParsedShortArrayOrThrow(@NotNull @NotNull String key, @NotNull @NotNull String splitOn, @Nullable @Nullable String start, @Nullable @Nullable String end) throws IllegalArgumentException Returns an array of short values by key.- Parameters:
key- keysplitOn- delimiter for parsing string into arraystart- start character to clear stringend- end character to clear string- Returns:
- array of short values
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to array of short values
-
getParsedIntArrayOrThrow
public int @NotNull [] getParsedIntArrayOrThrow(@NotNull @NotNull String key, @NotNull @NotNull String splitOn, @Nullable @Nullable String start, @Nullable @Nullable String end) throws IllegalArgumentException Returns an array of integers by key.- Parameters:
key- keysplitOn- separator for parsing string to arraystart- start character to clear stringend- end character to clear string- Returns:
- array of integers
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to array of integers
-
getParsedLongArrayOrThrow
public long @NotNull [] getParsedLongArrayOrThrow(@NotNull @NotNull String key, @NotNull @NotNull String splitOn, @Nullable @Nullable String start, @Nullable @Nullable String end) throws IllegalArgumentException Returns an array of long values by key.- Parameters:
key- keysplitOn- separator for parsing string into arraystart- start character to clear stringend- end character to clear string- Returns:
- array of long values
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to array of long values
-
getParsedFloatArrayOrThrow
public float @NotNull [] getParsedFloatArrayOrThrow(@NotNull @NotNull String key, @NotNull @NotNull String splitOn, @Nullable @Nullable String start, @Nullable @Nullable String end) throws IllegalArgumentException Returns an array of floats by key.- Parameters:
key- keysplitOn- separator for parsing string to arraystart- start character to clear stringend- end character to clear string- Returns:
- array of floats
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to array of floats
-
getParsedDoubleArrayOrThrow
public double @NotNull [] getParsedDoubleArrayOrThrow(@NotNull @NotNull String key, @NotNull @NotNull String splitOn, @Nullable @Nullable String start, @Nullable @Nullable String end) throws IllegalArgumentException Returns an array of doubles by key.- Parameters:
key- keysplitOn- separator for parsing string into arraystart- start character to clear stringend- end character to clear string- Returns:
- array of doubles
- Throws:
IllegalArgumentException- if value is not found or cannot be converted to array of doubles
-
putObject
Puts an object into the map by key if the value is not null.- Parameters:
key- keyvalue- value
-
putBoolean
Puts a boolean value into the map by key.- Parameters:
key- keyvalue- value
-
putByte
Puts a byte value into the map by key.- Parameters:
key- keyvalue- value
-
putShort
Puts a short value into the map by key.- Parameters:
key- keyvalue- value
-
putInt
Puts an integer value into the map by key.- Parameters:
key- keyvalue- value
-
putLong
Puts a long value into the map by key.- Parameters:
key- keyvalue- value
-
putFloat
Puts a float value into the map by key.- Parameters:
key- keyvalue- value
-
putDouble
Puts a double value into the map by key.- Parameters:
key- keyvalue- value
-
putString
Puts a string value into the map by key if the value is not null.- Parameters:
key- keyvalue- value
-
putEnum
Puts a string value into the map by key if the value is not null.- Parameters:
key- keyvalue- value
-
toString
Returns a string representation of the map. -
keySet
Returns the set of map keys. -
values
Returns a collection of map values. -
entrySet
Returns the map recordset. -
clear
public void clear()Clears the map. -
size
public int size()Returns the size of the map. -
isEmpty
public boolean isEmpty()Checks if the map is empty. -
containsKey
Checks if the map contains the key.- Specified by:
containsKeyin interfaceMap<String,Object> - Parameters:
key- key- Returns:
- true if the map contains the key, false otherwise
-
containsValue
Checks if the map contains a value.- Specified by:
containsValuein interfaceMap<String,Object> - Parameters:
value- value- Returns:
- true if the map contains a value, false otherwise
-
get
Returns the value by key. -
put
Puts a value into the map by key. -
putAll
Copies all records from another map to the current one. -
remove
Deletes a record by key. -
getOrDefault
Returns the value by key, or the default value if the key is not found.- Specified by:
getOrDefaultin interfaceMap<String,Object> - Parameters:
key- keydefaultValue- default value- Returns:
- the value by key or default value
-
forEach
Performs the given action for each entry in the map. -
replaceAll
Replaces all values in the map with the results of calling the given function.- Specified by:
replaceAllin interfaceMap<String,Object> - Parameters:
function- the function to apply to the values
-
putIfAbsent
Puts a value into the map by key if the key is missing.- Specified by:
putIfAbsentin interfaceMap<String,Object> - Parameters:
key- keyvalue- value- Returns:
- previous value by key or null if the key was not found
-
remove
Deletes a record by key and value. -
replace
Replaces a value by key -
replace
Replaces the value by key. -
computeIfAbsent
public Object computeIfAbsent(String key, @NotNull @NotNull Function<? super String, ?> mappingFunction) Calculates the value for the key if the key is missing, using the given function.- Specified by:
computeIfAbsentin interfaceMap<String,Object> - Parameters:
key- the keymappingFunction- the function to calculate the value- Returns:
- the calculated value, or null if the function returned null
-
computeIfPresent
public Object computeIfPresent(String key, @NotNull @NotNull BiFunction<? super String, ? super Object, ?> remappingFunction) Computes a new value for the key, if the key is present, using the given function.- Specified by:
computeIfPresentin interfaceMap<String,Object> - Parameters:
key- the keyremappingFunction- the function to compute the new value- Returns:
- the new value, or null if the function returned null
-
compute
public Object compute(String key, @NotNull @NotNull BiFunction<? super String, ? super @Nullable Object, ?> remappingFunction) Calculates a new value for the key using the given function. -
merge
public Object merge(String key, @NotNull @NotNull Object value, @NotNull @NotNull BiFunction<? super Object, ? super Object, ?> remappingFunction) Merge a value by key with a given value using a given function.
-