Created
December 31, 2013 13:20
-
-
Save chalup/8196709 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.futuresimple.base.util; | |
import com.google.common.base.Predicate; | |
import com.google.common.collect.AbstractIterator; | |
import com.google.common.collect.Iterators; | |
import com.google.common.collect.PeekingIterator; | |
import java.util.Iterator; | |
public abstract class SkipWhile { | |
private SkipWhile() { | |
} | |
public static <T> Iterable<T> skipWhile(final Iterable<T> iterable, final Predicate<T> predicate) { | |
return new Iterable<T>() { | |
@Override | |
public Iterator<T> iterator() { | |
final PeekingIterator<T> iterator = Iterators.peekingIterator(iterable.iterator()); | |
return new AbstractIterator<T>() { | |
boolean mDoneSkipping = false; | |
@Override | |
protected T computeNext() { | |
if (!mDoneSkipping) { | |
while (iterator.hasNext() && predicate.apply(iterator.peek())) { | |
iterator.next(); | |
} | |
mDoneSkipping = true; | |
} | |
if (iterator.hasNext()) { | |
return iterator.next(); | |
} else { | |
return endOfData(); | |
} | |
} | |
}; | |
} | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.futuresimple.base.tests; | |
import static com.futuresimple.base.util.SkipWhile.skipWhile; | |
import static org.fest.assertions.Assertions.assertThat; | |
import com.google.common.base.Predicates; | |
import com.google.common.collect.Lists; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.robolectric.RobolectricTestRunner; | |
import org.robolectric.annotation.Config; | |
import java.util.ArrayList; | |
@RunWith(RobolectricTestRunner.class) | |
@Config(reportSdk = 10) | |
public class SkipWhileTest { | |
@Test | |
public void shouldSkipElementsMatchingThePredicate() throws Exception { | |
Iterable<Integer> skipped = skipWhile(Lists.newArrayList(1, 2, 3), Predicates.equalTo(1)); | |
assertThat(skipped).containsOnly(2, 3); | |
} | |
@Test | |
public void shouldReturnEmptyIterableWhenAllElementsMatchThePredicate() throws Exception { | |
Iterable<Integer> skipped = skipWhile(Lists.newArrayList(1, 1, 1), Predicates.equalTo(1)); | |
assertThat(skipped).isEmpty(); | |
} | |
@Test | |
public void shouldReturnTheSameIterableWhenFirstElementDoesNotMatchThePredicate() throws Exception { | |
ArrayList<Integer> iterable = Lists.newArrayList(3, 2, 1); | |
Iterable<Integer> skipped = skipWhile(iterable, Predicates.equalTo(1)); | |
assertThat(Lists.newArrayList(skipped)).isEqualTo(iterable); | |
} | |
@Test | |
public void shouldGracefullyHandleEmptyIterable() throws Exception { | |
Iterable<Integer> skipped = skipWhile(new ArrayList<Integer>(), Predicates.equalTo(1)); | |
assertThat(skipped).isEmpty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment