public class MapConfiguration extends AbstractConfiguration implements Cloneable
A Map based Configuration.
This implementation of the Configuration interface is
initialized with a java.util.Map. The methods of the
Configuration 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 a get() on
the map. Analogously, setProperty() or
addProperty() operations write new data into the map. If a value
is added to an existing property, a java.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 the
Configuration interface, e.g. by calling
addProperty(); 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 the
setTrimmingDisabled(boolean) method. The whole list splitting
behavior can be disabled using the
AbstractConfiguration.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 this Configuration 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 throw ClassCastException exceptions later.
| Modifier and Type | Field and Description |
|---|---|
protected Map<String,Object> |
map
The Map decorated by this configuration.
|
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN| Constructor and Description |
|---|
MapConfiguration(Map<String,?> map)
Create a Configuration decorator around the specified Map.
|
MapConfiguration(Properties props)
Creates a new instance of
MapConfiguration and initializes its
content from the specified Properties object. |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addPropertyDirect(String key,
Object value)
Adds a key/value pair to the Configuration.
|
protected void |
clearPropertyDirect(String key)
Removes the specified property from this configuration.
|
Object |
clone()
Returns a copy of this object.
|
boolean |
containsKey(String key)
Check if the configuration contains the specified key.
|
Iterator<String> |
getKeys()
Get the list of the keys contained in the configuration.
|
Map<String,Object> |
getMap()
Return the Map decorated by this configuration.
|
Object |
getProperty(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.
|
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, subsetaddConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, createErrorEvent, createEvent, fireError, fireEvent, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, setDetailEventspublic MapConfiguration(Map<String,?> map)
map - the mappublic MapConfiguration(Properties props)
MapConfiguration and initializes its
content from the specified Properties object. The resulting
configuration is not connected to the Properties object, but all
keys which are strings are copied (keys of other types are ignored).props - the Properties object defining the content of this
configurationNullPointerException - if the Properties object is
nullpublic Map<String,Object> getMap()
public boolean isTrimmingDisabled()
public void setTrimmingDisabled(boolean trimmingDisabled)
trimmingDisabled - a flag whether trimming of property values should
be disabledpublic Object getProperty(String key)
ConfigurationConfiguration 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 the Configuration 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).getProperty in interface Configurationkey - property to retrieveprotected void addPropertyDirect(String key, Object value)
AbstractConfigurationaddPropertyDirect in class AbstractConfigurationkey - key to use for mappingvalue - object to storepublic boolean isEmpty()
ConfigurationisEmpty in interface Configurationtrue if the configuration contains no property,
false otherwise.public boolean containsKey(String key)
ConfigurationcontainsKey in interface Configurationkey - the key whose presence in this configuration is to be testedtrue if the configuration contains a value for this
key, false otherwiseprotected void clearPropertyDirect(String key)
AbstractConfigurationclearProperty() after it has done some
preparations. It should be overridden in sub classes. This base
implementation is just left empty.clearPropertyDirect in class AbstractConfigurationkey - the key to be removedpublic Iterator<String> getKeys()
Configurationremove() 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 calling
Configuration.clearProperty(String) for this property. So it is
highly recommended to avoid using the iterator's remove()
method.getKeys in interface Configurationpublic Object clone()
clone in class EventSourceCopyright © 2001–2013 The Apache Software Foundation. All rights reserved.