@Immutable public interface Configuration
Configuration instance can be obtained
from Properties or loaded from a file, stream,
reader, URL, or classpath resource. They can
also be built by first creating a builder and then using that builder to populate and
return the immutable Configuration instance.
A Configuration object is basically a decorator around a Properties object. It has methods to get and convert
individual property values to numeric, boolean and String types, optionally using a default value if the given property value
does not exist. However, it is immutable, so it does not have any methods to set property values, allowing
it to be passed around and reused without concern that other components might change the underlying property values.
| Modifier and Type | Interface and Description |
|---|---|
static class |
Configuration.Builder
A builder of Configuration objects.
|
static interface |
Configuration.ConfigBuilder<C extends Configuration,B extends Configuration.ConfigBuilder<C,B>>
The basic interface for configuration builders.
|
| Modifier and Type | Method and Description |
|---|---|
default Map<String,String> |
asMap()
Get a copy of these configuration properties as a Properties object.
|
default Properties |
asProperties()
Get a copy of these configuration properties as a Properties object.
|
static Configuration.Builder |
copy(Configuration config)
Create a new
configuration builder that starts with a copy of the supplied configuration. |
static Configuration.Builder |
create()
Create a new
configuration builder. |
default Configuration.Builder |
edit()
Obtain an editor for a copy of this configuration.
|
static Configuration |
empty()
Obtain an empty configuration.
|
default Configuration |
filter(Predicate<? super String> matcher)
Return a new
Configuration that contains only the subset of keys that satisfy the given predicate. |
default void |
forEachMatchingFieldName(Pattern regex,
BiConsumer<String,String> function)
Apply the given function to all fields whose names match the given regular expression.
|
default <T> void |
forEachMatchingFieldName(Pattern regex,
int groupNumber,
BiFunction<String,String,T> groupExtractor,
BiConsumer<String,T> function)
For all fields whose names match the given regular expression, extract a value from the specified group in the regular
expression and call the supplied function.
|
default void |
forEachMatchingFieldName(String regex,
BiConsumer<String,String> function)
Apply the given function to all fields whose names match the given regular expression.
|
default <T> void |
forEachMatchingFieldName(String regex,
int groupNumber,
BiFunction<String,String,T> groupExtractor,
BiConsumer<String,T> function)
For all fields whose names match the given regular expression, extract a value from the specified group in the regular
expression and call the supplied function.
|
default <T> void |
forEachMatchingFieldNameWithBoolean(Pattern regex,
int groupNumber,
BiConsumer<String,Boolean> function)
For all fields whose names match the given regular expression, extract a boolean value from the first group in the regular
expression and call the supplied function.
|
default <T> void |
forEachMatchingFieldNameWithBoolean(String regex,
BiConsumer<String,Boolean> function)
For all fields whose names match the given regular expression, extract a boolean value from the first group in the regular
expression and call the supplied function.
|
default <T> void |
forEachMatchingFieldNameWithBoolean(String regex,
int groupNumber,
BiConsumer<String,Boolean> function)
For all fields whose names match the given regular expression, extract a boolean value from the first group in the regular
expression and call the supplied function.
|
default <T> void |
forEachMatchingFieldNameWithInteger(Pattern regex,
int groupNumber,
BiConsumer<String,Integer> function)
For all fields whose names match the given regular expression, extract an integer from the first group in the regular
expression and call the supplied function.
|
default <T> void |
forEachMatchingFieldNameWithInteger(String regex,
BiConsumer<String,Integer> function)
For all fields whose names match the given regular expression, extract an integer from the first group in the regular
expression and call the supplied function.
|
default <T> void |
forEachMatchingFieldNameWithInteger(String regex,
int groupNumber,
BiConsumer<String,Integer> function)
For all fields whose names match the given regular expression, extract an integer from the first group in the regular
expression and call the supplied function.
|
default <T> void |
forEachMatchingFieldNameWithString(Pattern regex,
int groupNumber,
BiConsumer<String,String> function)
For all fields whose names match the given regular expression, extract a string value from the first group in the regular
expression and call the supplied function.
|
default <T> void |
forEachMatchingFieldNameWithString(String regex,
BiConsumer<String,String> function)
For all fields whose names match the given regular expression, extract a string value from the first group in the regular
expression and call the supplied function.
|
default <T> void |
forEachMatchingFieldNameWithString(String regex,
int groupNumber,
BiConsumer<String,String> function)
For all fields whose names match the given regular expression, extract a string value from the first group in the regular
expression and call the supplied function.
|
static Configuration |
from(Map<String,String> properties)
Obtain a configuration instance by copying the supplied map of string keys and string values.
|
static Configuration |
from(Properties properties)
Obtain a configuration instance by copying the supplied Properties object.
|
static Configuration |
fromSystemProperties(String prefix)
Create a Configuration object that is populated by system properties, per
withSystemProperties(String). |
default boolean |
getBoolean(Field field)
Get the boolean value associated with the given field when that field has a default value.
|
default boolean |
getBoolean(Field field,
boolean defaultValue)
Get the boolean value associated with the given field, returning the field's default value if there is no such
key-value pair.
|
default Boolean |
getBoolean(Field field,
BooleanSupplier defaultValueSupplier)
Get the boolean value associated with the given key, using the given supplier to obtain a default value if there is no such
key-value pair.
|
default Boolean |
getBoolean(String key)
Get the boolean value associated with the given key.
|
default boolean |
getBoolean(String key,
boolean defaultValue)
Get the boolean value associated with the given key, returning the default value if there is no such key-value pair or
if the value could not be
parsed as a boolean value. |
default Boolean |
getBoolean(String key,
BooleanSupplier defaultValueSupplier)
Get the boolean value associated with the given key, using the given supplier to obtain a default value if there is no such
key-value pair.
|
default <T> T |
getInstance(Field field,
Class<T> clazz)
Get an instance of the class given by the value in the configuration associated with the given field.
|
default <T> T |
getInstance(Field field,
Class<T> type,
Supplier<ClassLoader> classloaderSupplier)
Get an instance of the class given by the value in the configuration associated with the given field.
|
default <T> T |
getInstance(String key,
Class<T> type)
Get an instance of the class given by the value in the configuration associated with the given key.
|
default <T> T |
getInstance(String key,
Class<T> type,
Supplier<ClassLoader> classloaderSupplier)
Get an instance of the class given by the value in the configuration associated with the given key.
|
default int |
getInteger(Field field)
Get the integer value associated with the given field, returning the field's default value if there is no such
key-value pair.
|
default int |
getInteger(Field field,
int defaultValue)
Get the integer value associated with the given field, returning the field's default value if there is no such
key-value pair.
|
default Integer |
getInteger(Field field,
IntSupplier defaultValueSupplier)
Get the integer value associated with the given key, using the given supplier to obtain a default value if there is no such
key-value pair.
|
default Integer |
getInteger(String key)
Get the integer value associated with the given key.
|
default int |
getInteger(String key,
int defaultValue)
Get the integer value associated with the given key, returning the default value if there is no such key-value pair or
if the value could not be
parsed as an integer. |
default Integer |
getInteger(String key,
IntSupplier defaultValueSupplier)
Get the integer value associated with the given key, using the given supplier to obtain a default value if there is no such
key-value pair.
|
default long |
getLong(Field field)
Get the long value associated with the given field, returning the field's default value if there is no such
key-value pair.
|
default long |
getLong(Field field,
long defaultValue)
Get the long value associated with the given field, returning the field's default value if there is no such
key-value pair.
|
default Long |
getLong(Field field,
LongSupplier defaultValueSupplier)
Get the long value associated with the given key, using the given supplier to obtain a default value if there is no such
key-value pair.
|
default Long |
getLong(String key)
Get the long value associated with the given key.
|
default long |
getLong(String key,
long defaultValue)
Get the long value associated with the given key, returning the default value if there is no such key-value pair or
if the value could not be
parsed as a long. |
default Long |
getLong(String key,
LongSupplier defaultValueSupplier)
Get the long value associated with the given key, using the given supplier to obtain a default value if there is no such
key-value pair.
|
default String |
getString(Field field)
Get the string value associated with the given field, returning the field's default value if there is no such key-value
pair in this configuration.
|
default String |
getString(Field field,
String defaultValue)
Get the string value associated with the given field, returning the field's default value if there is no such key-value
pair in this configuration.
|
default String |
getString(Field field,
Supplier<String> defaultValueSupplier)
Get the boolean value associated with the given key, using the given supplier to obtain a default value if there is no such
key-value pair.
|
String |
getString(String key)
Get the string value associated with the given key.
|
default String |
getString(String key,
String defaultValue)
Get the string value associated with the given key, returning the default value if there is no such key-value pair.
|
default String |
getString(String key,
Supplier<String> defaultValueSupplier)
Get the string value associated with the given key, returning the default value if there is no such key-value pair.
|
default List<String> |
getStrings(String key,
String regex)
Get the string value(s) associated with the given key, where the supplied regular expression is used to parse the single
string value into multiple values.
|
default boolean |
hasKey(String key)
Determine whether this configuration contains a key-value pair with the given key and the value is non-null
|
default boolean |
isEmpty()
Determine if this configuration is empty and has no properties.
|
Set<String> |
keys()
Get the set of keys in this configuration.
|
static Configuration |
load(File file)
Obtain a configuration instance by loading the Properties from the supplied file.
|
static Configuration |
load(InputStream stream)
Obtain a configuration instance by loading the Properties from the supplied stream.
|
static Configuration |
load(Reader reader)
Obtain a configuration instance by loading the Properties from the supplied reader.
|
static Configuration |
load(String path,
Class<?> clazz)
Obtain a configuration instance by loading the Properties from a file on the file system or classpath given by the supplied
path.
|
static Configuration |
load(String path,
ClassLoader classLoader)
Obtain a configuration instance by loading the Properties from a file on the file system or classpath given by the supplied
path.
|
static Configuration |
load(String path,
ClassLoader classLoader,
Consumer<String> logger)
Obtain a configuration instance by loading the Properties from a file on the file system or classpath given by the supplied
path.
|
static Configuration |
load(URL url)
Obtain a configuration instance by loading the Properties from the supplied URL.
|
default Configuration |
map(Function<String,String> mapper)
Return a new
Configuration that contains only the subset of keys that satisfy the given predicate. |
default Configuration |
subset(String prefix,
boolean removePrefix)
Return a new
Configuration that contains only the subset of keys that match the given prefix. |
default boolean |
validate(Field[] fields,
Consumer<String> problems)
Validate the supplied fields in this configuration.
|
default boolean |
validate(Iterable<Field> fields,
Consumer<String> problems)
Validate the supplied fields in this configuration.
|
default Configuration |
withSystemProperties(Function<String,String> propertyNameConverter)
Return a copy of this configuration except where acceptable system properties are used to overwrite properties copied from
this configuration.
|
default Configuration |
withSystemProperties(String prefix)
Return a copy of this configuration except where acceptable system properties are used to overwrite properties copied from
this configuration.
|
static Configuration.Builder create()
configuration builder.static Configuration.Builder copy(Configuration config)
configuration builder that starts with a copy of the supplied configuration.config - the configuration to copystatic Configuration fromSystemProperties(String prefix)
withSystemProperties(String).prefix - the required prefix for the system properties; may not be null but may be emptystatic Configuration empty()
static Configuration from(Properties properties)
Properties object is
copied so that the resulting Configuration cannot be modified.properties - the properties; may be null or emptystatic Configuration from(Map<String,String> properties)
properties - the properties; may be null or emptystatic Configuration load(URL url) throws IOException
url - the URL to the stream containing the configuration properties; may not be nullIOException - if there is an error reading the streamstatic Configuration load(File file) throws IOException
file - the file containing the configuration properties; may not be nullIOException - if there is an error reading the streamstatic Configuration load(InputStream stream) throws IOException
stream - the stream containing the properties; may not be nullIOException - if there is an error reading the streamstatic Configuration load(Reader reader) throws IOException
reader - the reader containing the properties; may not be nullIOException - if there is an error reading the streamstatic Configuration load(String path, Class<?> clazz) throws IOException
path - the path to the file containing the configuration properties; may not be nullclazz - the class whose classpath is to be used to find the file; may be nullIOException - if there is an error reading the streamstatic Configuration load(String path, ClassLoader classLoader) throws IOException
path - the path to the file containing the configuration properties; may not be nullclassLoader - the class loader to use; may be nullIOException - if there is an error reading the streamstatic Configuration load(String path, ClassLoader classLoader, Consumer<String> logger) throws IOException
path - the path to the file containing the configuration properties; may not be nullclassLoader - the class loader to use; may be nulllogger - the function that will be called with status updates; may be nullIOException - if there is an error reading the streamdefault Configuration.Builder edit()
default boolean hasKey(String key)
key - the keySet<String> keys()
String getString(String key)
key - the key for the configuration propertydefault String getString(String key, String defaultValue)
key - the key for the configuration propertydefaultValue - the value that should be returned by default if there is no such key-value pair in the configuration;
may be nulldefaultValue if there is no such key-value pair in the configurationdefault String getString(String key, Supplier<String> defaultValueSupplier)
key - the key for the configuration propertydefaultValueSupplier - the supplier of value that should be returned by default if there is no such key-value pair in
the configuration; may be null and may return nulldefaultValue if there is no such key-value pair in the configurationdefault String getString(Field field)
field - the field; may not be nulldefault value if there is no
such key-value pair in the configurationdefault String getString(Field field, String defaultValue)
field - the field; may not be nulldefaultValue - the default valuedefault value if there is no
such key-value pair in the configurationdefault List<String> getStrings(String key, String regex)
key - the key for the configuration propertyregex - the delimiting regular expressionString.split(String)default Integer getInteger(String key)
key - the key for the configuration propertydefault Long getLong(String key)
key - the key for the configuration propertydefault Boolean getBoolean(String key)
key - the key for the configuration propertydefault int getInteger(String key, int defaultValue)
parsed as an integer.key - the key for the configuration propertydefaultValue - the default valuedefault long getLong(String key, long defaultValue)
parsed as a long.key - the key for the configuration propertydefaultValue - the default valuedefault boolean getBoolean(String key, boolean defaultValue)
parsed as a boolean value.key - the key for the configuration propertydefaultValue - the default valuedefault Integer getInteger(String key, IntSupplier defaultValueSupplier)
key - the key for the configuration propertydefaultValueSupplier - the supplier for the default value; may be nulldefaultValueSupplier reference is null, or there is a key-value pair in the configuration but the value
could not be parsed as an integerdefault Long getLong(String key, LongSupplier defaultValueSupplier)
key - the key for the configuration propertydefaultValueSupplier - the supplier for the default value; may be nulldefaultValueSupplier reference is null, or there is a key-value pair in the configuration but the value
could not be parsed as a longdefault Boolean getBoolean(String key, BooleanSupplier defaultValueSupplier)
key - the key for the configuration propertydefaultValueSupplier - the supplier for the default value; may be nulldefaultValueSupplier reference is null, or there is a key-value pair in the configuration but the value
could not be parsed as a boolean valuedefault int getInteger(Field field)
field - the fieldNumberFormatException - if there is no name-value pair and the field has no default valuedefault long getLong(Field field)
field - the fieldNumberFormatException - if there is no name-value pair and the field has no default valuedefault boolean getBoolean(Field field)
field - the fieldNumberFormatException - if there is no name-value pair and the field has no default valuedefault int getInteger(Field field, int defaultValue)
field - the fielddefaultValue - the default valuedefault long getLong(Field field, long defaultValue)
field - the fielddefaultValue - the default valuedefault boolean getBoolean(Field field, boolean defaultValue)
field - the fielddefaultValue - the default valuedefault Integer getInteger(Field field, IntSupplier defaultValueSupplier)
field - the fielddefaultValueSupplier - the supplier for the default value; may be nulldefaultValueSupplier reference is null, or there is a key-value pair in the configuration but the value
could not be parsed as an integerdefault Long getLong(Field field, LongSupplier defaultValueSupplier)
field - the fielddefaultValueSupplier - the supplier for the default value; may be nulldefaultValueSupplier reference is null, or there is a key-value pair in the configuration but the value
could not be parsed as a longdefault Boolean getBoolean(Field field, BooleanSupplier defaultValueSupplier)
field - the fielddefaultValueSupplier - the supplier for the default value; may be nulldefaultValueSupplier reference is null, or there is a key-value pair in the configuration but the value
could not be parsed as a boolean valuedefault String getString(Field field, Supplier<String> defaultValueSupplier)
field - the fielddefaultValueSupplier - the supplier of value that should be returned by default if there is no such key-value pair in
the configuration; may be null and may return nulldefaultValue if there is no such key-value pair in the configurationdefault <T> T getInstance(String key, Class<T> type)
key - the key for the configuration propertytype - the Class of which the resulting object is expected to be an instance of; may not be nulldefault <T> T getInstance(String key, Class<T> type, Supplier<ClassLoader> classloaderSupplier)
key - the key for the configuration propertytype - the Class of which the resulting object is expected to be an instance of; may not be nullclassloaderSupplier - the supplier of the ClassLoader to be used to load the resulting class; may be null if this
class' ClassLoader should be useddefault <T> T getInstance(Field field, Class<T> clazz)
field - the field for the configuration propertyclazz - the Class of which the resulting object is expected to be an instance of; may not be nulldefault <T> T getInstance(Field field, Class<T> type, Supplier<ClassLoader> classloaderSupplier)
field - the field for the configuration propertytype - the Class of which the resulting object is expected to be an instance of; may not be nullclassloaderSupplier - the supplier of the ClassLoader to be used to load the resulting class; may be null if this
class' ClassLoader should be useddefault Configuration subset(String prefix, boolean removePrefix)
Configuration that contains only the subset of keys that match the given prefix.
If desired, the keys in the resulting Configuration will have the prefix (plus any terminating "." character if
needed) removed.
This method returns this Configuration instance if the supplied prefix is null or empty.
prefix - the prefixremovePrefix - true if the prefix (and any subsequent "." character) should be removed from the keys in the
resulting Configuration, or false if the keys in this Configuration should be used as-is in the resulting
Configurationdefault Configuration map(Function<String,String> mapper)
Configuration that contains only the subset of keys that satisfy the given predicate.mapper - that function that transforms keysdefault Configuration filter(Predicate<? super String> matcher)
Configuration that contains only the subset of keys that satisfy the given predicate.matcher - the function that determines whether a key should be included in the subsetdefault boolean isEmpty()
true if empty, or false otherwisedefault Properties asProperties()
default Map<String,String> asMap()
default Configuration withSystemProperties(String prefix)
_') are replaced with a
period ('.').prefix - the required prefix for the system propertiesdefault Configuration withSystemProperties(Function<String,String> propertyNameConverter)
propertyNameConverter - the function that will convert the name of each system property to an applicable property name
(or null if the system property name does not apply); may not be nulldefault boolean validate(Iterable<Field> fields, Consumer<String> problems)
fields parameter
are not validated.fields - the fieldsproblems - the consumer to be called with each problem; never nulltrue if the value is considered valid, or false if it is not validdefault boolean validate(Field[] fields, Consumer<String> problems)
fields parameter
are not validated.fields - the fieldsproblems - the consumer to be called with each problem; never nulltrue if the value is considered valid, or false if it is not validdefault void forEachMatchingFieldName(String regex, BiConsumer<String,String> function)
regex - the regular expression string; may not be nullfunction - the consumer that takes the name and value of matching fields; may not be nulldefault void forEachMatchingFieldName(Pattern regex, BiConsumer<String,String> function)
regex - the regular expression string; may not be nullfunction - the consumer that takes the name and value of matching fields; may not be nulldefault <T> void forEachMatchingFieldNameWithInteger(String regex, BiConsumer<String,Integer> function)
regex - the regular expression string; may not be nullfunction - the consumer that takes the value of matching field and the integer extracted from the field name; may not
be nulldefault <T> void forEachMatchingFieldNameWithInteger(String regex, int groupNumber, BiConsumer<String,Integer> function)
regex - the regular expression string; may not be nullgroupNumber - the number of the regular expression group containing the integer to be extracted; must be positivefunction - the consumer that takes the value of matching field and the integer extracted from the field name; may not
be nulldefault <T> void forEachMatchingFieldNameWithInteger(Pattern regex, int groupNumber, BiConsumer<String,Integer> function)
regex - the regular expression string; may not be nullgroupNumber - the number of the regular expression group containing the integer to be extracted; must be positivefunction - the consumer that takes the value of matching field and the integer extracted from the field name; may not
be nulldefault <T> void forEachMatchingFieldNameWithBoolean(String regex, BiConsumer<String,Boolean> function)
regex - the regular expression string; may not be nullfunction - the consumer that takes the value of matching field and the boolean extracted from the field name; may not
be nulldefault <T> void forEachMatchingFieldNameWithBoolean(String regex, int groupNumber, BiConsumer<String,Boolean> function)
regex - the regular expression string; may not be nullgroupNumber - the number of the regular expression group containing the boolean to be extracted; must be positivefunction - the consumer that takes the value of matching field and the boolean extracted from the field name; may not
be nulldefault <T> void forEachMatchingFieldNameWithBoolean(Pattern regex, int groupNumber, BiConsumer<String,Boolean> function)
regex - the regular expression string; may not be nullgroupNumber - the number of the regular expression group containing the boolean to be extracted; must be positivefunction - the consumer that takes the value of matching field and the boolean extracted from the field name; may not
be nulldefault <T> void forEachMatchingFieldNameWithString(String regex, BiConsumer<String,String> function)
regex - the regular expression string; may not be nullfunction - the consumer that takes the value of matching field and the string value extracted from the field name; may
not be nulldefault <T> void forEachMatchingFieldNameWithString(String regex, int groupNumber, BiConsumer<String,String> function)
regex - the regular expression string; may not be nullgroupNumber - the number of the regular expression group containing the string value to be extracted; must be positivefunction - the consumer that takes the value of matching field and the string value extracted from the field name; may
not be nulldefault <T> void forEachMatchingFieldNameWithString(Pattern regex, int groupNumber, BiConsumer<String,String> function)
regex - the regular expression string; may not be nullgroupNumber - the number of the regular expression group containing the string value to be extracted; must be positivefunction - the consumer that takes the value of matching field and the string value extracted from the field name; may
not be nulldefault <T> void forEachMatchingFieldName(String regex, int groupNumber, BiFunction<String,String,T> groupExtractor, BiConsumer<String,T> function)
regex - the regular expression string; may not be nullgroupNumber - the number of the regular expression group containing the string value to be extracted; must be positivegroupExtractor - the function that extracts the value from the groupfunction - the consumer that takes the value of matching field and the value extracted from the field name; may
not be nulldefault <T> void forEachMatchingFieldName(Pattern regex, int groupNumber, BiFunction<String,String,T> groupExtractor, BiConsumer<String,T> function)
regex - the regular expression string; may not be nullgroupNumber - the number of the regular expression group containing the string value to be extracted; must be positivegroupExtractor - the function that extracts the value from the groupfunction - the consumer that takes the value of matching field and the value extracted from the field name; may
not be nullCopyright © 2016 JBoss by Red Hat. All rights reserved.