public final class Configuration
extends java.lang.Object
To set or unset a configuration value, you call the setObject(String, Object)
method. If the second argument is null
, unsetting is performed, whereas any other value sets the configuration value.
Getting a configuration value, whether previously set or not, can be performed by calling getBoolean(String)
, getObject(String, Class, Object)
, getInt(String, int)
or a wide range of other
methods. What they all have in common is that they all return an instance of an OptionalBoolean
, an Optional
, an OptionalInt
or some other optional wrapper class.
If a configuration value is set using one type, it is possible that getting it using another type may work. So for instance, lets say you call setObject("A", Byte.valueOf((byte)(100)))
followed by the call
getInt("A")
, then you will get an OptionalInt
with the value 100
set. This is possible because some decoding is performed behind the scenes. Decoding isn't only performed on integer types.
The following list demonstrates the decoding that is possible so far given the String
representation of an Object
(using its toString()
method):
Boolean
is decoded after the following check Objects2.equalsIgnoreCase(toString, "0", "1", "false", "true")
.Byte
is decoded as Byte.decode(toString)
.Character
is decoded as Character.valueOf(toString.charAt(0))
.Double
is decoded as Double.valueOf(toString.replace(',', '.'))
.Float
is decoded as Float.valueOf(toString.replace(',', '.'))
.Integer
is decoded as Integer.decode(toString)
.Long
is decoded as Long.decode(toString)
.Short
is decoded as Short.decode(toString)
.The following code snippet demonstrates how you can use this API:
Configuration configuration = new Configuration();
configuration.addConfigurationObserver((configuration0, id, oldObject, newObject) -> System.out.println(newObject));
configuration.setObject("A", true);
OptionalBoolean a = configuration.getBoolean("A");
OptionalBoolean b = configuration.getBoolean("B", false);
This class is thread-safe and therefore also suitable for concurrent use without external synchronization. Although, this may not be true for all Object
s stored as configuration values.
Constructor and Description |
---|
Configuration()
Constructs a new empty
Configuration instance. |
Modifier and Type | Method and Description |
---|---|
boolean |
addConfigurationObserver(ConfigurationObserver configurationObserver)
Adds
configurationObserver , if absent. |
Configuration |
clear()
Clears all configuration values currently held by this
Configuration instance. |
org.macroing.cit.java.util.OptionalBoolean |
getBoolean(java.lang.String id)
Returns an
OptionalBoolean that may or may not contain a configuration value. |
org.macroing.cit.java.util.OptionalBoolean |
getBoolean(java.lang.String id,
boolean defaultValue)
Returns an
OptionalBoolean that may or may not contain a configuration value. |
org.macroing.cit.java.util.OptionalByte |
getByte(java.lang.String id)
Returns an
OptionalByte that may or may not contain a configuration value. |
org.macroing.cit.java.util.OptionalByte |
getByte(java.lang.String id,
byte defaultValue)
Returns an
OptionalByte that may or may not contain a configuration value. |
org.macroing.cit.java.util.OptionalChar |
getChar(java.lang.String id)
Returns an
OptionalChar that may or may not contain a configuration value. |
org.macroing.cit.java.util.OptionalChar |
getChar(java.lang.String id,
char defaultValue)
Returns an
OptionalChar that may or may not contain a configuration value. |
java.util.OptionalDouble |
getDouble(java.lang.String id)
Returns an
OptionalDouble that may or may not contain a configuration value. |
java.util.OptionalDouble |
getDouble(java.lang.String id,
double defaultValue)
Returns an
OptionalDouble that may or may not contain a configuration value. |
org.macroing.cit.java.util.OptionalFloat |
getFloat(java.lang.String id)
Returns an
OptionalFloat that may or may not contain a configuration value. |
org.macroing.cit.java.util.OptionalFloat |
getFloat(java.lang.String id,
float defaultValue)
Returns an
OptionalFloat that may or may not contain a configuration value. |
java.util.OptionalInt |
getInt(java.lang.String id)
Returns an
OptionalInt that may or may not contain a configuration value. |
java.util.OptionalInt |
getInt(java.lang.String id,
int defaultValue)
Returns an
OptionalInt that may or may not contain a configuration value. |
java.util.OptionalLong |
getLong(java.lang.String id)
Returns an
OptionalLong that may or may not contain a configuration value. |
java.util.OptionalLong |
getLong(java.lang.String id,
long defaultValue)
Returns an
OptionalLong that may or may not contain a configuration value. |
<T> java.util.Optional<T> |
getObject(java.lang.String id,
java.lang.Class<T> clazz)
Returns an
Optional that may or may not contain a configuration value. |
<T> java.util.Optional<T> |
getObject(java.lang.String id,
java.lang.Class<T> clazz,
T defaultValue)
Returns an
Optional that may or may not contain a configuration value. |
org.macroing.cit.java.util.OptionalShort |
getShort(java.lang.String id)
Returns an
OptionalShort that may or may not contain a configuration value. |
org.macroing.cit.java.util.OptionalShort |
getShort(java.lang.String id,
short defaultValue)
Returns an
OptionalShort that may or may not contain a configuration value. |
boolean |
removeConfigurationObserver(ConfigurationObserver configurationObserver)
Removes
configurationObserver , if present. |
Configuration |
setObject(java.lang.String id,
java.lang.Object object)
Sets or unsets a configuration value in this
Configuration instance. |
int |
size()
Returns the size of this
Configuration instance. |
public Configuration()
Configuration
instance.public boolean addConfigurationObserver(ConfigurationObserver configurationObserver)
configurationObserver
, if absent.
Returns true
if, and only if, configurationObserver
was added, false
otherwise.
If configurationObserver
is null
, a NullPointerException
will be thrown.
configurationObserver
- the ConfigurationObserver
to addtrue
if, and only if, configurationObserver
was added, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, configurationObserver
is null
public boolean removeConfigurationObserver(ConfigurationObserver configurationObserver)
configurationObserver
, if present.
Returns true
if, and only if, configurationObserver
was removed, false
otherwise.
If configurationObserver
is null
, a NullPointerException
will be thrown.
configurationObserver
- the ConfigurationObserver
to removetrue
if, and only if, configurationObserver
was removed, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, configurationObserver
is null
public Configuration clear()
Configuration
instance.
Returns itself, such that method chaining is possible.
When a configuration value has been cleared (or unset), all currently added ConfigurationObserver
s will be notified.
public Configuration setObject(java.lang.String id, java.lang.Object object)
Configuration
instance.
Returns itself, such that method chaining is possible.
If id
is null
, a NullPointerException
will be thrown.
To unset a configuration value, simply provide null
as the second argument (in place of object
).
When a configuration value has been set or unset, all currently added ConfigurationObserver
s will be notified. This is performed even if no noticeable change is made.
id
- the ID of the configuration valueobject
- the configuration value itself, or null
to unset itpublic int size()
Configuration
instance.Configuration
instancepublic <T> java.util.Optional<T> getObject(java.lang.String id, java.lang.Class<T> clazz)
Optional
that may or may not contain a configuration value.
If either id
or clazz
are null
, a NullPointerException
will be thrown.
T
- the type of the configuration valueid
- the ID of the configuration valueclazz
- the Class
corresponding to the type of the configuration valueOptional
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, either id
or clazz
are null
public <T> java.util.Optional<T> getObject(java.lang.String id, java.lang.Class<T> clazz, T defaultValue)
Optional
that may or may not contain a configuration value.
If either id
or clazz
are null
, a NullPointerException
will be thrown.
T
- the type of the configuration valueid
- the ID of the configuration valueclazz
- the Class
corresponding to the type of the configuration valuedefaultValue
- the default configuration value, which may be null
, and will be used if no such configuration value can be foundOptional
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, either id
or clazz
are null
public org.macroing.cit.java.util.OptionalBoolean getBoolean(java.lang.String id)
OptionalBoolean
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valueOptionalBoolean
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public org.macroing.cit.java.util.OptionalBoolean getBoolean(java.lang.String id, boolean defaultValue)
OptionalBoolean
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valuedefaultValue
- the default configuration value, that will be used if no such configuration value can be foundOptionalBoolean
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public org.macroing.cit.java.util.OptionalByte getByte(java.lang.String id)
OptionalByte
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valueOptionalByte
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public org.macroing.cit.java.util.OptionalByte getByte(java.lang.String id, byte defaultValue)
OptionalByte
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valuedefaultValue
- the default configuration value, that will be used if no such configuration value can be foundOptionalByte
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public org.macroing.cit.java.util.OptionalChar getChar(java.lang.String id)
OptionalChar
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valueOptionalChar
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public org.macroing.cit.java.util.OptionalChar getChar(java.lang.String id, char defaultValue)
OptionalChar
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valuedefaultValue
- the default configuration value, that will be used if no such configuration value can be foundOptionalChar
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public java.util.OptionalDouble getDouble(java.lang.String id)
OptionalDouble
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valueOptionalDouble
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public java.util.OptionalDouble getDouble(java.lang.String id, double defaultValue)
OptionalDouble
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valuedefaultValue
- the default configuration value, that will be used if no such configuration value can be foundOptionalDouble
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public org.macroing.cit.java.util.OptionalFloat getFloat(java.lang.String id)
OptionalFloat
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valueOptionalFloat
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public org.macroing.cit.java.util.OptionalFloat getFloat(java.lang.String id, float defaultValue)
OptionalFloat
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valuedefaultValue
- the default configuration value, that will be used if no such configuration value can be foundOptionalFloat
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public java.util.OptionalInt getInt(java.lang.String id)
OptionalInt
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valueOptionalInt
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public java.util.OptionalInt getInt(java.lang.String id, int defaultValue)
OptionalInt
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valuedefaultValue
- the default configuration value, that will be used if no such configuration value can be foundOptionalInt
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public java.util.OptionalLong getLong(java.lang.String id)
OptionalLong
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valueOptionalLong
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public java.util.OptionalLong getLong(java.lang.String id, long defaultValue)
OptionalLong
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valuedefaultValue
- the default configuration value, that will be used if no such configuration value can be foundOptionalLong
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public org.macroing.cit.java.util.OptionalShort getShort(java.lang.String id)
OptionalShort
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valueOptionalShort
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null
public org.macroing.cit.java.util.OptionalShort getShort(java.lang.String id, short defaultValue)
OptionalShort
that may or may not contain a configuration value.
If id
is null
, a NullPointerException
will be thrown.
id
- the ID of the configuration valuedefaultValue
- the default configuration value, that will be used if no such configuration value can be foundOptionalShort
that may or may not contain a configuration valuejava.lang.NullPointerException
- thrown if, and only if, id
is null