Class ConfigObject

java.lang.Object
org.omegazero.common.config.ConfigObject
All Implemented Interfaces:
Serializable

public class ConfigObject extends Object implements Serializable
A read-only map of key-value pairs, where the value is any type of data identified by the key string, providing a layer of abstraction for different data representations. Created by a Configuration instance.
Since:
2.4
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Map<String,Object>
    The data.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty ConfigObject.
    Creates a ConfigObject with the given data.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a clone of this ConfigObject, which contains the same elements as this ConfigObject.
    boolean
    Returns whether the given key exists in this ConfigObject.
    boolean
    Returns whether the given value exists in this ConfigObject.
    Creates a new HashMap with all values of this ConfigObject.
    Returns an unmodifiable set of all entries in this ConfigObject.
    boolean
    Determines whether the given object is equal to this ConfigObject.
    get(String key)
    Returns a value associated with the given key in this ConfigObject, or null if the given key has no value set.
    Returns a ConfigArray associated with the given key in this ConfigObject.
    boolean
    Returns a boolean associated with the given key in this ConfigObject.
    double
    Returns a double associated with the given key in this ConfigObject.
    float
    Returns a float associated with the given key in this ConfigObject.
    int
    Returns an int associated with the given key in this ConfigObject.
    long
    Returns a long associated with the given key in this ConfigObject.
    Returns a ConfigObject associated with the given key in this ConfigObject.
    Returns a String associated with the given key in this ConfigObject.
    int
    Returns a hash code for this ConfigObject.
    boolean
    Returns true if this ConfigObject contains no key-value pairs.
    Returns an unmodifiable set of all keys of this ConfigObject.
    Creates a new ConfigObject that contains all properties of this and the given ConfigObject.
    Returns a ConfigArray associated with the given key in this ConfigObject, or null if no mapping the the given key exists or is not a ConfigArray.
    boolean
    optBoolean(String key, boolean def)
    Returns a boolean associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not a boolean.
    double
    optDouble(String key, double def)
    Returns a double associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not a double.
    float
    optFloat(String key, float def)
    Returns a float associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not a float.
    int
    optInt(String key, int def)
    Returns an int associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not an int.
    long
    optLong(String key, long def)
    Returns a long associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not a long.
    Returns a ConfigObject associated with the given key in this ConfigObject, or null if no mapping the the given key exists or is not a ConfigObject.
    optString(String key, String def)
    Returns a String associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not a String.
    void
    Populates all fields with the ConfigurationOption annotation of the given targetObject with values from this ConfigObject using reflection.
    int
    Returns the number of key-value pairs in this ConfigObject.
    Returns a string representation of this ConfigObject.
    Returns an unmodifiable collection of all values of this ConfigObject.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • ConfigObject

      public ConfigObject()
      Creates an empty ConfigObject.
    • ConfigObject

      public ConfigObject(Map<String,Object> data)
      Creates a ConfigObject with the given data.

      Changes on the given map have no effects on this ConfigObject.

      Parameters:
      data - The data
  • Method Details

    • size

      public int size()
      Returns the number of key-value pairs in this ConfigObject.
      Returns:
      The number of key-value pairs
      See Also:
    • isEmpty

      public boolean isEmpty()
      Returns true if this ConfigObject contains no key-value pairs.
      Returns:
      true if empty
      See Also:
    • containsKey

      public boolean containsKey(Object key)
      Returns whether the given key exists in this ConfigObject.
      Parameters:
      key - The key to search for
      Returns:
      true if the key exists
      See Also:
    • containsValue

      public boolean containsValue(Object value)
      Returns whether the given value exists in this ConfigObject.
      Parameters:
      value - The value to search for
      Returns:
      true if the value exists
      See Also:
    • keySet

      public Set<String> keySet()
      Returns an unmodifiable set of all keys of this ConfigObject.
      Returns:
      A set of all keys
    • values

      public Collection<Object> values()
      Returns an unmodifiable collection of all values of this ConfigObject.
      Returns:
      A collection of all values
    • entrySet

      public Set<Map.Entry<String,Object>> entrySet()
      Returns an unmodifiable set of all entries in this ConfigObject.
      Returns:
      A set of entries
    • copyData

      public Map<String,Object> copyData()
      Creates a new HashMap with all values of this ConfigObject. Changes to the returned map have no effect on this ConfigObject.
      Returns:
      A Map with all values of this ConfigObject
    • merge

      public ConfigObject merge(ConfigObject other)
      Creates a new ConfigObject that contains all properties of this and the given ConfigObject. If both have a property with the same key, the value of the property in the given ConfigObject is put into the new ConfigObject.
      Both provided objects stay unchanged.
      Parameters:
      other - The ConfigObject to merge this one with, possibly overriding values of this ConfigObject
      Returns:
      A new ConfigObject with values merged from this and other
    • get

      public Object get(String key)
      Returns a value associated with the given key in this ConfigObject, or null if the given key has no value set. Note that a return value of null may also indicate that the given key is explicitly mapped to null.

      This method can be used to get a value of any type. To eliminate the need for type checking in the caller code, any of the get* and opt* methods may be used instead. The name and return value of those methods indicates the type that is checked for. Corresponding methods in both groups are equivalent, except that the methods starting with get throw an IllegalArgumentException if the value does not exist or is not the expected type, while the methods starting with opt return null or the value passed to the def argument instead.

      Parameters:
      key - The key
      Returns:
      The value mapped to the given key or null if no such value exists
    • getObject

      public ConfigObject getObject(String key)
      Returns a ConfigObject associated with the given key in this ConfigObject.
      Parameters:
      key - The key
      Returns:
      The ConfigObject
      Throws:
      IllegalArgumentException - If no mapping with the given key exists or is not a ConfigObject
    • getArray

      public ConfigArray getArray(String key)
      Returns a ConfigArray associated with the given key in this ConfigObject.
      Parameters:
      key - The key
      Returns:
      The ConfigArray
      Throws:
      IllegalArgumentException - If no mapping with the given key exists or is not a ConfigArray
    • getString

      public String getString(String key)
      Returns a String associated with the given key in this ConfigObject.
      Parameters:
      key - The key
      Returns:
      The String
      Throws:
      IllegalArgumentException - If no mapping with the given key exists or is not a String
    • getInt

      public int getInt(String key)
      Returns an int associated with the given key in this ConfigObject.
      Parameters:
      key - The key
      Returns:
      The int
      Throws:
      IllegalArgumentException - If no mapping with the given key exists or is not an int
    • getLong

      public long getLong(String key)
      Returns a long associated with the given key in this ConfigObject.
      Parameters:
      key - The key
      Returns:
      The long
      Throws:
      IllegalArgumentException - If no mapping with the given key exists or is not a long
    • getFloat

      public float getFloat(String key)
      Returns a float associated with the given key in this ConfigObject.
      Parameters:
      key - The key
      Returns:
      The float
      Throws:
      IllegalArgumentException - If no mapping with the given key exists or is not a float
    • getDouble

      public double getDouble(String key)
      Returns a double associated with the given key in this ConfigObject.
      Parameters:
      key - The key
      Returns:
      The double
      Throws:
      IllegalArgumentException - If no mapping with the given key exists or is not a double
    • getBoolean

      public boolean getBoolean(String key)
      Returns a boolean associated with the given key in this ConfigObject.
      Parameters:
      key - The key
      Returns:
      The boolean
      Throws:
      IllegalArgumentException - If no mapping with the given key exists or is not a boolean
    • optObject

      public ConfigObject optObject(String key)
      Returns a ConfigObject associated with the given key in this ConfigObject, or null if no mapping the the given key exists or is not a ConfigObject.
      Parameters:
      key - The key
      Returns:
      The ConfigObject, or null if it does not exist
    • optArray

      public ConfigArray optArray(String key)
      Returns a ConfigArray associated with the given key in this ConfigObject, or null if no mapping the the given key exists or is not a ConfigArray.
      Parameters:
      key - The key
      Returns:
      The ConfigArray, or null if it does not exist
    • optString

      public String optString(String key, String def)
      Returns a String associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not a String.
      Parameters:
      key - The key
      def - The default value
      Returns:
      The String
    • optInt

      public int optInt(String key, int def)
      Returns an int associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not an int.
      Parameters:
      key - The key
      def - The default value
      Returns:
      The int
    • optLong

      public long optLong(String key, long def)
      Returns a long associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not a long.
      Parameters:
      key - The key
      def - The default value
      Returns:
      The long
    • optFloat

      public float optFloat(String key, float def)
      Returns a float associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not a float.
      Parameters:
      key - The key
      def - The default value
      Returns:
      The float
    • optDouble

      public double optDouble(String key, double def)
      Returns a double associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not a double.
      Parameters:
      key - The key
      def - The default value
      Returns:
      The double
    • optBoolean

      public boolean optBoolean(String key, boolean def)
      Returns a boolean associated with the given key in this ConfigObject, or def if no mapping the the given key exists or is not a boolean.
      Parameters:
      key - The key
      def - The default value
      Returns:
      The boolean
    • populateConfigurationOptions

      public void populateConfigurationOptions(Object targetObject)
      Populates all fields with the ConfigurationOption annotation of the given targetObject with values from this ConfigObject using reflection.

      The name of the field is the key used in this ConfigObject to get the value. If a value in this ConfigObject does not have the same (or similar) type of the corresponding field, a ConfigurationException is thrown. If no value for an option is present in this ConfigObject, a ConfigurationException is thrown for required options; field values of other options stay unchanged.

      This method supports the following field type: all primitive types and their boxed types, String, ConfigObject, ConfigArray, List.

      Parameters:
      targetObject - The target object
      Throws:
      ConfigurationException - If a value has the incorrect type, or the value of a required option is missing
      ConfigurationException - If a reflective operation fails
      UnsupportedOperationException - If a field type is unsupported
      Since:
      2.10
    • equals

      public boolean equals(Object o)
      Determines whether the given object is equal to this ConfigObject.

      Another object is equal if it is also a ConfigObject and contains the same mappings.

      Overrides:
      equals in class Object
      See Also:
    • hashCode

      public int hashCode()
      Returns a hash code for this ConfigObject.
      Overrides:
      hashCode in class Object
      See Also:
    • clone

      public ConfigObject clone()
      Returns a clone of this ConfigObject, which contains the same elements as this ConfigObject.
      Overrides:
      clone in class Object
    • toString

      public String toString()
      Returns a string representation of this ConfigObject.
      Overrides:
      toString in class Object