@Test public void testSingleUserSet() { LongIterator iter = new AdaptiveSparseItemIterator(context, LongUtils.packedSet(42)); LongList items = LongIterators.pour(iter); assertThat(items, containsInAnyOrder(2L, 5L, 9L)); }
@Test public void testTwoUserSet() { LongIterator iter = new AdaptiveSparseItemIterator(context, LongUtils.packedSet(42, 39)); LongList items = LongIterators.pour(iter); assertThat(items, hasSize(5)); assertThat(items, containsInAnyOrder(2L, 5L, 9L, 7L, 13L)); }
@Test public void testLowerBound() { LongIterator iter = new AdaptiveSparseItemIterator(context, LongUtils.packedSet(42, 39), 5); LongList items = LongIterators.pour(iter); assertThat(items, hasSize(3)); assertThat(items, containsInAnyOrder(9L, 7L, 13L)); }
@Test public void testAdaptive() { LongIterator iter = new AdaptiveSparseItemIterator(context, LongUtils.packedSet(12, 42)); LongList items = LongIterators.pour(iter); // since 12 has too many items, it will use all items instead of 42's items assertThat(items, hasSize(context.getItems().size())); assertThat(LongUtils.packedSet(items), equalTo(context.getItems())); } }
/** * Pours an iterator, returning a type-specific list. * * <p> * This method iterates over the given type-specific iterator and returns a list * containing the returned elements. Iteration on the returned list is * guaranteed to produce the elements in the same order in which they appeared * in the iterator. * * @param i * a type-specific iterator. * @return a type-specific list containing the returned elements. */ public static LongList pour(final LongIterator i) { return pour(i, Integer.MAX_VALUE); } private static class IteratorWrapper implements LongIterator {
/** * Pours an iterator into a type-specific collection. * * <p> * This method iterates over the given type-specific iterator and adds the * returned elements to the given collection. * * @param i * a type-specific iterator. * @param s * a type-specific collection. * @return the number of elements poured. Note that this is the number of * elements returned by the iterator, which is not necessarily the * number of elements that have been added to the collection (because of * duplicates). */ public static int pour(final LongIterator i, final LongCollection s) { return pour(i, s, Integer.MAX_VALUE); } /**
/** * Pours an iterator, returning a type-specific list, with a limit on the number * of elements. * * <p> * This method iterates over the given type-specific iterator and returns a * type-specific list containing the returned elements (up to {@code max}). * Iteration on the returned list is guaranteed to produce the elements in the * same order in which they appeared in the iterator. * * * @param i * a type-specific iterator. * @param max * the maximum number of elements to be poured. * @return a type-specific list containing the returned elements, up to * {@code max}. */ public static LongList pour(final LongIterator i, int max) { final LongArrayList l = new LongArrayList(); pour(i, l, max); l.trim(); return l; } /**