public interface ImmutableConfiguration
The main interface for accessing configuration data in a read-only fashion.
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, a
ConversionException
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
the setThrowExceptionOnMissing()
method, with which
it can be configured to throw a NoSuchElementException
exception in that case. (Note that getter methods for
primitive types in AbstractConfiguration
always throw an exception for missing properties because there is no
way of overloading the return value.)
Modifier and Type | Method and Description |
---|---|
boolean |
containsKey(String key)
Checks if the configuration contains the specified key.
|
<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)
Deprecated.
This method should not be used any more because its signature does not allow type-safe invocations; use
get(Class, String, Object) instead which offers the same functionality; for instance, to query
for an array of ints use int[] result = config.get(int[].class, "myArrayKey", someDefault); . |
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)
Gets a
Boolean associated with the given configuration key. |
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.
|
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. |
default Duration |
getDuration(String key)
Gets a
Duration associated with the given configuration key. |
default 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.
|
default <T extends Enum<T>> |
getEnum(String key,
Class<T> enumType)
Gets an enum associated with the given configuration key.
|
default <T extends Enum<T>> |
getEnum(String key,
Class<T> enumType,
T defaultValue)
Gets the enum associated with the given configuration key.
|
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. |
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.
|
<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.
|
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.
|
Object |
getProperty(String key)
Gets a property from the configuration.
|
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)
Gets an array of strings associated with the given configuration key.
|
ImmutableConfiguration |
immutableSubset(String prefix)
Return a decorator immutable Configuration containing every key from the current Configuration that starts with the
specified prefix.
|
boolean |
isEmpty()
Checks if the configuration is empty.
|
int |
size()
Returns the number of keys stored in this configuration.
|
boolean containsKey(String key)
key
- the key whose presence in this configuration is to be testedtrue
if the configuration contains a value for this key, false
otherwise<T> T get(Class<T> cls, String key)
AbstractConfiguration.isThrowExceptionOnMissing()
is set to
true
.T
- the target type of the valuecls
- the target class of the valuekey
- the key of the valueNoSuchElementException
- if the key doesn't map to an existing object and
throwExceptionOnMissing=true
ConversionException
- if the value is not compatible with the requested
type<T> T get(Class<T> cls, String key, T defaultValue)
T
- the target type of the valuecls
- the target class of the valuekey
- the key of the valuedefaultValue
- the default valueConversionException
- if the value is not compatible with the requested
typeObject getArray(Class<?> cls, String key)
cls
- the type expected for the elements of the arraykey
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not
compatible with a list of the specified class.@Deprecated Object getArray(Class<?> cls, String key, Object defaultValue)
get(Class, String, Object)
instead which offers the same functionality; for instance, to query
for an array of ints use int[] result = config.get(int[].class, "myArrayKey", someDefault);
.cls
- the type expected for the elements of the arraykey
- the configuration key.defaultValue
- the default valueConversionException
- is thrown if the key maps to an object that is not
compatible with an array of the specified class.IllegalArgumentException
- if the default value is not an array of the specified typeBigDecimal getBigDecimal(String key)
BigDecimal
associated with the given configuration key.key
- The configuration key.BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
BigDecimal
associated with the given configuration key. If the key doesn't map to an existing object,
the default value is returned.key
- The configuration key.defaultValue
- The default value.BigInteger getBigInteger(String key)
BigInteger
associated with the given configuration key.key
- The configuration key.BigInteger getBigInteger(String key, BigInteger defaultValue)
BigInteger
associated with the given configuration key. If the key doesn't map to an existing object,
the default value is returned.key
- The configuration key.defaultValue
- The default value.boolean getBoolean(String key)
key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
Boolean.boolean getBoolean(String key, boolean defaultValue)
key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Boolean.Boolean getBoolean(String key, Boolean defaultValue)
Boolean
associated with the given configuration key.key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Boolean.byte getByte(String key)
key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
Byte.byte getByte(String key, byte defaultValue)
key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Byte.Byte getByte(String key, Byte defaultValue)
Byte
associated with the given configuration key.key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Byte.<T> Collection<T> getCollection(Class<T> cls, String key, Collection<T> target)
getCollection(Class, String, Collection, Collection)
passing in null as default value.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)ConversionException
- if the conversion is not possible<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:
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)ConversionException
- if the conversion is not possibledouble getDouble(String key)
key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
Double.double getDouble(String key, double defaultValue)
key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Double.Double getDouble(String key, Double defaultValue)
Double
associated with the given configuration key.key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Double.default Duration getDuration(String key)
Duration
associated with the given configuration key.key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
Duration.default Duration getDuration(String key, Duration defaultValue)
Duration
associated with the given configuration key.key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Duration.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.key
- the configuration keyString 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.key
- the configuration keydecoder
- the ConfigurationDecoder
(must not be null)IllegalArgumentException
- if a null decoder is passeddefault <T extends Enum<T>> T getEnum(String key, Class<T> enumType)
T
- The enum type whose constant is to be returned.enumType
- the Class
object of the enum type from which to return a constantkey
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
String.default <T extends Enum<T>> T getEnum(String key, Class<T> enumType, T defaultValue)
T
- The enum type whose constant is to be returned.key
- The configuration key.enumType
- the Class
object of the enum type from which to return a constantdefaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Enum.float getFloat(String key)
key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
Float.float getFloat(String key, float defaultValue)
key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Float.Float getFloat(String key, Float defaultValue)
Float
associated with the given configuration key. If the key doesn't map to an existing object, the
default value is returned.key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Float.int getInt(String key)
key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
Integer.int getInt(String key, int defaultValue)
key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Integer.Integer getInteger(String key, Integer defaultValue)
Integer
associated with the given configuration key. If the key doesn't map to an existing object,
the default value is returned.key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Integer.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.Iterator<String> getKeys(String prefix)
db.user, db.pwd, db.url, window.xpos, window.ypos
,getKeys("db");
db.user, db.pwd, db.url
.prefix
- The prefix to test against.getKeys()
<T> List<T> getList(Class<T> cls, String key)
T
- the type expected for the elements of the listcls
- the class expected for the elements of the listkey
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not
compatible with a list of the specified class.<T> List<T> getList(Class<T> cls, String key, List<T> defaultValue)
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.ConversionException
- is thrown if the key maps to an object that is not
compatible with a list of the specified class.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.key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
List.List<Object> getList(String key, List<?> defaultValue)
key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
List.getList(Class, String, List)
long getLong(String key)
key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
Long.long getLong(String key, long defaultValue)
key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Long.Long getLong(String key, Long defaultValue)
Long
associated with the given configuration key. If the key doesn't map to an existing object, the
default value is returned.key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Long.Properties getProperties(String key)
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.key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
String/List.IllegalArgumentException
- if one of the tokens is malformed (does not contain an equals sign).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).key
- property to retrieveshort getShort(String key)
key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
Short.short getShort(String key, short defaultValue)
key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Short.Short getShort(String key, Short defaultValue)
Short
associated with the given configuration key. If the key doesn't map to an existing object, the
default value is returned.key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
Short.String getString(String key)
key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
String.String getString(String key, String defaultValue)
key
- The configuration key.defaultValue
- The default value.ConversionException
- is thrown if the key maps to an object that is not a
String.String[] getStringArray(String key)
key
- The configuration key.ConversionException
- is thrown if the key maps to an object that is not a
String/List of Strings.ImmutableConfiguration immutableSubset(String prefix)
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)
prefix
- The prefix used to select the properties.boolean isEmpty()
true
if the configuration contains no property, false
otherwise.int size()
isEmpty()
method.Copyright © 2001–2022 The Apache Software Foundation. All rights reserved.