T
- the type of data held in the boxpublic interface MarketDataBox<T>
A box can contain a single value for the data or it can contain multiple values, one for each scenario. If it contains a single value then the same value is used in every scenario.
Wrapping the data in a box allows a simple interface for looking up market data that hides whether there is one value or multiple values. Without the box every function that uses market data would have to handle the two cases separately.
The box also takes care of transforming the market data when using it to build other market data values
(see the apply
methods). This means that market data functions and perturbations don't need
different logic to deal with single and multiple values.
Using a box allows scenario data to be stored more efficiently in some cases. For example, curve data for multiple scenarios can include one copy of the x-axis data which is used in all scenarios. If a separate curve were stored for each scenario that data would be unnecessarily stored multiple times.
In some cases a function might need to access the data for all scenarios at the same time. For example, if
part of the calculation is the same for all scenarios it can be done once and reused instead of recalculated
for each scenario. In this case a ScenarioMarketDataId
should be used to retrieve the scenario
value from the market data container.
Modifier and Type | Method and Description |
---|---|
<U,R> MarketDataBox<R> |
combineWith(MarketDataBox<U> other,
BiFunction<T,U,R> fn)
Applies a function to the market data in this box and another box and returns a box containing the result.
|
static <T> MarketDataBox<T> |
empty()
Obtains an instance containing no market data.
|
Class<?> |
getMarketDataType()
Gets the type of the market data value used in each scenario.
|
int |
getScenarioCount()
Gets the number of scenarios for which this box contains data.
|
ScenarioArray<T> |
getScenarioValue()
Gets the market data value containing data for multiple scenarios.
|
T |
getSingleValue()
Gets the single market data value used for all scenarios if available.
|
T |
getValue(int scenarioIndex)
Gets the market data value associated with the specified scenario.
|
default boolean |
isScenarioValue()
Checks if this box contains market data for multiple scenarios.
|
boolean |
isSingleValue()
Checks if this box contains a single market data value that is used for all scenarios.
|
<R> MarketDataBox<R> |
map(Function<T,R> fn)
Applies a function to the contents of the box and returns another box.
|
<R> MarketDataBox<R> |
mapWithIndex(int scenarioCount,
ObjIntFunction<T,R> fn)
Applies a function to the contents of the box once for each scenario and returns a box containing
the values returned from the function.
|
static <T> MarketDataBox<T> |
ofScenarioValue(ScenarioArray<T> scenarioValue)
Obtains an instance containing a scenario market data value with data for multiple scenarios.
|
static <T> MarketDataBox<T> |
ofScenarioValues(List<T> scenarioValues)
Obtains an instance containing a scenario market data value with data for multiple scenarios.
|
static <T> MarketDataBox<T> |
ofScenarioValues(T... scenarioValues)
Obtains an instance containing a scenario market data value with data for multiple scenarios.
|
static <T> MarketDataBox<T> |
ofSingleValue(T singleValue)
Obtains an instance containing a single market data value that is used in all scenarios.
|
Stream<T> |
stream()
Returns a stream over the contents of the box.
|
static <T> MarketDataBox<T> ofSingleValue(T singleValue)
T
- the type of the market data value used in each scenariosingleValue
- the market data value containing data for a single scenariostatic <T> MarketDataBox<T> ofScenarioValue(ScenarioArray<T> scenarioValue)
The market data is made up of multiple values, one for each scenario.
The ScenarioArray
instance may provide optimized internal storage of these values.
A box may be created that contains a value for one scenario. Such a box is distinct from
a box created using ofSingleValue(Object)
, which is valid for any number of scenarios.
T
- the type of the market data value used in each scenarioscenarioValue
- the market data value containing data for multiple scenarios@SafeVarargs static <T> MarketDataBox<T> ofScenarioValues(T... scenarioValues)
The market data is made up of multiple values, one for each scenario.
A box may be created that contains a value for one scenario. Such a box is distinct from
a box created using ofSingleValue(Object)
, which is valid for any number of scenarios.
T
- the type of the market data value used in each scenarioscenarioValues
- the market data values for each scenariostatic <T> MarketDataBox<T> ofScenarioValues(List<T> scenarioValues)
The market data is made up of multiple values, one for each scenario.
A box may be created that contains a value for one scenario. Such a box is distinct from
a box created using ofSingleValue(Object)
, which is valid for any number of scenarios.
T
- the type of the market data value used in each scenarioscenarioValues
- the market data values for each scenariostatic <T> MarketDataBox<T> empty()
T
- the type of the market data value used in each scenarioT getSingleValue()
If this box contains data for multiple scenarios an exception is thrown.
This method should only be called if isSingleValue()
returns true
or isScenarioValue()
return false
.
UnsupportedOperationException
- if this box contains data for multiple scenariosScenarioArray<T> getScenarioValue()
If this box contains data for a single scenario an exception is thrown.
This method should only be called if isSingleValue()
returns false
or isScenarioValue()
return true
.
UnsupportedOperationException
- if this box contains data for a single scenarioT getValue(int scenarioIndex)
scenarioIndex
- the index of the scenarioIndexOutOfBoundsException
- if the index is invalidboolean isSingleValue()
default boolean isScenarioValue()
int getScenarioCount()
A "single value" box can be used with any number of scenarios. To indicate this, the method will return -1.
Class<?> getMarketDataType()
<R> MarketDataBox<R> map(Function<T,R> fn)
The box implementation takes care of checking whether it contains a single value or a scenario value, applying the function to the value for each scenario and packing the return value into a box.
This is primarily intended for use by market data factories which might receive single values or scenario values from upstream market data factories.
R
- the return type of the functionfn
- a function to apply to the market data in the box<R> MarketDataBox<R> mapWithIndex(int scenarioCount, ObjIntFunction<T,R> fn)
The scenario count
of the box must be one or it must be equal to the
scenarioCount
argument. The scenario count
of the return value
will be equal to scenarioCount
.
The box implementation takes care of checking whether it contains a single value or a scenario value, applying the function to the value for each scenario and packing the return values into a box.
This is primarily intended to be used by perturbations which generate separate market data values for each scenario data by applying a function to the existing value for the scenario.
R
- the type of the returned market datascenarioCount
- the total number of scenariosfn
- the function that is invoked with a scenario index and the market data value for that scenario.
The return value is used as the scenario data in the returned box<U,R> MarketDataBox<R> combineWith(MarketDataBox<U> other, BiFunction<T,U,R> fn)
The box implementation takes care of checking whether the input boxes contain single values or a scenario values, applying the function to the value for each scenario and packing the return value into a box.
This is primarily intended for use by market data factories which might receive single values or scenario values from upstream market data factories.
U
- the type of market data in the other boxR
- the type of the market data returned in the result of the functionother
- another market data boxfn
- the function invoked with the market data from each box. The return value is used to build the data
in the returned boxCopyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.