/** * Returns a stream of the values. * <p> * The stream will return the value for each scenario. * * @return a stream of the values */ public default Stream<T> stream() { return IntStream.range(0, getScenarioCount()).mapToObj(i -> get(i)); }
/** * 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)); }
@Override public Stream<T> stream() { return value.stream(); }
@Override public int getScenarioCount() { return value.getScenarioCount(); }
@Override public Class<?> getMarketDataType() { return value.get(0).getClass(); }
public void test_ofSingleValue() { ScenarioArray<String> test = ScenarioArray.ofSingleValue(3, "aaa"); SingleScenarioArray<String> expected = SingleScenarioArray.of(3, "aaa"); assertEquals(test, expected); }
private <R> MarketDataBox<R> applyToScenarios(Function<Integer, R> fn) { List<R> results = IntStream.range(0, value.getScenarioCount()) .mapToObj(fn::apply) .collect(toImmutableList()); return MarketDataBox.ofScenarioValues(results); }
@Override public <R> MarketDataBox<R> map(Function<T, R> fn) { return applyToScenarios(i -> fn.apply(value.get(i))); }
@Override public T getValue(int scenarioIndex) { ArgChecker.inRange(scenarioIndex, 0, value.getScenarioCount(), "scenarioIndex"); return value.get(scenarioIndex); }
/** * 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)); }
private <U, R> MarketDataBox<R> combineWithMultiple(MarketDataBox<U> other, BiFunction<T, U, R> fn) { ScenarioArray<U> otherValue = other.getScenarioValue(); int scenarioCount = otherValue.getScenarioCount(); List<R> values = IntStream.range(0, scenarioCount) .mapToObj(i -> fn.apply(value, other.getValue(i))) .collect(toImmutableList()); return MarketDataBox.ofScenarioValues(values); }
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)); }
public void test_stream() { ScenarioArray<String> test = new ScenarioArray<String>() { @Override public int getScenarioCount() { return 3; } @Override public String get(int scenarioIndex) { return "" + scenarioIndex; } }; List<String> output = test.stream().collect(toImmutableList()); assertEquals(output, ImmutableList.of("0", "1", "2")); }
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))); }
public void test_of_function() { ScenarioArray<String> test = ScenarioArray.of(3, i -> Integer.toString(i + 1)); DefaultScenarioArray<String> expected = DefaultScenarioArray.of("1", "2", "3"); assertEquals(test, expected); }
public void getScenarioValue() { MarketDataBox<Integer> box = MarketDataBox.ofScenarioValues(27, 28, 29); ScenarioArray<Integer> scenarioValue = box.getScenarioValue(); assertThat(scenarioValue.getScenarioCount()).isEqualTo(3); assertThat(scenarioValue.get(0)).isEqualTo(27); assertThat(scenarioValue.get(1)).isEqualTo(28); assertThat(scenarioValue.get(2)).isEqualTo(29); }
public void test_of_list() { ScenarioArray<String> test = ScenarioArray.of(ImmutableList.of("1", "2", "3")); DefaultScenarioArray<String> expected = DefaultScenarioArray.of("1", "2", "3"); assertEquals(test, expected); }
public void test_of_array() { ScenarioArray<String> test = ScenarioArray.of("1", "2", "3"); DefaultScenarioArray<String> expected = DefaultScenarioArray.of("1", "2", "3"); assertEquals(test, expected); }