return MapStream.of(ini).mapValues(b -> b.build()).toMap();
/** * Builds the sensitivity from the provided data. * <p> * If all the values for a single sensitivity are tenor-based, or all are date-based, * then the resulting sensitivity will have the tenors sorted. * * @return the sensitivities instance */ public CurveSensitivities build() { return CurveSensitivities.of(info, MapStream.of(data) .mapValues(CurrencyParameterSensitivitiesBuilder::build) .toMap()); }
/** * Returns a collector which creates a multi currency amount array by combining a stream of * currency amount arrays. * <p> * The arrays in the stream must all have the same length. * * @return the collector */ public static Collector<CurrencyAmountArray, ?, MultiCurrencyAmountArray> toMultiCurrencyAmountArray() { return Collector.<CurrencyAmountArray, Map<Currency, CurrencyAmountArray>, MultiCurrencyAmountArray>of( // accumulate into a map HashMap::new, (map, ca) -> map.merge(ca.getCurrency(), ca, CurrencyAmountArray::plus), // combine two maps (map1, map2) -> { map2.values().forEach((ca2) -> map1.merge(ca2.getCurrency(), ca2, CurrencyAmountArray::plus)); return map1; }, // convert to MultiCurrencyAmountArray map -> { Map<Currency, DoubleArray> currencyArrayMap = MapStream.of(map).mapValues(caa -> caa.getValues()).toMap(); return MultiCurrencyAmountArray.of(currencyArrayMap); }, UNORDERED); }
/** * Returns a collector which creates a multi currency scenario array by combining a stream of * currency scenario arrays. * <p> * The arrays in the stream must all have the same length. * * @return the collector */ public static Collector<CurrencyScenarioArray, ?, MultiCurrencyScenarioArray> toMultiCurrencyScenarioArray() { return Collector.<CurrencyScenarioArray, Map<Currency, CurrencyAmountArray>, MultiCurrencyScenarioArray>of( // accumulate into a map HashMap::new, (map, ca) -> map.merge(ca.getCurrency(), ca.getAmounts(), CurrencyAmountArray::plus), // combine two maps (map1, map2) -> { map2.values().forEach((ca2) -> map1.merge(ca2.getCurrency(), ca2, CurrencyAmountArray::plus)); return map1; }, // convert to MultiCurrencyScenarioArray map -> { Map<Currency, DoubleArray> currencyArrayMap = MapStream.of(map).mapValues(caa -> caa.getValues()).toMap(); return MultiCurrencyScenarioArray.of(currencyArrayMap); }, UNORDERED); }
public void mapKeysAndValuesToValues() { Map<String, String> expected = ImmutableMap.of("one", "one1", "two", "two2", "three", "three3", "four", "four4"); Map<String, String> result = MapStream.of(map).mapValues((k, v) -> k + v).toMap(); assertThat(result).isEqualTo(expected); }
/** * Obtains an instance using a function to create the entries. * <p> * The function is passed the index and returns the {@code MultiCurrencyAmount} for that index. * * @param size the number of elements, at least size one * @param valueFunction the function used to obtain each value * @return an instance initialized using the function * @throws IllegalArgumentException is size is zero or less */ public static MultiCurrencyAmountArray of(int size, IntFunction<MultiCurrencyAmount> valueFunction) { Map<Currency, double[]> map = new HashMap<>(); for (int i = 0; i < size; i++) { MultiCurrencyAmount mca = valueFunction.apply(i); for (CurrencyAmount ca : mca.getAmounts()) { double[] array = map.computeIfAbsent(ca.getCurrency(), c -> new double[size]); array[i] = ca.getAmount(); } } return new MultiCurrencyAmountArray(size, MapStream.of(map).mapValues(array -> DoubleArray.ofUnsafe(array)).toMap()); }
CurveGroupName groupName = repoEntry.getKey(); Map<Pair<RepoGroup, Currency>, Curve> repoCurves = MapStream.of(repoEntry.getValue()) .mapValues(name -> queryCurve(name, curves, date, groupName, "Repo")) .toMap(); Map<Pair<LegalEntityGroup, Currency>, Curve> issuerCurves = MapStream.of(legalEntityGroups.get(groupName)) .mapValues(name -> queryCurve(name, curves, date, groupName, "Issuer")) .toMap(); builder.put(date, LegalEntityCurveGroup.of(groupName, repoCurves, issuerCurves));
/** * Obtains an instance from the specified multi-currency amounts. * * @param amounts the amounts * @return an instance with the specified amounts */ public static MultiCurrencyAmountArray of(List<MultiCurrencyAmount> amounts) { int size = amounts.size(); HashMap<Currency, double[]> valueMap = new HashMap<>(); for (int i = 0; i < size; i++) { MultiCurrencyAmount multiCurrencyAmount = amounts.get(i); for (CurrencyAmount currencyAmount : multiCurrencyAmount.getAmounts()) { double[] currencyValues = valueMap.computeIfAbsent(currencyAmount.getCurrency(), ccy -> new double[size]); currencyValues[i] = currencyAmount.getAmount(); } } Map<Currency, DoubleArray> doubleArrayMap = MapStream.of(valueMap).mapValues(v -> DoubleArray.ofUnsafe(v)).toMap(); return new MultiCurrencyAmountArray(size, doubleArrayMap); }
public void mapValuesToValues() { Map<String, Integer> expected = ImmutableMap.of("one", 2, "two", 4, "three", 6, "four", 8); Map<String, Integer> result = MapStream.of(map).mapValues(v -> v * 2).toMap(); assertThat(result).isEqualTo(expected); }
/** * Checks and adjusts the parameter metadata. * <p> * The supplied function is invoked for each parameter metadata in this sensitivities. * If the function returns the same metadata for two different inputs, the sensitivity value will be summed. * A typical use case would be to normalize parameter metadata tenors to be valid for an underlying system. * * @param mdFn the function for checking and adjusting the metadata * @return the adjusted sensitivity * @throws RuntimeException if the function throws an exception */ public CurveSensitivities withParameterMetadatas(UnaryOperator<ParameterMetadata> mdFn) { return new CurveSensitivities( info, MapStream.of(typedSensitivities) .mapValues(sens -> sens.withParameterMetadatas(mdFn)) .toMap()); }
/** * Converts the sensitivities in this instance to an equivalent in the specified currency. * <p> * Any FX conversion that is required will use rates from the provider. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the sensitivity object expressed in terms of the result currency * @throws RuntimeException if no FX rate could be found */ @Override public CurveSensitivities convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { return new CurveSensitivities( info, MapStream.of(typedSensitivities) .mapValues(v -> v.convertedTo(resultCurrency, rateProvider)) .toMap()); }
/** * Checks and adjusts the market data names. * <p> * The supplied function is invoked for each market data name in this sensitivities. * A typical use case would be to convert index names to curve names valid for an underlying system. * * @param nameFn the function for checking and adjusting the name * @return the adjusted sensitivity * @throws RuntimeException if the function throws an exception */ public CurveSensitivities withMarketDataNames(Function<MarketDataName<?>, MarketDataName<?>> nameFn) { return new CurveSensitivities( info, MapStream.of(typedSensitivities) .mapValues(sens -> sens.withMarketDataNames(nameFn)) .toMap()); }
Messages.format("Error processing resource as CSV file: {}", resource), ex); return MapStream.of(builders).mapValues(builder -> builder.build()).toMap();