Represents a collection of
KeyValue
entries with various utility methods for
manipulation, transformation, and expansion. This interface serves as a central point
for managing key-value pairs, allowing operations such as filtering, mapping, and
interpolation.
KeyValues is basically a glorified Supplier<Stream<KeyValue>>
. If the key
values will be streamed multiple times it is recommended to call memoize()
which will copy the key values if the key values are not already memoized.
Key Features:
- Provides methods for building key-value pairs with
KeyValues.Builder
. - Supports functional transformations using
map(UnaryOperator)
,filter(Predicate)
, andflatMap(Function)
. - Allows interpolation and expansion of values using variable substitution.
- Can convert key-values to a
Map
representation.
Usage Example:
The following example demonstrates how to create, expand, and transform key-values:// Create a builder and add key-value pairs
KeyValues.Builder builder = KeyValues.builder();
builder.add("key1", "value1");
builder.add("key2", "${key1}-${key3}");
// Build the KeyValues collection
KeyValues kvs = builder.build();
// Interpolate values using a Variables map
Variables variables = Variables.builder().add("key1", "interpolated").add("key3", "value3").build();
KeyValues expanded = kvs.expand(variables);
// Convert to a map
Map<String, String> map = expanded.toMap();
System.out.println(map); // {key1=value1, key2=value1-value3}
- See Also:
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic final class
A builder for constructingKeyValues
instances from key-value pairs. -
Method Summary
Modifier and TypeMethodDescriptionstatic KeyValues.Builder
builder()
Creates a newKeyValues.Builder
with a default, empty source.static KeyValues.Builder
builder
(KeyValuesResource resource) Creates a newKeyValues.Builder
for constructing aKeyValues
instance.Collects a stream ofKeyValue
into aKeyValues
instance.collector
(KeyValues.Builder builder) Collects a stream ofMap.Entry
into aKeyValues
using the provided builder.static KeyValues
Creates aKeyValues
instance by copying the given collection.static KeyValues
empty()
Returns an emptyKeyValues
instance.default KeyValues
Expands the key-values using variable interpolation and return a new key values where all the value parts of the keys are replaced with the interpolation results.default KeyValues
Filters the key-value pairs based on a predicate.default KeyValues
Flattens the key-values by applying a mapping function that returns anotherKeyValues
.default String
format
(KeyValuesMedia media) A convenience method to format key values to a String mainly for unit testing to maintain fluent chain.interpolate
(Variables variables) Interpolates the values using the providedVariables
.iterator()
last()
This method gets the last element of the key values stream.default KeyValues
map
(UnaryOperator<KeyValue> kv) Applies a transformation function to each key-value pair in the collection.default KeyValues
memoize()
Returns a memoized version of thisKeyValues
which means repeated calls to iteratore or stream over the KeyValues will always generate the same result.static KeyValues
Creates a single key values instance.default KeyValues
redact()
Redacts sensitive key-value entries by replacing their values.stream()
Returns a stream of the containedKeyValue
entries.default SequencedMap
<String, String> toMap()
Converts the key-values to a map where each key maps to its expanded value.Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
stream
Returns a stream of the containedKeyValue
entries.- Returns:
- a
Stream
of key-value pairs.
-
builder
Creates a newKeyValues.Builder
for constructing aKeyValues
instance.- Parameters:
resource
- aKeyValuesResource
defining the source URI and reference key-value.- Returns:
- a new
KeyValues.Builder
.
-
builder
Creates a newKeyValues.Builder
with a default, empty source.- Returns:
- a new
KeyValues.Builder
.
-
empty
Returns an emptyKeyValues
instance.- Returns:
- an empty
KeyValues
collection.
-
copyOf
Creates aKeyValues
instance by copying the given collection.- Parameters:
kvs
- aSequencedCollection
of key-value pairs.- Returns:
- a new
KeyValues
instance containing the provided key-values.
-
of
Creates a single key values instance.- Parameters:
keyValue
- single key value.- Returns:
- keyvalues of one key value.
-
collector
Collects a stream ofKeyValue
into aKeyValues
instance.- Returns:
- a
Collector
that accumulates key-values into aKeyValues
object.
-
collector
Collects a stream ofMap.Entry
into aKeyValues
using the provided builder.- Parameters:
builder
- theKeyValues.Builder
to use for constructing the key-values.- Returns:
- a
Collector
that accumulates entries into aKeyValues
object.
-
iterator
-
map
Applies a transformation function to each key-value pair in the collection.- Parameters:
kv
- aUnaryOperator
to transform each key-value.- Returns:
- a new
KeyValues
with transformed entries.
-
filter
Filters the key-value pairs based on a predicate.- Parameters:
predicate
- aPredicate
to test each key-value.- Returns:
- a new
KeyValues
with filtered entries.
-
last
This method gets the last element of the key values stream. This ergonomic is provided because later keys override earlier keys of the same name.- Returns:
- optional of the last key value if there is one.
-
flatMap
Flattens the key-values by applying a mapping function that returns anotherKeyValues
.- Parameters:
func
- a function that transforms aKeyValue
into anotherKeyValues
.- Returns:
- a new flattened
KeyValues
.
-
interpolate
Interpolates the values using the providedVariables
. The key value pairs inthis
take precedence over the passed in variables such that if there is a matching key in this and in variables the value of the key value in this will be used.- Parameters:
variables
- aVariables
map for value substitution.- Returns:
- a map of interpolated key-values.
- See Also:
-
expand
Expands the key-values using variable interpolation and return a new key values where all the value parts of the keys are replaced with the interpolation results.- Parameters:
variables
- aVariables
map for value substitution.- Returns:
- a new
KeyValues
with expanded values. - See Also:
-
memoize
Returns a memoized version of thisKeyValues
which means repeated calls to iteratore or stream over the KeyValues will always generate the same result.- Returns:
- a memoized
KeyValues
instance.
-
redact
Redacts sensitive key-value entries by replacing their values.- Returns:
- a new
KeyValues
with redacted entries.
-
toMap
Converts the key-values to a map where each key maps to its expanded value. The returned Map is sequenced based on the order of the key-values. Note this call does not do any interpolation! If interpolation is desired it needs to be done prior withexpand(Variables)
orinterpolate(Variables)
should be used.- Returns:
- a
Map
of key-value pairs.
-
format
A convenience method to format key values to a String mainly for unit testing to maintain fluent chain.- Parameters:
media
- which will be used to callKeyValuesMedia.formatter()
- Returns:
- formatted key values
- Throws:
UnsupportedOperationException
- if formatting is not supported by the media.- See Also:
-