/** * Returns a {@code BinaryOperator} that combines {@code ValueWithFailures} objects using the provided combiner * function. * <p> * This would be used as follows (with a static import): * <pre> * stream.reduce(combiningValues(Guavate::concatToList)); * * stream.reduce(baseValueWithFailures, combiningValues(Guavate::concatToList)); * </pre> * <p> * This replaces code of the form: * <pre> * stream.reduce((vwf1, vwf2) -> vwf1.combinedWith(vwf2, Guavate::concatToList)); * * stream.reduce(baseValueWithFailures, (vwf1, vwf2) -> vwf1.combinedWith(vwf2, Guavate::concatToList)); * </pre> * * @param combiner the combiner of the values * @param <T> the type of the values * @return the combining binary operator */ public static <T> BinaryOperator<ValueWithFailures<T>> combiningValues(BinaryOperator<T> combiner) { ArgChecker.notNull(combiner, "combiner"); return (vwf1, vwf2) -> vwf1.combinedWith(vwf2, combiner); }
/** * Parses one or more CSV format trade files with a quiet type filter. * <p> * A type is specified to filter the trades. * Trades that do not match the type are silently dropped. * <p> * CSV files sometimes contain a Unicode Byte Order Mark. * Callers are responsible for handling this, such as by using {@link UnicodeBom}. * * @param <T> the trade type * @param charSources the CSV character sources * @param tradeType the trade type to return * @return the loaded trades, all errors are captured in the result */ public <T extends Trade> ValueWithFailures<List<T>> parse(Collection<CharSource> charSources, Class<T> tradeType) { try { ValueWithFailures<List<T>> result = ValueWithFailures.of(ImmutableList.of()); for (CharSource charSource : charSources) { ValueWithFailures<List<T>> singleResult = parseFile(charSource, tradeType); result = result.combinedWith(singleResult, Guavate::concatToList); } return result; } catch (RuntimeException ex) { return ValueWithFailures.of(ImmutableList.of(), FailureItem.of(FailureReason.ERROR, ex)); } }
/** * Parses one or more CSV format position files. * <p> * A type is specified to filter the positions. * If the type is {@link SecurityPosition}, then ETD parsing will proceed as per {@link #parseLightweight(Collection)}. * Otherwise, ETD parsing will proceed as per {@link #parse(Collection)}. * <p> * CSV files sometimes contain a Unicode Byte Order Mark. * Callers are responsible for handling this, such as by using {@link UnicodeBom}. * * @param <T> the position type * @param charSources the CSV character sources * @param positionType the position type to return * @return the loaded positions, all errors are captured in the result */ public <T extends Position> ValueWithFailures<List<T>> parse(Collection<CharSource> charSources, Class<T> positionType) { try { ValueWithFailures<List<T>> result = ValueWithFailures.of(ImmutableList.of()); for (CharSource charSource : charSources) { ValueWithFailures<List<T>> singleResult = parseFile(charSource, positionType); result = result.combinedWith(singleResult, Guavate::concatToList); } return result; } catch (RuntimeException ex) { return ValueWithFailures.of(ImmutableList.of(), FailureItem.of(FailureReason.ERROR, ex)); } }
public void test_combinedWith_differentTypes() { ValueWithFailures<Boolean> base = ValueWithFailures.of(Boolean.TRUE, ImmutableList.of(FAILURE1)); ValueWithFailures<Integer> other = ValueWithFailures.of(Integer.valueOf(1), ImmutableList.of(FAILURE2)); ValueWithFailures<String> test = base.combinedWith(other, (a, b) -> a.toString() + b.toString()); assertEquals(test.getValue(), "true1"); assertEquals(test.getFailures(), ImmutableList.of(FAILURE1, FAILURE2)); }
public void test_combinedWith() { ValueWithFailures<List<String>> base = ValueWithFailures.of(ImmutableList.of("a"), ImmutableList.of(FAILURE1)); ValueWithFailures<List<String>> other = ValueWithFailures.of(ImmutableList.of("b", "c"), ImmutableList.of(FAILURE2)); ValueWithFailures<List<String>> test = base.combinedWith(other, Guavate::concatToList); assertEquals(test.getValue(), ImmutableList.of("a", "b", "c")); assertEquals(test.getFailures(), ImmutableList.of(FAILURE1, FAILURE2)); }