@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case 111972721: // value return ((ValueWithFailures<?>) bean).getValue(); case 675938345: // failures return ((ValueWithFailures<?>) bean).getFailures(); } return super.propertyGet(bean, propertyName, quiet); }
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)); }
@Test public void test_combining() { 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 = Stream.of(base, other) .reduce(ValueWithFailures.combiningValues(Guavate::concatToList)) .get(); assertEquals(test.getValue(), ImmutableList.of("a", "b", "c")); assertEquals(test.getFailures(), ImmutableList.of(FAILURE1, FAILURE2)); }
/** * Returns a new instance with the specified value, combining the failures. * <p> * This can be useful as an inline alternative to {@link #flatMap(Function)}. * * @param <R> the type of the value in the returned result * @param valueWithFailures the new value with failures * @return the combined instance of value and failures */ public <R> ValueWithFailures<R> withValue(ValueWithFailures<R> valueWithFailures) { return ValueWithFailures.of( valueWithFailures.getValue(), concatToList(this.failures, valueWithFailures.getFailures())); }
/** * Returns a collector that can be used to create a ValueWithFailure instance from a stream of ValueWithFailure * instances. * <p> * The {@link Collector} returned performs a reduction of its {@link ValueWithFailures} input elements under a * specified {@link BinaryOperator} using the provided identity. * * @param <T> the type of the success value in the {@link ValueWithFailures} * @param identityValue the identity value * @param operator the operator used for the reduction. * @return a {@link Collector} */ public static <T> Collector<ValueWithFailures<T>, ?, ValueWithFailures<T>> toValueWithFailures( T identityValue, BinaryOperator<T> operator) { return Collectors.reducing(ValueWithFailures.of(identityValue), combiningValues(operator)); }
public void test_of_array() { ValueWithFailures<String> test = ValueWithFailures.of("success", FAILURE1, FAILURE2); assertEquals(test.hasFailures(), true); assertEquals(test.getValue(), "success"); assertEquals(test.getFailures(), ImmutableList.of(FAILURE1, FAILURE2)); }
public void test_withValue_ValueWithFailures() { ValueWithFailures<List<String>> base = ValueWithFailures.of(ImmutableList.of("a"), ImmutableList.of(FAILURE1)); ValueWithFailures<String> test = base.withValue(ValueWithFailures.of("combined", ImmutableList.of(FAILURE2))); assertEquals(test.getValue(), "combined"); assertEquals(test.getFailures(), ImmutableList.of(FAILURE1, FAILURE2)); }
public void test_withAdditionalFailures() { ValueWithFailures<String> base = ValueWithFailures.of("combined", ImmutableList.of(FAILURE1)); ValueWithFailures<String> test = base.withAdditionalFailures(ImmutableList.of(FAILURE2)); assertEquals(test.getValue(), "combined"); assertEquals(test.getFailures(), ImmutableList.of(FAILURE1, FAILURE2)); }
public void test_parse_list_ioException() { CharSource source = new CharSource() { @Override public Reader openStream() throws IOException { throw new IOException("Oops"); } }; ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER.parse(ImmutableList.of(source)); assertEquals(test.getFailures().size(), 1); assertEquals(test.getFailures().get(0).getReason(), FailureReason.PARSING); assertEquals(test.getFailures().get(0).getMessage().startsWith("CSV file could not be parsed: "), true); assertEquals(test.getFailures().get(0).getMessage().contains("Oops"), true); }
public void test_flatMap() { ValueWithFailures<List<String>> base = ValueWithFailures.of(ImmutableList.of("1", "a", "2"), ImmutableList.of(FAILURE1)); ValueWithFailures<List<Integer>> test = base.flatMap(this::flatMapFunction); assertEquals(test.getValue(), ImmutableList.of(Integer.valueOf(1), Integer.valueOf(2))); assertEquals(test.getFailures().size(), 2); assertEquals(test.getFailures().get(0), FAILURE1); assertEquals(test.getFailures().get(1).getReason(), FailureReason.INVALID); }
public void test_map() { ValueWithFailures<List<String>> base = ValueWithFailures.of(ImmutableList.of("1", "2"), ImmutableList.of(FAILURE1)); ValueWithFailures<List<Integer>> test = base.map(list -> list.stream().map(s -> Integer.valueOf(s)).collect(toImmutableList())); assertEquals(test.getValue(), ImmutableList.of(Integer.valueOf(1), Integer.valueOf(2))); assertEquals(test.getFailures(), ImmutableList.of(FAILURE1)); }
/** * Returns a new instance with the specified value, retaining the current failures. * <p> * This can be useful as an inline alternative to {@link #map(Function)}. * * @param <R> the type of the value in the returned result * @param value the new value * @return the combined instance of value and failures */ public <R> ValueWithFailures<R> withValue(R value) { return ValueWithFailures.of(value, this.failures); }
public void test_parseFiltering() { PositionCsvLoader test = PositionCsvLoader.standard(); assertEquals(test.parse(ImmutableList.of(FILE.getCharSource())).getValue().size(), 3); // 7 errors assertEquals(test.parse(ImmutableList.of(FILE.getCharSource()), SecurityPosition.class).getValue().size(), 10); assertEquals(test.parse(ImmutableList.of(FILE.getCharSource()), ResolvableSecurityPosition.class).getValue().size(), 3); assertEquals(test.parse(ImmutableList.of(FILE.getCharSource()), GenericSecurityPosition.class).getValue().size(), 1); }
public void test_toValueWithFailures() { List<Double> testList = ImmutableList.of(5d, 6d, 7d); ValueWithFailures<Double> result = testList.stream() .map(value -> mockCalc(value)) .collect(ValueWithFailures.toValueWithFailures(1d, (val1, val2) -> val1 * val2)); assertEquals(result.getValue(), 210d); //5 * 6 * 7 = 210 List<FailureItem> failures = result.getFailures(); assertEquals(failures.size(), 3); //One failure item for each element in testList. assertEquals(failures.get(0).getMessage(), Messages.format("Error calculating result for input value {}", 5d)); assertEquals(failures.get(1).getMessage(), Messages.format("Error calculating result for input value {}", 6d)); assertEquals(failures.get(2).getMessage(), Messages.format("Error calculating result for input value {}", 7d)); }
/** * 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)); } }
/** * 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); }
/** * Creates an instance wrapping the success value and failures. * * @param <T> the type of the success value * @param successValue the success value * @param failures the failures * @return an instance wrapping the value and failures */ public static <T> ValueWithFailures<T> of(T successValue, List<FailureItem> failures) { return new ValueWithFailures<>(successValue, failures); }
public void test_of_array_noFailures() { ValueWithFailures<String> test = ValueWithFailures.of("success"); assertEquals(test.hasFailures(), false); assertEquals(test.getValue(), "success"); assertEquals(test.getFailures(), ImmutableList.of()); }
public void test_withValue_value() { ValueWithFailures<List<String>> base = ValueWithFailures.of(ImmutableList.of("a"), ImmutableList.of(FAILURE1)); ValueWithFailures<String> test = base.withValue("combined"); assertEquals(test.getValue(), "combined"); assertEquals(test.getFailures(), ImmutableList.of(FAILURE1)); }
List<FailureItem> failures = new ArrayList<>(parsed.getFailures()); for (Trade trade : parsed.getValue()) { if (tradeTypes.contains(trade.getClass())) { valid.add(trade); return ValueWithFailures.of(valid, failures);