Interface ImmutableConfiguration
- All Known Subinterfaces:
Configuration
,FileBasedConfiguration
,HierarchicalConfiguration<T>
,ImmutableHierarchicalConfiguration
- All Known Implementing Classes:
AbstractConfiguration
,AbstractHierarchicalConfiguration
,AbstractYAMLBasedConfiguration
,AppletConfiguration
,BaseConfiguration
,BaseHierarchicalConfiguration
,CombinedConfiguration
,CompositeConfiguration
,DatabaseConfiguration
,DataConfiguration
,DynamicCombinedConfiguration
,EnvironmentConfiguration
,INIConfiguration
,JNDIConfiguration
,JSONConfiguration
,MapConfiguration
,PatternSubtreeConfigurationWrapper
,PropertiesConfiguration
,PropertyListConfiguration
,ServletConfiguration
,ServletContextConfiguration
,ServletFilterConfiguration
,ServletRequestConfiguration
,SubnodeConfiguration
,SubsetConfiguration
,SystemConfiguration
,XMLConfiguration
,XMLPropertiesConfiguration
,XMLPropertyListConfiguration
,YAMLConfiguration
The main interface for accessing configuration data in a read-only fashion.
The major part of the methods defined in this interface deals with accessing properties of various data types. There
is a generic getProperty()
method, which returns the value of the queried property in its raw data type.
Other getter methods try to convert this raw data type into a specific data type. If this fails, a
ConversionException
will be thrown.
For most of the property getter methods an overloaded version exists that allows to specify a default value, which
will be returned if the queried property cannot be found in the configuration. The behavior of the methods that do
not take a default value in case of a missing property is not defined by this interface and depends on a concrete
implementation. E.g. the AbstractConfiguration
class, which is the base class of most configuration
implementations provided by this package, per default returns null if a property is not found, but provides
the setThrowExceptionOnMissing()
method, with which
it can be configured to throw a NoSuchElementException
exception in that case. (Note that getter methods for
primitive types in AbstractConfiguration
always throw an exception for missing properties because there is no
way of overloading the return value.)
- Since:
- 2.0
-
Method Summary
Modifier and TypeMethodDescriptionboolean
containsKey
(String key) Checks if the configuration contains the specified key.default boolean
containsValue
(Object value) Tests whether this configuration contains one or more matches to this value.<T> T
Gets an object of the specified type associated with the given configuration key.<T> T
Gets an object of the specified type associated with the given configuration key using a default value.Gets an array of typed objects associated with the given configuration key.Deprecated.getBigDecimal
(String key) Gets aBigDecimal
associated with the given configuration key.getBigDecimal
(String key, BigDecimal defaultValue) Gets aBigDecimal
associated with the given configuration key.getBigInteger
(String key) Gets aBigInteger
associated with the given configuration key.getBigInteger
(String key, BigInteger defaultValue) Gets aBigInteger
associated with the given configuration key.boolean
getBoolean
(String key) Gets a boolean associated with the given configuration key.boolean
getBoolean
(String key, boolean defaultValue) Gets a boolean associated with the given configuration key.getBoolean
(String key, Boolean defaultValue) Gets aBoolean
associated with the given configuration key.byte
Gets a byte associated with the given configuration key.byte
Gets a byte associated with the given configuration key.Gets aByte
associated with the given configuration key.<T> Collection<T>
getCollection
(Class<T> cls, String key, Collection<T> target) Gets a collection of typed objects associated with the given configuration key.<T> Collection<T>
getCollection
(Class<T> cls, String key, Collection<T> target, Collection<T> defaultValue) Gets a collection of typed objects associated with the given configuration key using the values in the specified default collection if the key does not map to an existing object.double
Gets a double associated with the given configuration key.double
Gets a double associated with the given configuration key.Gets aDouble
associated with the given configuration key.default Duration
getDuration
(String key) Gets aDuration
associated with the given configuration key.default Duration
getDuration
(String key, Duration defaultValue) Gets aDuration
associated with the given configuration key.getEncodedString
(String key) Gets the value of a string property that is stored in encoded form in this configuration using a defaultConfigurationDecoder
.getEncodedString
(String key, ConfigurationDecoder decoder) Gets the value of a string property that is stored in encoded form in this configuration.default <T extends Enum<T>>
TGets an enum associated with the given configuration key.default <T extends Enum<T>>
TGets the enum associated with the given configuration key.float
Gets a float associated with the given configuration key.float
Gets a float associated with the given configuration key.Gets aFloat
associated with the given configuration key.int
Gets a int associated with the given configuration key.int
Gets a int associated with the given configuration key.getInteger
(String key, Integer defaultValue) Gets anInteger
associated with the given configuration key.getKeys()
Gets the list of the keys contained in the configuration.Gets the list of the keys contained in the configuration that match the specified prefix.Gets the list of the keys contained in the configuration that match the specified prefix.<T> List<T>
Gets a list of typed objects associated with the given configuration key returning a null if the key doesn't map to an existing object.<T> List<T>
Gets a list of typed objects associated with the given configuration key returning the specified default value if the key doesn't map to an existing object.Gets a List of the values associated with the given configuration key.Gets a List of strings associated with the given configuration key.long
Gets a long associated with the given configuration key.long
Gets a long associated with the given configuration key.Gets aLong
associated with the given configuration key.getProperties
(String key) Gets a list of properties associated with the given configuration key.getProperty
(String key) Gets a property from the configuration.short
Gets a short associated with the given configuration key.short
Gets a short associated with the given configuration key.Gets aShort
associated with the given configuration key.Gets a string associated with the given configuration key.Gets a string associated with the given configuration key.String[]
getStringArray
(String key) Gets an array of strings associated with the given configuration key.immutableSubset
(String prefix) Return a decorator immutable Configuration containing every key from the current Configuration that starts with the specified prefix.boolean
isEmpty()
Checks if the configuration is empty.int
size()
Returns the number of keys stored in this configuration.
-
Method Details
-
containsKey
Checks if the configuration contains the specified key.- Parameters:
key
- the key whose presence in this configuration is to be tested- Returns:
true
if the configuration contains a value for this key,false
otherwise
-
containsValue
Tests whether this configuration contains one or more matches to this value. This operation stops at first match but may be more expensive than thecontainsKey
method.- Parameters:
value
- value whose presence in this configuration is to be tested- Returns:
true
if this configuration maps one or more keys to the specified value, false otherwise.- Since:
- 2.11.0
-
get
Gets an object of the specified type associated with the given configuration key. If the key doesn't map to an existing object, the method returns null unlessAbstractConfiguration.isThrowExceptionOnMissing()
is set totrue
.- Type Parameters:
T
- the target type of the value- Parameters:
cls
- the target class of the valuekey
- the key of the value- Returns:
- the value of the requested type for the key
- Throws:
NoSuchElementException
- if the key doesn't map to an existing object andthrowExceptionOnMissing=true
ConversionException
- if the value is not compatible with the requested type- Since:
- 2.0
-
get
Gets an object of the specified type associated with the given configuration key using a default value. If the key doesn't map to an existing object, the default value is returned.- Type Parameters:
T
- the target type of the value- Parameters:
cls
- the target class of the valuekey
- the key of the valuedefaultValue
- the default value- Returns:
- the value of the requested type for the key
- Throws:
ConversionException
- if the value is not compatible with the requested type- Since:
- 2.0
-
getArray
Gets an array of typed objects associated with the given configuration key. If the key doesn't map to an existing object, an empty list is returned.- Parameters:
cls
- the type expected for the elements of the arraykey
- The configuration key.- Returns:
- The associated array if the key is found, and the value compatible with the type specified.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not compatible with a list of the specified class.- Since:
- 2.0
-
getArray
Deprecated.This method should not be used any more because its signature does not allow type-safe invocations; useget(Class, String, Object)
instead which offers the same functionality; for instance, to query for an array of ints useint[] result = config.get(int[].class, "myArrayKey", someDefault);
.Gets an array of typed objects associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
cls
- the type expected for the elements of the arraykey
- the configuration key.defaultValue
- the default value- Returns:
- The associated array if the key is found, and the value compatible with the type specified.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not compatible with an array of the specified class.IllegalArgumentException
- if the default value is not an array of the specified type- Since:
- 2.0
-
getBigDecimal
Gets aBigDecimal
associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated BigDecimal if key is found and has valid format
-
getBigDecimal
Gets aBigDecimal
associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated BigDecimal if key is found and has valid format, default value otherwise.
-
getBigInteger
Gets aBigInteger
associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated BigInteger if key is found and has valid format
-
getBigInteger
Gets aBigInteger
associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated BigInteger if key is found and has valid format, default value otherwise.
-
getBoolean
Gets a boolean associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated boolean.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Boolean.
-
getBoolean
Gets a boolean associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated boolean.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Boolean.
-
getBoolean
Gets aBoolean
associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated boolean if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Boolean.
-
getByte
Gets a byte associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated byte.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Byte.
-
getByte
Gets a byte associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated byte.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Byte.
-
getByte
Gets aByte
associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated byte if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Byte.
-
getCollection
Gets a collection of typed objects associated with the given configuration key. This method works likegetCollection(Class, String, Collection, Collection)
passing in null as default value.- Type Parameters:
T
- the element type of the result list- Parameters:
cls
- the element class of the result listkey
- the configuration keytarget
- the target collection (may be null)- Returns:
- the collection to which data was added
- Throws:
ConversionException
- if the conversion is not possible- Since:
- 2.0
-
getCollection
<T> Collection<T> getCollection(Class<T> cls, String key, Collection<T> target, Collection<T> defaultValue) Gets a collection of typed objects associated with the given configuration key using the values in the specified default collection if the key does not map to an existing object. This method is similar togetList()
, however, it allows specifying a target collection. Results are added to this collection. This is useful if the data retrieved should be added to a specific kind of collection, e.g. a set to remove duplicates. The return value is as follows:- If the key does not map to an existing object and the default value is null, the method returns null.
- If the target collection is not null and data has been added (either from the resolved property value or from the default collection), the target collection is returned.
- If the target collection is null and data has been added (either from the resolved property value or from the default collection), return value is the target collection created by this method.
- Type Parameters:
T
- the element type of the result list- Parameters:
cls
- the element class of the result listkey
- the configuration keytarget
- the target collection (may be null)defaultValue
- the default value (may be null)- Returns:
- the collection to which data was added
- Throws:
ConversionException
- if the conversion is not possible- Since:
- 2.0
-
getDouble
Gets a double associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated double.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Double.
-
getDouble
Gets a double associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated double.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Double.
-
getDouble
Gets aDouble
associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated double if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Double.
-
getDuration
Gets aDuration
associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated Duration if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Duration.- Since:
- 2.8.0
-
getDuration
Gets aDuration
associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated Duration if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Duration.- Since:
- 2.8.0
-
getEncodedString
Gets the value of a string property that is stored in encoded form in this configuration using a defaultConfigurationDecoder
. This method works like the method with the same name, but it uses a defaultConfigurationDecoder
associated with this configuration. It depends on a specific implementation how this default decoder is obtained.- Parameters:
key
- the configuration key- Returns:
- the plain string value of the specified encoded property
-
getEncodedString
Gets the value of a string property that is stored in encoded form in this configuration. This method obtains the value of the string property identified by the given key. This value is then passed to the providedConfigurationDecoder
. The value returned by theConfigurationDecoder
is passed to the caller. If the key is not associated with a value, the decoder is not invoked; depending on this configuration's settings either null is returned or an exception is thrown.- Parameters:
key
- the configuration keydecoder
- theConfigurationDecoder
(must not be null)- Returns:
- the plain string value of the specified encoded property
- Throws:
IllegalArgumentException
- if a null decoder is passed
-
getEnum
Gets an enum associated with the given configuration key.- Type Parameters:
T
- The enum type whose constant is to be returned.- Parameters:
enumType
- theClass
object of the enum type from which to return a constantkey
- The configuration key.- Returns:
- The associated enum.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a String.- Since:
- 2.8.0
-
getEnum
Gets the enum associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Type Parameters:
T
- The enum type whose constant is to be returned.- Parameters:
key
- The configuration key.enumType
- theClass
object of the enum type from which to return a constantdefaultValue
- The default value.- Returns:
- The associated enum if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Enum.- Since:
- 2.8.0
-
getFloat
Gets a float associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated float.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Float.
-
getFloat
Gets a float associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated float.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Float.
-
getFloat
Gets aFloat
associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated float if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Float.
-
getInt
Gets a int associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated int.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Integer.
-
getInt
Gets a int associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated int.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Integer.
-
getInteger
Gets anInteger
associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated int if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Integer.
-
getKeys
Gets the list of the keys contained in the configuration. The returned iterator can be used to obtain all defined keys. It does not allow removing elements from this configuration via itsremove()
method. Note that the keys of this configuration are returned in a form, so that they can be directly evaluated; escaping of special characters (if necessary) has already been performed.- Returns:
- An Iterator.
-
getKeys
Gets the list of the keys contained in the configuration that match the specified prefix. For instance, if the configuration contains the following keys:
db.user, db.pwd, db.url, window.xpos, window.ypos
,
an invocation ofgetKeys("db");
will return the keys below:
db.user, db.pwd, db.url
.
Note that the prefix itself is included in the result set if there is a matching key. The exact behavior - how the prefix is actually interpreted - depends on a concrete implementation.- Parameters:
prefix
- The prefix to test against.- Returns:
- An Iterator of keys that match the prefix.
- See Also:
-
getKeys
Gets the list of the keys contained in the configuration that match the specified prefix. For instance, if the configuration contains the following keys:
db@user, db@pwd, db@url, window.xpos, window.ypos
,
an invocation ofgetKeys("db","@");
will return the keys below:
db@user, db@pwd, db@url
.
Note that the prefix itself is included in the result set if there is a matching key. The exact behavior - how the prefix is actually interpreted - depends on a concrete implementation.- Parameters:
prefix
- The prefix to test against.delimiter
- The prefix delimiter.- Returns:
- An Iterator of keys that match the prefix.
- Since:
- 2.10.0
- See Also:
-
getList
Gets a list of typed objects associated with the given configuration key returning a null if the key doesn't map to an existing object.- Type Parameters:
T
- the type expected for the elements of the list- Parameters:
cls
- the class expected for the elements of the listkey
- The configuration key.- Returns:
- The associated list if the key is found.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not compatible with a list of the specified class.- Since:
- 2.0
-
getList
Gets a list of typed objects associated with the given configuration key returning the specified default value if the key doesn't map to an existing object. This method recursively retrieves all values stored for the passed in key, i.e. if one of these values is again a complex object like an array or a collection (which may be the case for some concrete subclasses), all values are extracted and added to the resulting list - performing a type conversion if necessary.- Type Parameters:
T
- the type expected for the elements of the list- Parameters:
cls
- the class expected for the elements of the listkey
- the configuration key.defaultValue
- the default value.- Returns:
- The associated List.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not compatible with a list of the specified class.- Since:
- 2.0
-
getList
Gets a List of the values associated with the given configuration key. This method is different from the genericgetList()
method in that it does not recursively obtain all values stored for the specified property key. Rather, only the first level of the hierarchy is processed. So the resulting list may contain complex objects like arrays or collections - depending on the storage structure used by a concrete subclass. If the key doesn't map to an existing object, an empty List is returned.- Parameters:
key
- The configuration key.- Returns:
- The associated List.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a List.
-
getList
Gets a List of strings associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated List of strings.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a List.- See Also:
-
getLong
Gets a long associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated long.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Long.
-
getLong
Gets a long associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated long.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Long.
-
getLong
Gets aLong
associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated long if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Long.
-
getProperties
Gets a list of properties associated with the given configuration key. This method expects the given key to have an arbitrary number of String values, each of which is of the formkey=value
. These strings are split at the equals sign, and the key parts will become keys of the returnedProperties
object, the value parts become values.- Parameters:
key
- The configuration key.- Returns:
- The associated properties if key is found.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a String/List.IllegalArgumentException
- if one of the tokens is malformed (does not contain an equals sign).
-
getProperty
Gets a property from the configuration. This is the most basic get method for retrieving values of properties. In a typical implementation of theConfiguration
interface the other get methods (that return specific data types) will internally make use of this method. On this level variable substitution is not yet performed. The returned object is an internal representation of the property value for the passed in key. It is owned by theConfiguration
object. So a caller should not modify this object. It cannot be guaranteed that this object will stay constant over time (i.e. further update operations on the configuration may change its internal state).- Parameters:
key
- property to retrieve- Returns:
- the value to which this configuration maps the specified key, or null if the configuration contains no mapping for this key.
-
getShort
Gets a short associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated short.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Short.
-
getShort
Gets a short associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated short.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Short.
-
getShort
Gets aShort
associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated short if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a Short.
-
getString
Gets a string associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated string.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a String.
-
getString
Gets a string associated with the given configuration key. If the key doesn't map to an existing object, the default value is returned.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated string if key is found and has valid format, default value otherwise.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a String.
-
getStringArray
Gets an array of strings associated with the given configuration key. If the key doesn't map to an existing object an empty array is returned- Parameters:
key
- The configuration key.- Returns:
- The associated string array if key is found.
- Throws:
ConversionException
- is thrown if the key maps to an object that is not a String/List of Strings.
-
immutableSubset
Return a decorator immutable Configuration containing every key from the current Configuration that starts with the specified prefix. The prefix is removed from the keys in the subset. For example, if the configuration contains the following properties:prefix.number = 1 prefix.string = Apache prefixed.foo = bar prefix = Jakarta
the immutable Configuration returned bysubset("prefix")
will contain the properties:number = 1 string = Apache = Jakarta
(The key for the value "Jakarta" is an empty string)- Parameters:
prefix
- The prefix used to select the properties.- Returns:
- a subset immutable configuration
-
isEmpty
boolean isEmpty()Checks if the configuration is empty.- Returns:
true
if the configuration contains no property,false
otherwise.
-
size
int size()Returns the number of keys stored in this configuration. Note that a concrete implementation is not guaranteed to be efficient; for some implementations it may be expensive to determine the size. Especially, if you just want to check whether a configuration is empty, it is preferable to use theisEmpty()
method.- Returns:
- the number of keys stored in this configuration
-
get(Class, String, Object)
instead which offers the same functionality; for instance, to query for an array of ints useint[] result = config.get(int[].class, "myArrayKey", someDefault);
.