/** * Checks if this market data contains a value for the specified identifier. * * @param id the identifier to find * @return true if the market data contains a value for the identifier */ public default boolean containsValue(MarketDataId<?> id) { return findValue(id).isPresent(); }
@Override @SuppressWarnings("unchecked") public <R> Optional<MarketDataBox<R>> findValue(MarketDataId<R> id) { if (this.id.equals(id)) { return Optional.of((MarketDataBox<R>) value); } return underlying.findValue(id); }
@Override public <T> Optional<T> findValue(MarketDataId<T> id) { Optional<MarketDataBox<T>> optionalBox = marketData.findValue(id); return optionalBox.map(box -> box.getValue(scenarioIndex)); }
@Override public <T> MarketDataBox<T> getValue(MarketDataId<T> id) { Optional<MarketDataBox<T>> value1 = underlying1.findValue(id); return value1.isPresent() ? value1.get() : underlying2.getValue(id); }
/** * Gets the market data value associated with the specified identifier. * <p> * The result is a box that provides data for all scenarios. * If this market data instance contains the identifier, the value will be returned. * Otherwise, an exception will be thrown. * * @param <T> the type of the market data value * @param id the identifier to find * @return the market data value box providing data for all scenarios * @throws MarketDataNotFoundException if the identifier is not found */ public default <T> MarketDataBox<T> getValue(MarketDataId<T> id) { return findValue(id) .orElseThrow(() -> new MarketDataNotFoundException(Messages.format( "Market data not found for '{}' of type '{}'", id, id.getClass().getSimpleName()))); }
public void test_of() { Map<MarketDataId<?>, MarketDataBox<?>> dataMap = ImmutableMap.of(ID1, BOX1); Map<ObservableId, LocalDateDoubleTimeSeries> tsMap = ImmutableMap.of(ID1, TIME_SERIES); ScenarioMarketData test = ScenarioMarketData.of(2, VAL_DATE, dataMap, tsMap); assertThat(test.getValuationDate()).isEqualTo(MarketDataBox.ofSingleValue(VAL_DATE)); assertThat(test.containsValue(ID1)).isTrue(); assertThat(test.containsValue(ID2)).isFalse(); assertThat(test.getValue(ID1)).isEqualTo(BOX1); assertThrows(() -> test.getValue(ID2), MarketDataNotFoundException.class); assertThat(test.findValue(ID1)).hasValue(BOX1); assertThat(test.findValue(ID2)).isEmpty(); assertThat(test.getIds()).isEqualTo(ImmutableSet.of(ID1)); assertThat(test.getTimeSeries(ID1)).isEqualTo(TIME_SERIES); assertThat(test.getTimeSeries(ID2)).isEqualTo(LocalDateDoubleTimeSeries.empty()); }
public void test_empty() { ScenarioMarketData test = ScenarioMarketData.empty(); assertThat(test.getValuationDate()).isEqualTo(MarketDataBox.empty()); assertThat(test.containsValue(ID1)).isFalse(); assertThat(test.containsValue(ID2)).isFalse(); assertThrows(() -> test.getValue(ID1), MarketDataNotFoundException.class); assertThrows(() -> test.getValue(ID2), MarketDataNotFoundException.class); assertThat(test.findValue(ID1)).isEmpty(); assertThat(test.findValue(ID2)).isEmpty(); assertThat(test.getIds()).isEqualTo(ImmutableSet.of()); assertThat(test.getTimeSeries(ID1)).isEqualTo(LocalDateDoubleTimeSeries.empty()); assertThat(test.getTimeSeries(ID2)).isEqualTo(LocalDateDoubleTimeSeries.empty()); }
public void test_of_noScenarios() { Map<MarketDataId<?>, MarketDataBox<?>> dataMap = ImmutableMap.of(ID1, MarketDataBox.empty()); ScenarioMarketData test = ScenarioMarketData.of(0, VAL_DATE, dataMap, ImmutableMap.of()); assertThat(test.getValuationDate()).isEqualTo(MarketDataBox.ofSingleValue(VAL_DATE)); assertThat(test.containsValue(ID1)).isTrue(); assertThat(test.containsValue(ID2)).isFalse(); assertThat(test.getValue(ID1)).isEqualTo(MarketDataBox.empty()); assertThrows(() -> test.getValue(ID2), MarketDataNotFoundException.class); assertThat(test.findValue(ID1)).hasValue(MarketDataBox.empty()); assertThat(test.findValue(ID2)).isEmpty(); assertThat(test.getIds()).isEqualTo(ImmutableSet.of(ID1)); assertThat(test.getTimeSeries(ID1)).isEqualTo(LocalDateDoubleTimeSeries.empty()); assertThat(test.getTimeSeries(ID2)).isEqualTo(LocalDateDoubleTimeSeries.empty()); }