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, 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()
);
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceConfiguration.BuilderBuilder forConfigurations.static classConfiguration.Default -
Field Summary
Fields Modifier and Type Field Description static charKEY_SEPARATORThe separator char ('.') which is used to connect simple keys to full-qualified ones. -
Method Summary
Modifier and Type Method Description static Configuration.BuilderBuilder()Pseudo-constructor method to create a newConfiguration.Builder.Configurationchild(String key)Gets the assigned child-configuration of the specified key, ornullif 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.booleancontains(String key)Checks if this configuration contains the specified key.Configurationdetach()Creates a new Configuration instance with all entries and child-configurations of this configuration, but with no parent, which makes it a root configuration.Stringget(String key)Gets the assigned value of the specified key, ornullif the configuration doesn't contain the key.<T> Tget(String key, Class<T> type)Gets the assigned value of the specified key.default BooleangetBoolean(String key)Gets the assigned value of the specified key asBoolean, ornullif the configuration doesn't contain the key.default BytegetByte(String key)Gets the assigned value of the specified key asByte, ornullif the configuration doesn't contain the key.default DoublegetDouble(String key)Gets the assigned value of the specified key asDouble, ornullif the configuration doesn't contain the key.default FloatgetFloat(String key)Gets the assigned value of the specified key asFloat, ornullif the configuration doesn't contain the key.default IntegergetInteger(String key)Gets the assigned value of the specified key asInteger, ornullif the configuration doesn't contain the key.default LonggetLong(String key)Gets the assigned value of the specified key asLong, ornullif the configuration doesn't contain the key.default ShortgetShort(String key)Gets the assigned value of the specified key asShort, ornullif the configuration doesn't contain the key.default booleanisRoot()Checks if this configuration is the root, meaning it has no parent.Stringkey()Gets the key of this child-configuration ornullif this is the root configuration.Iterable<String>keys()Gets all keys of this configuration, but not of the child-configurations.static ConfigurationLoad(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.Configurationparent()Gets this configuration's parent, ornullif this is the root configuration.default Configurationroot()Gets the root configuration, which may be this.default voidstore(ConfigurationStorer storer, ConfigurationAssembler assembler)Stores this configuration to an external target.XGettingTable<String,String>table()Converts all entries of this configuration to aXGettingTable.voidtraverse(Consumer<Configuration> consumer)Traverses this and all child-configurations recursively.ConfigurationValueMapperProvidervalueMapperProvider()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, ornullif 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, ornullif 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, ornullif 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, ornullif 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, ornullif 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, ornullif 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, ornullif 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, ornullif 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. ornullif the configuration doesn't contain the key.The String value is parsed by the registered
ConfigurationValueMapperfor the specified type.- Parameters:
key- the key to look uptype- the type to map to- Returns:
- the assigned value, or
null - Throws:
ConfigurationExceptionNoValueMapperFound- if noConfigurationValueMapperis 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.- Parameters:
key- the key to look uptype- the type to map to- Returns:
- a filled or empty
Optional - Throws:
ConfigurationExceptionNoValueMapperFound- if noConfigurationValueMapperis 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:
trueif this configuration contains the key,falseotherwise
-
key
String key()Gets the key of this child-configuration ornullif 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, ornullif 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, ornullif 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:
trueif this configuration is the root,falseotherwise
-
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
XGettingTablecontaining 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
XGettingTablecontaining 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
Mapcontaining 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
Mapcontaining 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
-