@Test public void collectSortedListLeftEmpty() { List<Integer> left = Collections.emptyList(); List<Integer> right = Arrays.asList(2, 4, 5, 6); List<Integer> result = ParallelFlux.sortedMerger(left, right, Integer::compareTo); assertThat(result) .containsExactly(2, 4, 5, 6); }
@Test public void collectSortedListRightEmpty() { List<Integer> left = Arrays.asList(2, 4, 5, 6); List<Integer> right = Collections.emptyList(); List<Integer> result = ParallelFlux.sortedMerger(left, right, Integer::compareTo); assertThat(result) .containsExactly(2, 4, 5, 6); }
@Test public void collectSortedListBothEmpty() { List<Integer> result = ParallelFlux.sortedMerger(Collections.emptyList(), Collections.emptyList(), Integer::compareTo); assertThat(result) .isEmpty(); }
@Test public void collectSortedListRightLarger() { List<Integer> left = Arrays.asList(1, 3); List<Integer> right = Arrays.asList(2, 4, 5, 6); List<Integer> result = ParallelFlux.sortedMerger(left, right, Integer::compareTo); assertThat(result) .containsExactly(1, 2, 3, 4, 5, 6); }
@Test public void collectSortedListLeftLarger() { List<Integer> left = Arrays.asList(2, 4, 5, 6); List<Integer> right = Arrays.asList(1, 3); List<Integer> result = ParallelFlux.sortedMerger(left, right, Integer::compareTo); assertThat(result) .containsExactly(1, 2, 3, 4, 5, 6); }
/** * Sorts the 'rails' according to the comparator and returns a full sorted list as a * Publisher. * <p> * This operator requires a finite source ParallelFlux. * * @param comparator the comparator to compare elements * @param capacityHint the expected number of total elements * * @return the new Mono instance */ public final Mono<List<T>> collectSortedList(Comparator<? super T> comparator, int capacityHint) { int ch = capacityHint / parallelism() + 1; ParallelFlux<List<T>> railReduced = reduce(() -> new ArrayList<>(ch), (a, b) -> { a.add(b); return a; }); ParallelFlux<List<T>> railSorted = railReduced.map(list -> { list.sort(comparator); return list; }); Mono<List<T>> merged = railSorted.reduce((a, b) -> sortedMerger(a, b, comparator)); return merged; }
/** * Sorts the 'rails' according to the comparator and returns a full sorted list as a * Publisher. * <p> * This operator requires a finite source ParallelFlux. * * @param comparator the comparator to compare elements * @param capacityHint the expected number of total elements * * @return the new Mono instance */ public final Mono<List<T>> collectSortedList(Comparator<? super T> comparator, int capacityHint) { int ch = capacityHint / parallelism() + 1; ParallelFlux<List<T>> railReduced = reduce(() -> new ArrayList<>(ch), (a, b) -> { a.add(b); return a; }); ParallelFlux<List<T>> railSorted = railReduced.map(list -> { list.sort(comparator); return list; }); Mono<List<T>> merged = railSorted.reduce((a, b) -> sortedMerger(a, b, comparator)); return merged; }