Fluent Post-processor Builder
To avoid builder functions with excessive amounts of unused parameters or several versions of the builder depending on the different possible setups of the post-processor, we recommend using a fluent builder. This builder is intended to be attached to the usual flow of the Copper API fluent commands.
Principles
The metric parameters can be set up at three different stages:
- The builder constructor.
- Directly on the call of a builder function (the preferred way).
- At the last stage, during the call of the
build
function.
Constructor stage
We highly recommend defining two constructors; one without parameters and a second one with the plugin key to be able to also override the post-processor with its builder. At the constructor stage, you can set up some properties that are fixed for the post-processor such as the output type or the real-time behavior by calling either the predefined functions or the copper functions:
private Builder() {
this(PLUGIN_KEY);
}
protected Builder(@NonNull String pluginKey) {
super(pluginKey);
expandLevel = null; // Initialization of the parameters used on the build stage
pp = pp.withInheritedContinuousQueryHandlers(); // Call of the Copper API
withType(ILiteralType.DOUBLE); // Call of the inner API
Direct API call
Here is an example of how to directly pass the command to the Copper API:
@Contract("_ -> this")
public @NonNull Builder withIndexFinder(@NonNull String indexFinder) {
pp = pp.withProperty(FINDER, indexFinder);
return this;
}
Builder call
The builder properties can be used for the final setup of the metric. Here is an example:
@Override
public @NonNull CopperPostProcessor build() {
// Call of the underlying Copper API
if(continuousQueryHandlers != null) {
pp = pp.withContinuousQueryHandlers(continuousQueryHandlers);
} else {
pp = pp.withInheritedContinuousQueryHandlers();
}
if(metric1 == null || metric2 == null) { // Some consistency checks
throw new UnsupportedOperationException("You must define metric1 and metric2");
}
return super.build()
.withUnderlyingMeasures(metric1, metric2); // Direct call just after the build
}
Builder definition
The builder has to override the class BaseCopperPostProcessorBuilder
to have access to the basic builder functions.
It can also extend the following:
WithAnUnderlying
if it needs one single underlying.withAggregationFunction
if the aggregation function has to be set and may be different fromSUM
.withAnalysisLevels
if the metric may be propagated along extra analysis levels.withConfidenceLevel
if the metric may have a confidence level defined such as VaR or ES. By default, the builder should be provided by the static methodmeasure
.
@Contract(" -> new")
public static @NonNull Builder measure() {
return new Builder();
}
public static class Builder extends BaseCopperPostProcessorBuilder<Builder> {
protected Builder() {
this(PLUGIN_KEY);
}
protected Builder(@NotNull String pluginKey) {
super(pluginKey);
pp = pp.withInheritedContinuousQueryHandlers();
}
}
API Usage
When correctly set up, the fluent API can be used seamlessly with the Copper API:
FX.measure() // Retrieval of the builder
.withUnderlyingMeasure(Copper.sum(PNL))
.withAsOfDateLevel(LevelIdentifier.simple("AsOfDate"))
.withSourceCurrencyLevel(LevelIdentifier.simple("Currency"))
.as("FXRatePPTest") // Here we are back to the Copper API
.publish(context);
If a Copper function is not available, you can move to the Copper API explicitly by calling the build method:
FX.measure() // Retrieval of the builder
.withUnderlyingMeasure(Copper.sum(PNL))
.withAsOfDateLevel(LevelIdentifier.simple("AsOfDate"))
.withSourceCurrencyLevel(LevelIdentifier.simple("Currency"))
.build() // Explicit call to the end of the PostProcessor API
.as("FXRatePPTest") // Here we are back to the Copper API
.publish(context);
Provided methods
Name | Class or interfaces | Parameters | Usage |
---|---|---|---|
build | BaseCopperPostProcessorBuilder | Set up the metric and return a CopperPostProcessor object ready to be used on the Copper API. |
|
publish | BaseCopperPostProcessorBuilder | ICopperContext | Shorthand for build().publish(context) . |
withinFolder | BaseCopperPostProcessorBuilder | String | Shorthand for build().withinFolder(folder) . |
withFormatter | BaseCopperPostProcessorBuilder | String | Shorthand for build().withFormatter(formatter) . |
isVisible | BaseCopperPostProcessorBuilder | boolean | Shorthand for build().isVisible(visible) . |
per | BaseCopperPostProcessorBuilder | CopperLevel… | Shorthand for build().per(levels) . |
as | BaseCopperPostProcessorBuilder | String | Shorthand for build().as(name) . |
withName | BaseCopperPostProcessorBuilder | String | Shorthand for build().as(name) . |
witDescription | BaseCopperPostProcessorBuilder | String | Shorthand for build().witDescription(description) . |
withType | BaseCopperPostProcessorBuilder | int | Sets the type of the measure on the builder stage. If not called, the type will be double. |
withType | BaseCopperPostProcessorBuilder | String | Sets the type of the measure on the builder stage. If not called, the type will be double. |
cast | BaseCopperPostProcessorBuilder | Returns the builder on its right type base on the template type, used internally to avoid breaking the fluent chain. | |
withUnderlyingMeasure | WithAnUnderlying | CopperMeasure | Defines the underlying measure of the current measure. |
withUnderlyingMeasure | WithAnUnderlying | String | Defines the underlying measure of the current measure. |
withAggregationFunction | WithAggregationFunction | String | Changes the aggregation function used by the leaf post-processors or by the partitioned ones. |
withAnalysisLevels | WithAnalysisLevels | LevelIdentifier… | Adds extra analysis levels to a post-processor where the measure will be expanded. |
withAnalysisLevels | WithAnalysisLevels | String… | Adds extra analysis levels to a post-processor where the measure will be expanded. |
withConfidenceLevel | WithConfidenceLevel | double | Sets the “defaultConfidence” property used by the VaR/ES/… metrics. |
withConfidenceLevel | WithConfidenceLevel | String | Sets the “defaultConfidence” property used by the VaR/ES/… metrics. |