Class MapConfiguration
- java.lang.Object
-
- org.apache.commons.configuration2.event.BaseEventSource
-
- org.apache.commons.configuration2.AbstractConfiguration
-
- org.apache.commons.configuration2.MapConfiguration
-
- All Implemented Interfaces:
java.lang.Cloneable
,Configuration
,EventSource
,ImmutableConfiguration
,SynchronizerSupport
- 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 aMap
. 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, aList
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.The handling of list delimiters is a bit different for this configuration implementation: When a property of type String is queried, it is passed to the current
ListDelimiterHandler
which may generate multiple values. Note that per default a list delimiter handler is set which does not do any list splitting, so this feature is disabled. It can be enabled by setting a properly configuredListDelimiterHandler
implementation, e.g. aDefaultListDelimiterHandler
object.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
-
-
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.private boolean
trimmingDisabled
A flag whether trimming of property values should be disabled.
-
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
which uses the specifiedProperties
object as its data store.
-
Method Summary
All Methods Static 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.protected boolean
containsKeyInternal(java.lang.String key)
Actually checks whether the specified key is contained in this configuration.protected boolean
containsValueInternal(java.lang.Object value)
Tests whether this configuration contains one or more matches to this value.protected java.util.Iterator<java.lang.String>
getKeysInternal()
Actually creates an iterator for iterating over the keys in this configuration.java.util.Map<java.lang.String,java.lang.Object>
getMap()
Gets the Map decorated by this configuration.protected java.lang.Object
getPropertyInternal(java.lang.String key)
Actually obtains the value of the specified property.protected boolean
isEmptyInternal()
Actually checks whether this configuration contains data.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.protected int
sizeInternal()
Actually calculates the size of this configuration.private static java.util.Map<java.lang.String,java.lang.Object>
toMap(java.util.Properties props)
Helper method for converting the type of theProperties
object to a supported map type.java.lang.String
toString()
Converts this object to a String suitable for debugging and logging.-
Methods inherited from class org.apache.commons.configuration2.AbstractConfiguration
addErrorLogListener, addProperty, addPropertyInternal, append, beginRead, beginWrite, clear, clearInternal, clearProperty, cloneInterpolator, contains, containsKey, containsValue, copy, endRead, endWrite, get, get, getArray, getArray, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getCollection, getCollection, getConfigurationDecoder, getConversionHandler, getDouble, getDouble, getDouble, getDuration, getDuration, getEncodedString, getEncodedString, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getKeys, getKeys, getKeys, getKeysInternal, getKeysInternal, getList, getList, getList, getList, getListDelimiterHandler, getLogger, getLong, getLong, getLong, getProperties, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getStringArray, getSynchronizer, immutableSubset, initLogger, installInterpolator, interpolate, interpolate, interpolatedConfiguration, isEmpty, isScalarValue, isThrowExceptionOnMissing, lock, setConfigurationDecoder, setConversionHandler, setDefaultLookups, setInterpolator, setListDelimiterHandler, setLogger, setParentInterpolator, setPrefixLookups, setProperty, setPropertyInternal, setSynchronizer, setThrowExceptionOnMissing, size, subset, unlock
-
Methods inherited from class org.apache.commons.configuration2.event.BaseEventSource
addEventListener, clearErrorListeners, clearEventListeners, copyEventListeners, createErrorEvent, createEvent, fireError, fireEvent, getEventListenerRegistrations, getEventListeners, isDetailEvents, removeEventListener, setDetailEvents
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.configuration2.ImmutableConfiguration
getEnum, getEnum
-
-
-
-
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
which uses the specifiedProperties
object as its data store. All changes of this configuration affect the givenProperties
object and vice versa. Note that whileProperties
actually implementsMap<Object, Object>
, we expect it to contain only string keys. Other key types will lead toClassCastException
exceptions on certain methods.- Parameters:
props
- theProperties
object defining the content of this configuration- Since:
- 1.8
-
-
Method Detail
-
toMap
private static java.util.Map<java.lang.String,java.lang.Object> toMap(java.util.Properties props)
Helper method for converting the type of theProperties
object to a supported map type. As stated by the comment of the constructor, we expect theProperties
object to contain only String key; therefore, it is safe to do this cast.- Parameters:
props
- theProperties
to be copied- Returns:
- a newly created map with all string keys of the properties
-
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
-
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 must be overridden in sub classes.- Specified by:
clearPropertyDirect
in classAbstractConfiguration
- Parameters:
key
- the key to be removed
-
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 classBaseEventSource
- Returns:
- the copy
- Since:
- 1.3
-
containsKeyInternal
protected boolean containsKeyInternal(java.lang.String key)
Description copied from class:AbstractConfiguration
Actually checks whether the specified key is contained in this configuration. This method is called bycontainsKey()
. It has to be defined by concrete subclasses.- Specified by:
containsKeyInternal
in classAbstractConfiguration
- Parameters:
key
- the key in question- Returns:
- true if this key is contained in this configuration, false otherwise
-
containsValueInternal
protected boolean containsValueInternal(java.lang.Object value)
Tests whether this configuration contains one or more matches to this value. This operation stops at first match but may be more expensive than the containsKey method.- Specified by:
containsValueInternal
in classAbstractConfiguration
- Parameters:
value
- the value in question- Returns:
true
if and only if some key maps to thevalue
argument in this configuration as determined by theequals
method;false
otherwise.- Since:
- 2.11.0
-
getKeysInternal
protected java.util.Iterator<java.lang.String> getKeysInternal()
Description copied from class:AbstractConfiguration
Actually creates an iterator for iterating over the keys in this configuration. This method is called bygetKeys()
, it has to be defined by concrete subclasses.- Specified by:
getKeysInternal
in classAbstractConfiguration
- Returns:
- an
Iterator
with all property keys in this configuration
-
getMap
public java.util.Map<java.lang.String,java.lang.Object> getMap()
Gets the Map decorated by this configuration.- Returns:
- the map this configuration is based onto
-
getPropertyInternal
protected java.lang.Object getPropertyInternal(java.lang.String key)
Description copied from class:AbstractConfiguration
Actually obtains the value of the specified property. This method is called bygetProperty()
. Concrete subclasses must define it to fetch the value of the desired property.- Specified by:
getPropertyInternal
in classAbstractConfiguration
- Parameters:
key
- the key of the property in question- Returns:
- the (raw) value of this property
-
isEmptyInternal
protected boolean isEmptyInternal()
Description copied from class:AbstractConfiguration
Actually checks whether this configuration contains data. This method is called byisEmpty()
. It has to be defined by concrete subclasses.- Specified by:
isEmptyInternal
in classAbstractConfiguration
- Returns:
- true if this configuration contains no data, false otherwise
-
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
-
sizeInternal
protected int sizeInternal()
Description copied from class:AbstractConfiguration
Actually calculates the size of this configuration. This method is called bysize()
with a read lock held. The base implementation provided here calculates the size based on the iterator returned bygetKeys()
. Sub classes which can determine the size in a more efficient way should override this method.- Overrides:
sizeInternal
in classAbstractConfiguration
- Returns:
- the size of this configuration (i.e. the number of keys)
-
toString
public java.lang.String toString()
Converts this object to a String suitable for debugging and logging.- Overrides:
toString
in classjava.lang.Object
- Since:
- 2.3
-
-