@Test public void mergeBiFunction() throws Exception { MergerBiFunction<Integer> f = new MergerBiFunction<Integer>(Functions.<Integer>naturalComparator()); assertEquals(0, f.apply(Collections.<Integer>emptyList(), Collections.<Integer>emptyList()).size()); assertEquals(Arrays.asList(1, 2), f.apply(Collections.<Integer>emptyList(), Arrays.asList(1, 2))); for (int i = 0; i < 4; i++) { int k = 0; List<Integer> list1 = new ArrayList<Integer>(); for (int j = 0; j < i; j++) { list1.add(k++); } List<Integer> list2 = new ArrayList<Integer>(); for (int j = i; j < 4; j++) { list2.add(k++); } assertEquals(Arrays.asList(0, 1, 2, 3), f.apply(list1, list2)); } }
/** * Sorts the 'rails' according to the comparator and returns a full sorted list as a Publisher. * <p> * This operator requires a finite source ParallelFlowable. * * @param comparator the comparator to compare elements * @param capacityHint the expected number of total elements * @return the new Flowable instance */ @CheckReturnValue @NonNull public final Flowable<List<T>> toSortedList(@NonNull Comparator<? super T> comparator, int capacityHint) { ObjectHelper.requireNonNull(comparator, "comparator is null"); ObjectHelper.verifyPositive(capacityHint, "capacityHint"); int ch = capacityHint / parallelism() + 1; ParallelFlowable<List<T>> railReduced = reduce(Functions.<T>createArrayList(ch), ListAddBiConsumer.<T>instance()); ParallelFlowable<List<T>> railSorted = railReduced.map(new SorterFunction<T>(comparator)); Flowable<List<T>> merged = railSorted.reduce(new MergerBiFunction<T>(comparator)); return RxJavaFlowablePlugins.onAssembly(merged); }