/** * Creates a fluent iterable with the first {@code size} elements of this fluent iterable. If this * fluent iterable does not contain that many elements, the returned fluent iterable will have the * same behavior as this fluent iterable. The returned fluent iterable's iterator supports {@code * remove()} if this fluent iterable's iterator does. * * <p><b>{@code Stream} equivalent:</b> {@link Stream#limit} (same). * * @param maxSize the maximum number of elements in the returned fluent iterable * @throws IllegalArgumentException if {@code size} is negative */ public final FluentIterable<E> limit(int maxSize) { return from(Iterables.limit(getDelegate(), maxSize)); }
public static void assertContains(MaterializedResult all, MaterializedResult expectedSubset) { for (MaterializedRow row : expectedSubset.getMaterializedRows()) { if (!all.getMaterializedRows().contains(row)) { fail(format("expected row missing: %s\nAll %s rows:\n %s\nExpected subset %s rows:\n %s\n", row, all.getMaterializedRows().size(), Joiner.on("\n ").join(Iterables.limit(all, 100)), expectedSubset.getMaterializedRows().size(), Joiner.on("\n ").join(Iterables.limit(expectedSubset, 100)))); } } }
public void testLimit_illegalArgument() { List<String> list = newArrayList("a", "b", "c"); try { Iterables.limit(list, -1); fail(); } catch (IllegalArgumentException expected) { } }
public void testLimit() { Iterable<String> iterable = newArrayList("foo", "bar", "baz"); Iterable<String> limited = Iterables.limit(iterable, 2); List<String> expected = ImmutableList.of("foo", "bar"); List<String> actual = newArrayList(limited); assertEquals(expected, actual); assertCanIterateAgain(limited); assertEquals("[foo, bar]", limited.toString()); }
@Test public void forTree_breadthFirst_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(TREE); Iterable<Character> result = Traverser.forGraph(graph).breadthFirst('h'); assertEqualCharNodes(Iterables.limit(result, 2), "hd"); assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "hd"); assertThat(graph.requestedNodes).containsExactly('h', 'h', 'h', 'd', 'd'); }
@Test public void forGraph_depthFirstPreOrder_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(DIAMOND_GRAPH); Iterable<Character> result = Traverser.forGraph(graph).depthFirstPreOrder('a'); assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'd'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b', 'd', 'd'); }
@Test public void forGraph_depthFirstPostOrder_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(DIAMOND_GRAPH); Iterable<Character> result = Traverser.forGraph(graph).depthFirstPostOrder('a'); assertEqualCharNodes(Iterables.limit(result, 2), "db"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'd'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "db"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b', 'd', 'd'); }
@Test public void forTree_depthFirstPreOrder_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(TREE); Iterable<Character> result = Traverser.forGraph(graph).depthFirstPreOrder('h'); assertEqualCharNodes(Iterables.limit(result, 2), "hd"); assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'a'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "hd"); assertThat(graph.requestedNodes).containsExactly('h', 'h', 'h', 'd', 'd', 'a', 'a'); }
@Test public void forTree_depthFirstPostOrder_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(TREE); Iterable<Character> result = Traverser.forGraph(graph).depthFirstPostOrder('h'); assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('h', 'h', 'd', 'a', 'b'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('h', 'h', 'h', 'd', 'd', 'a', 'a', 'b', 'b'); }
@Test public void forGraph_breadthFirstIterable_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(DIAMOND_GRAPH); Iterable<Character> result = Traverser.forGraph(graph).breadthFirst(charactersOf("ab")); assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'b'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b', 'b'); }
@Test public void forTree_depthFirstPostOrderIterable_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(TREE); Iterable<Character> result = Traverser.forGraph(graph).depthFirstPostOrder(charactersOf("dg")); assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('a', 'b', 'd', 'd', 'g'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'b', 'd', 'd', 'd', 'g'); }
/** * Checks that the elements of the iterable are calculated on the fly. Concretely, that means that * {@link SuccessorsFunction#successors(Object)} can only be called for a subset of all nodes. */ @Test public void forGraph_breadthFirst_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(DIAMOND_GRAPH); Iterable<Character> result = Traverser.forGraph(graph).breadthFirst('a'); assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b'); }
@Test public void forGraph_depthFirstPreOrderIterable_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(DIAMOND_GRAPH); Iterable<Character> result = Traverser.forGraph(graph).depthFirstPreOrder(charactersOf("ac")); assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'c', 'd'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "ab"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b', 'c', 'd', 'd'); }
@Test public void forTree_breadthFirstIterable_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(TREE); Iterable<Character> result = Traverser.forGraph(graph).breadthFirst(charactersOf("dg")); assertEqualCharNodes(Iterables.limit(result, 3), "dga"); assertThat(graph.requestedNodes).containsExactly('a', 'd', 'd', 'g', 'g'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 3), "dga"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'd', 'd', 'd', 'g', 'g', 'g'); }
@Test public void forTree_depthFirstPreOrderIterable_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(TREE); Iterable<Character> result = Traverser.forGraph(graph).depthFirstPreOrder(charactersOf("dg")); assertEqualCharNodes(Iterables.limit(result, 2), "da"); assertThat(graph.requestedNodes).containsExactly('a', 'd', 'd', 'g'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "da"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'd', 'd', 'd', 'g'); }
@Test public void forGraph_depthFirstPostOrderIterable_iterableIsLazy() { RequestSavingGraph graph = new RequestSavingGraph(DIAMOND_GRAPH); Iterable<Character> result = Traverser.forGraph(graph).depthFirstPostOrder(charactersOf("ac")); assertEqualCharNodes(Iterables.limit(result, 2), "db"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'b', 'c', 'd'); // Iterate again to see if calculation is done again assertEqualCharNodes(Iterables.limit(result, 2), "db"); assertThat(graph.requestedNodes).containsExactly('a', 'a', 'a', 'b', 'b', 'c', 'd', 'd'); }
@Test public void testTwo() throws Exception { final List<Integer> nums = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); final int threshold = 2; SequenceTestHelper.testAll( Sequences.simple(nums).limit(threshold), Lists.newArrayList(Iterables.limit(nums, threshold)) ); }
@Test public void testOne() throws Exception { final List<Integer> nums = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); final int threshold = 1; SequenceTestHelper.testAll( Sequences.simple(nums).limit(threshold), Lists.newArrayList(Iterables.limit(nums, threshold)) ); }
public void testFileTraverser_multipleDirectoryLayers_breadthFirstStartsWithTopLayer() throws Exception { Path fileA = newFile("file-a"); Path dir1 = newDir("dir-1"); newFile("dir-1/file-b"); newFile("dir-1/dir-2"); assertThat(Iterables.limit(MoreFiles.fileTraverser().breadthFirst(rootDir), 3)) .containsExactly(rootDir, fileA, dir1); }
public void testFileTraverser_multipleDirectoryLayers_breadthFirstStartsWithTopLayer() throws Exception { File fileA = newFile("file-a"); File dir1 = newDir("dir-1"); newFile("dir-1/file-b"); newFile("dir-1/dir-2"); assertThat(Iterables.limit(Files.fileTraverser().breadthFirst(rootDir), 3)) .containsExactly(rootDir, fileA, dir1); }