public void combineWithScenarioBox() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); MarketDataBox<Integer> otherBox = MarketDataBox.ofScenarioValues(15, 16, 17); MarketDataBox<Integer> resultBox = box.combineWith(otherBox, (v1, v2) -> v1 + v2); assertThat(resultBox.isScenarioValue()).isTrue(); assertThat(resultBox.getScenarioCount()).isEqualTo(3); assertThat(resultBox.getValue(0)).isEqualTo(42); assertThat(resultBox.getValue(1)).isEqualTo(43); assertThat(resultBox.getValue(2)).isEqualTo(44); }
public void getMarketDataType() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); assertThat(box.getMarketDataType()).isEqualTo(Integer.class); }
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 MarketDataBox.ofSingleValue(fn.apply(value, otherValue)); }
private void checkAndUpdateScenarioCount(MarketDataBox<?> value) { if (value.isScenarioValue()) { if (scenarioCount == -1) { scenarioCount = value.getScenarioCount(); } else if (value.getScenarioCount() != scenarioCount) { throw new IllegalArgumentException(Messages.format( "All values must have the same number of scenarios, expecting {} but received {}", scenarioCount, value.getScenarioCount())); } } }
/** * Test that the box always returns the same value for any non-negative scenario index. */ public void getValue() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); assertThat(box.getValue(0)).isEqualTo(27); assertThat(box.getValue(Integer.MAX_VALUE)).isEqualTo(27); assertThrows(() -> box.getValue(-1), IllegalArgumentException.class); }
public void test_none() { ScenarioPerturbation<Double> test = ScenarioPerturbation.none(); assertEquals(test.getScenarioCount(), 1); MarketDataBox<Double> box1 = MarketDataBox.ofScenarioValues(1d, 2d, 3d); assertEquals(test.applyTo(box1, REF_DATA), box1); MarketDataBox<Double> box2 = MarketDataBox.ofSingleValue(1d); assertEquals(test.applyTo(box2, REF_DATA), box2); }
/** * Tests that applying a function multiple times to the value creates a box of scenario values. */ public void mapWithIndex() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); MarketDataBox<Integer> scenarioBox = box.mapWithIndex(3, (v, idx) -> v + idx); assertThat(scenarioBox.isScenarioValue()).isTrue(); assertThat(scenarioBox.getScenarioCount()).isEqualTo(3); assertThat(scenarioBox.getValue(0)).isEqualTo(27); assertThat(scenarioBox.getValue(1)).isEqualTo(28); assertThat(scenarioBox.getValue(2)).isEqualTo(29); }
@Override public <R> MarketDataBox<R> map(Function<T, R> fn) { return MarketDataBox.ofSingleValue(fn.apply(value)); }
/** * Tests that applying a function multiple times to the value creates a box of scenario values. */ public void mapWithIndex() { MarketDataBox<Integer> box = MarketDataBox.ofScenarioValues(27, 28, 29); MarketDataBox<Integer> scenarioBox = box.mapWithIndex(3, (v, idx) -> v + idx); assertThat(scenarioBox.isScenarioValue()).isTrue(); assertThat(scenarioBox.getScenarioCount()).isEqualTo(3); assertThat(scenarioBox.getValue(0)).isEqualTo(27); assertThat(scenarioBox.getValue(1)).isEqualTo(29); assertThat(scenarioBox.getValue(2)).isEqualTo(31); }
public void combineWithSingleBox() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); MarketDataBox<Integer> otherBox = MarketDataBox.ofSingleValue(15); MarketDataBox<Integer> resultBox = box.combineWith(otherBox, (v1, v2) -> v1 + v2); assertThat(resultBox.isSingleValue()).isTrue(); assertThat(resultBox.getValue(0)).isEqualTo(42); }
@Override public <T> Optional<T> findValue(MarketDataId<T> id) { Optional<MarketDataBox<T>> optionalBox = marketData.findValue(id); return optionalBox.map(box -> box.getValue(scenarioIndex)); }
static void checkType(MarketDataId<?> id, MarketDataBox<?> box, int scenarioCount) { if (box == null) { throw new IllegalArgumentException(Messages.format( "Value for identifier '{}' must not be null", id)); } if (box.isScenarioValue() && box.getScenarioCount() != scenarioCount) { throw new IllegalArgumentException(Messages.format( "Value for identifier '{}' should have had {} scenarios but had {}", id, scenarioCount, box.getScenarioCount())); } if (box.getScenarioCount() > 0 && !id.getMarketDataType().isInstance(box.getValue(0))) { throw new ClassCastException(Messages.format( "Value for identifier '{}' does not implement expected type '{}': '{}'", id, id.getMarketDataType().getSimpleName(), box)); } }
public void getValue() { MarketDataBox<Integer> box = MarketDataBox.ofScenarioValues(27, 28, 29); assertThat(box.getValue(0)).isEqualTo(27); assertThat(box.getValue(1)).isEqualTo(28); assertThat(box.getValue(2)).isEqualTo(29); assertThrows(() -> box.getValue(-1), IllegalArgumentException.class, "Expected 0 <= 'scenarioIndex' < 3, but found -1"); assertThrows(() -> box.getValue(3), IllegalArgumentException.class, "Expected 0 <= 'scenarioIndex' < 3, but found 3"); }
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); }
public void isSingleOrScenarioValue() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); assertThat(box.isSingleValue()).isTrue(); assertThat(box.isScenarioValue()).isFalse(); }
@Override public MarketDataBox<ParameterizedData> applyTo( MarketDataBox<ParameterizedData> marketData, ReferenceData refData) { log.debug("Applying {} point shift to ParameterizedData '{}'", shiftType, marketData.getValue(0).toString()); return marketData.mapWithIndex( shifts.rowCount(), (prams, scenarioIndex) -> applyShifts(scenarioIndex, prams)); }
public void isSingleOrScenarioValue() { MarketDataBox<Integer> box = MarketDataBox.ofScenarioValues(27, 28, 29); assertThat(box.isSingleValue()).isFalse(); assertThat(box.isScenarioValue()).isTrue(); }
public void getScenarioCount() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); assertThat(box.getScenarioCount()).isEqualTo(-1); }
public void map() { MarketDataBox<Integer> box = MarketDataBox.ofSingleValue(27); MarketDataBox<Integer> result = box.map(v -> v * 2); assertThat(result).isEqualTo(MarketDataBox.ofSingleValue(54)); }