T - the target configuration typepublic final class Configuration<T> extends Object implements ConfigurationDataRetriever<T>, ConfigurationMetadataRetriever<T>
A Configuration may also be defined as a combination of a Source and a
Mapper, producing either a properties list, a JSON object, or a user-defined
bean.
Each Configuration may be assigned a namespace and precedence number, which
determines an order of importance. So, once stored in a common configuration container,
the object with the highest precedence value will be chosen first, taking precedence
over the other ones in the same namespace.
A Configuration object is eager by default, that is, the resource will
be loaded directly during build() time. Optionally, a Configuration may
be created with the lazy flag, indicating that the resource shall not be loaded
until really needed.
A Configuration may also be marked as optional, indicating that the
configuration shall be loaded quietly, that is, an "empty" Configuration object
will be instantiated even if the resource cannot be loaded (not default behavior).
IMPORTANT: Use a ConfigurationBuilder to create a
Configuration object. A builder instance can be retrieved by calling
builder(). For example:
Configuration<Properties> config = Configuration.<Properties>builder().source(new ClasspathFileSource<>("my.properties")).mapper(new PropertiesMapper()).namespace("default").precedence(10).build();
Source,
Mapper,
ConfigurationBuilder| Modifier and Type | Method and Description |
|---|---|
static <T> ConfigurationBuilder<T> |
builder()
Creates a new configuration builder.
|
boolean |
equals(Object other)
Indicates whether some other object is "equal to" this one.
|
Object |
get(String key)
Returns the object associated with the specified
key. |
T |
getBean()
Returns the configuration bean used by this data retriever, typically for manual
handling and/or troubleshooting purposes.
|
Boolean |
getBoolean(String key)
Returns the
Boolean object associated with the specified key. |
Double |
getDouble(String key)
Returns the
Double object associated with the specified key. |
Integer |
getInteger(String key)
Returns the
Integer object associated with the specified key. |
Long |
getLong(String key)
Returns the
Long object associated with the specified key. |
Boolean |
getMandatoryBoolean(String key)
Returns the
Boolean object associated with the specified key, throwing
an exception if not found. |
Double |
getMandatoryDouble(String key)
Returns the
Double object associated with the specified key, throwing
an exception if not found. |
Integer |
getMandatoryInteger(String key)
Returns the
Integer object associated with the specified key, throwing
an exception if not found. |
Long |
getMandatoryLong(String key)
Returns the
Long object associated with the specified key, throwing an
exception if not found. |
String |
getMandatoryString(String key)
Returns the
String object associated with the specified key, throwing
an exception if not found. |
Mapper<T> |
getMapper()
Returns the
Mapper object associated with this Configuration. |
String |
getNamespace()
Returns the namespace defined for this
Configuration object. |
int |
getPrecedence()
Returns the precedence value defined for this
Configuration object. |
Source<T> |
getSource()
Returns the
Source object associated with this Configuration. |
String |
getString(String key)
Returns the
String object associated with the specified key. |
int |
hashCode() |
boolean |
isLazy()
Returns a flag indicating whether this
Configuration is lazy, i.e., not loaded
until needed. |
boolean |
isOptional()
Returns a flag indicating whether this
Configuration is optional. |
Configuration<T> |
merge(Configuration<T> other)
Combines this
Configuration with another one, producing a new
Configuration, with the following characteristics:
The resulting Configuration will receive all the elements from both
Configuration objects
In case of conflicting keys, the values at the highest-precedence
Configuration will be selected
The metadata of the highest-precedence Configuration (namespace and
precedence) will be applied to the new Configuration
Note: The other Configuration must be of the same type as the
current one. |
String |
toString() |
public static <T> ConfigurationBuilder<T> builder()
T - the target configuration typeConfigurationBuilderpublic String getNamespace()
ConfigurationMetadataRetrieverConfiguration object.getNamespace in interface ConfigurationMetadataRetriever<T>Configurationpublic int getPrecedence()
ConfigurationMetadataRetrieverConfiguration object.
In a common container, objects with higher-precedence may be selected first in case of key collision.
getPrecedence in interface ConfigurationMetadataRetriever<T>Configurationpublic Source<T> getSource()
ConfigurationMetadataRetrieverSource object associated with this Configuration.getSource in interface ConfigurationMetadataRetriever<T>Source instancepublic Mapper<T> getMapper()
ConfigurationMetadataRetrieverMapper object associated with this Configuration.getMapper in interface ConfigurationMetadataRetriever<T>Mapper instancepublic boolean isOptional()
ConfigurationMetadataRetrieverConfiguration is optional.
An optional Configuration object may behave quietly in the event of a failure
to load the data.
isOptional in interface ConfigurationMetadataRetriever<T>true if this Configuration setup is optional; false,
otherwisepublic boolean isLazy()
ConfigurationMetadataRetrieverConfiguration is lazy, i.e., not loaded
until needed.isLazy in interface ConfigurationMetadataRetriever<T>true if this Configuration is lazy; false, otherwisepublic T getBean()
ConfigurationDataRetrievergetBean in interface ConfigurationDataRetriever<T>null if the
Configuration is marked as optional)public Object get(String key)
ConfigurationDataRetrieverkey.
Notes:
get in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPath)key. Depending on the
actual implementation, the result may be either null or an empty array
if the specified key is not foundpublic Boolean getBoolean(String key)
ConfigurationDataRetrieverBoolean object associated with the specified key.getBoolean in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPath)Boolean object associated with the specified key;
null if not foundpublic Integer getInteger(String key)
ConfigurationDataRetrieverInteger object associated with the specified key.getInteger in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPathInteger object associated with the specified key;
null if not foundpublic Long getLong(String key)
ConfigurationDataRetrieverLong object associated with the specified key.getLong in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPathLong object associated with the specified key; null
if not foundpublic Double getDouble(String key)
ConfigurationDataRetrieverDouble object associated with the specified key.getDouble in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPathDouble object associated with the specified key;
null if not foundpublic String getString(String key)
ConfigurationDataRetrieverString object associated with the specified key.getString in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPath)String object associated with the specified key;
null if not foundpublic boolean equals(Object other)
Two Configuration objects can be considered equal if both share the same
namespace and Source.
public Boolean getMandatoryBoolean(String key)
ConfigurationDataRetrieverBoolean object associated with the specified key, throwing
an exception if not found.getMandatoryBoolean in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPath)Boolean value associated with the specified key; never
nullpublic Integer getMandatoryInteger(String key)
ConfigurationDataRetrieverInteger object associated with the specified key, throwing
an exception if not found.getMandatoryInteger in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPath)Integer object associated with the specified key; never
nullpublic Long getMandatoryLong(String key)
ConfigurationDataRetrieverLong object associated with the specified key, throwing an
exception if not found.getMandatoryLong in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPath)Long object associated with the specified key; never
nullpublic Double getMandatoryDouble(String key)
ConfigurationDataRetrieverDouble object associated with the specified key, throwing
an exception if not found.getMandatoryDouble in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPath)Double object associated with the specified key; never
nullpublic String getMandatoryString(String key)
ConfigurationDataRetrieverString object associated with the specified key, throwing
an exception if not found.getMandatoryString in interface ConfigurationDataRetriever<T>key - the object key (some implementations may also accept a path expression, e.g:
JSONPath)String object associated with the specified key; never
nullpublic Configuration<T> merge(Configuration<T> other)
Configuration with another one, producing a new
Configuration, with the following characteristics:
Configuration will receive all the elements from both
Configuration objectsConfiguration will be selectedConfiguration (namespace and
precedence) will be applied to the new Configuration
Note: The other Configuration must be of the same type as the
current one.
other - the Configuration to be merged with this one; not nullConfiguration resulting from the combination of this object and
the specified oneNullPointerException - if the other Configuration is nullConfigurationMergerCopyright © 2022. All rights reserved.