/** * Helper method to transform a list of lists into their immutable counterparts. * * @param lists the list of lists * @param <T> the type of element in the list * @return the immutable lists */ private static <T> ImmutableList<ImmutableList<T>> toImmutables(List<List<T>> lists) { return lists.stream() .map(ImmutableList::copyOf) .collect(Guavate.toImmutableList()); }
private static Set<DayOfWeek> parseWeekends(String str) { List<String> split = Splitter.on(',').splitToList(str); return split.stream() .map(v -> DOW_PARSER.parse(v, DayOfWeek::from)) .collect(toImmutableSet()); }
/** * Returns an immutable map built from the entries in the stream. * <p> * The keys must be unique or an exception will be thrown. * Duplicate keys can be handled using {@link #toMap(BiFunction)}. * <p> * This is a terminal operation. * * @return an immutable map built from the entries in the stream * @throws IllegalArgumentException if the same key occurs more than once */ public ImmutableMap<K, V> toMap() { return underlying.collect(Guavate.toImmutableMap(Entry::getKey, Entry::getValue)); }
private static Map<Object, Integer> buildNodeMap(List<Object> nodeIdentifiers) { return Guavate.zipWithIndex(nodeIdentifiers.stream()) .collect(toImmutableMap(ObjIntPair::getFirst, ObjIntPair::getSecond)); }
@Override public ImmutableSet<OvernightIndex> getOvernightIndices() { return indexCurves.keySet().stream() .flatMap(filtering(OvernightIndex.class)) .collect(toImmutableSet()); }
private static ImmutableSet<IborIndex> buildIndices(RateComputation floatingRate) { ImmutableSet.Builder<Index> builder = ImmutableSet.builder(); floatingRate.collectIndices(builder); return builder.build().stream() .map(casting(IborIndex.class)) .collect(toImmutableSet()); }
public void test_load_genericSecurity() { PositionCsvLoader test = PositionCsvLoader.standard(); ValueWithFailures<List<Position>> trades = test.load(FILE); List<GenericSecurityPosition> filtered = trades.getValue().stream() .flatMap(filtering(GenericSecurityPosition.class)) .collect(toImmutableList()); assertEquals(filtered.size(), 1); assertBeanEquals(SECURITY3FULL, filtered.get(0)); }
private static ImmutableMap<CurveGroupName, RatesCurveGroupDefinition> parse0( CharSource groupsCharSource, CharSource settingsCharSource, Map<CurveName, SeasonalityDefinition> seasonality, Collection<CharSource> curveNodeCharSources) { // load curve groups and settings List<RatesCurveGroupDefinition> curveGroups = RatesCurveGroupDefinitionCsvLoader.parseCurveGroupDefinitions(groupsCharSource); Map<CurveName, LoadedCurveSettings> settingsMap = RatesCurvesCsvLoader.parseCurveSettings(settingsCharSource); // load curve definitions List<CurveDefinition> curveDefinitions = curveNodeCharSources.stream() .flatMap(res -> parseSingle(res, settingsMap).stream()) .collect(toImmutableList()); // Add the curve definitions to the curve group definitions return curveGroups.stream() .map(groupDefinition -> groupDefinition.withCurveDefinitions(curveDefinitions).withSeasonalityDefinitions(seasonality)) .collect(toImmutableMap(groupDefinition -> groupDefinition.getName())); }
@Override public ScenarioArray<?> convertedTo(Currency resultCurrency, ScenarioFxRateProvider fxRateProvider) { int scenarioCount = getScenarioCount(); if (fxRateProvider.getScenarioCount() != scenarioCount) { throw new IllegalArgumentException(Messages.format( "Expected {} FX rates but received {}", scenarioCount, fxRateProvider.getScenarioCount())); } ImmutableList<Object> converted = zipWithIndex(values.stream()) .map(tp -> convert(resultCurrency, fxRateProvider, tp.getFirst(), tp.getSecond())) .collect(toImmutableList()); return DefaultScenarioArray.of(converted); }
/** * Finds the period that contains the specified date. * <p> * The search is performed using unadjusted dates. * * @param date the date to find the period for * @return the period, empty if not found * @throws IllegalArgumentException if more than one period matches */ public Optional<CapitalIndexedBondPaymentPeriod> findPeriod(LocalDate date) { return periodicPayments.stream() .filter(p -> p.contains(date)) .reduce(ensureOnlyOne()); }
/** * Creates a failed result combining multiple failed results. * <p> * The input results can be successes or failures, only the failures will be included in the created result. * Intended to be used with {@link #anyFailures(Iterable)}. * <blockquote><pre> * if (Result.anyFailures(results) { * return Result.failure(results); * } * </pre></blockquote> * * @param <R> the expected type of the result * @param results multiple results, of which at least one must be a failure, not empty * @return a failed result wrapping multiple other failed results * @throws IllegalArgumentException if results is empty or contains nothing but successes */ public static <R> Result<R> failure(Iterable<? extends Result<?>> results) { ArgChecker.notEmpty(results, "results"); ImmutableSet<FailureItem> items = Guavate.stream(results) .filter(Result::isFailure) .map(Result::getFailure) .flatMap(f -> f.getItems().stream()) .collect(Guavate.toImmutableSet()); if (items.isEmpty()) { throw new IllegalArgumentException("All results were successes"); } return new Result<>(Failure.of(items)); }
/** * Checks if all the results are successful. * * @param results the results to check * @return true if all of the results are successes */ public static boolean allSuccessful(Iterable<? extends Result<?>> results) { return Guavate.stream(results).allMatch(Result::isSuccess); }
/** * Calibrates a single curve group, containing one or more curves. * <p> * The calibration is defined using {@link RatesCurveGroupDefinition}. * Observable market data, time-series and FX are also needed to complete the calibration. * The valuation date is defined by the market data. * <p> * The Jacobian matrices are computed and stored in curve metadata. * * @param curveGroupDefn the curve group definition * @param marketData the market data required to build a trade for the instrument, including time-series * @param refData the reference data, used to resolve the trades * @return the rates provider resulting from the calibration */ public ImmutableRatesProvider calibrate( RatesCurveGroupDefinition curveGroupDefn, MarketData marketData, ReferenceData refData) { Map<Index, LocalDateDoubleTimeSeries> timeSeries = marketData.getTimeSeriesIds().stream() .flatMap(filtering(IndexQuoteId.class)) .collect(toImmutableMap(id -> id.getIndex(), id -> marketData.getTimeSeries(id))); ImmutableRatesProvider knownData = ImmutableRatesProvider.builder(marketData.getValuationDate()) .fxRateProvider(MarketDataFxRateProvider.of(marketData)) .timeSeries(timeSeries) .build(); return calibrate(ImmutableList.of(curveGroupDefn), knownData, marketData, refData); }
private double computeIndexFactor(CdsIndexIsdaCreditCurveNode node, MarketData marketData) { double numDefaulted = node.getLegalEntityIds().stream() .map(s -> marketData.getValue(LegalEntityInformationId.of(s))) .map(casting(LegalEntityInformation.class)) .filter(LegalEntityInformation::isDefaulted) .collect(Collectors.toList()) .size(); double numTotal = node.getLegalEntityIds().size(); return (numTotal - numDefaulted) / numTotal; }
public void test_filtering() { List<Number> list = ImmutableList.of(1, 2d, 3f, 4, (short) 5, 6L, 7); assertEquals(Stream.empty().flatMap(Guavate.filtering(Integer.class)).collect(toList()), ImmutableList.of()); assertEquals(list.stream().flatMap(Guavate.filtering(Integer.class)).collect(toList()), ImmutableList.of(1, 4, 7)); assertEquals(list.stream().flatMap(Guavate.filtering(Double.class)).collect(toList()), ImmutableList.of(2d)); }
public void test_load_security() { PositionCsvLoader test = PositionCsvLoader.standard(); ValueWithFailures<List<Position>> trades = test.load(FILE); List<SecurityPosition> filtered = trades.getValue().stream() .flatMap(filtering(SecurityPosition.class)) .collect(toImmutableList()); assertEquals(filtered.size(), 2); assertBeanEquals(SECURITY1, filtered.get(0)); assertBeanEquals(SECURITY2, filtered.get(1)); }
@Override public ImmutableSet<PriceIndex> getPriceIndices() { return indexCurves.keySet().stream() .flatMap(filtering(PriceIndex.class)) .collect(toImmutableSet()); }
/** * Finds the period that contains the specified date. * <p> * The search is performed using unadjusted dates. * * @param date the date to find the period for * @return the period, empty if not found * @throws IllegalArgumentException if more than one period matches */ public Optional<CreditCouponPaymentPeriod> findPeriod(LocalDate date) { return paymentPeriods.stream() .filter(p -> p.contains(date)) .reduce(ensureOnlyOne()); }
/** * Checks if any of the results are failures. * * @param results the results to check * @return true if any of the results are failures */ public static boolean anyFailures(Iterable<? extends Result<?>> results) { return Guavate.stream(results).anyMatch(Result::isFailure); }
public void test_casting() { assertEquals(Stream.empty().map(Guavate.casting(Integer.class)).collect(toList()), ImmutableList.of()); List<Number> baseList = Arrays.asList(1, 2, 3); List<Integer> castList = baseList.stream().map(Guavate.casting(Integer.class)).collect(toList()); assertEquals(castList, baseList); List<Number> baseListMixed = ImmutableList.of(1, 2f, 3); assertThrows( ClassCastException.class, () -> baseListMixed.stream().map(Guavate.casting(Short.class)).collect(toList())); }