P
- the type of the parent builder (when calling AbstractParent.and()
method)V
- the type of the valuepublic class ConfigurationValueBuilderDelegate<P,V> extends AbstractParent<P> implements ConfigurationValueBuilder<P,V>
This is useful when a ConfigurationValueBuilder
is used for a
particular parent and it must be inherited. As the parent types are not the
same, you can't directly use the same reference. So this implementation wraps
the original reference but as it is a new instance, it can have a different
parent builder.
parent
Constructor and Description |
---|
ConfigurationValueBuilderDelegate(P parent,
ConfigurationValueBuilder<?,V> delegate)
Wraps the delegate builder.
|
Modifier and Type | Method and Description |
---|---|
ConfigurationValueBuilder<P,V> |
defaultValue(MayOverride<V> possibleNewValue)
Register a default value only if
MayOverride.override(Object)
returns true. |
ConfigurationValueBuilder<P,V> |
defaultValue(V value)
Register a default value.
|
ConfigurationValueBuilder<P,V> |
properties(String... properties)
Register one or several property keys.
|
ConfigurationValueBuilder<P,V> |
value(Optional<V> optionalValue)
Register an optional value only if
Optional.isPresent() returns
true. |
and
public ConfigurationValueBuilderDelegate(P parent, ConfigurationValueBuilder<?,V> delegate)
parent
- the new parent used for chainingdelegate
- the instance that will really be updatedpublic ConfigurationValueBuilder<P,V> properties(String... properties)
ConfigurationValueBuilder
.properties("${custom.property.high-priority}", "${custom.property.low-priority}");The properties are not immediately evaluated. The evaluation will be done when the
MessagingBuilder.build()
method is called.
If you provide several property keys, evaluation will be done on the
first key and if the property exists (see EnvironmentBuilder
),
its value is used. If the first property doesn't exist in properties,
then it tries with the second one and so on.
The property keys may be provided without ${} expression markers:
.properties("custom.property.high-priority", "custom.property.low-priority");In this case, the keys are automatically wrapped for later evaluation.
properties
in interface ConfigurationValueBuilder<P,V>
properties
- the property keyspublic ConfigurationValueBuilder<P,V> defaultValue(V value)
ConfigurationValueBuilder
If this method is called several times, only the last value is used.
If null
value is set, it unregisters previously registered
default value.
defaultValue
in interface ConfigurationValueBuilder<P,V>
value
- the default valuepublic ConfigurationValueBuilder<P,V> value(Optional<V> optionalValue)
ConfigurationValueBuilder
Optional.isPresent()
returns
true. This value is used even if the developer has set a value for a
property key.
This is a convenient method for fluent chaining. Without optional, the code would look like this:
builder.host().defaultValue("localhost");
if (myPortValue != null) {
builder.port().value(myPortValue);
}
if (username != null) {
builder.username().value(username);
}
Thanks to Optional
, developer can write:
builder
.host().defaultValue("localhost").and()
.port().value(Optional.ofNullable(myPortValue)).and()
.username().value(Optional.ofNullable(username));
If this method is called several times, only the last "present" value is used.
WARNING: This is for advanced usage only. Developer that
uses Ogham should not set the value of the property using this method
because the value set like this takes precedence over properties. It
means that the developer can't configure the resulting value from
external configuration. This method is designed for particular automatic
configuration (using Configurer
s).
value
in interface ConfigurationValueBuilder<P,V>
optionalValue
- the optional value to setpublic ConfigurationValueBuilder<P,V> defaultValue(MayOverride<V> possibleNewValue)
ConfigurationValueBuilder
MayOverride.override(Object)
returns true.
Automatic configuration is based on priority order. Higher priority is
applied first. It means that the lowest priority is applied last and
overrides any default value set by a Configurer
with higher
priority using ConfigurationValueBuilder.defaultValue(Object)
.
This method gives more control on how Configurer
s should provide
a default value. Each Configurer
can decide if its default value
should override or not a previously default value set by a
Configurer
with higher priority. It can also be used to control
default value override or not with null
value.
If every Configurer
uses
MayOverride.overrideIfNotSet(Object)
:
.defaultValue(MayOverride.overrideIfNotSet(newValue))Then the default value comes from the first applied
Configurer
(the one with highest priority) that sets a non-null default value. It
becomes more consistent with properties declaration (first registered
property has highest priority).defaultValue
in interface ConfigurationValueBuilder<P,V>
possibleNewValue
- indicates if the new default value should be applied according
to previous valueCopyright © 2021. All rights reserved.