/** * 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()); }
private void retryDisjoints() { ensureCapacity(disjointRates.keySet() .stream() .flatMap(cp -> Stream.of(cp.getBase(), cp.getCounter()))); while (true) { int initialSize = disjointRates.size(); ImmutableMap<CurrencyPair, Double> addable = MapStream.of(disjointRates) .filterKeys(pair -> currencies.containsKey(pair.getBase()) || currencies.containsKey(pair.getCounter())) .toMap(); MapStream.of(addable).forEach((pair, rate) -> addNewRate(pair.getBase(), pair.getCounter(), rate)); addable.keySet().stream().forEach(disjointRates::remove); if (disjointRates.size() == initialSize) { // No effect so break out break; } } }
/** * Maps the sensitivity metadata. * * @return this, for chaining */ CurrencyParameterSensitivityBuilder mapMetadata(UnaryOperator<ParameterMetadata> metadataFn) { ImmutableMap<ParameterMetadata, Double> base = ImmutableMap.copyOf(sensitivity); sensitivity.clear(); MapStream.of(base) .mapKeys(metadataFn) .forEach((md, v) -> add(md, v)); return this; }
/** * Merges this set of sensitivities with another set. * <p> * This returns a new curve sensitivities with a combined map of typed sensitivities. * Any sensitivities of the same type will be combined as though using * {@link CurrencyParameterSensitivities#mergedWith(CurrencyParameterSensitivities)}. * * @param other the other parameter sensitivities * @return an instance based on this one, with the other instance added */ public CurveSensitivities mergedWith(Map<CurveSensitivitiesType, CurrencyParameterSensitivities> other) { // this uses a collector to merge all the instances at once which is more efficient than reduction // because it creates a single CurrencyParameterSensitivitiesBuilder ImmutableMap<CurveSensitivitiesType, CurrencyParameterSensitivities> combinedSens = MapStream.concat(MapStream.of(typedSensitivities), MapStream.of(other)) .toMapGrouping(mergeSensitivities()); return new CurveSensitivities(info, combinedSens); }
public void coverage() { MapStream.empty() .filter(e -> false) .distinct() .sorted() .sorted((e1, e2) -> 0) .peek(e -> e.toString()) .limit(0) .skip(0) .sequential() .parallel() .unordered() .onClose(() -> System.out.println()) .close(); MapStream.empty().anyMatch(e -> true); MapStream.empty().allMatch(e -> true); MapStream.empty().noneMatch(e -> true); MapStream.empty().count(); MapStream.empty().findAny(); MapStream.empty().findFirst(); MapStream.empty().max((e1, e2) -> 0); MapStream.empty().min((e1, e2) -> 0); MapStream.empty().iterator(); MapStream.empty().spliterator(); MapStream.empty().isParallel(); MapStream.empty().map(e -> e); MapStream.empty().mapToInt(e -> 0); MapStream.empty().mapToLong(e -> 0); MapStream.empty().mapToDouble(e -> 0); MapStream.empty().flatMap(e -> Stream.empty());
public void ofStream_2arg() { Stream<String> letters = Stream.of("a", "b", "c"); Map<String, String> expected = ImmutableMap.of("A", "aa", "B", "bb", "C", "cc"); Map<String, String> result = MapStream.of(letters, letter -> letter.toUpperCase(Locale.ENGLISH), letter -> letter + letter).toMap(); assertThat(result).isEqualTo(expected); }
/** * Returns a copy of this object containing the specified seasonality definitions. * <p> * Seasonality definitions are ignored if there is no entry in this definition with the same curve name. * * @param seasonalityDefinitions seasonality definitions * @return a copy of this object containing the specified seasonality definitions */ public RatesCurveGroupDefinition withSeasonalityDefinitions(Map<CurveName, SeasonalityDefinition> seasonalityDefinitions) { Set<CurveName> curveNames = entries.stream().map(entry -> entry.getCurveName()).collect(toSet()); Map<CurveName, SeasonalityDefinition> filteredDefinitions = MapStream.of(seasonalityDefinitions) .filterKeys(cn -> curveNames.contains(cn)).toMap(); return new RatesCurveGroupDefinition( name, entries, curveDefinitions, filteredDefinitions, computeJacobian, computePvSensitivityToMarketQuote); }
public void concatGeneric() { ImmutableMap<String, Object> map1 = ImmutableMap.of("one", 1, "two", 2, "three", 3); ImmutableMap<Object, Integer> map2 = ImmutableMap.of("three", 7, "four", 4); ImmutableMap<Object, Object> result = MapStream.concat(MapStream.of(map1), MapStream.of(map2)).toMap((a, b) -> a); assertThat(result).isEqualTo(map); }
/** * Obtains an instance from a valuation date and map of values. * <p> * Use the {@linkplain #builder(LocalDate) builder} for more more complex use cases, * including setting time-series. * * @param valuationDate the valuation date associated with the market data * @param values the market data values * @return a set of market data containing the values in the map * @throws ClassCastException if a value does not match the parameterized type associated with the identifier */ public static ImmutableMarketData of(LocalDate valuationDate, Map<? extends MarketDataId<?>, ?> values) { MapStream.of(values).forEach((id, value) -> checkType(id, value)); return new ImmutableMarketData(valuationDate, values, ImmutableMap.of()); }
MapStream.of(curveSens.getTypedSensitivities()) .flatMapValues(sens -> sens.getSensitivities().stream()) .mapKeys((type, sens) -> Pair.of(sens.getMarketDataName().getName(), type)) .sortedKeys() .forEach((pair, paramSens) -> write( pair.getFirst(), pair.getSecond(), curveSens, paramSens, additionalHeaders, containsDates, csv));
public void filterValues() { Map<String, Integer> expected = ImmutableMap.of("one", 1, "two", 2); Map<String, Integer> result = MapStream.of(map).filterValues(v -> v < 3).toMap(); assertThat(result).isEqualTo(expected); }
public void flatMapKeysAndValuesToKeys() { Map<String, Integer> expected = ImmutableMap.<String, Integer>builder() .put("one", 1) .put("1", 1) .put("two", 2) .put("2", 2) .put("three", 3) .put("3", 3) .put("four", 4) .put("4", 4) .build(); ImmutableMap<String, Integer> result = MapStream.of(map) .flatMapKeys((key, value) -> Stream.of(key, Integer.toString(value))) .toMap(); assertThat(result).isEqualTo(expected); }
FxMatrixBuilder merge(FxMatrixBuilder other) { // Find the common currencies Optional<Currency> common = currencies.keySet() .stream() .filter(other.currencies::containsKey) .findFirst(); Currency commonCurrency = common.orElseThrow(() -> new IllegalArgumentException( "There are no currencies in common between " + currencies.keySet() + " and " + other.currencies.keySet())); // Add in all currencies that we don't already have MapStream.of(other.currencies) .filterKeys(ccy -> !ccy.equals(commonCurrency) && !currencies.containsKey(ccy)) .forEach((ccy, idx) -> addCurrencyPair(commonCurrency, ccy, other.getRate(commonCurrency, ccy))); return this; }
/** * Builds the sensitivity from the provided data. * * @return the sensitivities instance */ CurrencyParameterSensitivity build() { ImmutableSet<Class<?>> metadataTypes = sensitivity.keySet().stream().map(Object::getClass).collect(toImmutableSet()); if (metadataTypes.size() == 1) { if (TenoredParameterMetadata.class.isAssignableFrom(metadataTypes.iterator().next())) { Map<ParameterMetadata, Double> sorted = MapStream.of(sensitivity) .sortedKeys(Comparator.comparing(k -> ((TenoredParameterMetadata) k).getTenor())) .toMap(); return CurrencyParameterSensitivity.of(marketDataName, currency, sorted); } if (DatedParameterMetadata.class.isAssignableFrom(metadataTypes.iterator().next())) { Map<ParameterMetadata, Double> sorted = MapStream.of(sensitivity) .sortedKeys(Comparator.comparing(k -> ((DatedParameterMetadata) k).getDate())) .toMap(); return CurrencyParameterSensitivity.of(marketDataName, currency, sorted); } } return CurrencyParameterSensitivity.of(marketDataName, currency, sensitivity); }
public void toMapGroupingWithCollector() { Map<String, Integer> map = ImmutableMap.of("a", 1, "aa", 2, "b", 10, "bb", 20, "c", 1); Map<String, Integer> expected = ImmutableMap.of("a", 3, "b", 30, "c", 1); Map<String, Integer> result = MapStream.of(map).mapKeys(s -> s.substring(0, 1)) .toMapGrouping(reducing(0, Integer::sum)); assertThat(result).isEqualTo(expected); }
/** * Obtains an instance from a map of currency to amount. * * @param map the map of currency to amount * @return the amount */ public static MultiCurrencyAmount of(Map<Currency, Double> map) { ArgChecker.noNulls(map, "map"); return MapStream.of(map).map(CurrencyAmount::of).collect(MultiCurrencyAmount.collectorInternal()); }
public void flatMapKeysAndValuesToValues() { List<Pair<String, String>> expected = ImmutableList.of( Pair.of("one", "one"), Pair.of("one", "1"), Pair.of("two", "two"), Pair.of("two", "2"), Pair.of("three", "three"), Pair.of("three", "3"), Pair.of("four", "four"), Pair.of("four", "4")); List<Pair<String, String>> result = MapStream.of(map) .flatMapValues((key, value) -> Stream.of(key, Integer.toString(value))) .map((k, v) -> Pair.of(k, v)) .collect(toList()); assertThat(result).isEqualTo(expected); }
@Override public EvaluationResult evaluate( Map<?, ?> map, CalculationFunctions functions, String firstToken, List<String> remainingTokens) { return MapStream.of(map) .filterKeys(key -> firstToken.equalsIgnoreCase(key.toString())) .findFirst() .map(e -> EvaluationResult.success(e.getValue(), remainingTokens)) .orElse(invalidTokenFailure(map, firstToken)); } }