public abstract class AbstractConfiguration extends BaseEventSource implements Configuration
Abstract configuration class. Provides basic functionality but does not store any data.
If you want to write your own Configuration class then you should implement only abstract methods from this class. A
lot of functionality needed by typical implementations of the Configuration
interface is already provided by
this base class. Following is a list of features implemented here:
Configuration
interface are already
handled by this base class. A concrete sub class only needs to provide a generic getProperty()
method.${var}
) will
be replaced by their corresponding values.ListDelimiterHandler
object which can be set using the setListDelimiterHandler(ListDelimiterHandler)
method. It is disabled per
default. To enable this feature, set a suitable ListDelimiterHandler
, e.g. an instance of
DefaultListDelimiterHandler
configured
with the desired list delimiter character.setThrowExceptionOnMissing()
method this behavior can be changed to throw an exception when a requested
property cannot be found.EVENT_XXX
constants to get an impression about which event types are supported.Synchronizer
interface.
Most methods defined by the Configuration
interface are already implemented in this class. Many method
implementations perform basic book-keeping tasks (e.g. firing events, handling synchronization), and then delegate to
other (protected) methods executing the actual work. Subclasses override these protected methods to define or adapt
behavior. The public entry point methods are final to prevent subclasses from breaking basic functionality.
Constructor and Description |
---|
AbstractConfiguration()
Creates a new instance of
AbstractConfiguration . |
Modifier and Type | Method and Description |
---|---|
void |
addErrorLogListener()
Adds a special
EventListener object to this configuration that will log all internal errors. |
void |
addProperty(String key,
Object value)
Add a property to the configuration.
|
protected abstract void |
addPropertyDirect(String key,
Object value)
Adds a key/value pair to the Configuration.
|
protected void |
addPropertyInternal(String key,
Object value)
Actually adds a property to this configuration.
|
void |
append(Configuration c)
Appends the content of the specified configuration to this configuration.
|
protected void |
beginRead(boolean optimize)
Notifies this configuration's
Synchronizer that a read operation is about to start. |
protected void |
beginWrite(boolean optimize)
Notifies this configuration's
Synchronizer that an update operation is about to start. |
void |
clear()
Remove all properties from the configuration.
|
protected void |
clearInternal()
Clears the whole configuration.
|
void |
clearProperty(String key)
Removes the specified property from this configuration.
|
protected abstract void |
clearPropertyDirect(String key)
Removes the specified property from this configuration.
|
protected void |
cloneInterpolator(AbstractConfiguration orgConfig)
Creates a clone of the
ConfigurationInterpolator used by this instance. |
boolean |
containsKey(String key)
Checks if the configuration contains the specified key.
|
protected abstract boolean |
containsKeyInternal(String key)
Actually checks whether the specified key is contained in this configuration.
|
void |
copy(Configuration c)
Copies the content of the specified configuration into this configuration.
|
protected void |
endRead()
Notifies this configuration's
Synchronizer that a read operation has finished. |
protected void |
endWrite()
Notifies this configuration's
Synchronizer that an update operation has finished. |
<T> T |
get(Class<T> cls,
String key)
Gets an object of the specified type associated with the given configuration key.
|
<T> T |
get(Class<T> cls,
String key,
T defaultValue)
Gets an object of the specified type associated with the given configuration key using a default value.
|
Object |
getArray(Class<?> cls,
String key)
Gets an array of typed objects associated with the given configuration key.
|
Object |
getArray(Class<?> cls,
String key,
Object defaultValue)
Gets an array of typed objects associated with the given configuration key.
|
BigDecimal |
getBigDecimal(String key)
Gets a
BigDecimal associated with the given configuration key. |
BigDecimal |
getBigDecimal(String key,
BigDecimal defaultValue)
Gets a
BigDecimal associated with the given configuration key. |
BigInteger |
getBigInteger(String key)
Gets a
BigInteger associated with the given configuration key. |
BigInteger |
getBigInteger(String key,
BigInteger defaultValue)
Gets a
BigInteger 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.
|
Boolean |
getBoolean(String key,
Boolean defaultValue)
Obtains the value of the specified key and tries to convert it into a
Boolean object. |
byte |
getByte(String key)
Gets a byte associated with the given configuration key.
|
byte |
getByte(String key,
byte defaultValue)
Gets a byte associated with the given configuration key.
|
Byte |
getByte(String key,
Byte defaultValue)
Gets a
Byte 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.
|
ConfigurationDecoder |
getConfigurationDecoder()
Returns the
ConfigurationDecoder used by this instance. |
ConversionHandler |
getConversionHandler()
Returns the
ConversionHandler used by this instance. |
double |
getDouble(String key)
Gets a double associated with the given configuration key.
|
double |
getDouble(String key,
double defaultValue)
Gets a double associated with the given configuration key.
|
Double |
getDouble(String key,
Double defaultValue)
Gets a
Double associated with the given configuration key. |
Duration |
getDuration(String key)
Gets a
Duration associated with the given configuration key. |
Duration |
getDuration(String key,
Duration defaultValue)
Gets a
Duration associated with the given configuration key. |
String |
getEncodedString(String key)
Gets the value of a string property that is stored in encoded form in this configuration using a default
ConfigurationDecoder . |
String |
getEncodedString(String key,
ConfigurationDecoder decoder)
Gets the value of a string property that is stored in encoded form in this configuration.
|
float |
getFloat(String key)
Gets a float associated with the given configuration key.
|
float |
getFloat(String key,
float defaultValue)
Gets a float associated with the given configuration key.
|
Float |
getFloat(String key,
Float defaultValue)
Gets a
Float associated with the given configuration key. |
int |
getInt(String key)
Gets a int associated with the given configuration key.
|
int |
getInt(String key,
int defaultValue)
Gets a int associated with the given configuration key.
|
Integer |
getInteger(String key,
Integer defaultValue)
Gets an
Integer associated with the given configuration key. |
ConfigurationInterpolator |
getInterpolator()
Returns the
ConfigurationInterpolator object that manages the lookup objects for resolving variables. |
Iterator<String> |
getKeys()
Gets the list of the keys contained in the configuration.
|
Iterator<String> |
getKeys(String prefix)
Gets the list of the keys contained in the configuration that match the specified prefix.
|
protected abstract Iterator<String> |
getKeysInternal()
Actually creates an iterator for iterating over the keys in this configuration.
|
protected Iterator<String> |
getKeysInternal(String prefix)
Returns an
Iterator with all property keys starting with the specified prefix. |
<T> List<T> |
getList(Class<T> cls,
String key)
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> |
getList(Class<T> cls,
String key,
List<T> defaultValue)
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.
|
List<Object> |
getList(String key)
Gets a List of the values associated with the given configuration key.
|
List<Object> |
getList(String key,
List<?> defaultValue)
Gets a List of strings associated with the given configuration key.
|
ListDelimiterHandler |
getListDelimiterHandler()
Returns the
ListDelimiterHandler used by this instance. |
ConfigurationLogger |
getLogger()
Returns the logger used by this configuration object.
|
long |
getLong(String key)
Gets a long associated with the given configuration key.
|
long |
getLong(String key,
long defaultValue)
Gets a long associated with the given configuration key.
|
Long |
getLong(String key,
Long defaultValue)
Gets a
Long associated with the given configuration key. |
Properties |
getProperties(String key)
Gets a list of properties associated with the given configuration key.
|
Properties |
getProperties(String key,
Properties defaults)
Get a list of properties associated with the given configuration key.
|
Object |
getProperty(String key)
Gets a property from the configuration.
|
protected abstract Object |
getPropertyInternal(String key)
Actually obtains the value of the specified property.
|
short |
getShort(String key)
Gets a short associated with the given configuration key.
|
short |
getShort(String key,
short defaultValue)
Gets a short associated with the given configuration key.
|
Short |
getShort(String key,
Short defaultValue)
Gets a
Short associated with the given configuration key. |
String |
getString(String key)
Gets a string associated with the given configuration key.
|
String |
getString(String key,
String defaultValue)
Gets a string associated with the given configuration key.
|
String[] |
getStringArray(String key)
Get an array of strings associated with the given configuration key.
|
Synchronizer |
getSynchronizer()
Returns the object responsible for synchronizing this configuration.
|
ImmutableConfiguration |
immutableSubset(String prefix)
Return a decorator immutable Configuration containing every key from the current Configuration that starts with the
specified prefix.
|
protected void |
initLogger(ConfigurationLogger log)
Initializes the logger.
|
void |
installInterpolator(Map<String,? extends Lookup> prefixLookups,
Collection<? extends Lookup> defLookups)
Creates and installs a new
ConfigurationInterpolator for this Configuration based on the passed in
arguments. |
protected Object |
interpolate(Object value)
Returns the interpolated value.
|
protected String |
interpolate(String base)
interpolate key names to handle ${key} stuff
|
Configuration |
interpolatedConfiguration()
Returns a configuration with the same content as this configuration, but with all variables replaced by their actual
values.
|
boolean |
isEmpty()
Checks if the configuration is empty.
|
protected abstract boolean |
isEmptyInternal()
Actually checks whether this configuration contains data.
|
protected boolean |
isScalarValue(Object value)
Checks whether the specified object is a scalar value.
|
boolean |
isThrowExceptionOnMissing()
Returns true if missing values throw Exceptions.
|
void |
lock(LockMode mode)
Locks this object for the specified mode.
|
void |
setConfigurationDecoder(ConfigurationDecoder configurationDecoder)
Sets the
ConfigurationDecoder for this configuration. |
void |
setConversionHandler(ConversionHandler conversionHandler)
Sets the
ConversionHandler to be used by this instance. |
void |
setDefaultLookups(Collection<? extends Lookup> lookups)
Adds all
Lookup objects in the given collection as default lookups (i.e. |
void |
setInterpolator(ConfigurationInterpolator ci)
Sets the
ConfigurationInterpolator object to be used by this Configuration . |
void |
setListDelimiterHandler(ListDelimiterHandler listDelimiterHandler)
Sets the
ListDelimiterHandler to be used by this instance. |
void |
setLogger(ConfigurationLogger log)
Allows setting the logger to be used by this configuration object.
|
void |
setParentInterpolator(ConfigurationInterpolator parent)
Sets the specified
ConfigurationInterpolator as the parent of this configuration's
ConfigurationInterpolator . |
void |
setPrefixLookups(Map<String,? extends Lookup> lookups)
Registers all
Lookup objects in the given map at the current ConfigurationInterpolator of this
configuration. |
void |
setProperty(String key,
Object value)
Set a property, this will replace any previously set values.
|
protected void |
setPropertyInternal(String key,
Object value)
Actually sets the value of a property.
|
void |
setSynchronizer(Synchronizer synchronizer)
Sets the object responsible for synchronizing this configuration.
|
void |
setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
Allows to set the
throwExceptionOnMissing flag. |
int |
size()
Returns the number of keys stored in this configuration.
|
protected int |
sizeInternal()
Actually calculates the size of this configuration.
|
Configuration |
subset(String prefix)
Return a decorator Configuration containing every key from the current Configuration that starts with the specified
prefix.
|
void |
unlock(LockMode mode)
Releases a lock of this object that was obtained using the
SynchronizerSupport.lock(LockMode) method. |
addEventListener, clearErrorListeners, clearEventListeners, clone, copyEventListeners, createErrorEvent, createEvent, fireError, fireEvent, getEventListenerRegistrations, getEventListeners, isDetailEvents, removeEventListener, setDetailEvents
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getEnum, getEnum
public AbstractConfiguration()
AbstractConfiguration
.public ListDelimiterHandler getListDelimiterHandler()
ListDelimiterHandler
used by this instance.ListDelimiterHandler
public void setListDelimiterHandler(ListDelimiterHandler listDelimiterHandler)
Sets the ListDelimiterHandler
to be used by this instance. This object is invoked every time when dealing
with string properties that may contain a list delimiter and thus have to be split to multiple values. Per default, a
ListDelimiterHandler
implementation is set which does not support list splitting. This can be changed for
instance by setting a DefaultListDelimiterHandler
object.
Warning: Be careful when changing the list delimiter handler when the configuration has already been loaded/populated. List handling is typically applied already when properties are added to the configuration. If later another handler is set which processes lists differently, results may be unexpected; some operations may even cause exceptions.
listDelimiterHandler
- the ListDelimiterHandler
to be used (must not be null)IllegalArgumentException
- if the ListDelimiterHandler
is nullpublic ConversionHandler getConversionHandler()
ConversionHandler
used by this instance.ConversionHandler
public void setConversionHandler(ConversionHandler conversionHandler)
ConversionHandler
to be used by this instance. The ConversionHandler
is responsible for
every kind of data type conversion. It is consulted by all get methods returning results in specific data types. A
newly created configuration uses a default ConversionHandler
implementation. This can be changed while
initializing the configuration (e.g. via a builder). Note that access to this property is not synchronized.conversionHandler
- the ConversionHandler
to be used (must not be null)IllegalArgumentException
- if the ConversionHandler
is nullpublic void setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
throwExceptionOnMissing
flag. This flag controls the behavior of property getter methods
that return objects if the requested property is missing. If the flag is set to false (which is the default
value), these methods will return null. If set to true, they will throw a
NoSuchElementException
exception. Note that getter methods for primitive data types are not affected by this
flag.throwExceptionOnMissing
- The new value for the propertypublic boolean isThrowExceptionOnMissing()
public ConfigurationInterpolator getInterpolator()
ConfigurationInterpolator
object that manages the lookup objects for resolving variables.
Unless a custom interpolator has been set or the instance has been modified, the returned interpolator will
resolve values from this configuration instance and support the
default prefix lookups
.getInterpolator
in interface Configuration
ConfigurationInterpolator
associated with this configurationConfigurationInterpolator.getDefaultPrefixLookups()
public final void setInterpolator(ConfigurationInterpolator ci)
ConfigurationInterpolator
object to be used by this Configuration
. This object is invoked
for each access of a string property in order to substitute variables which may be contained. The argument can be
null to disable interpolation at all. This implementation sets the passed in object without further modifications. A null argument is
allowed; this disables interpolation.setInterpolator
in interface Configuration
ci
- the new ConfigurationInterpolator
public final void installInterpolator(Map<String,? extends Lookup> prefixLookups, Collection<? extends Lookup> defLookups)
ConfigurationInterpolator
for this Configuration
based on the passed in
arguments. This method creates a default ConfigurationInterpolator
instance and initializes it with the
passed in Lookup
objects. It also adds a special default Lookup
object that tries to resolve
variables by matching them with properties contained in this Configuration
. This is also the main difference
to the Configuration.setInterpolator(ConfigurationInterpolator)
method which sets the passed in object as is without
adding this special lookup. This implementation creates a new ConfigurationInterpolator
instance and initializes it with
the given Lookup
objects. In addition, it adds a specialized default Lookup
object which queries this
Configuration
.installInterpolator
in interface Configuration
prefixLookups
- the map with Lookup
objects associated with specific prefixes (can be null)defLookups
- a collection with default Lookup
objects (can be null)ConfigurationInterpolator
public void setPrefixLookups(Map<String,? extends Lookup> lookups)
Lookup
objects in the given map at the current ConfigurationInterpolator
of this
configuration. The set of default lookup objects (for variables without a prefix) is not modified by this method. If
this configuration does not have a ConfigurationInterpolator
, a new instance is created. Note: This method is
mainly intended to be used for initializing a configuration when it is created by a builder. Normal client code
should better call installInterpolator(Map, Collection)
to define the ConfigurationInterpolator
in a
single step.lookups
- a map with new Lookup
objects and their prefixes (may be null)public void setDefaultLookups(Collection<? extends Lookup> lookups)
Lookup
objects in the given collection as default lookups (i.e. lookups without a variable prefix)
to the ConfigurationInterpolator
object of this configuration. In addition, it adds a specialized default
Lookup
object which queries this Configuration
. The set of Lookup
objects with prefixes is
not modified by this method. If this configuration does not have a ConfigurationInterpolator
, a new instance
is created. Note: This method is mainly intended to be used for initializing a configuration when it is created by a
builder. Normal client code should better call installInterpolator(Map, Collection)
to define the
ConfigurationInterpolator
in a single step.lookups
- the collection with default Lookup
objects to be addedpublic void setParentInterpolator(ConfigurationInterpolator parent)
ConfigurationInterpolator
as the parent of this configuration's
ConfigurationInterpolator
. If this configuration does not have a ConfigurationInterpolator
, a new
instance is created. Note: This method is mainly intended to be used for initializing a configuration when it is
created by a builder. Normal client code can directly update the ConfigurationInterpolator
.parent
- the parent ConfigurationInterpolator
to be setpublic void setConfigurationDecoder(ConfigurationDecoder configurationDecoder)
ConfigurationDecoder
for this configuration. This object is used by
getEncodedString(String)
.configurationDecoder
- the ConfigurationDecoder
public ConfigurationDecoder getConfigurationDecoder()
ConfigurationDecoder
used by this instance.ConfigurationDecoder
protected void cloneInterpolator(AbstractConfiguration orgConfig)
ConfigurationInterpolator
used by this instance. This method can be called by
clone()
implementations of derived classes. Normally, the ConfigurationInterpolator
of a
configuration instance must not be shared with other instances because it contains a specific Lookup
object
pointing to the owning configuration. This has to be taken into account when cloning a configuration. This method
creates a new ConfigurationInterpolator
for this configuration instance which contains all lookup objects
from the original ConfigurationInterpolator
except for the configuration specific lookup pointing to the
passed in original configuration. This one is replaced by a corresponding Lookup
referring to this
configuration.orgConfig
- the original configuration from which this one was clonedpublic ConfigurationLogger getLogger()
public void setLogger(ConfigurationLogger log)
log
- the new loggerpublic final void addErrorLogListener()
EventListener
object to this configuration that will log all internal errors. This method is
intended to be used by certain derived classes, for which it is known that they can fail on property access (e.g.
DatabaseConfiguration
).public final Synchronizer getSynchronizer()
Synchronizer
has been set, a NoOpSynchronizer
is returned. So, per default, instances of
AbstractConfiguration
are not thread-safe unless a suitable Synchronizer
is set!getSynchronizer
in interface SynchronizerSupport
Synchronizer
used by this instancepublic final void setSynchronizer(Synchronizer synchronizer)
Synchronizer
object when initializing this configuration instance in order to make it thread-safe.setSynchronizer
in interface SynchronizerSupport
synchronizer
- the new Synchronizer
; can be null, then this instance uses a
NoOpSynchronizer
public final void lock(LockMode mode)
LockMode
.
When done the unlock()
must be called with the same LockMode
argument. In practice, a
try-finally construct should be used as in the following example:
SynchronizerSupport syncSupport = ...; syncSupport.lock(LockMode.READ); try { // read access to syncSupport } finally { syncSupport.unlock(LockMode.READ); }Note: Always use this method for obtaining a lock rather than accessing the object's
Synchronizer
directly. An implementation may perform additional actions which are not executed when only interacting with the
Synchronizer
. This implementation delegates to beginRead()
or beginWrite()
, depending on the
LockMode
argument. Subclasses can override these protected methods to perform additional steps when a
configuration is locked.lock
in interface SynchronizerSupport
mode
- the LockMode
NullPointerException
- if the argument is nullpublic final void unlock(LockMode mode)
SynchronizerSupport.lock(LockMode)
method. This method must always be
called pair-wise with lock()
. The argument must match to the one passed to the corresponding lock()
call; otherwise, the behavior of the Synchronizer
is unspecified. This implementation delegates to endRead()
or endWrite()
, depending on the
LockMode
argument. Subclasses can override these protected methods to perform additional steps when a
configuration's lock is released.unlock
in interface SynchronizerSupport
mode
- the LockMode
NullPointerException
- if the argument is nullprotected void beginRead(boolean optimize)
Synchronizer
that a read operation is about to start. This method is called by
all methods which access this configuration in a read-only mode. Subclasses may override it to perform additional
actions before this read operation. The boolean optimize argument can be evaluated by overridden methods in
derived classes. Some operations which require a lock do not need a fully initialized configuration object. By
setting this flag to true, such operations can give a corresponding hint. An overridden
implementation of beginRead()
can then decide to skip some initialization steps. All basic operations in this
class (and most of the basic Configuration
implementations) call this method with a parameter value of
false. In any case the inherited method must be called! Otherwise, proper synchronization is
not guaranteed.optimize
- a flag whether optimization can be performedprotected void endRead()
Synchronizer
that a read operation has finished. This method is called by all
methods which access this configuration in a read-only manner at the end of their execution. Subclasses may override
it to perform additional actions after this read operation. In any case the inherited method must be called!
Otherwise, the read lock will not be released.protected void beginWrite(boolean optimize)
Synchronizer
that an update operation is about to start. This method is called
by all methods which modify this configuration. Subclasses may override it to perform additional operations before an
update. For a description of the boolean optimize argument refer to the documentation of
beginRead()
. In any case the inherited method must be called! Otherwise, proper synchronization is
not guaranteed.optimize
- a flag whether optimization can be performedbeginRead(boolean)
protected void endWrite()
Synchronizer
that an update operation has finished. This method is called by
all methods which modify this configuration at the end of their execution. Subclasses may override it to perform
additional operations after an update. In any case the inherited method must be called! Otherwise, the write
lock will not be released.public final void addProperty(String key, Object value)
Configuration
resource.loader = fileis already present in the configuration and you call
addProperty("resource.loader", "classpath")Then you will end up with a List like the following:
["file", "classpath"]
addProperty
in interface Configuration
key
- The key to add the property to.value
- The value to add.protected void addPropertyInternal(String key, Object value)
addProperty()
. It performs list
splitting if necessary and delegates to addPropertyDirect(String, Object)
for every single property value.key
- the key of the property to be addedvalue
- the new property valueprotected abstract void addPropertyDirect(String key, Object value)
key
- key to use for mappingvalue
- object to storeprotected String interpolate(String base)
base
- string to interpolateprotected Object interpolate(Object value)
ConfigurationInterpolator
. If no
ConfigurationInterpolator
is set, the passed in value is returned without changes.value
- the value to interpolatepublic Configuration subset(String prefix)
Configuration
prefix.number = 1 prefix.string = Apache prefixed.foo = bar prefix = Jakartathe Configuration returned by
subset("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.
subset
in interface Configuration
prefix
- The prefix used to select the properties.SubsetConfiguration
public ImmutableConfiguration immutableSubset(String prefix)
ImmutableConfiguration
prefix.number = 1 prefix.string = Apache prefixed.foo = bar prefix = Jakartathe immutable Configuration returned by
subset("prefix")
will contain the properties:
number = 1 string = Apache = Jakarta(The key for the value "Jakarta" is an empty string)
immutableSubset
in interface ImmutableConfiguration
prefix
- The prefix used to select the properties.public final void setProperty(String key, Object value)
Configuration
setProperty
in interface Configuration
key
- The key of the property to changevalue
- The new valueprotected void setPropertyInternal(String key, Object value)
setProperty()
. It provides a default
implementation of this functionality by clearing the specified key and delegating to addProperty()
.
Subclasses should override this method if they can provide a more efficient algorithm for setting a property value.key
- the property keyvalue
- the new property valuepublic final void clearProperty(String key)
clearPropertyDirect()
, which will do the real work.clearProperty
in interface Configuration
key
- the key to be removedprotected abstract void clearPropertyDirect(String key)
clearProperty()
after it has
done some preparations. It must be overridden in sub classes.key
- the key to be removedpublic final void clear()
Configuration
clear
in interface Configuration
protected void clearInternal()
clear()
after some preparations have been made. This
base implementation uses the iterator provided by getKeys()
to remove every single property. Subclasses
should override this method if there is a more efficient way of clearing the configuration.public final Iterator<String> getKeys()
remove()
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. This implementation takes care of synchronization and then delegates to getKeysInternal()
for
obtaining the actual iterator. Note that depending on a concrete implementation, an iteration may fail if the
configuration is updated concurrently.getKeys
in interface ImmutableConfiguration
public final Iterator<String> getKeys(String prefix)
db.user, db.pwd, db.url, window.xpos, window.ypos
,getKeys("db");
db.user, db.pwd, db.url
.getKeys("db");
will find the keys db
, db.user
, or db.password
,
but not the key dbdriver
.getKeys
in interface ImmutableConfiguration
prefix
- The prefix to test against.ImmutableConfiguration.getKeys()
protected abstract Iterator<String> getKeysInternal()
getKeys()
, it has to be defined by concrete subclasses.Iterator
with all property keys in this configurationprotected Iterator<String> getKeysInternal(String prefix)
Iterator
with all property keys starting with the specified prefix. This method is called by
getKeys(String)
. It is fully implemented by delegating to getKeysInternal()
and returning a special
iterator which filters for the passed in prefix. Subclasses can override it if they can provide a more efficient way
to iterate over specific keys only.prefix
- the prefix for the keys to be taken into accountIterator
returning the filtered keyspublic final Object getProperty(String key)
Configuration
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). This implementation ensures proper synchronization. Subclasses have to define the abstract
getPropertyInternal()
method which is called from here.getProperty
in interface ImmutableConfiguration
key
- property to retrieveprotected abstract Object getPropertyInternal(String key)
getProperty()
. Concrete
subclasses must define it to fetch the value of the desired property.key
- the key of the property in questionpublic final boolean isEmpty()
isEmptyInternal()
.isEmpty
in interface ImmutableConfiguration
true
if the configuration contains no property, false
otherwise.protected abstract boolean isEmptyInternal()
isEmpty()
. It has to be
defined by concrete subclasses.public final int size()
ImmutableConfiguration.isEmpty()
method. This implementation handles synchronization and delegates to sizeInternal()
.size
in interface ImmutableConfiguration
protected int sizeInternal()
size()
with a read lock held.
The base implementation provided here calculates the size based on the iterator returned by getKeys()
. Sub
classes which can determine the size in a more efficient way should override this method.public final boolean containsKey(String key)
containsKeyInternal()
.containsKey
in interface ImmutableConfiguration
key
- the key whose presence in this configuration is to be testedtrue
if the configuration contains a value for this key, false
otherwiseprotected abstract boolean containsKeyInternal(String key)
containsKey()
. It has to be defined by concrete subclasses.key
- the key in questionpublic Properties getProperties(String key)
ImmutableConfiguration
key=value
. These strings are split at the
equals sign, and the key parts will become keys of the returned Properties
object, the value parts become
values.getProperties
in interface ImmutableConfiguration
key
- The configuration key.public Properties getProperties(String key, Properties defaults)
key
- The configuration key.defaults
- Any default values for the returned Properties
object. Ignored if null
.ConversionException
- is thrown if the key maps to an object that is not a String/List of Strings.IllegalArgumentException
- if one of the tokens is malformed (does not contain an equals sign).public boolean getBoolean(String key)
ImmutableConfiguration
getBoolean
in interface ImmutableConfiguration
key
- The configuration key.public boolean getBoolean(String key, boolean defaultValue)
ImmutableConfiguration
getBoolean
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public Boolean getBoolean(String key, Boolean defaultValue)
Boolean
object. If the property has no
value, the passed in default value will be used.getBoolean
in interface ImmutableConfiguration
key
- the key of the propertydefaultValue
- the default valueBoolean
ConversionException
- if the value cannot be converted to a Boolean
public byte getByte(String key)
ImmutableConfiguration
getByte
in interface ImmutableConfiguration
key
- The configuration key.public byte getByte(String key, byte defaultValue)
ImmutableConfiguration
getByte
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public Byte getByte(String key, Byte defaultValue)
ImmutableConfiguration
Byte
associated with the given configuration key.getByte
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public double getDouble(String key)
ImmutableConfiguration
getDouble
in interface ImmutableConfiguration
key
- The configuration key.public double getDouble(String key, double defaultValue)
ImmutableConfiguration
getDouble
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public Double getDouble(String key, Double defaultValue)
ImmutableConfiguration
Double
associated with the given configuration key.getDouble
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public Duration getDuration(String key)
ImmutableConfiguration
Duration
associated with the given configuration key.getDuration
in interface ImmutableConfiguration
key
- The configuration key.public Duration getDuration(String key, Duration defaultValue)
ImmutableConfiguration
Duration
associated with the given configuration key.getDuration
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public float getFloat(String key)
ImmutableConfiguration
getFloat
in interface ImmutableConfiguration
key
- The configuration key.public float getFloat(String key, float defaultValue)
ImmutableConfiguration
getFloat
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public Float getFloat(String key, Float defaultValue)
ImmutableConfiguration
Float
associated with the given configuration key. If the key doesn't map to an existing object, the
default value is returned.getFloat
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public int getInt(String key)
ImmutableConfiguration
getInt
in interface ImmutableConfiguration
key
- The configuration key.public int getInt(String key, int defaultValue)
ImmutableConfiguration
getInt
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public Integer getInteger(String key, Integer defaultValue)
ImmutableConfiguration
Integer
associated with the given configuration key. If the key doesn't map to an existing object,
the default value is returned.getInteger
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public long getLong(String key)
ImmutableConfiguration
getLong
in interface ImmutableConfiguration
key
- The configuration key.public long getLong(String key, long defaultValue)
ImmutableConfiguration
getLong
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public Long getLong(String key, Long defaultValue)
ImmutableConfiguration
Long
associated with the given configuration key. If the key doesn't map to an existing object, the
default value is returned.getLong
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public short getShort(String key)
ImmutableConfiguration
getShort
in interface ImmutableConfiguration
key
- The configuration key.public short getShort(String key, short defaultValue)
ImmutableConfiguration
getShort
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public Short getShort(String key, Short defaultValue)
ImmutableConfiguration
Short
associated with the given configuration key. If the key doesn't map to an existing object, the
default value is returned.getShort
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public BigDecimal getBigDecimal(String key)
BigDecimal
associated with the given configuration key.getBigDecimal
in interface ImmutableConfiguration
key
- The configuration key.setThrowExceptionOnMissing(boolean)
public BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
ImmutableConfiguration
BigDecimal
associated with the given configuration key. If the key doesn't map to an existing object,
the default value is returned.getBigDecimal
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public BigInteger getBigInteger(String key)
BigInteger
associated with the given configuration key.getBigInteger
in interface ImmutableConfiguration
key
- The configuration key.setThrowExceptionOnMissing(boolean)
public BigInteger getBigInteger(String key, BigInteger defaultValue)
ImmutableConfiguration
BigInteger
associated with the given configuration key. If the key doesn't map to an existing object,
the default value is returned.getBigInteger
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public String getString(String key)
getString
in interface ImmutableConfiguration
key
- The configuration key.setThrowExceptionOnMissing(boolean)
public String getString(String key, String defaultValue)
ImmutableConfiguration
getString
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.public String getEncodedString(String key, ConfigurationDecoder decoder)
ConfigurationDecoder
. The value returned by the ConfigurationDecoder
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. This implementation delegates to getString(String)
in order to obtain the value of the passed
in key. This value is passed to the decoder. Because getString()
is used behind the scenes all standard
features like handling of missing keys and interpolation work as expected.getEncodedString
in interface ImmutableConfiguration
key
- the configuration keydecoder
- the ConfigurationDecoder
(must not be null)public String getEncodedString(String key)
ConfigurationDecoder
. This method works like the method with the same name, but it uses a default
ConfigurationDecoder
associated with this configuration. It depends on a specific implementation how this
default decoder is obtained. This implementation makes use of the ConfigurationDecoder
set for this configuration. If no
such object has been set, an IllegalStateException
exception is thrown.getEncodedString
in interface ImmutableConfiguration
key
- the configuration keyIllegalStateException
- if no ConfigurationDecoder
is setsetConfigurationDecoder(ConfigurationDecoder)
public String[] getStringArray(String key)
ListDelimiterHandler
is consulted to find out whether the string can be split into multiple values.getStringArray
in interface ImmutableConfiguration
key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a String/List of Strings.setListDelimiterHandler(ListDelimiterHandler)
public List<Object> getList(String key)
getList()
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.getList
in interface ImmutableConfiguration
key
- The configuration key.getStringArray(String)
public List<Object> getList(String key, List<?> defaultValue)
ImmutableConfiguration
getList
in interface ImmutableConfiguration
key
- The configuration key.defaultValue
- The default value.ImmutableConfiguration.getList(Class, String, List)
public <T> T get(Class<T> cls, String key)
ImmutableConfiguration
isThrowExceptionOnMissing()
is set to
true
.get
in interface ImmutableConfiguration
T
- the target type of the valuecls
- the target class of the valuekey
- the key of the valuepublic <T> T get(Class<T> cls, String key, T defaultValue)
ConversionHandler
to perform the actual type conversion.get
in interface ImmutableConfiguration
T
- the target type of the valuecls
- the target class of the valuekey
- the key of the valuedefaultValue
- the default valuepublic Object getArray(Class<?> cls, String key)
ImmutableConfiguration
getArray
in interface ImmutableConfiguration
cls
- the type expected for the elements of the arraykey
- The configuration key.public Object getArray(Class<?> cls, String key, Object defaultValue)
ConversionHandler
to perform the actual type conversion.
If this results in a null result (because the property is undefined), the default value is returned. It is
checked whether the default value is an array with the correct component type. If not, an exception is thrown.getArray
in interface ImmutableConfiguration
cls
- the type expected for the elements of the arraykey
- the configuration key.defaultValue
- the default valueIllegalArgumentException
- if the default value is not a compatible arraypublic <T> List<T> getList(Class<T> cls, String key)
ImmutableConfiguration
getList
in interface ImmutableConfiguration
T
- the type expected for the elements of the listcls
- the class expected for the elements of the listkey
- The configuration key.public <T> List<T> getList(Class<T> cls, String key, List<T> defaultValue)
getCollection()
. As target collection a newly
created ArrayList
is passed in.getList
in interface ImmutableConfiguration
T
- the type expected for the elements of the listcls
- the class expected for the elements of the listkey
- the configuration key.defaultValue
- the default value.public <T> Collection<T> getCollection(Class<T> cls, String key, Collection<T> target)
ImmutableConfiguration
ImmutableConfiguration.getCollection(Class, String, Collection, Collection)
passing in null as default value.getCollection
in interface ImmutableConfiguration
T
- the element type of the result listcls
- the the element class of the result listkey
- the configuration keytarget
- the target collection (may be null)public <T> Collection<T> getCollection(Class<T> cls, String key, Collection<T> target, Collection<T> defaultValue)
getList()
,
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:
ConversionHandler
to perform the actual conversion. If no
target collection is provided, an ArrayList
is created.getCollection
in interface ImmutableConfiguration
T
- the element type of the result listcls
- the the element class of the result listkey
- the configuration keytarget
- the target collection (may be null)defaultValue
- the default value (may be null)protected boolean isScalarValue(Object value)
getList()
and
getStringArray()
if the property requested is not a string, a list, or an array. If it returns true,
the calling method transforms the value to a string and returns a list or an array with this single element. This
implementation returns true if the value is of a wrapper type for a primitive type.value
- the value to be checkedpublic void copy(Configuration c)
clone()
methods should be
used.c
- the configuration to copy (can be null, then this operation will have no effect)public void append(Configuration c)
clone()
methods should be used.c
- the configuration to be appended (can be null, then this operation will have no effect)public Configuration interpolatedConfiguration()
${var}
will be resolved as far as possible (if a variable cannot be resolved, it remains
unchanged). This operation is useful if the content of a configuration is to be exported or processed by an external
component that does not support variable interpolation.ConfigurationRuntimeException
- if this configuration cannot be clonedprotected final void initLogger(ConfigurationLogger log)
log
- the loggerCopyright © 2001–2022 The Apache Software Foundation. All rights reserved.