Interface MarketDataBox<T>

  • Type Parameters:
    T - the type of data held in the box

    public interface MarketDataBox<T>
    A box which can provide values for an item of market data used in scenarios.

    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.

    • Method Detail

      • ofSingleValue

        static <T> MarketDataBox<T> ofSingleValue​(T singleValue)
        Obtains an instance containing a single market data value that is used in all scenarios.
        Type Parameters:
        T - the type of the market data value used in each scenario
        Parameters:
        singleValue - the market data value containing data for a single scenario
        Returns:
        a box containing a single market data value that is used in all scenarios
      • ofScenarioValue

        static <T> MarketDataBox<T> ofScenarioValue​(ScenarioArray<T> scenarioValue)
        Obtains an instance containing a scenario market data value with data for multiple scenarios.

        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.

        Type Parameters:
        T - the type of the market data value used in each scenario
        Parameters:
        scenarioValue - the market data value containing data for multiple scenarios
        Returns:
        a box containing a scenario market data value with data for multiple scenarios
      • ofScenarioValues

        @SafeVarargs
        static <T> MarketDataBox<T> ofScenarioValues​(T... scenarioValues)
        Obtains an instance containing a scenario market data value with data for multiple scenarios.

        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.

        Type Parameters:
        T - the type of the market data value used in each scenario
        Parameters:
        scenarioValues - the market data values for each scenario
        Returns:
        a box containing a scenario market data value with data for multiple scenarios
      • ofScenarioValues

        static <T> MarketDataBox<T> ofScenarioValues​(List<T> scenarioValues)
        Obtains an instance containing a scenario market data value with data for multiple scenarios.

        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.

        Type Parameters:
        T - the type of the market data value used in each scenario
        Parameters:
        scenarioValues - the market data values for each scenario
        Returns:
        a box containing a scenario market data value with data for multiple scenarios
      • empty

        static <T> MarketDataBox<T> empty()
        Obtains an instance containing no market data.
        Type Parameters:
        T - the type of the market data value used in each scenario
        Returns:
        a box containing no market data
      • getSingleValue

        T getSingleValue()
        Gets the single market data value used for all scenarios if available.

        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.

        Returns:
        the single market data value used for all scenarios if available
        Throws:
        UnsupportedOperationException - if this box contains data for multiple scenarios
      • getScenarioValue

        ScenarioArray<T> getScenarioValue()
        Gets the market data value containing data for multiple scenarios.

        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.

        Returns:
        the market data value containing data for multiple scenarios
        Throws:
        UnsupportedOperationException - if this box contains data for a single scenario
      • getValue

        T getValue​(int scenarioIndex)
        Gets the market data value associated with the specified scenario.
        Parameters:
        scenarioIndex - the index of the scenario
        Returns:
        the market data value associated with the scenario
        Throws:
        IndexOutOfBoundsException - if the index is invalid
      • isSingleValue

        boolean isSingleValue()
        Checks if this box contains a single market data value that is used for all scenarios.
        Returns:
        true if this box contains a single market data value that is used for all scenarios
      • isScenarioValue

        default boolean isScenarioValue()
        Checks if this box contains market data for multiple scenarios.
        Returns:
        true if this box contains market data for multiple scenarios
      • getScenarioCount

        int getScenarioCount()
        Gets the number of scenarios for which this box contains data.

        A "single value" box can be used with any number of scenarios. To indicate this, the method will return -1.

        Returns:
        the number of scenarios for which this box contains data, -1 if the number is not specified
      • getMarketDataType

        Class<?> getMarketDataType()
        Gets the type of the market data value used in each scenario.
        Returns:
        the type of the market data value used in each scenario
      • map

        <R> MarketDataBox<R> map​(Function<T,​R> fn)
        Applies a function to the contents of the box and returns another box.

        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.

        Type Parameters:
        R - the return type of the function
        Parameters:
        fn - a function to apply to the market data in the box
        Returns:
        a result wrapping a box containing the return value of the function
      • mapWithIndex

        <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.

        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.

        Type Parameters:
        R - the type of the returned market data
        Parameters:
        scenarioCount - the total number of scenarios
        fn - 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
        Returns:
        a box containing market data created by applying the function to the contents of this box
      • combineWith

        <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.

        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.

        Type Parameters:
        U - the type of market data in the other box
        R - the type of the market data returned in the result of the function
        Parameters:
        other - another market data box
        fn - the function invoked with the market data from each box. The return value is used to build the data in the returned box
        Returns:
        a box containing market data created by applying the function to the data in this box and another box
      • stream

        Stream<T> stream()
        Returns a stream over the contents of the box.
        Returns:
        a stream over the contents of the box