/** * Returns a view of this fluent iterable that skips its first {@code numberToSkip} elements. If * this fluent iterable contains fewer than {@code numberToSkip} elements, the returned fluent * iterable skips all of its elements. * * <p>Modifications to this fluent iterable before a call to {@code iterator()} are reflected in * the returned fluent iterable. That is, the its iterator skips the first {@code numberToSkip} * elements that exist when the iterator is created, not when {@code skip()} is called. * * <p>The returned fluent iterable's iterator supports {@code remove()} if the {@code Iterator} of * this fluent iterable supports it. Note that it is <i>not</i> possible to delete the last * skipped element by immediately calling {@code remove()} on the returned fluent iterable's * iterator, as the {@code Iterator} contract states that a call to {@code * remove()} before a * call to {@code next()} will throw an {@link IllegalStateException}. * * <p><b>{@code Stream} equivalent:</b> {@link Stream#skip} (same). */ public final FluentIterable<E> skip(int numberToSkip) { return from(Iterables.skip(getDelegate(), numberToSkip)); }
@Override protected Iterator<Integer> newTargetIterator() { return skip(newArrayList(1, 2, 3), 1).iterator(); } }.test();
@Override @CanIgnoreReturnValue public boolean addAll(Collection<? extends E> collection) { int size = collection.size(); if (size >= maxSize) { clear(); return Iterables.addAll(this, Iterables.skip(collection, size - maxSize)); } return standardAddAll(collection); }
@Override protected Iterator<Integer> newTargetIterator() { return skip(newLinkedHashSet(asList(1, 2, 3)), 1).iterator(); } }.test();
public void testSkip_structurallyModifiedSkipAllList() throws Exception { List<String> list = newArrayList("a", "b", "c"); Iterable<String> tail = skip(list, 2); list.subList(0, 2).clear(); assertTrue(Iterables.isEmpty(tail)); }
public void testSkip_structurallyModifiedSkipAll() throws Exception { Collection<String> set = newLinkedHashSet(asList("a", "b", "c")); Iterable<String> tail = skip(set, 2); set.remove("a"); set.remove("b"); assertFalse(tail.iterator().hasNext()); }
public void testSkip_allOfMutableList_modifiable() { List<String> list = newArrayList("a", "b"); Iterator<String> iterator = skip(list, 2).iterator(); try { iterator.remove(); fail("Expected IllegalStateException"); } catch (IllegalStateException expected) { } }
public void testSkip_allOfImmutableList_modifiable() { List<String> list = ImmutableList.of("a", "b"); Iterator<String> iterator = skip(list, 2).iterator(); try { iterator.remove(); fail("Expected UnsupportedOperationException"); } catch (UnsupportedOperationException expected) { } }
public void testSkip_illegalArgument() { List<String> list = newArrayList("a", "b", "c"); try { skip(list, -1); fail(); } catch (IllegalArgumentException expected) { } }
public void testSkip_skipNone() { Collection<String> set = ImmutableSet.of("a", "b"); assertEquals(newArrayList("a", "b"), newArrayList(skip(set, 0))); }
public void testSkip_pastEnd() { Collection<String> set = ImmutableSet.of("a", "b"); assertEquals(emptyList(), newArrayList(skip(set, 20))); }
@Override @CanIgnoreReturnValue public boolean addAll(Collection<? extends E> collection) { int size = collection.size(); if (size >= maxSize) { clear(); return Iterables.addAll(this, Iterables.skip(collection, size - maxSize)); } return standardAddAll(collection); }
public void testSkip_pastEndList() { Collection<String> list = newArrayList("a", "b"); assertEquals(emptyList(), newArrayList(skip(list, 20))); }
public void testSkip_skipNoneList() { Collection<String> list = newArrayList("a", "b"); assertEquals(newArrayList("a", "b"), newArrayList(skip(list, 0))); }
public void testSkip_removal() { Collection<String> set = Sets.newHashSet("a", "b"); Iterator<String> iterator = skip(set, 2).iterator(); try { iterator.next(); } catch (NoSuchElementException suppressed) { // We want remove() to fail even after a failed call to next(). } try { iterator.remove(); fail("Expected IllegalStateException"); } catch (IllegalStateException expected) { } }
public void testSkip_structurallyModifiedSkipSomeList() throws Exception { List<String> list = newArrayList("a", "b", "c"); Iterable<String> tail = skip(list, 1); list.subList(1, 3).clear(); list.addAll(0, newArrayList("A", "B", "C")); assertThat(tail).containsExactly("B", "C", "a").inOrder(); }
public void testSkip_structurallyModifiedSkipSome() throws Exception { Collection<String> set = newLinkedHashSet(asList("a", "b", "c")); Iterable<String> tail = skip(set, 1); set.remove("b"); set.addAll(newArrayList("A", "B", "C")); assertThat(tail).containsExactly("c", "A", "B", "C").inOrder(); }
public void testSkip_nonStructurallyModifiedList() throws Exception { List<String> list = newArrayList("a", "b", "c"); Iterable<String> tail = skip(list, 1); Iterator<String> tailIterator = tail.iterator(); list.set(2, "C"); assertEquals("b", tailIterator.next()); assertEquals("C", tailIterator.next()); assertFalse(tailIterator.hasNext()); }