Interface Configuration
- All Known Implementing Classes:
Configuration.Default
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,
ConfigurationValueMapper
s 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() );
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
Configuration.Builder
Builder forConfiguration
s.static class
Configuration.Default
-
Field Summary
Fields Modifier and Type Field Description static char
KEY_SEPARATOR
The separator char ('.') which is used to connect simple keys to full-qualified ones. -
Method Summary
Modifier and Type Method Description static Configuration.Builder
Builder()
Pseudo-constructor method to create a newConfiguration.Builder
.Configuration
child(String key)
Gets the assigned child-configuration of the specified key, ornull
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 aMap
.XGettingTable<String,String>
coalescedTable()
Converts all entries of this configuration and all child-configurations recursively to aXGettingTable
.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, ornull
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 asBoolean
, ornull
if the configuration doesn't contain the key.default Byte
getByte(String key)
Gets the assigned value of the specified key asByte
, ornull
if the configuration doesn't contain the key.default Double
getDouble(String key)
Gets the assigned value of the specified key asDouble
, ornull
if the configuration doesn't contain the key.default Float
getFloat(String key)
Gets the assigned value of the specified key asFloat
, ornull
if the configuration doesn't contain the key.default Integer
getInteger(String key)
Gets the assigned value of the specified key asInteger
, ornull
if the configuration doesn't contain the key.default Long
getLong(String key)
Gets the assigned value of the specified key asLong
, ornull
if the configuration doesn't contain the key.default Short
getShort(String key)
Gets the assigned value of the specified key asShort
, ornull
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 ornull
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 aMap
.default Optional<String>
opt(String key)
Gets the assigned value of the specified key asOptional
, 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 asOptional
, 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 asOptional
, 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 asOptional
, 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 asOptional
, 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 asOptional
, 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 asOptional
, 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 asOptional
, 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 asOptional
, which is empty if the configuration doesn't contain the key.Configuration
parent()
Gets this configuration's parent, ornull
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 aXGettingTable
.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.
-
Field Details
-
KEY_SEPARATOR
static final char KEY_SEPARATORThe separator char ('.') which is used to connect simple keys to full-qualified ones.- See Also:
- Constant Field Values
-
-
Method Details
-
Builder
Pseudo-constructor method to create a newConfiguration.Builder
.- Returns:
- a new builder
-
Load
Convenience method to load a configuration from an external source.This is shortcut for
Configuration.Builder().load(loader, parser).buildConfiguration()
- Parameters:
loader
- the loader to retrieve the inputparser
- the parser to parse the input- Returns:
- the created configuration
-
get
Gets the assigned value of the specified key, ornull
if the configuration doesn't contain the key.- Parameters:
key
- the key to look up- Returns:
- the assigned value, or
null
-
getBoolean
Gets the assigned value of the specified key asBoolean
, ornull
if the configuration doesn't contain the key.The String value is parsed according to
Boolean.parseBoolean(String)
.- Parameters:
key
- the key to look up- Returns:
- the assigned value, or
null
-
getByte
Gets the assigned value of the specified key asByte
, ornull
if the configuration doesn't contain the key.The String value is parsed according to
Byte.parseByte(String)
.- Parameters:
key
- the key to look up- Returns:
- the assigned value, or
null
-
getShort
Gets the assigned value of the specified key asShort
, ornull
if the configuration doesn't contain the key.The String value is parsed according to
Short.parseShort(String)
.- Parameters:
key
- the key to look up- Returns:
- the assigned value, or
null
-
getInteger
Gets the assigned value of the specified key asInteger
, ornull
if the configuration doesn't contain the key.The String value is parsed according to
Integer.parseInt(String)
.- Parameters:
key
- the key to look up- Returns:
- the assigned value, or
null
-
getLong
Gets the assigned value of the specified key asLong
, ornull
if the configuration doesn't contain the key.The String value is parsed according to
Long.parseLong(String)
.- Parameters:
key
- the key to look up- Returns:
- the assigned value, or
null
-
getFloat
Gets the assigned value of the specified key asFloat
, ornull
if the configuration doesn't contain the key.The String value is parsed according to
Float.parseFloat(String)
.- Parameters:
key
- the key to look up- Returns:
- the assigned value, or
null
-
getDouble
Gets the assigned value of the specified key asDouble
, ornull
if the configuration doesn't contain the key.The String value is parsed according to
Double.parseDouble(String)
.- Parameters:
key
- the key to look up- Returns:
- the assigned value, or
null
-
get
Gets the assigned value of the specified key. ornull
if the configuration doesn't contain the key.The String value is parsed by the registered
ConfigurationValueMapper
for the specified type.- Type Parameters:
T
- the value type- Parameters:
key
- the key to look uptype
- the type to map to- Returns:
- the assigned value, or
null
- Throws:
ConfigurationExceptionNoValueMapperFound
- if noConfigurationValueMapper
is found for the typeConfigurationExceptionValueMappingFailed
- if the mapping to the target type fails
-
opt
Gets the assigned value of the specified key asOptional
, which is empty if the configuration doesn't contain the key.- Parameters:
key
- the key to look up- Returns:
- a filled or empty
Optional
-
optBoolean
Gets the assigned value of the specified key asOptional
, which is empty if the configuration doesn't contain the key.- Parameters:
key
- the key to look up- Returns:
- a filled or empty
Optional
- See Also:
getBoolean(String)
-
optByte
Gets the assigned value of the specified key asOptional
, which is empty if the configuration doesn't contain the key.- Parameters:
key
- the key to look up- Returns:
- a filled or empty
Optional
- See Also:
getByte(String)
-
optShort
Gets the assigned value of the specified key asOptional
, which is empty if the configuration doesn't contain the key.- Parameters:
key
- the key to look up- Returns:
- a filled or empty
Optional
- See Also:
getShort(String)
-
optInteger
Gets the assigned value of the specified key asOptional
, which is empty if the configuration doesn't contain the key.- Parameters:
key
- the key to look up- Returns:
- a filled or empty
Optional
- See Also:
getInteger(String)
-
optLong
Gets the assigned value of the specified key asOptional
, which is empty if the configuration doesn't contain the key.- Parameters:
key
- the key to look up- Returns:
- a filled or empty
Optional
- See Also:
getLong(String)
-
optFloat
Gets the assigned value of the specified key asOptional
, which is empty if the configuration doesn't contain the key.- Parameters:
key
- the key to look up- Returns:
- a filled or empty
Optional
- See Also:
getFloat(String)
-
optDouble
Gets the assigned value of the specified key asOptional
, which is empty if the configuration doesn't contain the key.- Parameters:
key
- the key to look up- Returns:
- a filled or empty
Optional
- See Also:
getDouble(String)
-
opt
Gets the assigned value of the specified key asOptional
, which is empty if the configuration doesn't contain the key.- Type Parameters:
T
- the value type- Parameters:
key
- the key to look uptype
- the type to map to- Returns:
- a filled or empty
Optional
- Throws:
ConfigurationExceptionNoValueMapperFound
- if noConfigurationValueMapper
is found for the typeConfigurationExceptionValueMappingFailed
- if the mapping to the target type fails- See Also:
get(String, Class)
-
contains
Checks if this configuration contains the specified key.- Parameters:
key
- the key to look up- Returns:
true
if this configuration contains the key,false
otherwise
-
key
String key()Gets the key of this child-configuration ornull
if this is the root configuration.- Returns:
- this child-configuration's key
-
keys
Gets all keys of this configuration, but not of the child-configurations.- Returns:
- an iterable with all keys
-
child
Gets the assigned child-configuration of the specified key, ornull
if the configuration doesn't contain the key.- Parameters:
key
- the key to look up- Returns:
- the assigned child-configuration, or
null
-
children
Iterable<? extends Configuration> children()Gets all direct child-configurations.- Returns:
- all child-configurations
-
parent
Configuration parent()Gets this configuration's parent, ornull
if this is the root configuration.- Returns:
- the parent or
null
-
isRoot
default boolean isRoot()Checks if this configuration is the root, meaning it has no parent.- Returns:
true
if this configuration is the root,false
otherwise
-
root
Gets the root configuration, which may be this.- Returns:
- the configuration's root
-
traverse
Traverses this and all child-configurations recursively.- Parameters:
consumer
- the consumer to accept all configurations
-
table
XGettingTable<String,String> table()Converts all entries of this configuration to aXGettingTable
.- Returns:
- a
XGettingTable
containing all entries of this configurations - See Also:
coalescedTable()
-
coalescedTable
XGettingTable<String,String> coalescedTable()Converts all entries of this configuration and all child-configurations recursively to aXGettingTable
.- Returns:
- a
XGettingTable
containing all entries of this and all child-configurations
-
map
Converts all entries of this configuration to aMap
.Because configurations are immutable, changes made in the resulting map will not reflect back.
- Returns:
- a
Map
containing all entries of this configurations - See Also:
coalescedMap()
-
coalescedMap
Converts all entries of this configuration and all child-configurations recursively to aMap
.Because configurations are immutable, changes made in the resulting map will not reflect back.
- Returns:
- a
Map
containing all entries of this and all child-configurations
-
valueMapperProvider
ConfigurationValueMapperProvider valueMapperProvider()Gets the value mapper provider which is assigned to this configuration.- Returns:
- the assigned value mapper
- See Also:
get(String, Class)
-
detach
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.The original configuration (this) remains untouched.
- Returns:
- a new, detached configuration
-
store
Stores this configuration to an external target.- Parameters:
storer
- the storer to write toassembler
- the assembler for the desired format
-