P
- the type of the parentV
- the type of the valuepublic class ConfigurationValueBuilderHelper<P,V> extends AbstractParent<P> implements ConfigurationValueBuilder<P,V>
If a value is explicitly set by developer, it means that the value is hard-coded in its own code. This value can't be overridden by a property or default value so the developer value always takes precedence. For example, Ogham could be configured like this:
.port() .properties("mail.smtp.port", "mail.port") .devaultValue(25)The developer can configure Ogham using a configuration file in the classpath to provide values for local development environment. Example of content of the configuration file:
mail.smtp.port = 557The developer may want to test its code and may need to have a different port value in test:
@Test public void testMail() { MessagingBuilder builder = MessagingBuilder.standard(); builder .email().sender(JavaMailBuilder.class) .port(10025) ...The value
10025
must be the value available in the test regardless of
the value of the properties and default value.
NOTE: This class is for internal use (or extensions for
Ogham). The developer that uses Ogham should not see
setValue(Object)
and getValue()
methods.
parent
Constructor and Description |
---|
ConfigurationValueBuilderHelper(P parent,
Class<V> valueClass,
BuildContext buildContext)
Initializes with the parent builder and the type of the value used for
conversion.
|
Modifier and Type | Method and Description |
---|---|
ConfigurationValueBuilderHelper<P,V> |
defaultValue(MayOverride<V> possibleNewValue)
Register a default value only if
MayOverride.override(Object)
returns true. |
ConfigurationValueBuilderHelper<P,V> |
defaultValue(V value)
Register a default value.
|
V |
getValue()
Get the final value.
|
V |
getValue(V defaultValue)
Get the final value.
|
boolean |
hasValueOrProperties()
Returns true if a value, default value or properties have been set.
|
ConfigurationValueBuilderHelper<P,V> |
properties(String... properties)
Register one or several property keys.
|
void |
setValue(V value)
If a value is explicitly set by developer, it means that the value is
hard-coded in its own code.
|
ConfigurationValueBuilderHelper<P,V> |
value(Optional<V> optionalValue)
Register an optional value only if
Optional.isPresent() returns
true. |
and
public ConfigurationValueBuilderHelper(P parent, Class<V> valueClass, BuildContext buildContext)
parent
- the parent buildervalueClass
- the type of the valuebuildContext
- for registering instances and property evaluationpublic ConfigurationValueBuilderHelper<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 ConfigurationValueBuilderHelper<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 ConfigurationValueBuilderHelper<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 valuepublic ConfigurationValueBuilderHelper<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 void setValue(V value)
.port() .properties("mail.smtp.port", "mail.port") .devaultValue(25)The developer can configure Ogham using a configuration file in the classpath to provide values for local development environment. Example of content of the configuration file:
mail.smtp.port = 557The developer may want to test its code and may need to have a different port value in test:
@Test public void testMail() { MessagingBuilder builder = MessagingBuilder.standard(); builder .email().sender(JavaMailBuilder.class) .port(10025) ...The value
10025
must be the value available in the test
regardless of the value of the properties and default value.
IMPORTANT: Configurer
s should
not use this method directly because it prevents the possibility
to override the value from external configuration.
value
- the value set by the developer in its own codepublic V getValue()
setValue(Object)
,
this value is returnedvalue(Optional)
, and
Optional.isPresent()
returns true this value is returnedpublic V getValue(V defaultValue)
setValue(Object)
,
this value is returnedvalue(Optional)
, and
Optional.isPresent()
returns true this value is returneddefaultValue
- the default value to use if there isn't a non-null valuepublic boolean hasValueOrProperties()
Copyright © 2021. All rights reserved.