Class StatsSet

java.lang.Object
host.anzo.commons.collection.StatsSet
All Implemented Interfaces:
Map<String,Object>

public class StatsSet extends Object implements Map<String,Object>
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.
  • Constructor Details

    • StatsSet

      public StatsSet(@NotNull @NotNull Supplier<Map<String,Object>> mapFactory)
      Creates a new StatsSet instance using the factory to create the internal map.
      Parameters:
      mapFactory - factory to create the internal map
  • Method Details

    • wrap

      public static StatsSet wrap(@NotNull @NotNull Map<String,Object> map)
      Creates a new StatsSet instance and wraps the given map.
      Parameters:
      map - the map to wrap
      Returns:
      a new StatsSet instance
    • 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 a StatsSet. For example: (key1)(spliter2)(value1)(spliter1)(key2)(spliter2)(value2)(spliter1)(keyN)(spliter2)(valueN)
      Parameters:
      params - parameter string
      splitter1 - separator between key-value pairs
      splitter2 - separator between key and value
      mapFactory - factory for creating the internal map
      Returns:
      a new StatsSet instance with the data from the parameter string
    • getBooleanOrThrow

      public boolean getBooleanOrThrow(@NotNull @NotNull String key) throws IllegalArgumentException
      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

      public boolean getBooleanOrDefault(@NotNull @NotNull String key, boolean defaultValue)
      Returns a boolean value for the key, or a default value if the key is not found.
      Parameters:
      key - key
      defaultValue - default value
      Returns:
      boolean or default value
    • getByteOrThrow

      public byte getByteOrThrow(@NotNull @NotNull String key) throws IllegalArgumentException
      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

      public byte getByteOrDefault(@NotNull @NotNull String key, byte defaultValue)
      Returns the byte value for the key, or the default value if the key is not found.
      Parameters:
      key - the key
      defaultValue - the default value
      Returns:
      the byte value or default value
    • getShortOrThrow

      public short getShortOrThrow(@NotNull @NotNull String key) throws IllegalArgumentException
      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

      public short getShortOrDefault(@NotNull @NotNull String key, short defaultValue)
      Returns a short value by key, or a default value if the key is not found.
      Parameters:
      key - key
      defaultValue - default value
      Returns:
      short value or default value
    • getIntOrThrow

      public int getIntOrThrow(@NotNull @NotNull String key) throws IllegalArgumentException
      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

      public int getIntOrDefault(@NotNull @NotNull String key, int defaultValue)
      Returns an integer value for the key, or a default value if the key is not found.
      Parameters:
      key - key
      defaultValue - default value
      Returns:
      integer value or default value
    • getLongOrThrow

      public long getLongOrThrow(@NotNull @NotNull String key) throws IllegalArgumentException
      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

      public long getLongOrDefault(@NotNull @NotNull String key, long defaultValue)
      Returns a long value by key, or a default value if the key is not found.
      Parameters:
      key - key
      defaultValue - default value
      Returns:
      long value or default value
    • getFloatOrThrow

      public float getFloatOrThrow(@NotNull @NotNull String key) throws IllegalArgumentException
      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

      public float getFloatOrDefault(@NotNull @NotNull String key, float defaultValue)
      Returns a float value by key, or a default value if the key is not found.
      Parameters:
      key - key
      defaultValue - default value
      Returns:
      float value or default value
    • getDoubleOrThrow

      public double getDoubleOrThrow(@NotNull @NotNull String key) throws IllegalArgumentException
      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

      public double getDoubleOrDefault(@NotNull @NotNull String key, double defaultValue)
      Returns a double value by key, or a default value if the key is not found.
      Parameters:
      key - key
      defaultValue - 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 - key
      defaultValue - 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 key
      enumClass - 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 key
      enumClass - the enumeration class
      defaultValue - the default value
      Returns:
      the enumeration value or default value
      Throws:
      IllegalArgumentException - if the class is not an enum
    • getRawObject

      @Nullable public @Nullable Object getRawObject(@NotNull @NotNull String key)
      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 - key
      type - 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 key
      type - the class to cast the object to
      defaultValue - 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 - key
      splitOn - delimiter for parsing string into array
      start - start character to clear string
      end - 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 - key
      start - start character to clear the string
      end - 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 key
      splitOn - the separator for parsing the string into an array
      start - the starting character to clear the string
      end - 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 - key
      splitOn - delimiter for parsing string into array
      start - start character to clear string
      end - 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 - key
      splitOn - separator for parsing string to array
      start - start character to clear string
      end - 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 - key
      splitOn - separator for parsing string into array
      start - start character to clear string
      end - 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 - key
      splitOn - separator for parsing string to array
      start - start character to clear string
      end - 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 - key
      splitOn - separator for parsing string into array
      start - start character to clear string
      end - 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

      public void putObject(@NotNull @NotNull String key, @Nullable @Nullable Object value)
      Puts an object into the map by key if the value is not null.
      Parameters:
      key - key
      value - value
    • putBoolean

      public void putBoolean(@NotNull @NotNull String key, boolean value)
      Puts a boolean value into the map by key.
      Parameters:
      key - key
      value - value
    • putByte

      public void putByte(@NotNull @NotNull String key, byte value)
      Puts a byte value into the map by key.
      Parameters:
      key - key
      value - value
    • putShort

      public void putShort(@NotNull @NotNull String key, short value)
      Puts a short value into the map by key.
      Parameters:
      key - key
      value - value
    • putInt

      public void putInt(@NotNull @NotNull String key, int value)
      Puts an integer value into the map by key.
      Parameters:
      key - key
      value - value
    • putLong

      public void putLong(@NotNull @NotNull String key, long value)
      Puts a long value into the map by key.
      Parameters:
      key - key
      value - value
    • putFloat

      public void putFloat(@NotNull @NotNull String key, float value)
      Puts a float value into the map by key.
      Parameters:
      key - key
      value - value
    • putDouble

      public void putDouble(@NotNull @NotNull String key, double value)
      Puts a double value into the map by key.
      Parameters:
      key - key
      value - value
    • putString

      public void putString(@NotNull @NotNull String key, @Nullable @Nullable String value)
      Puts a string value into the map by key if the value is not null.
      Parameters:
      key - key
      value - value
    • putEnum

      public void putEnum(@NotNull @NotNull String key, @Nullable @Nullable Enum<?> value)
      Puts a string value into the map by key if the value is not null.
      Parameters:
      key - key
      value - value
    • toString

      public String toString()
      Returns a string representation of the map.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the map
    • keySet

      @NotNull public @NotNull Set<String> keySet()
      Returns the set of map keys.
      Specified by:
      keySet in interface Map<String,Object>
      Returns:
      the set of map keys
    • values

      @NotNull public @NotNull Collection<Object> values()
      Returns a collection of map values.
      Specified by:
      values in interface Map<String,Object>
      Returns:
      a collection of map values
    • entrySet

      @NotNull public @NotNull Set<Map.Entry<String,Object>> entrySet()
      Returns the map recordset.
      Specified by:
      entrySet in interface Map<String,Object>
      Returns:
      the map recordset
    • clear

      public void clear()
      Clears the map.
      Specified by:
      clear in interface Map<String,Object>
    • size

      public int size()
      Returns the size of the map.
      Specified by:
      size in interface Map<String,Object>
      Returns:
      the size of the map
    • isEmpty

      public boolean isEmpty()
      Checks if the map is empty.
      Specified by:
      isEmpty in interface Map<String,Object>
      Returns:
      true if the map is empty, false otherwise
    • containsKey

      public boolean containsKey(Object key)
      Checks if the map contains the key.
      Specified by:
      containsKey in interface Map<String,Object>
      Parameters:
      key - key
      Returns:
      true if the map contains the key, false otherwise
    • containsValue

      public boolean containsValue(Object value)
      Checks if the map contains a value.
      Specified by:
      containsValue in interface Map<String,Object>
      Parameters:
      value - value
      Returns:
      true if the map contains a value, false otherwise
    • get

      @Nullable public @Nullable Object get(Object key)
      Returns the value by key.
      Specified by:
      get in interface Map<String,Object>
      Parameters:
      key - key
      Returns:
      the value by key or null if the key is not found
    • put

      @Nullable public @Nullable Object put(String key, Object value)
      Puts a value into the map by key.
      Specified by:
      put in interface Map<String,Object>
      Parameters:
      key - key
      value - value
      Returns:
      previous value by key or null if key was not found
    • putAll

      public void putAll(@NotNull @NotNull Map<? extends String,?> map)
      Copies all records from another map to the current one.
      Specified by:
      putAll in interface Map<String,Object>
      Parameters:
      map - the map from which to copy records
    • remove

      public Object remove(Object key)
      Deletes a record by key.
      Specified by:
      remove in interface Map<String,Object>
      Parameters:
      key - key
      Returns:
      the deleted value or null if the key was not found
    • getOrDefault

      public Object getOrDefault(Object key, Object defaultValue)
      Returns the value by key, or the default value if the key is not found.
      Specified by:
      getOrDefault in interface Map<String,Object>
      Parameters:
      key - key
      defaultValue - default value
      Returns:
      the value by key or default value
    • forEach

      public void forEach(BiConsumer<? super String,? super Object> action)
      Performs the given action for each entry in the map.
      Specified by:
      forEach in interface Map<String,Object>
      Parameters:
      action - the action to perform
    • replaceAll

      public void replaceAll(BiFunction<? super String,? super Object,?> function)
      Replaces all values ​​in the map with the results of calling the given function.
      Specified by:
      replaceAll in interface Map<String,Object>
      Parameters:
      function - the function to apply to the values
    • putIfAbsent

      @Nullable public @Nullable Object putIfAbsent(String key, Object value)
      Puts a value into the map by key if the key is missing.
      Specified by:
      putIfAbsent in interface Map<String,Object>
      Parameters:
      key - key
      value - value
      Returns:
      previous value by key or null if the key was not found
    • remove

      public boolean remove(Object key, Object value)
      Deletes a record by key and value.
      Specified by:
      remove in interface Map<String,Object>
      Parameters:
      key - key
      value - value
      Returns:
      true if the record was deleted, otherwise false
    • replace

      public boolean replace(String key, Object oldValue, Object newValue)
      Replaces a value by key
      Specified by:
      replace in interface Map<String,Object>
      Parameters:
      key - key
      oldValue - old value
      newValue - new value
      Returns:
      true if the value was replaced, otherwise false
    • replace

      @Nullable public @Nullable Object replace(String key, Object value)
      Replaces the value by key.
      Specified by:
      replace in interface Map<String,Object>
      Parameters:
      key - key
      value - value
      Returns:
      previous value by key or null if key was not found
    • 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:
      computeIfAbsent in interface Map<String,Object>
      Parameters:
      key - the key
      mappingFunction - 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:
      computeIfPresent in interface Map<String,Object>
      Parameters:
      key - the key
      remappingFunction - 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.
      Specified by:
      compute in interface Map<String,Object>
      Parameters:
      key - the key
      remappingFunction - the function to calculate the new value
      Returns:
      the new value or null if the function returned null
    • 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.
      Specified by:
      merge in interface Map<String,Object>
      Parameters:
      key - the key
      value - the value
      remappingFunction - the function to merge the values
      Returns:
      the new value or null if the function returned null