/** * Obtains an instance containing the specified value. * * @param <T> the type of the market data value * @param value the market data value which can provide data for multiple scenarios * @return a market data box containing the value */ public static <T> ScenarioMarketDataBox<T> of(ScenarioArray<T> value) { return new ScenarioMarketDataBox<>(value); }
@Override public <U, R> MarketDataBox<R> combineWith(MarketDataBox<U> other, BiFunction<T, U, R> fn) { return other.isSingleValue() ? combineWithSingle(other, fn) : combineWithMultiple(other, fn); }
/** * Restricted copy constructor. * @param beanToCopy the bean to copy from, not null */ private Builder(ScenarioMarketDataBox<T> beanToCopy) { this.value = beanToCopy.getValue(); }
@Override public <R> MarketDataBox<R> mapWithIndex(int scenarioCount, ObjIntFunction<T, R> fn) { if (scenarioCount != getScenarioCount()) { throw new IllegalArgumentException( Messages.format( "Scenario count {} does not equal the scenario count of the value {}", scenarioCount, getScenarioCount())); } List<R> perturbedValues = IntStream.range(0, scenarioCount) .mapToObj(idx -> fn.apply(getValue(idx), idx)) .collect(toImmutableList()); return MarketDataBox.ofScenarioValues(perturbedValues); }
@Override public <R> MarketDataBox<R> map(Function<T, R> fn) { return applyToScenarios(i -> fn.apply(value.get(i))); }
/** * Obtains an instance containing a scenario market data value with data for multiple scenarios. * <p> * The market data is made up of multiple values, one for each scenario. * <p> * A box may be created that contains a value for one scenario. Such a box is distinct from * a box created using {@link #ofSingleValue(Object)}, which is valid for any number of scenarios. * * @param <T> the type of the market data value used in each scenario * @param scenarioValues the market data values for each scenario * @return a box containing a scenario market data value with data for multiple scenarios */ public static <T> MarketDataBox<T> ofScenarioValues(List<T> scenarioValues) { return ScenarioMarketDataBox.of(scenarioValues); }
private <U, R> MarketDataBox<R> combineWithSingle(MarketDataBox<U> other, BiFunction<T, U, R> fn) { U otherValue = other.getSingleValue(); return applyToScenarios(i -> fn.apply(value.get(i), otherValue)); }
/** * Obtains an instance containing a scenario market data value with data for multiple scenarios. * <p> * The market data is made up of multiple values, one for each scenario. * The {@link ScenarioArray} instance may provide optimized internal storage of these values. * <p> * A box may be created that contains a value for one scenario. Such a box is distinct from * a box created using {@link #ofSingleValue(Object)}, which is valid for any number of scenarios. * * @param <T> the type of the market data value used in each scenario * @param scenarioValue the market data value containing data for multiple scenarios * @return a box containing a scenario market data value with data for multiple scenarios */ public static <T> MarketDataBox<T> ofScenarioValue(ScenarioArray<T> scenarioValue) { return ScenarioMarketDataBox.of(scenarioValue); }
@Override public ScenarioMarketDataBox<T> build() { return new ScenarioMarketDataBox<>( value); }
private <R, U> MarketDataBox<R> combineWithMultiple(MarketDataBox<U> other, BiFunction<T, U, R> fn) { ScenarioArray<U> otherValue = other.getScenarioValue(); if (otherValue.getScenarioCount() != value.getScenarioCount()) { String message = Messages.format( "Scenario values must have the same number of scenarios. {} has {} scenarios, {} has {}", value, value.getScenarioCount(), otherValue, otherValue.getScenarioCount()); throw new IllegalArgumentException(message); } return applyToScenarios(i -> fn.apply(value.get(i), otherValue.get(i))); }
/** * Obtains an instance containing a scenario market data value with data for multiple scenarios. * <p> * The market data is made up of multiple values, one for each scenario. * <p> * A box may be created that contains a value for one scenario. Such a box is distinct from * a box created using {@link #ofSingleValue(Object)}, which is valid for any number of scenarios. * * @param <T> the type of the market data value used in each scenario * @param scenarioValues the market data values for each scenario * @return a box containing a scenario market data value with data for multiple scenarios */ @SafeVarargs public static <T> MarketDataBox<T> ofScenarioValues(T... scenarioValues) { return ScenarioMarketDataBox.of(scenarioValues); }
@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case 111972721: // value return ((ScenarioMarketDataBox<?>) bean).getValue(); } return super.propertyGet(bean, propertyName, quiet); }
/** * Obtains an instance containing the specified market data values, one for each scenario. * * @param <T> the type of the market data value * @param values the single market data values, one for each scenario * @return a scenario market data box containing single market data values, one for each scenario */ @SafeVarargs public static <T> ScenarioMarketDataBox<T> of(T... values) { return new ScenarioMarketDataBox<>(ScenarioArray.of(values)); }
/** * Obtains an instance containing the specified market data values, one for each scenario. * * @param <T> the type of the market data value * @param values single market data values, one for each scenario * @return a scenario market data box containing single market data values, one for each scenario */ public static <T> ScenarioMarketDataBox<T> of(List<T> values) { return new ScenarioMarketDataBox<>(ScenarioArray.of(values)); }