Interface ScenarioArray<T>

  • Type Parameters:
    T - the type of each individual value
    All Known Implementing Classes:
    CurrencyScenarioArray, DoubleScenarioArray, FxRateScenarioArray, MultiCurrencyScenarioArray, QuoteScenarioArray

    public interface ScenarioArray<T>
    An array of values, one for each scenario.

    Implementations of this interface provide scenario-specific values.

    In the simplest case, this might be a list of values, one for each scenario. This is handled by this factory methods on this interface.

    There are two obvious reasons for creating implementations of this interface with special handling for certain types of value:

    • Reducing memory usage
    • Improving performance
    For example, if the system stores multiple copies of a curve as a list it must store the x-values with each copy of the curve. This data is mostly redundant as the x-values are the same in every scenario. A custom data type for storing scenario data for a curve can store one set of x-values shared between all scenarios, reducing memory footprint.

    When dealing with primitive data it is likely be more efficient to store the scenario values in a primitive array instead of using a list. This removes the need for boxing and reduces memory footprint. Also, if a function calculates values for all scenarios at the same time, it is likely to be more efficient if the data is stored in arrays as the values will be stored in a contiguous block of memory.

    The generic type parameter is the type of the single value associated with each scenario. For example, in the case of optimized curve storage, the single value is a curve.

    • Method Detail

      • of

        @SafeVarargs
        static <T> ScenarioArray<T> of​(T... values)
        Obtains an instance from the specified array of values.
        Type Parameters:
        T - the type of the value
        Parameters:
        values - the values, one value for each scenario
        Returns:
        an instance with the specified values
      • of

        static <T> ScenarioArray<T> of​(List<T> values)
        Obtains an instance from the specified list of values.
        Type Parameters:
        T - the type of the value
        Parameters:
        values - the values, one value for each scenario
        Returns:
        an instance with the specified values
      • of

        static <T> ScenarioArray<T> of​(int scenarioCount,
                                       IntFunction<T> valueFunction)
        Obtains an instance using a function to create the entries.

        The function is passed the scenario index and returns the value for that index.

        Type Parameters:
        T - the type of the value
        Parameters:
        scenarioCount - the number of scenarios
        valueFunction - the function used to obtain each value
        Returns:
        an instance initialized using the function
        Throws:
        IllegalArgumentException - is size is zero or less
      • ofSingleValue

        static <T> ScenarioArray<T> ofSingleValue​(int scenarioCount,
                                                  T value)
        Obtains an instance from a single value where the value applies to all scenarios.
        Type Parameters:
        T - the type of the value
        Parameters:
        scenarioCount - the nnumber of scenarios
        value - the single value, used for all scenarios
        Returns:
        an instance with the specified values
      • getScenarioCount

        int getScenarioCount()
        Gets the number of scenarios.
        Returns:
        the number of scenarios
      • get

        T get​(int scenarioIndex)
        Gets the value at the specified scenario index.
        Parameters:
        scenarioIndex - the zero-based index of the scenario
        Returns:
        the value at the specified index
        Throws:
        IndexOutOfBoundsException - if the index is invalid
      • stream

        default Stream<T> stream()
        Returns a stream of the values.

        The stream will return the value for each scenario.

        Returns:
        a stream of the values