T
- Data type of the property, e.g., Integer, Booleanpublic class DynamicContextualProperty<T> extends PropertyWrapper<T>
The value is defined as a JSON blob that consists of multiple conditions and value associated with each condition. The following is an example:
[
{
"if":
{"@environment":["prod"],
"@region":["us-east-1"]
},
"value":5
},
{
"if":
{"@environment":["test", "dev"]},
"value":10
},
{
"value":2
}
]
This blob means that
In order to make this work, a Predicate is needed to determine if the current runtime context matches any of the conditions described in the JSON blob. The predicate can be passed in as a parameter of the constructor, otherwise the default one will interpret each key in the "dimensions" as a key of a DynamicProperty, and the list of values are the acceptable values of the DynamicProperty.
For example:
String json = ... // string as the above JSON blob
ConfigurationManager.getConfigInstance().setProperty("@environment", "test"); // no need to do this in real application as property is automatically set
ConfigurationManager.getConfigInstance().setProperty("contextualProp", json);
DynamicContextualProperty<Integer> prop = new DynamicContextualProperty<Integer>("contextualProp", 0);
prop.get(); // returns 10 as "@environment" == "test" matches the second condition
Modifier and Type | Class and Description |
---|---|
static class |
DynamicContextualProperty.Value<T> |
defaultValue, prop
Constructor and Description |
---|
DynamicContextualProperty(java.lang.String propName,
T defaultValue) |
DynamicContextualProperty(java.lang.String propName,
T defaultValue,
<any> predicate) |
Modifier and Type | Method and Description |
---|---|
T |
getValue()
Get current typed value of the property.
|
protected void |
propertyChanged()
Called when the property value is updated.
|
addCallback, addValidator, getChangedTimestamp, getDefaultValue, getDynamicProperty, getName, propertyChanged, registerSubClassWithNoCallback, removeAllCallbacks, toString, validate
public DynamicContextualProperty(java.lang.String propName, T defaultValue, <any> predicate)
public DynamicContextualProperty(java.lang.String propName, T defaultValue)
protected final void propertyChanged()
PropertyWrapper
propertyChanged
in class PropertyWrapper<T>
public T getValue()
PropertyWrapper