From 08c6430edbbf38484685444426c67e2323a62e24 Mon Sep 17 00:00:00 2001 From: Will Nicholson Date: Thu, 4 Jul 2019 14:43:03 +0100 Subject: [PATCH 1/3] Utility methods for merging streams of ValueWithFailures --- .../collect/result/ValueWithFailures.java | 110 ++++++++++++++---- .../collect/result/ValueWithFailuresTest.java | 30 +++++ 2 files changed, 119 insertions(+), 21 deletions(-) diff --git a/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java b/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java index 3d9c3f77b9..dfa66c2db9 100644 --- a/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java +++ b/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java @@ -8,10 +8,12 @@ import static com.opengamma.strata.collect.Guavate.concatToList; import java.io.Serializable; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Set; import java.util.function.BiFunction; import java.util.function.BinaryOperator; import java.util.function.Function; @@ -32,7 +34,9 @@ import org.joda.beans.impl.direct.DirectMetaPropertyMap; import org.joda.beans.impl.direct.DirectPrivateBeanBuilder; +import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.opengamma.strata.collect.ArgChecker; /** @@ -158,6 +162,70 @@ public static BinaryOperator> combiningValues(BinaryOpe return Collectors.reducing(ValueWithFailures.of(identityValue), combiningValues(operator)); } + /** + * Returns a collector that can be used to create a combined ValueWithFailure instance from a stream of + * ValueWithFailure instances. + *

+ * The {@link Collector} returned performs a reduction of its {@link ValueWithFailures} input elements into a list + * of elements, hopefully without producing as many intermediary lists. + * + * @param the type of the success value in the {@link ValueWithFailures} + * @return a {@link Collector} + */ + public static Collector, ?, ValueWithFailures>> toValueListWithFailures() { + return Collector.of( + () -> new StreamBuilder<>(ImmutableList.builder()), + StreamBuilder::add, + StreamBuilder::combine, + StreamBuilder::build); + } + + /** + * Returns a collector that can be used to create a combined ValueWithFailure instance from a stream of + * ValueWithFailure instances. + *

+ * The {@link Collector} returned performs a reduction of its {@link ValueWithFailures} input elements into a set + * of elements, hopefully without producing as many intermediary sets. + * + * @param the type of the success value in the {@link ValueWithFailures} + * @return a {@link Collector} + */ + public static Collector, ?, ValueWithFailures>> toValueSetWithFailures() { + return Collector.of( + () -> new StreamBuilder<>(ImmutableSet.builder()), + StreamBuilder::add, + StreamBuilder::combine, + StreamBuilder::build); + } + + // mutable combined instance builder for use in stream collection + private static final class StreamBuilder> { + + private final B values; + private final ImmutableList.Builder failures = ImmutableList.builder(); + + private StreamBuilder(B values) { + this.values = values; + } + + private void add(ValueWithFailures item) { + values.add(item.getValue()); + failures.addAll(item.getFailures()); + } + + private StreamBuilder combine(StreamBuilder builder) { + values.addAll(builder.values.build()); + failures.addAll(builder.failures.build()); + return this; + } + + // Cast to the right collection type, can assume the methods in this class are using the correct types + private > ValueWithFailures build() { + //noinspection unchecked + return (ValueWithFailures) ValueWithFailures.of(values.build(), failures.build()); + } + } + //------------------------------------------------------------------------- /** * Checks if there are any failures. @@ -177,8 +245,8 @@ public boolean hasFailures() { * It is strongly advised to ensure that the function cannot throw an exception. * Exceptions from the function are not caught. * - * @param the type of the value in the returned result - * @param function the function to transform the value with + * @param the type of the value in the returned result + * @param function the function to transform the value with * @return the transformed instance of value and failures */ public ValueWithFailures map(Function function) { @@ -196,8 +264,8 @@ public ValueWithFailures map(Function function) { * It is strongly advised to ensure that the function cannot throw an exception. * Exceptions from the function are not caught. * - * @param the type of the value in the returned result - * @param function the function to transform the value with + * @param the type of the value in the returned result + * @param function the function to transform the value with * @return the transformed instance of value and failures */ public ValueWithFailures flatMap(Function> function) { @@ -214,11 +282,11 @@ public ValueWithFailures flatMap(Function *

* It is strongly advised to ensure that the function cannot throw an exception. * Exceptions from the function are not caught. - * - * @param the type of the value in the other instance - * @param the type of the value in the returned result - * @param other the other instance - * @param combiner the function that combines the two values + * + * @param the type of the value in the other instance + * @param the type of the value in the returned result + * @param other the other instance + * @param combiner the function that combines the two values * @return the combined instance of value and failures */ public ValueWithFailures combinedWith(ValueWithFailures other, BiFunction combiner) { @@ -232,9 +300,9 @@ public ValueWithFailures combinedWith(ValueWithFailures other, BiFu * Returns a new instance with the specified value, retaining the current failures. *

* This can be useful as an inline alternative to {@link #map(Function)}. - * - * @param the type of the value in the returned result - * @param value the new value + * + * @param the type of the value in the returned result + * @param value the new value * @return the combined instance of value and failures */ public ValueWithFailures withValue(R value) { @@ -245,10 +313,10 @@ public ValueWithFailures withValue(R value) { * Returns a new instance with the specified value, combining the failures. *

* This can be useful as an inline alternative to {@link #flatMap(Function)}. - * - * @param the type of the value in the returned result - * @param value the new value - * @param additionalFailures the additional failures + * + * @param the type of the value in the returned result + * @param value the new value + * @param additionalFailures the additional failures * @return the combined instance of value and failures */ public ValueWithFailures withValue(R value, List additionalFailures) { @@ -259,9 +327,9 @@ public ValueWithFailures withValue(R value, List additionalF * Returns a new instance with the specified value, combining the failures. *

* This can be useful as an inline alternative to {@link #flatMap(Function)}. - * - * @param the type of the value in the returned result - * @param valueWithFailures the new value with failures + * + * @param 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 ValueWithFailures withValue(ValueWithFailures valueWithFailures) { @@ -272,8 +340,8 @@ public ValueWithFailures withValue(ValueWithFailures valueWithFailures /** * Returns a new instance with the specified failures, retaining the current value. - * - * @param additionalFailures the additional failures + * + * @param additionalFailures the additional failures * @return the combined instance of value and failures */ public ValueWithFailures withAdditionalFailures(List additionalFailures) { diff --git a/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java b/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java index d65845dc44..4c00a3fb39 100644 --- a/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java +++ b/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java @@ -13,11 +13,13 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.stream.Stream; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.opengamma.strata.collect.Guavate; import com.opengamma.strata.collect.Messages; @@ -177,6 +179,34 @@ public void test_toValueWithFailures() { assertEquals(failures.get(2).getMessage(), Messages.format("Error calculating result for input value {}", 7d)); } + public void test_toValueListWithFailures() { + List testList = ImmutableList.of(5d, 6d, 7d); + ValueWithFailures> result = testList.stream() + .map(value -> mockCalc(value)) + .collect(ValueWithFailures.toValueListWithFailures()); + + assertEquals(result.getValue(), ImmutableList.of(5d, 6d, 7d)); + List 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)); + } + + public void test_toValueSetWithFailures() { + List testList = ImmutableList.of(5d, 6d, 7d); + ValueWithFailures> result = testList.stream() + .map(value -> mockCalc(value)) + .collect(ValueWithFailures.toValueSetWithFailures()); + + assertEquals(result.getValue(), ImmutableSet.of(5d, 6d, 7d)); + List 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)); + } + //------------------------------------------------------------------------- public void coverage() { ValueWithFailures test = ValueWithFailures.of("success", FAILURE1, FAILURE2); From e53eca02dc94c5aaad5c09a0187b16fa97a57127 Mon Sep 17 00:00:00 2001 From: Will Nicholson Date: Fri, 5 Jul 2019 08:02:47 +0100 Subject: [PATCH 2/3] Combine values as set --- .../collect/result/ValueWithFailures.java | 41 +++++++++++++++---- .../collect/result/ValueWithFailuresTest.java | 34 ++++++++------- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java b/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java index d7c31f6374..9c597676f5 100644 --- a/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java +++ b/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java @@ -169,7 +169,7 @@ public static BinaryOperator> combiningValues(BinaryOpe * @param the type of the success value in the {@link ValueWithFailures} * @return a {@link Collector} */ - public static Collector, ?, ValueWithFailures>> toCombinedValuesAsList() { + public static Collector, ?, ValueWithFailures>> toCombinedValuesAsList() { return Collector.of( () -> new StreamBuilder<>(ImmutableList.builder()), StreamBuilder::add, @@ -178,13 +178,13 @@ public static BinaryOperator> combiningValues(BinaryOpe } /** - * Returns a collector that can be used to create a {@code ValueWithFailure} instance with a list of success values + * Returns a collector that can be used to create a {@code ValueWithFailure} instance with a set of success values * from a stream of {@code ValueWithFailure} instances. * * @param the type of the success value in the {@link ValueWithFailures} * @return a {@link Collector} */ - public static Collector, ?, ValueWithFailures>> toCombinedValuesAsSet() { + public static Collector, ?, ValueWithFailures>> toCombinedValuesAsSet() { return Collector.of( () -> new StreamBuilder<>(ImmutableSet.builder()), StreamBuilder::add, @@ -195,20 +195,45 @@ public static BinaryOperator> combiningValues(BinaryOpe /** * Converts a list of value with failures to a single value with failures, combining the values into a list. *

- * Effectively, this converts {@code List>} to {@code ValueWithFailures>}. + * Effectively, this converts {@code Iterable>} to {@code ValueWithFailures>}. * * @param the type of the success value in the {@link ValueWithFailures} * @return a new instance with a list of success values */ - public static ValueWithFailures> combineValuesAsList(List> items) { + public static ValueWithFailures> combineValuesAsList( + Iterable> items) { + ImmutableList.Builder values = ImmutableList.builder(); - ImmutableList.Builder failures = ImmutableList.builder(); + ImmutableList failures = combine(items, values); + return ValueWithFailures.of(values.build(), failures); + } + + /** + * Converts a list of value with failures to a single value with failures, combining the values into a set. + *

+ * Effectively, this converts {@code Iterable>} to {@code ValueWithFailures>}. + * + * @param the type of the success value in the {@link ValueWithFailures} + * @return a new instance with a set of success values + */ + public static ValueWithFailures> combineValuesAsSet( + Iterable> items) { + ImmutableSet.Builder values = ImmutableSet.builder(); + ImmutableList failures = combine(items, values); + return ValueWithFailures.of(values.build(), failures); + } + + private static ImmutableList combine( + Iterable> items, + ImmutableCollection.Builder values) { + + ImmutableList.Builder failures = ImmutableList.builder(); for (ValueWithFailures item : items) { values.add(item.getValue()); failures.addAll(item.getFailures()); } - return ValueWithFailures.of(values.build(), failures.build()); + return failures.build(); } // mutable combined instance builder for use in stream collection @@ -221,7 +246,7 @@ private StreamBuilder(B values) { this.values = values; } - private void add(ValueWithFailures item) { + private void add(ValueWithFailures item) { values.add(item.getValue()); failures.addAll(item.getFailures()); } diff --git a/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java b/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java index 87fe283e09..b2cad4507b 100644 --- a/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java +++ b/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java @@ -179,13 +179,14 @@ public void test_toValueWithFailures() { assertEquals(failures.get(2).getMessage(), Messages.format("Error calculating result for input value {}", 7d)); } - public void test_toValueListWithFailures() { + + public void test_toCombinedValuesAsSet() { List testList = ImmutableList.of(5d, 6d, 7d); - ValueWithFailures> result = testList.stream() + ValueWithFailures> result = testList.stream() .map(value -> mockCalc(value)) - .collect(ValueWithFailures.toValueListWithFailures()); + .collect(ValueWithFailures.toCombinedValuesAsSet()); - assertEquals(result.getValue(), ImmutableList.of(5d, 6d, 7d)); + assertEquals(result.getValue(), ImmutableSet.of(5d, 6d, 7d)); List 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)); @@ -193,13 +194,17 @@ public void test_toValueListWithFailures() { assertEquals(failures.get(2).getMessage(), Messages.format("Error calculating result for input value {}", 7d)); } - public void test_toValueSetWithFailures() { + public void test_toCombinedValuesAsList() { List testList = ImmutableList.of(5d, 6d, 7d); - ValueWithFailures> result = testList.stream() + ImmutableList> listOfValueWithFailures = testList.stream() .map(value -> mockCalc(value)) - .collect(ValueWithFailures.toValueSetWithFailures()); + .collect(toImmutableList()); + + ValueWithFailures> result = listOfValueWithFailures.stream() + .collect(ValueWithFailures.toCombinedValuesAsList()); + + assertEquals(result.getValue(), ImmutableList.of(5d, 6d, 7d)); - assertEquals(result.getValue(), ImmutableSet.of(5d, 6d, 7d)); List 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)); @@ -207,16 +212,15 @@ public void test_toValueSetWithFailures() { assertEquals(failures.get(2).getMessage(), Messages.format("Error calculating result for input value {}", 7d)); } - public void test_toValueWithFailuresList() { + public void test_combineAsList() { List testList = ImmutableList.of(5d, 6d, 7d); ImmutableList> listOfValueWithFailures = testList.stream() .map(value -> mockCalc(value)) .collect(toImmutableList()); - ValueWithFailures> result = listOfValueWithFailures.stream() - .collect(ValueWithFailures.toCombinedValuesAsList()); + ValueWithFailures> result = ValueWithFailures.combineValuesAsList(listOfValueWithFailures); - assertEquals(result.getValue().size(), 3); + assertEquals(result.getValue(), ImmutableList.of(5d, 6d, 7d)); List failures = result.getFailures(); assertEquals(failures.size(), 3); //One failure item for each element in testList. @@ -225,15 +229,15 @@ public void test_toValueWithFailuresList() { assertEquals(failures.get(2).getMessage(), Messages.format("Error calculating result for input value {}", 7d)); } - public void test_combineAsList() { + public void test_combineAsSet() { List testList = ImmutableList.of(5d, 6d, 7d); ImmutableList> listOfValueWithFailures = testList.stream() .map(value -> mockCalc(value)) .collect(toImmutableList()); - ValueWithFailures> result = ValueWithFailures.combineValuesAsList(listOfValueWithFailures); + ValueWithFailures> result = ValueWithFailures.combineValuesAsSet(listOfValueWithFailures); - assertEquals(result.getValue().size(), 3); + assertEquals(result.getValue(), ImmutableSet.of(5d, 6d, 7d)); List failures = result.getFailures(); assertEquals(failures.size(), 3); //One failure item for each element in testList. From 45430e33b53c97ee4928d58ffc34c95626e71a11 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Fri, 5 Jul 2019 10:54:48 +0100 Subject: [PATCH 3/3] Combines multiple ValueWithFailure into one Add Guavate.concatToSet --- .../com/opengamma/strata/collect/Guavate.java | 17 ++++++++- .../collect/result/ValueWithFailures.java | 36 ++++++++++++------- .../opengamma/strata/collect/GuavateTest.java | 16 +++++++++ .../collect/result/ValueWithFailuresTest.java | 36 +++++++------------ 4 files changed, 68 insertions(+), 37 deletions(-) diff --git a/modules/collect/src/main/java/com/opengamma/strata/collect/Guavate.java b/modules/collect/src/main/java/com/opengamma/strata/collect/Guavate.java index 09b5d21915..eee799898e 100644 --- a/modules/collect/src/main/java/com/opengamma/strata/collect/Guavate.java +++ b/modules/collect/src/main/java/com/opengamma/strata/collect/Guavate.java @@ -70,7 +70,7 @@ private Guavate() { *

* This is harder than it should be, a method {@code Stream.of(Iterable)} * would have been appropriate, but cannot be added now. - * + * * @param the type of element in the iterable * @param iterables the iterables to combine * @return the list that combines the inputs @@ -80,6 +80,21 @@ public static ImmutableList concatToList(Iterable... iterabl return ImmutableList.copyOf(Iterables.concat(iterables)); } + /** + * Concatenates a number of iterables into a single set. + *

+ * This is harder than it should be, a method {@code Stream.of(Iterable)} + * would have been appropriate, but cannot be added now. + * + * @param the type of element in the iterable + * @param iterables the iterables to combine + * @return the set that combines the inputs + */ + @SafeVarargs + public static ImmutableSet concatToSet(Iterable... iterables) { + return ImmutableSet.copyOf(Iterables.concat(iterables)); + } + //------------------------------------------------------------------------- /** * Combines two distinct maps into a single map. diff --git a/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java b/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java index 9c597676f5..bd96da7985 100644 --- a/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java +++ b/modules/collect/src/main/java/com/opengamma/strata/collect/result/ValueWithFailures.java @@ -144,11 +144,13 @@ public static BinaryOperator> combiningValues(BinaryOpe } /** - * Returns a collector that can be used to create a ValueWithFailure instance from a stream of ValueWithFailure - * instances. + * Returns a collector that can be used to create a combined {@code ValueWithFailure} + * from a stream of separate instances. *

* The {@link Collector} returned performs a reduction of its {@link ValueWithFailures} input elements under a * specified {@link BinaryOperator} using the provided identity. + *

+ * This collects a {@code Stream>} to a {@code ValueWithFailures}. * * @param the type of the success value in the {@link ValueWithFailures} * @param identityValue the identity value @@ -163,8 +165,10 @@ public static BinaryOperator> combiningValues(BinaryOpe } /** - * Returns a collector that can be used to create a {@code ValueWithFailure} instance with a list of success values - * from a stream of {@code ValueWithFailure} instances. + * Returns a collector that creates a combined {@code ValueWithFailure} from a stream + * of separate instances, combining into an immutable list. + *

+ * This collects a {@code Stream>} to a {@code ValueWithFailures>}. * * @param the type of the success value in the {@link ValueWithFailures} * @return a {@link Collector} @@ -178,8 +182,10 @@ public static BinaryOperator> combiningValues(BinaryOpe } /** - * Returns a collector that can be used to create a {@code ValueWithFailure} instance with a set of success values - * from a stream of {@code ValueWithFailure} instances. + * Returns a collector that creates a combined {@code ValueWithFailure} from a stream + * of separate instances, combining into an immutable set. + *

+ * This collects a {@code Stream>} to a {@code ValueWithFailures>}. * * @param the type of the success value in the {@link ValueWithFailures} * @return a {@link Collector} @@ -193,11 +199,13 @@ public static BinaryOperator> combiningValues(BinaryOpe } /** - * Converts a list of value with failures to a single value with failures, combining the values into a list. + * Combines separate instances of {@code ValueWithFailure} into a single instance, + * using a list to collect the values. *

- * Effectively, this converts {@code Iterable>} to {@code ValueWithFailures>}. + * This converts {@code Iterable>} to {@code ValueWithFailures>}. * * @param the type of the success value in the {@link ValueWithFailures} + * @param items the items to combine * @return a new instance with a list of success values */ public static ValueWithFailures> combineValuesAsList( @@ -209,11 +217,13 @@ public static ValueWithFailures> combineValuesAsList( } /** - * Converts a list of value with failures to a single value with failures, combining the values into a set. + * Combines separate instances of {@code ValueWithFailure} into a single instance, + * using a set to collect the values. *

- * Effectively, this converts {@code Iterable>} to {@code ValueWithFailures>}. + * This converts {@code Iterable>} to {@code ValueWithFailures>}. * * @param the type of the success value in the {@link ValueWithFailures} + * @param items the items to combine * @return a new instance with a set of success values */ public static ValueWithFailures> combineValuesAsSet( @@ -224,6 +234,7 @@ public static ValueWithFailures> combineValuesAsSet( return ValueWithFailures.of(values.build(), failures); } + // combines the collection into the specified builder private static ImmutableList combine( Iterable> items, ImmutableCollection.Builder values) { @@ -237,6 +248,7 @@ private static ImmutableList combine( } // mutable combined instance builder for use in stream collection + // using a single dedicated collector is more efficient than a reduction with multiple calls to combinedWith() private static final class StreamBuilder> { private final B values; @@ -257,9 +269,9 @@ private StreamBuilder combine(StreamBuilder builder) { return this; } - // Cast to the right collection type, can assume the methods in this class are using the correct types + // cast to the right collection type, can assume the methods in this class are using the correct types + @SuppressWarnings("unchecked") private > ValueWithFailures build() { - //noinspection unchecked return (ValueWithFailures) ValueWithFailures.of(values.build(), failures.build()); } } diff --git a/modules/collect/src/test/java/com/opengamma/strata/collect/GuavateTest.java b/modules/collect/src/test/java/com/opengamma/strata/collect/GuavateTest.java index ec3b9a069e..e7b43b57ab 100644 --- a/modules/collect/src/test/java/com/opengamma/strata/collect/GuavateTest.java +++ b/modules/collect/src/test/java/com/opengamma/strata/collect/GuavateTest.java @@ -22,6 +22,7 @@ import java.util.Locale; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.CountDownLatch; @@ -68,6 +69,21 @@ public void test_concatToList_differentTypes() { assertEquals(test, ImmutableList.of(1, 2, 3, 10d, 20d, 30d)); } + //------------------------------------------------------------------------- + public void test_concatToSet() { + Iterable iterable1 = Arrays.asList("a", "b", "c"); + Iterable iterable2 = Arrays.asList("d", "e", "f", "a"); + Set test = Guavate.concatToSet(iterable1, iterable2); + assertEquals(test, ImmutableSet.of("a", "b", "c", "d", "e", "f")); + } + + public void test_concatToSet_differentTypes() { + Iterable iterable1 = Arrays.asList(1, 2, 3, 2); + Iterable iterable2 = Arrays.asList(10d, 20d, 30d); + Set test = Guavate.concatToSet(iterable1, iterable2); + assertEquals(test, ImmutableSet.of(1, 2, 3, 10d, 20d, 30d)); + } + //------------------------------------------------------------------------- public void test_combineMap() { Map map1 = ImmutableMap.of("a", "one", "b", "two"); diff --git a/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java b/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java index b2cad4507b..39f13b94a0 100644 --- a/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java +++ b/modules/collect/src/test/java/com/opengamma/strata/collect/result/ValueWithFailuresTest.java @@ -166,12 +166,11 @@ public void test_withAdditionalFailures() { //------------------------------------------------------------------------- public void test_toValueWithFailures() { - List testList = ImmutableList.of(5d, 6d, 7d); - ValueWithFailures result = testList.stream() + ValueWithFailures result = Stream.of(5d, 6d, 7d) .map(value -> mockCalc(value)) .collect(ValueWithFailures.toValueWithFailures(1d, (val1, val2) -> val1 * val2)); - assertEquals(result.getValue(), 210d); //5 * 6 * 7 = 210 + List 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)); @@ -179,14 +178,12 @@ public void test_toValueWithFailures() { assertEquals(failures.get(2).getMessage(), Messages.format("Error calculating result for input value {}", 7d)); } - - public void test_toCombinedValuesAsSet() { - List testList = ImmutableList.of(5d, 6d, 7d); - ValueWithFailures> result = testList.stream() + public void test_toCombinedValuesAsList() { + ValueWithFailures> result = Stream.of(5d, 6d, 7d) .map(value -> mockCalc(value)) - .collect(ValueWithFailures.toCombinedValuesAsSet()); + .collect(ValueWithFailures.toCombinedValuesAsList()); + assertEquals(result.getValue(), ImmutableList.of(5d, 6d, 7d)); - assertEquals(result.getValue(), ImmutableSet.of(5d, 6d, 7d)); List 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)); @@ -194,16 +191,11 @@ public void test_toCombinedValuesAsSet() { assertEquals(failures.get(2).getMessage(), Messages.format("Error calculating result for input value {}", 7d)); } - public void test_toCombinedValuesAsList() { - List testList = ImmutableList.of(5d, 6d, 7d); - ImmutableList> listOfValueWithFailures = testList.stream() + public void test_toCombinedValuesAsSet() { + ValueWithFailures> result = Stream.of(5d, 6d, 7d) .map(value -> mockCalc(value)) - .collect(toImmutableList()); - - ValueWithFailures> result = listOfValueWithFailures.stream() - .collect(ValueWithFailures.toCombinedValuesAsList()); - - assertEquals(result.getValue(), ImmutableList.of(5d, 6d, 7d)); + .collect(ValueWithFailures.toCombinedValuesAsSet()); + assertEquals(result.getValue(), ImmutableSet.of(5d, 6d, 7d)); List failures = result.getFailures(); assertEquals(failures.size(), 3); //One failure item for each element in testList. @@ -213,13 +205,11 @@ public void test_toCombinedValuesAsList() { } public void test_combineAsList() { - List testList = ImmutableList.of(5d, 6d, 7d); - ImmutableList> listOfValueWithFailures = testList.stream() + ImmutableList> listOfValueWithFailures = Stream.of(5d, 6d, 7d) .map(value -> mockCalc(value)) .collect(toImmutableList()); ValueWithFailures> result = ValueWithFailures.combineValuesAsList(listOfValueWithFailures); - assertEquals(result.getValue(), ImmutableList.of(5d, 6d, 7d)); List failures = result.getFailures(); @@ -230,13 +220,11 @@ public void test_combineAsList() { } public void test_combineAsSet() { - List testList = ImmutableList.of(5d, 6d, 7d); - ImmutableList> listOfValueWithFailures = testList.stream() + ImmutableList> listOfValueWithFailures = Stream.of(5d, 6d, 7d) .map(value -> mockCalc(value)) .collect(toImmutableList()); ValueWithFailures> result = ValueWithFailures.combineValuesAsSet(listOfValueWithFailures); - assertEquals(result.getValue(), ImmutableSet.of(5d, 6d, 7d)); List failures = result.getFailures();