public interface Configuration
The keys are just plain Strings with any content except the KEY_SEPARATOR ('.'), because
it connects simple keys to full-qualified ones.
Example configuration:
microstream | +-- storage | | | +-- storageDirectory = /home/my-storage | | | +-- backupDirectory = /home/backup-storage | +-- cache | | | +-- keyType = java.lang.String | | | +-- valueTytpe = java.lang.Double | +-- version = 1.2.3 | +-- production = trueTo access the
storageDirectory entry there are following ways.
Either get the child configurations and then the value entry
String directory = configuration.child("microstream").child("storage").get("storageDirectory");
or just use the full-qualified key, as a shortcut
String directory = configuration.get("microstream.storage.storageDirectory");
In order to translate the value entries into different types, ConfigurationValueMappers are used.
Predefined value mappers are there for the most commonly used types in configurations:
ConfigurationValueMapperProvider.Default().
Custom value mappers can be used as well, of course.
ConfigurationValueMapperProvider valueMapperProvider = ConfigurationValueMapperProvider.Default()
.add(new MyValueMapper())
.build();
Configuration configuration = Configuration.Builder()
.valueMapperProvider(valueMapperProvider)
.build();
boolean production = configuration.getBoolean("microstream.production");
MyType myType = configuration.get("key", MyType.class);
Configurations can be created with a Configuration.Builder. Builders are populated programmatically,
by a ConfigurationMapper or a ConfigurationParser.
// create configuration from external file
Configuration configuration = Configuration.Load(
ConfigurationLoader.New("config-production.xml"),
ConfigurationParserXml.New()
);
// create configuration from Map
Map<String, Object> otherFrameworkConfig = ...;
Configuration configuration = ConfigurationMapperMap.New()
.mapConfiguration(otherFrameworkConfig)
.buildConfiguration();
// create configuration from different sources
Configuration.Builder()
.load(
ConfigurationLoader.New("config-base.xml"),
ConfigurationParserXml.New()
)
.load(
ConfigurationLoader.New("config-production.xml"),
ConfigurationParserXml.New()
)
.buildConfiguration();
Configurations can be exported as well:
configuration.store(
ConfigurationStorer.New(Paths.get("home", "config-export.xml")),
ConfigurationAssemblerXml.New()
);
| Modifier and Type | Interface and Description |
|---|---|
static interface |
Configuration.Builder
Builder for
Configurations. |
static class |
Configuration.Default |
| Modifier and Type | Field and Description |
|---|---|
static char |
KEY_SEPARATOR
The separator char ('.') which is used to connect simple keys to full-qualified ones.
|
| Modifier and Type | Method and Description |
|---|---|
static Configuration.Builder |
Builder()
Pseudo-constructor method to create a new
Configuration.Builder. |
Configuration |
child(String key)
Gets the assigned child-configuration of the specified key,
or
null if the configuration doesn't contain the key. |
Iterable<? extends Configuration> |
children()
Gets all direct child-configurations.
|
Map<String,String> |
coalescedMap()
Converts all entries of this configuration and all child-configurations recursively to a
Map. |
XGettingTable<String,String> |
coalescedTable()
Converts all entries of this configuration and all child-configurations recursively to a
XGettingTable. |
boolean |
contains(String key)
Checks if this configuration contains the specified key.
|
Configuration |
detach()
Creates a new Configuration instance with all entries and child-configurations of this configuration,
but with no parent, which makes it a root configuration.
|
String |
get(String key)
Gets the assigned value of the specified key,
or
null if the configuration doesn't contain the key. |
<T> T |
get(String key,
Class<T> type)
Gets the assigned value of the specified key.
|
default Boolean |
getBoolean(String key)
Gets the assigned value of the specified key as
Boolean,
or null if the configuration doesn't contain the key. |
default Byte |
getByte(String key)
Gets the assigned value of the specified key as
Byte,
or null if the configuration doesn't contain the key. |
default Double |
getDouble(String key)
Gets the assigned value of the specified key as
Double,
or null if the configuration doesn't contain the key. |
default Float |
getFloat(String key)
Gets the assigned value of the specified key as
Float,
or null if the configuration doesn't contain the key. |
default Integer |
getInteger(String key)
Gets the assigned value of the specified key as
Integer,
or null if the configuration doesn't contain the key. |
default Long |
getLong(String key)
Gets the assigned value of the specified key as
Long,
or null if the configuration doesn't contain the key. |
default Short |
getShort(String key)
Gets the assigned value of the specified key as
Short,
or null if the configuration doesn't contain the key. |
default boolean |
isRoot()
Checks if this configuration is the root, meaning it has no parent.
|
String |
key()
Gets the key of this child-configuration or
null if this is the root configuration. |
Iterable<String> |
keys()
Gets all keys of this configuration, but not of the child-configurations.
|
static Configuration |
Load(ConfigurationLoader loader,
ConfigurationParser parser)
Convenience method to load a configuration from an external source.
|
Map<String,String> |
map()
Converts all entries of this configuration to a
Map. |
default Optional<String> |
opt(String key)
Gets the assigned value of the specified key as
Optional,
which is empty if the configuration doesn't contain the key. |
default <T> Optional<T> |
opt(String key,
Class<T> type)
Gets the assigned value of the specified key as
Optional,
which is empty if the configuration doesn't contain the key. |
default Optional<Boolean> |
optBoolean(String key)
Gets the assigned value of the specified key as
Optional,
which is empty if the configuration doesn't contain the key. |
default Optional<Byte> |
optByte(String key)
Gets the assigned value of the specified key as
Optional,
which is empty if the configuration doesn't contain the key. |
default Optional<Double> |
optDouble(String key)
Gets the assigned value of the specified key as
Optional,
which is empty if the configuration doesn't contain the key. |
default Optional<Float> |
optFloat(String key)
Gets the assigned value of the specified key as
Optional,
which is empty if the configuration doesn't contain the key. |
default Optional<Integer> |
optInteger(String key)
Gets the assigned value of the specified key as
Optional,
which is empty if the configuration doesn't contain the key. |
default Optional<Long> |
optLong(String key)
Gets the assigned value of the specified key as
Optional,
which is empty if the configuration doesn't contain the key. |
default Optional<Short> |
optShort(String key)
Gets the assigned value of the specified key as
Optional,
which is empty if the configuration doesn't contain the key. |
Configuration |
parent()
Gets this configuration's parent, or
null if this is the root configuration. |
default Configuration |
root()
Gets the root configuration, which may be this.
|
default void |
store(ConfigurationStorer storer,
ConfigurationAssembler assembler)
Stores this configuration to an external target.
|
XGettingTable<String,String> |
table()
Converts all entries of this configuration to a
XGettingTable. |
void |
traverse(Consumer<Configuration> consumer)
Traverses this and all child-configurations recursively.
|
ConfigurationValueMapperProvider |
valueMapperProvider()
Gets the value mapper provider which is assigned to this configuration.
|
static final char KEY_SEPARATOR
static Configuration.Builder Builder()
Configuration.Builder.static Configuration Load(ConfigurationLoader loader, ConfigurationParser parser)
This is shortcut for
Configuration.Builder().load(loader, parser).buildConfiguration()
loader - the loader to retrieve the inputparser - the parser to parse the inputString get(String key)
null if the configuration doesn't contain the key.key - the key to look upnulldefault Boolean getBoolean(String key)
Boolean,
or null if the configuration doesn't contain the key.
The String value is parsed according to Boolean.parseBoolean(String).
key - the key to look upnulldefault Byte getByte(String key)
Byte,
or null if the configuration doesn't contain the key.
The String value is parsed according to Byte.parseByte(String).
key - the key to look upnulldefault Short getShort(String key)
Short,
or null if the configuration doesn't contain the key.
The String value is parsed according to Short.parseShort(String).
key - the key to look upnulldefault Integer getInteger(String key)
Integer,
or null if the configuration doesn't contain the key.
The String value is parsed according to Integer.parseInt(String).
key - the key to look upnulldefault Long getLong(String key)
Long,
or null if the configuration doesn't contain the key.
The String value is parsed according to Long.parseLong(String).
key - the key to look upnulldefault Float getFloat(String key)
Float,
or null if the configuration doesn't contain the key.
The String value is parsed according to Float.parseFloat(String).
key - the key to look upnulldefault Double getDouble(String key)
Double,
or null if the configuration doesn't contain the key.
The String value is parsed according to Double.parseDouble(String).
key - the key to look upnull<T> T get(String key, Class<T> type)
null if the configuration doesn't contain the key.
The String value is parsed by the registered ConfigurationValueMapper for the specified type.
T - the value typekey - the key to look uptype - the type to map tonullConfigurationExceptionNoValueMapperFound - if no ConfigurationValueMapper is found for the typeConfigurationExceptionValueMappingFailed - if the mapping to the target type failsdefault Optional<String> opt(String key)
Optional,
which is empty if the configuration doesn't contain the key.key - the key to look upOptionaldefault Optional<Boolean> optBoolean(String key)
Optional,
which is empty if the configuration doesn't contain the key.key - the key to look upOptionalgetBoolean(String)default Optional<Byte> optByte(String key)
Optional,
which is empty if the configuration doesn't contain the key.key - the key to look upOptionalgetByte(String)default Optional<Short> optShort(String key)
Optional,
which is empty if the configuration doesn't contain the key.key - the key to look upOptionalgetShort(String)default Optional<Integer> optInteger(String key)
Optional,
which is empty if the configuration doesn't contain the key.key - the key to look upOptionalgetInteger(String)default Optional<Long> optLong(String key)
Optional,
which is empty if the configuration doesn't contain the key.key - the key to look upOptionalgetLong(String)default Optional<Float> optFloat(String key)
Optional,
which is empty if the configuration doesn't contain the key.key - the key to look upOptionalgetFloat(String)default Optional<Double> optDouble(String key)
Optional,
which is empty if the configuration doesn't contain the key.key - the key to look upOptionalgetDouble(String)default <T> Optional<T> opt(String key, Class<T> type)
Optional,
which is empty if the configuration doesn't contain the key.T - the value typekey - the key to look uptype - the type to map toOptionalConfigurationExceptionNoValueMapperFound - if no ConfigurationValueMapper is found for the typeConfigurationExceptionValueMappingFailed - if the mapping to the target type failsget(String, Class)boolean contains(String key)
key - the key to look uptrue if this configuration contains the key, false otherwiseString key()
null if this is the root configuration.Iterable<String> keys()
Configuration child(String key)
null if the configuration doesn't contain the key.key - the key to look upnullIterable<? extends Configuration> children()
Configuration parent()
null if this is the root configuration.nulldefault boolean isRoot()
true if this configuration is the root, false otherwisedefault Configuration root()
void traverse(Consumer<Configuration> consumer)
consumer - the consumer to accept all configurationsXGettingTable<String,String> table()
XGettingTable.XGettingTable containing all entries of this configurationscoalescedTable()XGettingTable<String,String> coalescedTable()
XGettingTable.XGettingTable containing all entries of this and all child-configurationsMap<String,String> map()
Map.
Because configurations are immutable, changes made in the resulting map will not reflect back.
Map containing all entries of this configurationscoalescedMap()Map<String,String> coalescedMap()
Map.
Because configurations are immutable, changes made in the resulting map will not reflect back.
Map containing all entries of this and all child-configurationsConfigurationValueMapperProvider valueMapperProvider()
get(String, Class)Configuration detach()
The original configuration (this) remains untouched.
default void store(ConfigurationStorer storer, ConfigurationAssembler assembler)
storer - the storer to write toassembler - the assembler for the desired formatCopyright © 2022 MicroStream Software. All rights reserved.