Class MapConfiguration
- java.lang.Object
-
- org.apache.commons.configuration.event.EventSource
-
- org.apache.commons.configuration.AbstractConfiguration
-
- org.apache.commons.configuration.MapConfiguration
-
- All Implemented Interfaces:
java.lang.Cloneable
,Configuration
- Direct Known Subclasses:
EnvironmentConfiguration
,SystemConfiguration
public class MapConfiguration extends AbstractConfiguration implements java.lang.Cloneable
A Map based Configuration.
This implementation of the
Configuration
interface is initialized with ajava.util.Map
. The methods of theConfiguration
interface are implemented on top of the content of this map. The following storage scheme is used:Property keys are directly mapped to map keys, i.e. the
getProperty()
method directly performs aget()
on the map. Analogously,setProperty()
oraddProperty()
operations write new data into the map. If a value is added to an existing property, ajava.util.List
is created, which stores the values of this property.An important use case of this class is to treat a map as a
Configuration
allowing access to its data through the richer interface. This can be a bit problematic in some cases because the map may contain values that need not adhere to the default storage scheme used by typical configuration implementations, e.g. regarding lists. In such cases care must be taken when manipulating the data through theConfiguration
interface, e.g. by callingaddProperty()
; results may be different than expected.An important point is the handling of list delimiters: If delimiter parsing is enabled (which it is per default),
getProperty()
checks whether the value of a property is a string and whether it contains the list delimiter character. If this is the case, the value is split at the delimiter resulting in a list. This split operation typically also involves trimming the single values as the list delimiter character may be surrounded by whitespace. Trimming can be disabled with thesetTrimmingDisabled(boolean)
method. The whole list splitting behavior can be disabled using theAbstractConfiguration.setDelimiterParsingDisabled(boolean)
method.Notice that list splitting is only performed for single string values. If a property has multiple values, the single values are not split even if they contain the list delimiter character.
As the underlying
Map
is directly used as store of the property values, the thread-safety of thisConfiguration
implementation depends on the map passed to the constructor.Notes about type safety: For properties with multiple values this implementation creates lists of type
Object
and stores them. If a property is assigned another value, the value is added to the list. This can cause problems if the map passed to the constructor already contains lists of other types. This should be avoided, otherwise it cannot be guaranteed that the application might throwClassCastException
exceptions later.- Since:
- 1.1
- Version:
- $Id: MapConfiguration.java 1534429 2013-10-22 00:45:36Z henning $
- Author:
- Emmanuel Bourg
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.String,java.lang.Object>
map
The Map decorated by this configuration.-
Fields inherited from class org.apache.commons.configuration.AbstractConfiguration
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN
-
-
Constructor Summary
Constructors Constructor Description MapConfiguration(java.util.Map<java.lang.String,?> map)
Create a Configuration decorator around the specified Map.MapConfiguration(java.util.Properties props)
Creates a new instance ofMapConfiguration
and initializes its content from the specifiedProperties
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addPropertyDirect(java.lang.String key, java.lang.Object value)
Adds a key/value pair to the Configuration.protected void
clearPropertyDirect(java.lang.String key)
Removes the specified property from this configuration.java.lang.Object
clone()
Returns a copy of this object.boolean
containsKey(java.lang.String key)
Check if the configuration contains the specified key.java.util.Iterator<java.lang.String>
getKeys()
Get the list of the keys contained in the configuration.java.util.Map<java.lang.String,java.lang.Object>
getMap()
Return the Map decorated by this configuration.java.lang.Object
getProperty(java.lang.String key)
Gets a property from the configuration.boolean
isEmpty()
Check if the configuration is empty.boolean
isTrimmingDisabled()
Returns the flag whether trimming of property values is disabled.void
setTrimmingDisabled(boolean trimmingDisabled)
Sets a flag whether trimming of property values is disabled.-
Methods inherited from class org.apache.commons.configuration.AbstractConfiguration
addErrorLogListener, addProperty, append, clear, clearProperty, copy, createInterpolator, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDefaultListDelimiter, getDelimiter, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getKeys, getList, getList, getListDelimiter, getLogger, getLong, getLong, getLong, getProperties, getProperties, getShort, getShort, getShort, getString, getString, getStringArray, getSubstitutor, interpolate, interpolate, interpolatedConfiguration, interpolateHelper, isDelimiterParsingDisabled, isScalarValue, isThrowExceptionOnMissing, resolveContainerStore, setDefaultListDelimiter, setDelimiter, setDelimiterParsingDisabled, setListDelimiter, setLogger, setProperty, setThrowExceptionOnMissing, subset
-
Methods inherited from class org.apache.commons.configuration.event.EventSource
addConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, createErrorEvent, createEvent, fireError, fireEvent, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, setDetailEvents
-
-
-
-
Field Detail
-
map
protected java.util.Map<java.lang.String,java.lang.Object> map
The Map decorated by this configuration.
-
-
Constructor Detail
-
MapConfiguration
public MapConfiguration(java.util.Map<java.lang.String,?> map)
Create a Configuration decorator around the specified Map. The map is used to store the configuration properties, any change will also affect the Map.- Parameters:
map
- the map
-
MapConfiguration
public MapConfiguration(java.util.Properties props)
Creates a new instance ofMapConfiguration
and initializes its content from the specifiedProperties
object. The resulting configuration is not connected to theProperties
object, but all keys which are strings are copied (keys of other types are ignored).- Parameters:
props
- theProperties
object defining the content of this configuration- Throws:
java.lang.NullPointerException
- if theProperties
object is null- Since:
- 1.8
-
-
Method Detail
-
getMap
public java.util.Map<java.lang.String,java.lang.Object> getMap()
Return the Map decorated by this configuration.- Returns:
- the map this configuration is based onto
-
isTrimmingDisabled
public boolean isTrimmingDisabled()
Returns the flag whether trimming of property values is disabled.- Returns:
- true if trimming of property values is disabled; false otherwise
- Since:
- 1.7
-
setTrimmingDisabled
public void setTrimmingDisabled(boolean trimmingDisabled)
Sets a flag whether trimming of property values is disabled. This flag is only evaluated if list splitting is enabled. Refer to the header comment for more information about list splitting and trimming.- Parameters:
trimmingDisabled
- a flag whether trimming of property values should be disabled- Since:
- 1.7
-
getProperty
public java.lang.Object getProperty(java.lang.String key)
Description copied from interface:Configuration
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).- Specified by:
getProperty
in interfaceConfiguration
- 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.
-
addPropertyDirect
protected void addPropertyDirect(java.lang.String key, java.lang.Object value)
Description copied from class:AbstractConfiguration
Adds a key/value pair to the Configuration. Override this method to provide write access to underlying Configuration store.- Specified by:
addPropertyDirect
in classAbstractConfiguration
- Parameters:
key
- key to use for mappingvalue
- object to store
-
isEmpty
public boolean isEmpty()
Description copied from interface:Configuration
Check if the configuration is empty.- Specified by:
isEmpty
in interfaceConfiguration
- Returns:
true
if the configuration contains no property,false
otherwise.
-
containsKey
public boolean containsKey(java.lang.String key)
Description copied from interface:Configuration
Check if the configuration contains the specified key.- Specified by:
containsKey
in interfaceConfiguration
- 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
-
clearPropertyDirect
protected void clearPropertyDirect(java.lang.String key)
Description copied from class:AbstractConfiguration
Removes the specified property from this configuration. This method is called byclearProperty()
after it has done some preparations. It should be overridden in sub classes. This base implementation is just left empty.- Overrides:
clearPropertyDirect
in classAbstractConfiguration
- Parameters:
key
- the key to be removed
-
getKeys
public java.util.Iterator<java.lang.String> getKeys()
Description copied from interface:Configuration
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 callingConfiguration.clearProperty(String)
for this property. So it is highly recommended to avoid using the iterator'sremove()
method.- Specified by:
getKeys
in interfaceConfiguration
- Returns:
- An Iterator.
-
clone
public java.lang.Object clone()
Returns a copy of this object. The returned configuration will contain the same properties as the original. Event listeners are not cloned.- Overrides:
clone
in classEventSource
- Returns:
- the copy
- Since:
- 1.3
-
-