public void testToImmutableTable() { Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>> collector = ImmutableTable.toImmutableTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue); Equivalence<ImmutableTable<String, String, Integer>> equivalence = Equivalence.equals() .<Cell<String, String, Integer>>pairwise() .onResultOf(ImmutableTable::cellSet); CollectorTester.of(collector, equivalence) .expectCollects( new ImmutableTable.Builder<String, String, Integer>() .put("one", "uno", 1) .put("two", "dos", 2) .put("three", "tres", 3) .build(), Tables.immutableCell("one", "uno", 1), Tables.immutableCell("two", "dos", 2), Tables.immutableCell("three", "tres", 3)); }
@Test @SuppressWarnings("unchecked") public void should_create_the_correct_stream_on_groupingBy_then_all_max_with_several_maxes_in_a_set() { // Given Stream<String> strings = Stream.of("one", "two", "two", "three", "three", "three", "four", "four", "four"); Collector<String, ?, Set<Map.Entry<String, Long>>> collector = CollectorsUtils.groupingByAndAllMaxBy( identity(), HashSet::new, Collectors.counting(), Map.Entry.comparingByValue() ); // When Set<Map.Entry<String, Long>> result = strings.collect(collector); // Then assertThat(result.size()).isEqualTo(2); assertThat(result).isExactlyInstanceOf(HashSet.class); assertThat(result).contains(new AbstractMap.SimpleImmutableEntry<>("three", 3L)); assertThat(result).contains(new AbstractMap.SimpleImmutableEntry<>("four", 3L)); }
@Test public void should_return_an_empty_optional_for_groupingBy_and_maxBy_on_an_empty_stream() { // Given Stream<String> strings = Stream.empty(); Collector<String, ?, Optional<Map.Entry<String, Long>>> collector = CollectorsUtils.groupingByAndMaxBy( identity(), counting(), Map.Entry.comparingByValue() ); // When Optional<Map.Entry<String, Long>> result = strings.collect(collector); // Then assertThat(result.isPresent()).isFalse(); }
@Test public void should_return_an_empty_optional_for_groupingBy_and_max_by_value_with_a_comparator_on_an_empty_stream() { // Given Stream<String> strings = Stream.empty(); Collector<String, ?, Optional<Map.Entry<String, Long>>> collector = CollectorsUtils.groupingByAndMaxByValue( identity(), counting(), Comparator.reverseOrder() ); // When Optional<Map.Entry<String, Long>> result = strings.collect(collector); // Then assertThat(result.isPresent()).isFalse(); }
@Test public void should_create_the_correct_stream_on_groupingBy_for_an_empty_stream() { // Given Stream<String> strings = Stream.empty(); Collector<String, ?, Stream<Map.Entry<Integer, List<String>>>> groupingByThenStream = CollectorsUtils.groupingByThenStream( String::length ); // When List<Map.Entry<Integer, List<String>>> entries = strings.collect(groupingByThenStream).collect(Collectors.toList()); // Then assertThat(entries).isEmpty(); }
/** * Returns a {@code Collector} that accumulates elements into a multiset created via the specified * {@code Supplier}, whose elements are the result of applying {@code elementFunction} to the * inputs, with counts equal to the result of applying {@code countFunction} to the inputs. * Elements are added in encounter order. * * <p>If the mapped elements contain duplicates (according to {@link Object#equals}), the element * will be added more than once, with the count summed over all appearances of the element. * * <p>Note that {@code stream.collect(toMultiset(function, e -> 1, supplier))} is equivalent to * {@code stream.map(function).collect(Collectors.toCollection(supplier))}. * * @since 22.0 */ public static <T, E, M extends Multiset<E>> Collector<T, ?, M> toMultiset( java.util.function.Function<T, E> elemFunction, java.util.function.ToIntFunction<T> countFunction, java.util.function.Supplier<M> implSupplier) { checkNotNull(elemFunction); checkNotNull(countFunction); checkNotNull(implSupplier); return Collector.of( implSupplier, (ms, t) -> ms.add(elemFunction.apply(t), countFunction.applyAsInt(t)), (ms1, ms2) -> { ms1.addAll(ms2); return ms1; }); }
@Test public void should_collect_flat_map_with_a_downstream_collector_and_a_flat_mapper_a_non_empty_stream_into_a_stream() { // Given Stream<String> strings = Stream.of("one", "two", "three"); Function<String, Stream<Character>> streamMapper = string -> string.chars().mapToObj(letter -> (char)letter); Collector<Stream<Character>, ?, Stream<Stream<Character>>> downstream = CollectorsUtils.mapToStream(); Function<Stream<Character>, Stream<String>> mapper = stream -> stream.map(letter -> Character.toString(letter)); Collector<String, ?, Stream<String>> streamCollector = CollectorsUtils.flatMapping(streamMapper, downstream, mapper); // When List<String> characters = strings.collect(streamCollector).collect(toList()); // Then assertThat(characters.size()).isEqualTo(11); assertThat(characters).containsExactly("o", "n", "e", "t", "w", "o", "t", "h", "r", "e", "e"); }
@Test public void should_create_the_correct_stream_on_groupingBy_then_n_maxes_with_one_max_and_n_bigger() { // Given Stream<String> strings = Stream.of("one", "two", "two", "three", "three", "four", "four", "four"); Collector<String, ?, List<Map.Entry<String, Long>>> collector = CollectorsUtils.groupingByAndMaxesBy( identity(), counting(), 10, Map.Entry.comparingByValue() ); // When List<Map.Entry<String, Long>> result = strings.collect(collector); // Then assertThat(result.size()).isEqualTo(4); assertThat(result).containsExactly( new AbstractMap.SimpleImmutableEntry<>("four", 3L), new AbstractMap.SimpleImmutableEntry<>("three", 2L), new AbstractMap.SimpleImmutableEntry<>("two", 2L), new AbstractMap.SimpleImmutableEntry<>("one", 1L) ); }
@Test public void should_stream_a_empty_stream_with_a_collection_mapper_into_the_correct_stream() { // Given Stream<String> strings = Stream.of("Hello", "world"); Function<String, List<Character>> mapper = (String s) -> s.chars().mapToObj(letter -> (char)letter).collect(toList()); // When Collector<String, ?, Stream<Stream<Character>>> collector = CollectorsUtils.mapToStream(mapper); Stream<Stream<Character>> result = strings.collect(collector); List<List<Character>> list = result.map(stream -> stream.collect(toList())).collect(toList()); // Then assertThat(list.size()).isEqualTo(2); assertThat(list.get(0)).containsExactly('H', 'e', 'l', 'l', 'o'); assertThat(list.get(1)).containsExactly('w', 'o', 'r', 'l', 'd'); }
@Test public void should_create_the_correct_stream_on_groupingBy_with_a_downstream_for_an_empty_stream() { // Given Stream<String> strings = Stream.empty(); Collector<String, ?, Stream<Map.Entry<Integer, Long>>> groupingByThenStream = CollectorsUtils.groupingByThenStream( String::length, counting() ); // When List<Map.Entry<Integer, Long>> entries = strings.collect(groupingByThenStream).collect(Collectors.toList()); // Then assertThat(entries).isEmpty(); }
public void testFlatteningToImmutableSetMultimap() { Collector<String, ?, ImmutableSetMultimap<Character, Character>> collector = ImmutableSetMultimap.flatteningToImmutableSetMultimap( str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c)); BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence = Equivalence.equals() .onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet())) .and(Equivalence.equals()); ImmutableSetMultimap<Character, Character> empty = ImmutableSetMultimap.of(); ImmutableSetMultimap<Character, Character> filled = ImmutableSetMultimap.<Character, Character>builder() .putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a')) .putAll('a', Arrays.asList('p', 'p', 'l', 'e')) .putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't')) .putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's')) .putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y')) .build(); CollectorTester.of(collector, equivalence) .expectCollects(empty) .expectCollects(filled, "banana", "apple", "carrot", "asparagus", "cherry"); }
@Test public void should_create_the_correct_stream_on_groupingBy_then_n_maxes_with_several_maxes_more_n_results() { // Given Stream<String> strings = Stream.of("one", "two", "two", "three", "three", "four", "four"); Collector<String, ?, List<Map.Entry<String, Long>>> collector = CollectorsUtils.groupingByAndMaxesBy( identity(), counting(), 2, Map.Entry.comparingByValue() ); // When List<Map.Entry<String, Long>> result = strings.collect(collector); // Then assertThat(result.size()).isEqualTo(3); assertThat(result).containsExactly( new AbstractMap.SimpleImmutableEntry<>("four", 2L), new AbstractMap.SimpleImmutableEntry<>("three", 2L), new AbstractMap.SimpleImmutableEntry<>("two", 2L) ); }
@Test @SuppressWarnings("unchecked") public void should_create_the_correct_stream_on_groupingBy_then_all_max_by_value_with_several_maxes() { // Given Stream<String> strings = Stream.of("one", "two", "two", "three", "three", "three", "four", "four", "four"); Collector<String, ?, Set<Map.Entry<String, Long>>> collector = CollectorsUtils.groupingByAndAllMaxByValue( Function.<String>identity(), counting() ); // When Set<Map.Entry<String, Long>> result = strings.collect(collector); // Then assertThat(result.size()).isEqualTo(2); assertThat(result).contains(new AbstractMap.SimpleImmutableEntry<>("three", 3L)); assertThat(result).contains(new AbstractMap.SimpleImmutableEntry<>("four", 3L)); }
@Test public void should_collect_flat_map_with_a_downstream_collector_a_non_empty_stream_into_a_stream() { // Given Stream<String> strings = Stream.of("one", "two", "three"); Function<String, Stream<Character>> streamMapper = string -> string.chars().mapToObj(letter -> (char)letter); Collector<Stream<Character>, ?, Stream<Stream<Character>>> downstream = CollectorsUtils.mapToStream(); Collector<String, ?, Stream<Character>> streamCollector = CollectorsUtils.flatMapping(streamMapper, downstream); // When List<Character> characters = strings.collect(streamCollector).collect(toList()); // Then assertThat(characters.size()).isEqualTo(11); assertThat(characters).containsExactly('o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r', 'e', 'e'); }
public void testToImmutableSortedMapMerging() { Collector<Entry<String, Integer>, ?, ImmutableSortedMap<String, Integer>> collector = ImmutableSortedMap.toImmutableSortedMap( Comparator.naturalOrder(), Entry::getKey, Entry::getValue, Integer::sum); Equivalence<ImmutableMap<String, Integer>> equivalence = Equivalence.equals() .<Entry<String, Integer>>pairwise() .onResultOf(ImmutableMap::entrySet); CollectorTester.of(collector, equivalence) .expectCollects( ImmutableSortedMap.of("one", 1, "three", 3, "two", 4), mapEntry("one", 1), mapEntry("two", 2), mapEntry("three", 3), mapEntry("two", 2)); }
@Test public void should_return_an_empty_map_for_max_entry_value_by_counting_on_an_empty_stream() { // Given Map<Integer, Stream<String>> map = new HashMap<>(); Stream<Map.Entry<Integer, Stream<String>>> entries = FunctionsUtils.<Integer, Stream<String>>toStreamOfEntries().apply(map); Collector<Map.Entry<Integer, Stream<String>>, ?, Map<Integer, Map.Entry<String, Long>>> collector = CollectorsUtils.maxEntryValueByCounting(); // When Map<Integer, Map.Entry<String, Long>> collect = entries.collect(collector); // Then assertThat(collect).isEmpty(); }
@Test public void should_return_the_result_for_groupingBy_and_max_by_value_with_a_comparator_on_a_non_empty_stream() { // Given Stream<String> strings = Stream.of("one", "one", "two", "two", "two"); Collector<String, ?, Optional<Map.Entry<String, Long>>> collector = CollectorsUtils.groupingByAndMaxByValue( identity(), counting(), Comparator.reverseOrder() ); // When Optional<Map.Entry<String, Long>> result = strings.collect(collector); // Then assertThat(result.isPresent()).isTrue(); assertThat(result.get()).isEqualTo(new AbstractMap.SimpleImmutableEntry<>("one", 2L)); }
public void testToImmutableListMultimap() { Collector<Entry<String, Integer>, ?, ImmutableListMultimap<String, Integer>> collector = ImmutableListMultimap.toImmutableListMultimap(Entry::getKey, Entry::getValue); BiPredicate<ImmutableListMultimap<?, ?>, ImmutableListMultimap<?, ?>> equivalence = Equivalence.equals() .onResultOf((ImmutableListMultimap<?, ?> mm) -> mm.asMap().entrySet().asList()) .and(Equivalence.equals()); CollectorTester.of(collector, equivalence) .expectCollects(ImmutableListMultimap.of()) .expectCollects( ImmutableListMultimap.of("a", 1, "b", 2, "a", 3, "c", 4), mapEntry("a", 1), mapEntry("b", 2), mapEntry("a", 3), mapEntry("c", 4)); }
public void testFlatteningToImmutableListMultimap() { Collector<String, ?, ImmutableListMultimap<Character, Character>> collector = ImmutableListMultimap.flatteningToImmutableListMultimap( str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c)); BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence = Equivalence.equals() .onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet())) .and(Equivalence.equals()); ImmutableListMultimap<Character, Character> empty = ImmutableListMultimap.of(); ImmutableListMultimap<Character, Character> filled = ImmutableListMultimap.<Character, Character>builder() .putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a')) .putAll('a', Arrays.asList('p', 'p', 'l', 'e')) .putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't')) .putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's')) .putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y')) .build(); CollectorTester.of(collector, equivalence) .expectCollects(empty) .expectCollects(filled, "banana", "apple", "carrot", "asparagus", "cherry"); }
/** * Returns a {@code Collector} that accumulates elements into an {@code ImmutableTable}. Each * input element is mapped to one cell in the returned table, with the rows, columns, and values * generated by applying the specified functions. * * <p>The returned {@code Collector} will throw a {@code NullPointerException} at collection time * if the row, column, or value functions return null on any input. * * @since 21.0 */ @Beta public static <T, R, C, V> Collector<T, ?, ImmutableTable<R, C, V>> toImmutableTable( Function<? super T, ? extends R> rowFunction, Function<? super T, ? extends C> columnFunction, Function<? super T, ? extends V> valueFunction) { checkNotNull(rowFunction); checkNotNull(columnFunction); checkNotNull(valueFunction); return Collector.of( () -> new ImmutableTable.Builder<R, C, V>(), (builder, t) -> builder.put(rowFunction.apply(t), columnFunction.apply(t), valueFunction.apply(t)), (b1, b2) -> b1.combine(b2), b -> b.build()); }
@Test public void should_create_the_correct_stream_on_groupingBy_then_n_maxes_with_several_maxes_and_n_greater() { // Given Stream<String> strings = Stream.of("one", "two", "two", "three", "three", "three", "four", "four", "four"); Collector<String, ?, List<Map.Entry<String, Long>>> collector = CollectorsUtils.groupingByAndMaxesBy( identity(), counting(), 10, Map.Entry.comparingByValue() ); // When List<Map.Entry<String, Long>> result = strings.collect(collector); // Then assertThat(result.size()).isEqualTo(4); assertThat(result).containsExactly( new AbstractMap.SimpleImmutableEntry<>("four", 3L), new AbstractMap.SimpleImmutableEntry<>("three", 3L), new AbstractMap.SimpleImmutableEntry<>("two", 2L), new AbstractMap.SimpleImmutableEntry<>("one", 1L) ); }
public void testToImmutableSortedMap() { Collector<Entry<String, Integer>, ?, ImmutableSortedMap<String, Integer>> collector = ImmutableSortedMap.toImmutableSortedMap( String.CASE_INSENSITIVE_ORDER, Entry::getKey, Entry::getValue); BiPredicate<ImmutableSortedMap<String, Integer>, ImmutableSortedMap<String, Integer>> equivalence = Equivalence.equals().onResultOf(ImmutableSortedMap<String, Integer>::comparator) .and(Equivalence.equals().onResultOf(map -> map.entrySet().asList())) .and(Equivalence.equals()); ImmutableSortedMap<String, Integer> expected = ImmutableSortedMap.<String, Integer>orderedBy(String.CASE_INSENSITIVE_ORDER) .put("one", 1) .put("three", 3) .put("two", 2) .build(); CollectorTester.of(collector, equivalence) .expectCollects(expected, mapEntry("one", 1), mapEntry("two", 2), mapEntry("three", 3)); }
@Test public void should_stream_a_empty_stream_with_a_stream_mapper_into_the_correct_stream() { // Given Stream<String> strings = Stream.of("Hello", "world"); Function<String, Stream<Character>> mapper = (String s) -> s.chars().mapToObj(letter -> (char)letter); // When Collector<String, ?, Stream<Stream<Character>>> collector = CollectorsUtils.toStream(mapper); Stream<Stream<Character>> result = strings.collect(collector); List<List<Character>> list = result.map(stream -> stream.collect(toList())).collect(toList()); // Then assertThat(list.size()).isEqualTo(2); assertThat(list.get(0)).containsExactly('H', 'e', 'l', 'l', 'o'); assertThat(list.get(1)).containsExactly('w', 'o', 'r', 'l', 'd'); }
@Test public void should_create_the_correct_stream_on_groupingBy_then_all_max_by_value_and_a_result_supplier_with_several_maxes() { // Given Stream<String> strings = Stream.of("one", "one", "two", "two", "three", "three", "three", "four", "four", "four"); Collector<String, ?, List<Map.Entry<String, Long>>> collector = CollectorsUtils.groupingByAndAllMaxByValue( Function.<String>identity(), ArrayList::new, counting() ); // When List<Map.Entry<String, Long>> result = strings.collect(collector); // Then assertThat(result.size()).isEqualTo(2); assertThat(result).isExactlyInstanceOf(ArrayList.class); assertThat(result).contains(new AbstractMap.SimpleImmutableEntry<>("three", 3L)); assertThat(result).contains(new AbstractMap.SimpleImmutableEntry<>("four", 3L)); }
public void testToImmutableTableMerging() { Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>> collector = ImmutableTable.toImmutableTable( Cell::getRowKey, Cell::getColumnKey, Cell::getValue, Integer::sum); Equivalence<ImmutableTable<String, String, Integer>> equivalence = Equivalence.equals() .<Cell<String, String, Integer>>pairwise() .onResultOf(ImmutableTable::cellSet); CollectorTester.of(collector, equivalence) .expectCollects( new ImmutableTable.Builder<String, String, Integer>() .put("one", "uno", 1) .put("two", "dos", 6) .put("three", "tres", 3) .build(), Tables.immutableCell("one", "uno", 1), Tables.immutableCell("two", "dos", 2), Tables.immutableCell("three", "tres", 3), Tables.immutableCell("two", "dos", 4)); }
/** * Returns a {@link Collector} that accumulates elements into a {@code Table} created using the * specified supplier, whose cells are generated by applying the provided mapping functions to the * input elements. Cells are inserted into the generated {@code Table} in encounter order. * * <p>If multiple input elements map to the same row and column, an {@code IllegalStateException} * is thrown when the collection operation is performed. * * @since 21.0 */ @Beta public static <T, R, C, V, I extends Table<R, C, V>> Collector<T, ?, I> toTable( java.util.function.Function<? super T, ? extends R> rowFunction, java.util.function.Function<? super T, ? extends C> columnFunction, java.util.function.Function<? super T, ? extends V> valueFunction, java.util.function.Supplier<I> tableSupplier) { return toTable( rowFunction, columnFunction, valueFunction, (v1, v2) -> { throw new IllegalStateException("Conflicting values " + v1 + " and " + v2); }, tableSupplier); }
public static Collector<DetailedTick, ?, Map<Integer, Byte>> groupingBy(TIME_FRAME time_frame) { return Collectors.groupingBy((tick) -> trim_time(tick.time, time_frame), new LiquidityCollector(time_frame)); }
public static <T> Collector<T, ?, ImmutableList<T>> toImmutableList() { Supplier<ImmutableList.Builder<T>> supplier = ImmutableList.Builder::new; BiConsumer<ImmutableList.Builder<T>, T> accumulator = ImmutableList.Builder::add; BinaryOperator<ImmutableList.Builder<T>> combiner = (l, r) -> l.addAll(r.build()); Function<ImmutableList.Builder<T>, ImmutableList<T>> finisher = ImmutableList.Builder::build; return Collector.of(supplier, accumulator, combiner, finisher); }
public static <T> Collector<T, ?, ImmutableSet<T>> toImmutableSet() { Supplier<ImmutableSet.Builder<T>> supplier = ImmutableSet.Builder::new; BiConsumer<ImmutableSet.Builder<T>, T> accumulator = ImmutableSet.Builder::add; BinaryOperator<ImmutableSet.Builder<T>> combiner = (l, r) -> l.addAll(r.build()); Function<ImmutableSet.Builder<T>, ImmutableSet<T>> finisher = ImmutableSet.Builder::build; return Collector.of(supplier, accumulator, combiner, finisher); }
static <T, K, V> Collector<T, ?, ImmutableBiMap<K, V>> toImmutableBiMap( Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction) { checkNotNull(keyFunction); checkNotNull(valueFunction); return Collector.of( ImmutableBiMap.Builder<K, V>::new, (builder, input) -> builder.put(keyFunction.apply(input), valueFunction.apply(input)), ImmutableBiMap.Builder::combine, ImmutableBiMap.Builder::build, new Collector.Characteristics[0]); }
public void testToImmutableSortedSet() { Collector<String, ?, ImmutableSortedSet<String>> collector = ImmutableSortedSet.toImmutableSortedSet(Ordering.natural()); BiPredicate<ImmutableSortedSet<String>, ImmutableSortedSet<String>> equivalence = Equivalence.equals().onResultOf(ImmutableSortedSet<String>::comparator) .and(Equivalence.equals().onResultOf(ImmutableSortedSet::asList)) .and(Equivalence.equals()); CollectorTester.of(collector, equivalence) .expectCollects( ImmutableSortedSet.of("a", "b", "c", "d"), "a", "b", "a", "c", "b", "b", "d"); }
public void testToImmutableTableConflict() { Collector<Cell<String, String, Integer>, ?, ImmutableTable<String, String, Integer>> collector = ImmutableTable.toImmutableTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue); try { Stream.of(Tables.immutableCell("one", "uno", 1), Tables.immutableCell("one", "uno", 2)) .collect(collector); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
public void testToTableNullValues() { Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector = Tables.toTable( Cell::getRowKey, Cell::getColumnKey, Cell::getValue, () -> ArrayTable.create(ImmutableList.of("one"), ImmutableList.of("uno"))); try { Stream.of(Tables.immutableCell("one", "uno", (Integer) null)).collect(collector); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
@Test public void should_collect_flatmap_an_empty_stream_into_an_empty_stream() { // Given Stream<String> strings = Stream.empty(); Function<String, Stream<Character>> flatMapper = string -> string.chars().mapToObj(letter -> (char)letter); Collector<String, ?, Stream<Character>> streamCollector = CollectorsUtils.flatMapping(flatMapper); // When List<Character> characters = strings.collect(streamCollector).collect(toList()); // Then assertThat(characters).isEmpty(); }
public void testToImmutableMapMerging() { Collector<Entry<String, Integer>, ?, ImmutableMap<String, Integer>> collector = ImmutableMap.toImmutableMap(Entry::getKey, Entry::getValue, Integer::sum); Equivalence<ImmutableMap<String, Integer>> equivalence = Equivalence.equals() .<Entry<String, Integer>>pairwise() .onResultOf(ImmutableMap::entrySet); CollectorTester.of(collector, equivalence) .expectCollects( ImmutableMap.of("one", 1, "two", 4, "three", 3), mapEntry("one", 1), mapEntry("two", 2), mapEntry("three", 3), mapEntry("two", 2)); }
public void testToImmutableSortedSet_customComparator() { Collector<String, ?, ImmutableSortedSet<String>> collector = ImmutableSortedSet.toImmutableSortedSet(String.CASE_INSENSITIVE_ORDER); BiPredicate<ImmutableSortedSet<String>, ImmutableSortedSet<String>> equivalence = (set1, set2) -> set1.equals(set2) && set1.asList().equals(set2.asList()) && set1.comparator().equals(set2.comparator()); ImmutableSortedSet<String> expected = ImmutableSortedSet.orderedBy(String.CASE_INSENSITIVE_ORDER) .add("a", "B", "c", "d") .build(); CollectorTester.of(collector, equivalence) .expectCollects(expected, "a", "B", "a", "c", "b", "b", "d"); }
/** * Returns a collector which forms a JsonArray from JsonElements * * @return a new collector */ public static Collector<JsonElement, JsonArrayBuilder, JsonArray> collectToArray() { return Collector.of( JsonBuilder::array, JsonArrayBuilder::add, (l, r) -> l.addAll(r.build()), JsonArrayBuilder::build ); }
@Beta public static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap( Comparator<? super K> comparator, Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction) { return CollectCollectors.toImmutableSortedMap(comparator, keyFunction, valueFunction); }
/** * Verifies that the specified expected result is always produced by collecting the * specified inputs, regardless of how the elements are divided. */ @SafeVarargs public final CollectorTester<T, A, R> expectCollects(@Nullable R expectedResult, T... inputs) { List<T> list = Arrays.asList(inputs); doExpectCollects(expectedResult, list); if (collector.characteristics().contains(Collector.Characteristics.UNORDERED)) { Collections.reverse(list); doExpectCollects(expectedResult, list); } return this; }
private <E extends Enum<E>> void selectorToString(StringBuilder sb, Set<E> c, E[] values) { if (!c.containsAll(Arrays.asList(values))) { sb.append(c.stream() .sorted((x, y) -> x.ordinal() - y.ordinal()) .map(v -> v.name().toLowerCase(Locale.US)) .collect(new Collector<CharSequence, StringJoiner, String>() { @Override public BiConsumer<StringJoiner, CharSequence> accumulator() { return StringJoiner::add; } @Override public Supplier<StringJoiner> supplier() { return () -> new StringJoiner(",", (sb.length() == 0)? "" : "-", "") .setEmptyValue(""); } @Override public BinaryOperator<StringJoiner> combiner() { return StringJoiner::merge; } @Override public Function<StringJoiner, String> finisher() { return StringJoiner::toString; } @Override public Set<Characteristics> characteristics() { return Collections.emptySet(); } })); } }