Interface Configuration
- All Known Subinterfaces:
JdbcConfiguration
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.
- Author:
- Randall Hauch
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic class
A builder of Configuration objects.static interface
Configuration.ConfigBuilder<C extends Configuration,
B extends Configuration.ConfigBuilder<C, B>> The basic interface for configuration builders. -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionasMap()
Get a copy of these configuration properties as a Properties object.Get a copy of these configuration properties with defaults as a Map.default Properties
Get a copy of these configuration properties as a Properties object.default Properties
asProperties
(Field.Set fields) Get a copy of these configuration properties as a Properties object.static Configuration.Builder
copy
(Configuration config) Create a newconfiguration builder
that starts with a copy of the supplied configuration.static Configuration.Builder
create()
Create a newconfiguration builder
.default Configuration.Builder
edit()
Obtain an editor for a copy of this configuration.static Configuration
empty()
Obtain an empty configuration.default Configuration
Return a newConfiguration
that contains only the subset of keys that satisfy the given predicate.default <T> void
forEach
(BiConsumer<String, String> function) Call the supplied function for each of the fields.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 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
(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
(Pattern regex, BiConsumer<String, String> function) Apply the given function to all fields whose names match the given regular expression.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
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
(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
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
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
(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
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.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
(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.static Configuration
Obtain a configuration instance by copying the supplied map of string keys and object values.static <T> Configuration
Obtain a configuration instance by copying the supplied map of string keys and object 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, perwithSystemProperties(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 beparsed
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 Duration
getDuration
(Field field, TemporalUnit unit) Gets the duration value associated with the given key.default <T> T
getInstance
(Field field, Class<T> type) 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> clazz, Configuration configuration) 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> clazz, Properties props) 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> clazz, Configuration configuration) 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 beparsed
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 <T> List<T>
default <T> List<T>
default long
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
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
Get the long value associated with the given key.default long
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 beparsed
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 Number
Get the numeric value associated with the given field, returning the field's default value if there is no such key-value pair.default Number
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 String
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
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
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.Get the string value associated with the given key.default String
Get the string value associated with the given key, returning the default value if there is no such key-value pair.default String
Get the string value associated with the given key, returning the default value if there is no such key-value pair.getStrings
(Field field, 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.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.getTrimmedStrings
(Field field, 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
Determine whether this configuration contains a key-value pair associated with the given field and the value is non-null.default boolean
Determine whether this configuration contains a key-value pair with the given key and the value is non-nulldefault boolean
isEmpty()
Determine if this configuration is empty and has no properties.keys()
Get the set of keys in this configuration.static Configuration
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
Obtain a configuration instance by loading the Properties from the supplied reader.static Configuration
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
Obtain a configuration instance by loading the Properties from the supplied URL.default Configuration
Return a newConfiguration
that contains only the subset of keys that satisfy the given predicate.default Configuration
mapped
(BiFunction<? super String, ? super String, String> mapper) Return a newConfiguration
that contains the mapped values.default Configuration
merge
(Configuration... configs) Return a newConfiguration
that merges severalConfiguration
s into one.default Configuration
Return a newConfiguration
that contains only the subset of keys that match the given prefix.Validate the supplied fields in this configuration.default boolean
validate
(Iterable<Field> fields, Field.ValidationOutput problems) Validate the supplied fields in this configuration.default boolean
validateAndRecord
(Iterable<Field> fields, Consumer<String> problems) Validate the supplied fields in this configuration.default Configuration
withMasked
(String keyRegex) Return a newConfiguration
that contains all of the same fields as this configuration, except with masked values for all keys that match the specified pattern.default Configuration
withMasked
(Pattern keyRegex) Return a newConfiguration
that contains all of the same fields as this configuration, except with masked values for all keys that match the specified pattern.default Configuration
Return a newConfiguration
that contains all of the same fields as this configuration, except with masked values for all keys that end in "password".default Configuration
withReplacedVariables
(Function<String, String> valuesByVariableName) Return a newConfiguration
that contains all of the same fields as this configuration, except with all variables in the fields replaced with values from the supplied function.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.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.
-
Field Details
-
CONFIGURATION_LOGGER
static final org.slf4j.Logger CONFIGURATION_LOGGER -
PASSWORD_PATTERN
-
-
Method Details
-
create
Create a newconfiguration builder
.- Returns:
- the configuration builder
-
copy
Create a newconfiguration builder
that starts with a copy of the supplied configuration.- Parameters:
config
- the configuration to copy; may be null- Returns:
- the configuration builder
-
fromSystemProperties
Create a Configuration object that is populated by system properties, perwithSystemProperties(String)
.- Parameters:
prefix
- the required prefix for the system properties; may not be null but may be empty- Returns:
- the configuration
-
empty
Obtain an empty configuration.- Returns:
- an empty configuration; never null
-
from
Obtain a configuration instance by copying the supplied Properties object. The suppliedProperties
object is copied so that the resulting Configuration cannot be modified.- Parameters:
properties
- the properties; may be null or empty- Returns:
- the configuration; never null
-
from
Obtain a configuration instance by copying the supplied map of string keys and object values. The entries within the map are copied so that the resulting Configuration cannot be modified.- Parameters:
properties
- the properties; may be null or empty- Returns:
- the configuration; never null
-
from
Obtain a configuration instance by copying the supplied map of string keys and object values. The entries within the map are copied so that the resulting Configuration cannot be modified.- Parameters:
properties
- the properties; may be null or emptyconversion
- the function that converts the supplied values into strings, or returnsnull
if the value is to be excluded- Returns:
- the configuration; never null
-
load
Obtain a configuration instance by loading the Properties from the supplied URL.- Parameters:
url
- the URL to the stream containing the configuration properties; may not be null- Returns:
- the configuration; never null
- Throws:
IOException
- if there is an error reading the stream
-
load
Obtain a configuration instance by loading the Properties from the supplied file.- Parameters:
file
- the file containing the configuration properties; may not be null- Returns:
- the configuration; never null
- Throws:
IOException
- if there is an error reading the stream
-
load
Obtain a configuration instance by loading the Properties from the supplied stream.- Parameters:
stream
- the stream containing the properties; may not be null- Returns:
- the configuration; never null
- Throws:
IOException
- if there is an error reading the stream
-
load
Obtain a configuration instance by loading the Properties from the supplied reader.- Parameters:
reader
- the reader containing the properties; may not be null- Returns:
- the configuration; never null
- Throws:
IOException
- if there is an error reading the stream
-
load
Obtain a configuration instance by loading the Properties from a file on the file system or classpath given by the supplied path.- Parameters:
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 null- Returns:
- the configuration; never null but possibly empty
- Throws:
IOException
- if there is an error reading the stream
-
load
Obtain a configuration instance by loading the Properties from a file on the file system or classpath given by the supplied path.- Parameters:
path
- the path to the file containing the configuration properties; may not be nullclassLoader
- the class loader to use; may be null- Returns:
- the configuration; never null but possibly empty
- Throws:
IOException
- if there is an error reading the stream
-
load
static Configuration load(String path, ClassLoader classLoader, Consumer<String> logger) throws IOException Obtain a configuration instance by loading the Properties from a file on the file system or classpath given by the supplied path.- Parameters:
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 null- Returns:
- the configuration; never null but possibly empty
- Throws:
IOException
- if there is an error reading the stream
-
edit
Obtain an editor for a copy of this configuration.- Returns:
- a builder that is populated with this configuration's key-value pairs; never null
-
hasKey
Determine whether this configuration contains a key-value pair with the given key and the value is non-null- Parameters:
key
- the key- Returns:
- true if the configuration contains the key, or false otherwise
-
hasKey
Determine whether this configuration contains a key-value pair associated with the given field and the value is non-null.- Parameters:
field
- the field; may not be null- Returns:
- true if the configuration contains the key, or false otherwise
-
keys
Get the set of keys in this configuration.- Returns:
- the set of keys; never null but possibly empty
-
getString
Get the string value associated with the given key.- Parameters:
key
- the key for the configuration property- Returns:
- the value, or null if the key is null or there is no such key-value pair in the configuration
-
getList
-
getList
-
getList
-
getList
-
getString
Get the string value associated with the given key, returning the default value if there is no such key-value pair.- Parameters:
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 null- Returns:
- the configuration value, or the
defaultValue
if there is no such key-value pair in the configuration
-
getString
Get the string value associated with the given key, returning the default value if there is no such key-value pair.- Parameters:
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 null- Returns:
- the configuration value, or the
defaultValue
if there is no such key-value pair in the configuration
-
getString
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.- Parameters:
field
- the field; may not be null- Returns:
- the configuration's value for the field, or the field's
default value
if there is no such key-value pair in the configuration
-
getString
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.- Parameters:
field
- the field; may not be nulldefaultValue
- the default value- Returns:
- the configuration's value for the field, or the field's
default value
if there is no such key-value pair in the configuration
-
getStrings
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.- Parameters:
field
- the field; may not be nullregex
- the delimiting regular expression- Returns:
- the list of string values; null only if there is no such key-value pair in the configuration
- See Also:
-
getStrings
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.- Parameters:
key
- the key for the configuration propertyregex
- the delimiting regular expression- Returns:
- the list of string values; null only if there is no such key-value pair in the configuration
- See Also:
-
getTrimmedStrings
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. In addition, all values will be trimmed.- Parameters:
field
- the field for the configuration propertyregex
- the delimiting regular expression- Returns:
- the list of string values; null only if there is no such key-value pair in the configuration
- See Also:
-
getInteger
Get the integer value associated with the given key.- Parameters:
key
- the key for the configuration property- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration, or the value could not be parsed as an integer
-
getLong
Get the long value associated with the given key.- Parameters:
key
- the key for the configuration property- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration, or the value could not be parsed as an integer
-
getBoolean
Get the boolean value associated with the given key.- Parameters:
key
- the key for the configuration property- Returns:
- the boolean value, or null if the key is null, there is no such key-value pair in the configuration, or the value could not be parsed as a boolean value
-
getInteger
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 beparsed
as an integer.- Parameters:
key
- the key for the configuration propertydefaultValue
- the default value- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration, or the value could not be parsed as an integer
-
getLong
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 beparsed
as a long.- Parameters:
key
- the key for the configuration propertydefaultValue
- the default value- Returns:
- the long value, or null if the key is null, there is no such key-value pair in the configuration, or the value could not be parsed as a long
-
getBoolean
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 beparsed
as a boolean value.- Parameters:
key
- the key for the configuration propertydefaultValue
- the default value- Returns:
- the boolean value, or null if the key is null, there is no such key-value pair in the configuration, or the value could not be parsed as a boolean value
-
getNumber
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.- Parameters:
key
- the key for the configuration propertydefaultValueSupplier
- the supplier for the default value; may be null- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration, the
defaultValueSupplier
reference is null, or there is a key-value pair in the configuration but the value could not be parsed as an integer
-
getInteger
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.- Parameters:
key
- the key for the configuration propertydefaultValueSupplier
- the supplier for the default value; may be null- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration, the
defaultValueSupplier
reference is null, or there is a key-value pair in the configuration but the value could not be parsed as an integer
-
getLong
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.- Parameters:
key
- the key for the configuration propertydefaultValueSupplier
- the supplier for the default value; may be null- Returns:
- the long value, or null if the key is null, there is no such key-value pair in the configuration, the
defaultValueSupplier
reference is null, or there is a key-value pair in the configuration but the value could not be parsed as a long
-
getBoolean
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.- Parameters:
key
- the key for the configuration propertydefaultValueSupplier
- the supplier for the default value; may be null- Returns:
- the boolean value, or null if the key is null, there is no such key-value pair in the configuration, the
defaultValueSupplier
reference is null, or there is a key-value pair in the configuration but the value could not be parsed as a boolean value
-
getNumber
Get the numeric value associated with the given field, returning the field's default value if there is no such key-value pair.- Parameters:
field
- the field- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration and there is no default value in the field or the default value could not be parsed as a long, or there is a key-value pair in the configuration but the value could not be parsed as an integer value
- Throws:
NumberFormatException
- if there is no name-value pair and the field has no default value
-
getInteger
Get the integer value associated with the given field, returning the field's default value if there is no such key-value pair.- Parameters:
field
- the field- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration and there is no default value in the field or the default value could not be parsed as a long, or there is a key-value pair in the configuration but the value could not be parsed as an integer value
- Throws:
NumberFormatException
- if there is no name-value pair and the field has no default value
-
getLong
Get the long value associated with the given field, returning the field's default value if there is no such key-value pair.- Parameters:
field
- the field- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration and there is no default value in the field or the default value could not be parsed as a long, or there is a key-value pair in the configuration but the value could not be parsed as a long value
- Throws:
NumberFormatException
- if there is no name-value pair and the field has no default value
-
getBoolean
Get the boolean value associated with the given field when that field has a default value. If the configuration does not have a name-value pair with the same name as the field, then the field's default value.- Parameters:
field
- the field- Returns:
- the boolean value, or null if the key is null, there is no such key-value pair in the configuration and there is no default value in the field or the default value could not be parsed as a long, or there is a key-value pair in the configuration but the value could not be parsed as a boolean value
- Throws:
NumberFormatException
- if there is no name-value pair and the field has no default value
-
getInteger
Get the integer value associated with the given field, returning the field's default value if there is no such key-value pair.- Parameters:
field
- the fielddefaultValue
- the default value- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration and there is no default value in the field or the default value could not be parsed as a long, or there is a key-value pair in the configuration but the value could not be parsed as an integer value
-
getLong
Get the long value associated with the given field, returning the field's default value if there is no such key-value pair.- Parameters:
field
- the fielddefaultValue
- the default value- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration and there is no default value in the field or the default value could not be parsed as a long, or there is a key-value pair in the configuration but the value could not be parsed as a long value
-
getBoolean
Get the boolean value associated with the given field, returning the field's default value if there is no such key-value pair.- Parameters:
field
- the fielddefaultValue
- the default value- Returns:
- the boolean value, or null if the key is null, there is no such key-value pair in the configuration and there is no default value in the field or the default value could not be parsed as a long, or there is a key-value pair in the configuration but the value could not be parsed as a boolean value
-
getInteger
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.- Parameters:
field
- the fielddefaultValueSupplier
- the supplier for the default value; may be null- Returns:
- the integer value, or null if the key is null, there is no such key-value pair in the configuration, the
defaultValueSupplier
reference is null, or there is a key-value pair in the configuration but the value could not be parsed as an integer
-
getLong
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.- Parameters:
field
- the fielddefaultValueSupplier
- the supplier for the default value; may be null- Returns:
- the long value, or null if the key is null, there is no such key-value pair in the configuration, the
defaultValueSupplier
reference is null, or there is a key-value pair in the configuration but the value could not be parsed as a long
-
getBoolean
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.- Parameters:
field
- the fielddefaultValueSupplier
- the supplier for the default value; may be null- Returns:
- the boolean value, or null if the key is null, there is no such key-value pair in the configuration, the
defaultValueSupplier
reference is null, or there is a key-value pair in the configuration but the value could not be parsed as a boolean value
-
getString
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.- Parameters:
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 null- Returns:
- the configuration value, or the
defaultValue
if there is no such key-value pair in the configuration
-
getDuration
Gets the duration value associated with the given key.- Parameters:
field
- the fieldunit
- the temporal unit of the duration value- Returns:
- the duration value associated with the given key
-
getInstance
Get an instance of the class given by the value in the configuration associated with the given key.- Parameters:
key
- the key for the configuration propertytype
- the Class of which the resulting object is expected to be an instance of; may not be null- Returns:
- the new instance, or null if there is no such key-value pair in the configuration or if there is a key-value configuration but the value could not be converted to an existing class with a zero-argument constructor
-
getInstance
Get an instance of the class given by the value in the configuration associated with the given key. The instance is created usingInstance(Configuration)
constructor.- Parameters:
key
- the key for the configuration propertyclazz
- the Class of which the resulting object is expected to be an instance of; may not be nullconfiguration
-Configuration
object that is passed as a parameter to the constructor- Returns:
- the new instance, or null if there is no such key-value pair in the configuration or if there is a key-value configuration but the value could not be converted to an existing class with a zero-argument constructor
-
getInstance
Get an instance of the class given by the value in the configuration associated with the given field.- Parameters:
field
- the field for the configuration propertytype
- the Class of which the resulting object is expected to be an instance of; may not be null- Returns:
- the new instance, or null if there is no such key-value pair in the configuration or if there is a key-value configuration but the value could not be converted to an existing class with a zero-argument constructor
-
getInstance
Get an instance of the class given by the value in the configuration associated with the given field. The instance is created usingInstance(Configuration)
constructor.- Parameters:
field
- the field for the configuration propertyclazz
- the Class of which the resulting object is expected to be an instance of; may not be nullconfiguration
- theConfiguration
object that is passed as a parameter to the constructor- Returns:
- the new instance, or null if there is no such key-value pair in the configuration or if there is a key-value configuration but the value could not be converted to an existing class with a zero-argument constructor
-
getInstance
Get an instance of the class given by the value in the configuration associated with the given field. The instance is created usingInstance(Configuration)
constructor.- Parameters:
field
- the field for the configuration propertyclazz
- the Class of which the resulting object is expected to be an instance of; may not be nullprops
- theProperties
object that is passed as a parameter to the constructor- Returns:
- the new instance, or null if there is no such key-value pair in the configuration or if there is a key-value configuration but the value could not be converted to an existing class with a zero-argument constructor
-
subset
Return a newConfiguration
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.- Parameters:
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 Configuration- Returns:
- the subset of this Configuration; never null
-
merge
Return a newConfiguration
that merges severalConfiguration
s into one.This method returns this Configuration instance if the supplied
configs
is null or empty.- Parameters:
configs
- Configurations to be merged- Returns:
- the subset of this Configuration; never null
-
map
Return a newConfiguration
that contains only the subset of keys that satisfy the given predicate.- Parameters:
mapper
- that function that transforms keys- Returns:
- the subset Configuration; never null
-
filter
Return a newConfiguration
that contains only the subset of keys that satisfy the given predicate.- Parameters:
matcher
- the function that determines whether a key should be included in the subset- Returns:
- the subset Configuration; never null
-
mapped
Return a newConfiguration
that contains the mapped values.- Parameters:
mapper
- the function that takes a key and value and returns the new mapped value- Returns:
- the Configuration with mapped values; never null
-
withReplacedVariables
Return a newConfiguration
that contains all of the same fields as this configuration, except with all variables in the fields replaced with values from the supplied function. Variables found in fields will be left as-is if the supplied function returns no value for the variable name(s).Variables may appear anywhere within a field value, and multiple variables can be used within the same field. Variables take the form:
variable := '${' variableNames [ ':' defaultValue ] '}' variableNames := variableName [ ',' variableNames ] variableName := // any characters except ',' and ':' and '}' defaultValue := // any characters except '}'
and examples of variables include:${var1}
${var1:defaultValue}
${var1,var2}
${var1,var2:defaultValue}
- Parameters:
valuesByVariableName
- the function that returns a variable value for a variable name; may not be null but may return null if the variable name is not known- Returns:
- the Configuration with masked values for matching keys; never null
-
withMaskedPasswords
Return a newConfiguration
that contains all of the same fields as this configuration, except with masked values for all keys that end in "password".- Returns:
- the Configuration with masked values for matching keys; never null
-
withMasked
Return a newConfiguration
that contains all of the same fields as this configuration, except with masked values for all keys that match the specified pattern.- Parameters:
keyRegex
- the regular expression to match against the keys- Returns:
- the Configuration with masked values for matching keys; never null
-
withMasked
Return a newConfiguration
that contains all of the same fields as this configuration, except with masked values for all keys that match the specified pattern.- Parameters:
keyRegex
- the regular expression to match against the keys- Returns:
- the Configuration with masked values for matching keys; never null
-
isEmpty
default boolean isEmpty()Determine if this configuration is empty and has no properties.- Returns:
true
if empty, orfalse
otherwise
-
asProperties
Get a copy of these configuration properties as a Properties object.- Returns:
- the properties object; never null
-
asProperties
Get a copy of these configuration properties as a Properties object.- Parameters:
fields
- the fields defining the defaults; may be null- Returns:
- the properties object; never null
-
asMap
Get a copy of these configuration properties as a Properties object.- Returns:
- the properties object; never null
-
asMap
Get a copy of these configuration properties with defaults as a Map.- Parameters:
fields
- the fields defining the defaults; may be null- Returns:
- the properties object; never null
-
withSystemProperties
Return a copy of this configuration except where acceptable system properties are used to overwrite properties copied from this configuration. All system properties whose name has the given prefix are added, where the prefix is removed from the system property name, it is converted to lower case, and each underscore character ('_
') are replaced with a period ('.
').- Parameters:
prefix
- the required prefix for the system properties- Returns:
- the resulting properties converted from the system properties; never null, but possibly empty
-
withSystemProperties
Return a copy of this configuration except where acceptable system properties are used to overwrite properties copied from this configuration. Each of the system properties is examined and passed to the supplied function; if the result of the function is a non-null string, then a property with that string as the name and the system property value are added to the returned configuration.- Parameters:
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 null- Returns:
- the resulting properties filtered from the input properties; never null, but possibly empty
-
validate
Validate the supplied fields in this configuration. Extra fields not described by the suppliedfields
parameter are not validated.- Parameters:
fields
- the fieldsproblems
- the consumer to be called with each problem; never null- Returns:
true
if the value is considered valid, orfalse
if it is not valid
-
validateAndRecord
Validate the supplied fields in this configuration. Extra fields not described by the suppliedfields
parameter are not validated.- Parameters:
fields
- the fieldsproblems
- the consumer to be called with each problem; never null- Returns:
true
if the value is considered valid, orfalse
if it is not valid
-
validate
Validate the supplied fields in this configuration. Extra fields not described by the suppliedfields
parameter are not validated.- Parameters:
fields
- the fields- Returns:
- the
ConfigValue
for each of the fields; never null
-
forEachMatchingFieldName
Apply the given function to all fields whose names match the given regular expression.- Parameters:
regex
- the regular expression string; may not be nullfunction
- the consumer that takes the name and value of matching fields; may not be null
-
forEachMatchingFieldName
Apply the given function to all fields whose names match the given regular expression.- Parameters:
regex
- the regular expression string; may not be nullfunction
- the consumer that takes the name and value of matching fields; may not be null
-
forEachMatchingFieldNameWithInteger
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.- Parameters:
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 null
-
forEachMatchingFieldNameWithInteger
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.- Parameters:
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 null
-
forEachMatchingFieldNameWithInteger
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.- Parameters:
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 null
-
forEachMatchingFieldNameWithBoolean
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.- Parameters:
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 null
-
forEachMatchingFieldNameWithBoolean
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.- Parameters:
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 null
-
forEachMatchingFieldNameWithBoolean
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.- Parameters:
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 null
-
forEachMatchingFieldNameWithString
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.- Parameters:
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 null
-
forEachMatchingFieldNameWithString
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.- Parameters:
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 null
-
forEachMatchingFieldNameWithString
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.- Parameters:
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 null
-
forEachMatchingFieldName
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.- Parameters:
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 null
-
forEachMatchingFieldName
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.- Parameters:
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 null
-
forEach
Call the supplied function for each of the fields.- Parameters:
function
- the consumer that takes the field name and the string value extracted from the field; may not be null
-