@Test public void merge_many() throws Exception { int numSources = 1000; int base = 1000; List<Source<IntBuf>> cursors = new ArrayList<>(); for (int i = 0; i < numSources; i++) { cursors.add(new IntSource(random(new Random(6502 + i), base))); } int last = -1; int count = 0; try (Source<IntBuf> c = Sources.merge(cursors, IntBuf.COMPARATOR)) { while (c.next()) { int next = c.get().value; assertTrue(last <= next); last = next; count++; } } assertThat(count, is(base * numSources)); }
public static <T> Source<T> merge( List<? extends Source<? extends T>> sortedSources, Comparator<? super T> comparator) { if (sortedSources.isEmpty()) { return empty(); } else if (sortedSources.size() == 1) { @SuppressWarnings("unchecked") Source<T> source = (Source<T>) sortedSources.get(0); return source; } return new HeapSource<>(sortedSources, comparator); }
Sources { public static <T> Source<T> merge( List<? extends Source<? extends T>> sortedSources, Comparator<? super T> comparator) { if (sortedSources.isEmpty()) { return empty(); } else if (sortedSources.size() == 1) { @SuppressWarnings("unchecked") Source<T> source = (Source<T>) sortedSources.get(0); return source; } return new HeapSource<>(sortedSources, comparator); } }
Sources { public static <T> Source<T> merge( List<? extends Source<? extends T>> sortedSources, Comparator<? super T> comparator) { if (sortedSources.isEmpty()) { return empty(); } else if (sortedSources.size() == 1) { @SuppressWarnings("unchecked") Source<T> source = (Source<T>) sortedSources.get(0); return source; } return new HeapSource<>(sortedSources, comparator); } private  Sources(); }
Sources { public static <T> Source<T> merge( List<? extends Source<? extends T>> sortedSources, Comparator<? super T> comparator) { if (sortedSources.isEmpty()) { return empty(); } else if (sortedSources.size() == 1) { @SuppressWarnings("unchecked") Source<T> source = (Source<T>) sortedSources.get(0); return source; } return new HeapSource<>(sortedSources, comparator); } private  Sources(); static Source<T> empty(); @SafeVarargs static Source<T> wrap(T... values); static Source<T> wrap(Iterator<? extends T> iterator); static Source<T> concat(List<? extends Source<? extends T>> sources); static Source<T> merge(
            List<? extends Source<? extends T>> sortedSources,
            Comparator<? super T> comparator); }
Sources { public static <T> Source<T> merge( List<? extends Source<? extends T>> sortedSources, Comparator<? super T> comparator) { if (sortedSources.isEmpty()) { return empty(); } else if (sortedSources.size() == 1) { @SuppressWarnings("unchecked") Source<T> source = (Source<T>) sortedSources.get(0); return source; } return new HeapSource<>(sortedSources, comparator); } private  Sources(); static Source<T> empty(); @SafeVarargs static Source<T> wrap(T... values); static Source<T> wrap(Iterator<? extends T> iterator); static Source<T> concat(List<? extends Source<? extends T>> sources); static Source<T> merge(
            List<? extends Source<? extends T>> sortedSources,
            Comparator<? super T> comparator);  }