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
Maprepresentation.
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
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classA builder for constructingKeyValuesinstances from key-value pairs. -
Method Summary
Modifier and TypeMethodDescriptionstatic KeyValues.Builderbuilder()Creates a newKeyValues.Builderwith a default, empty source.static KeyValues.Builderbuilder(KeyValuesResource resource) Creates a newKeyValues.Builderfor constructing aKeyValuesinstance.Collects a stream ofKeyValueinto aKeyValuesinstance.collector(KeyValues.Builder builder) Collects a stream ofMap.Entryinto aKeyValuesusing the provided builder.static KeyValuesCreates aKeyValuesinstance by copying the given collection.static KeyValuesempty()Returns an emptyKeyValuesinstance.default KeyValuesExpands 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 KeyValuesFilters the key-value pairs based on a predicate.default KeyValuesFlattens the key-values by applying a mapping function that returns anotherKeyValues.default Stringformat(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 KeyValuesmap(UnaryOperator<KeyValue> kv) Applies a transformation function to each key-value pair in the collection.default KeyValuesmemoize()Returns a memoized version of thisKeyValueswhich means repeated calls to iteratore or stream over the KeyValues will always generate the same result.static KeyValuesCreates a single key values instance.default KeyValuesredact()Redacts sensitive key-value entries by replacing their values.stream()Returns a stream of the containedKeyValueentries.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 containedKeyValueentries.- Returns:
- a
Streamof key-value pairs.
-
builder
Creates a newKeyValues.Builderfor constructing aKeyValuesinstance.- Parameters:
resource- aKeyValuesResourcedefining the source URI and reference key-value.- Returns:
- a new
KeyValues.Builder.
-
builder
Creates a newKeyValues.Builderwith a default, empty source.- Returns:
- a new
KeyValues.Builder.
-
empty
Returns an emptyKeyValuesinstance.- Returns:
- an empty
KeyValuescollection.
-
copyOf
Creates aKeyValuesinstance by copying the given collection.- Parameters:
kvs- aSequencedCollectionof key-value pairs.- Returns:
- a new
KeyValuesinstance 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 ofKeyValueinto aKeyValuesinstance.- Returns:
- a
Collectorthat accumulates key-values into aKeyValuesobject.
-
collector
Collects a stream ofMap.Entryinto aKeyValuesusing the provided builder.- Parameters:
builder- theKeyValues.Builderto use for constructing the key-values.- Returns:
- a
Collectorthat accumulates entries into aKeyValuesobject.
-
iterator
-
map
Applies a transformation function to each key-value pair in the collection.- Parameters:
kv- aUnaryOperatorto transform each key-value.- Returns:
- a new
KeyValueswith transformed entries.
-
filter
Filters the key-value pairs based on a predicate.- Parameters:
predicate- aPredicateto test each key-value.- Returns:
- a new
KeyValueswith 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 aKeyValueinto anotherKeyValues.- Returns:
- a new flattened
KeyValues.
-
interpolate
Interpolates the values using the providedVariables. The key value pairs inthistake 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- aVariablesmap 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- aVariablesmap for value substitution.- Returns:
- a new
KeyValueswith expanded values. - See Also:
-
memoize
Returns a memoized version of thisKeyValueswhich means repeated calls to iteratore or stream over the KeyValues will always generate the same result.- Returns:
- a memoized
KeyValuesinstance.
-
redact
Redacts sensitive key-value entries by replacing their values.- Returns:
- a new
KeyValueswith 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
Mapof 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:
-