Interface Configuration
-
- All Known Subinterfaces:
FileConfiguration
- All Known Implementing Classes:
AbstractConfiguration
,AbstractFileConfiguration
,AbstractHierarchicalFileConfiguration
,AbstractHierarchicalFileConfiguration.FileConfigurationDelegate
,AppletConfiguration
,BaseConfiguration
,CombinedConfiguration
,CompositeConfiguration
,DatabaseConfiguration
,DataConfiguration
,DefaultConfigurationBuilder
,DynamicCombinedConfiguration
,EnvironmentConfiguration
,HierarchicalConfiguration
,HierarchicalINIConfiguration
,HierarchicalReloadableConfiguration
,HierarchicalXMLConfiguration
,INIConfiguration
,JNDIConfiguration
,MapConfiguration
,MultiFileHierarchicalConfiguration
,PatternSubtreeConfigurationWrapper
,PropertiesConfiguration
,PropertyListConfiguration
,ServletConfiguration
,ServletContextConfiguration
,ServletFilterConfiguration
,ServletRequestConfiguration
,SubnodeConfiguration
,SubsetConfiguration
,SystemConfiguration
,XMLConfiguration
,XMLPropertiesConfiguration
,XMLPropertyListConfiguration
public interface Configuration
The main Configuration interface.
This interface allows accessing and manipulating a configuration object. 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, aConversionException
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 thesetThrowExceptionOnMissing()
method, with which it can be configured to throw aNoSuchElementException
exception in that case. (Note that getter methods for primitive types inAbstractConfiguration
always throw an exception for missing properties because there is no way of overloading the return value.)With the
addProperty()
andsetProperty()
methods new properties can be added to a configuration or the values of properties can be changed. WithclearProperty()
a property can be removed. Other methods allow to iterate over the contained properties or to create a subset configuration.- Version:
- $Id: Configuration.java 1534064 2013-10-21 08:44:33Z henning $
- Author:
- Commons Configuration team
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
addProperty(java.lang.String key, java.lang.Object value)
Add a property to the configuration.void
clear()
Remove all properties from the configuration.void
clearProperty(java.lang.String key)
Remove a property from the configuration.boolean
containsKey(java.lang.String key)
Check if the configuration contains the specified key.java.math.BigDecimal
getBigDecimal(java.lang.String key)
Get aBigDecimal
associated with the given configuration key.java.math.BigDecimal
getBigDecimal(java.lang.String key, java.math.BigDecimal defaultValue)
Get aBigDecimal
associated with the given configuration key.java.math.BigInteger
getBigInteger(java.lang.String key)
Get aBigInteger
associated with the given configuration key.java.math.BigInteger
getBigInteger(java.lang.String key, java.math.BigInteger defaultValue)
Get aBigInteger
associated with the given configuration key.boolean
getBoolean(java.lang.String key)
Get a boolean associated with the given configuration key.boolean
getBoolean(java.lang.String key, boolean defaultValue)
Get a boolean associated with the given configuration key.java.lang.Boolean
getBoolean(java.lang.String key, java.lang.Boolean defaultValue)
Get aBoolean
associated with the given configuration key.byte
getByte(java.lang.String key)
Get a byte associated with the given configuration key.byte
getByte(java.lang.String key, byte defaultValue)
Get a byte associated with the given configuration key.java.lang.Byte
getByte(java.lang.String key, java.lang.Byte defaultValue)
Get aByte
associated with the given configuration key.double
getDouble(java.lang.String key)
Get a double associated with the given configuration key.double
getDouble(java.lang.String key, double defaultValue)
Get a double associated with the given configuration key.java.lang.Double
getDouble(java.lang.String key, java.lang.Double defaultValue)
Get aDouble
associated with the given configuration key.float
getFloat(java.lang.String key)
Get a float associated with the given configuration key.float
getFloat(java.lang.String key, float defaultValue)
Get a float associated with the given configuration key.java.lang.Float
getFloat(java.lang.String key, java.lang.Float defaultValue)
Get aFloat
associated with the given configuration key.int
getInt(java.lang.String key)
Get a int associated with the given configuration key.int
getInt(java.lang.String key, int defaultValue)
Get a int associated with the given configuration key.java.lang.Integer
getInteger(java.lang.String key, java.lang.Integer defaultValue)
Get anInteger
associated with the given configuration key.java.util.Iterator<java.lang.String>
getKeys()
Get the list of the keys contained in the configuration.java.util.Iterator<java.lang.String>
getKeys(java.lang.String prefix)
Get the list of the keys contained in the configuration that match the specified prefix.java.util.List<java.lang.Object>
getList(java.lang.String key)
Get a List of strings associated with the given configuration key.java.util.List<java.lang.Object>
getList(java.lang.String key, java.util.List<?> defaultValue)
Get a List of strings associated with the given configuration key.long
getLong(java.lang.String key)
Get a long associated with the given configuration key.long
getLong(java.lang.String key, long defaultValue)
Get a long associated with the given configuration key.java.lang.Long
getLong(java.lang.String key, java.lang.Long defaultValue)
Get aLong
associated with the given configuration key.java.util.Properties
getProperties(java.lang.String key)
Get a list of properties associated with the given configuration key.java.lang.Object
getProperty(java.lang.String key)
Gets a property from the configuration.short
getShort(java.lang.String key)
Get a short associated with the given configuration key.short
getShort(java.lang.String key, short defaultValue)
Get a short associated with the given configuration key.java.lang.Short
getShort(java.lang.String key, java.lang.Short defaultValue)
Get aShort
associated with the given configuration key.java.lang.String
getString(java.lang.String key)
Get a string associated with the given configuration key.java.lang.String
getString(java.lang.String key, java.lang.String defaultValue)
Get a string associated with the given configuration key.java.lang.String[]
getStringArray(java.lang.String key)
Get an array of strings associated with the given configuration key.boolean
isEmpty()
Check if the configuration is empty.void
setProperty(java.lang.String key, java.lang.Object value)
Set a property, this will replace any previously set values.Configuration
subset(java.lang.String prefix)
Return a decorator Configuration containing every key from the current Configuration that starts with the specified prefix.
-
-
-
Method Detail
-
subset
Configuration subset(java.lang.String prefix)
Return a decorator 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 Configuration returned bysubset("prefix")
will contain the properties:number = 1 string = Apache = Jakarta
(The key for the value "Jakarta" is an empty string)Since the subset is a decorator and not a modified copy of the initial Configuration, any change made to the subset is available to the Configuration, and reciprocally.
- Parameters:
prefix
- The prefix used to select the properties.- Returns:
- a subset configuration
- See Also:
SubsetConfiguration
-
isEmpty
boolean isEmpty()
Check if the configuration is empty.- Returns:
true
if the configuration contains no property,false
otherwise.
-
containsKey
boolean containsKey(java.lang.String key)
Check 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
-
addProperty
void addProperty(java.lang.String key, java.lang.Object value)
Add a property to the configuration. If it already exists then the value stated here will be added to the configuration entry. For example, if the property:resource.loader = file
is already present in the configuration and you calladdProperty("resource.loader", "classpath")
Then you will end up with a List like the following:["file", "classpath"]
- Parameters:
key
- The key to add the property to.value
- The value to add.
-
setProperty
void setProperty(java.lang.String key, java.lang.Object value)
Set a property, this will replace any previously set values. Set values is implicitly a call to clearProperty(key), addProperty(key, value).- Parameters:
key
- The key of the property to changevalue
- The new value
-
clearProperty
void clearProperty(java.lang.String key)
Remove a property from the configuration.- Parameters:
key
- the key to remove along with corresponding value.
-
clear
void clear()
Remove all properties from the configuration.
-
getProperty
java.lang.Object getProperty(java.lang.String key)
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.
-
getKeys
java.util.Iterator<java.lang.String> getKeys(java.lang.String prefix)
Get 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()
-
getKeys
java.util.Iterator<java.lang.String> getKeys()
Get the list of the keys contained in the configuration. The returned iterator can be used to obtain all defined keys. Note that the exact behavior of the iterator'sremove()
method is specific to a concrete implementation. It may remove the corresponding property from the configuration, but this is not guaranteed. In any case it is no replacement for callingclearProperty(String)
for this property. So it is highly recommended to avoid using the iterator'sremove()
method.- Returns:
- An Iterator.
-
getProperties
java.util.Properties getProperties(java.lang.String key)
Get 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 form {code key=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.java.lang.IllegalArgumentException
- if one of the tokens is malformed (does not contain an equals sign).
-
getBoolean
boolean getBoolean(java.lang.String key)
Get 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
boolean getBoolean(java.lang.String key, boolean defaultValue)
Get 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
java.lang.Boolean getBoolean(java.lang.String key, java.lang.Boolean defaultValue)
Get 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
byte getByte(java.lang.String key)
Get 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
byte getByte(java.lang.String key, byte defaultValue)
Get 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
java.lang.Byte getByte(java.lang.String key, java.lang.Byte defaultValue)
Get 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.
-
getDouble
double getDouble(java.lang.String key)
Get 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
double getDouble(java.lang.String key, double defaultValue)
Get 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
java.lang.Double getDouble(java.lang.String key, java.lang.Double defaultValue)
Get 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.
-
getFloat
float getFloat(java.lang.String key)
Get 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
float getFloat(java.lang.String key, float defaultValue)
Get 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
java.lang.Float getFloat(java.lang.String key, java.lang.Float defaultValue)
Get 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
int getInt(java.lang.String key)
Get 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
int getInt(java.lang.String key, int defaultValue)
Get 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
java.lang.Integer getInteger(java.lang.String key, java.lang.Integer defaultValue)
Get 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.
-
getLong
long getLong(java.lang.String key)
Get 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
long getLong(java.lang.String key, long defaultValue)
Get 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
java.lang.Long getLong(java.lang.String key, java.lang.Long defaultValue)
Get 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.
-
getShort
short getShort(java.lang.String key)
Get 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
short getShort(java.lang.String key, short defaultValue)
Get 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
java.lang.Short getShort(java.lang.String key, java.lang.Short defaultValue)
Get 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.
-
getBigDecimal
java.math.BigDecimal getBigDecimal(java.lang.String key)
Get 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
java.math.BigDecimal getBigDecimal(java.lang.String key, java.math.BigDecimal defaultValue)
Get 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
java.math.BigInteger getBigInteger(java.lang.String key)
Get 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
java.math.BigInteger getBigInteger(java.lang.String key, java.math.BigInteger defaultValue)
Get 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.
-
getString
java.lang.String getString(java.lang.String key)
Get 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
java.lang.String getString(java.lang.String key, java.lang.String defaultValue)
Get 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
java.lang.String[] getStringArray(java.lang.String key)
Get 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.
-
getList
java.util.List<java.lang.Object> getList(java.lang.String key)
Get a List of strings associated with the given configuration key. 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
java.util.List<java.lang.Object> getList(java.lang.String key, java.util.List<?> defaultValue)
Get 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.
-
-