Created
February 12, 2013 09:46
-
-
Save eirikm/4761241 to your computer and use it in GitHub Desktop.
Guava spike
This file contains 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
import com.google.common.base.Predicate; | |
import com.google.common.collect.Collections2; | |
import com.google.common.collect.Lists; | |
import org.junit.Test; | |
import javax.annotation.Nullable; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import static org.fest.assertions.Assertions.assertThat; | |
/** | |
* @author Eirik Meland <eirik.meland@conduct.no> | |
*/ | |
public class Spike { | |
private final Predicate<Integer> even = new Predicate<Integer>() { | |
@Override | |
public boolean apply(@Nullable Integer input) { | |
if (input == null) { | |
return false; | |
} else { | |
return input % 2 == 0; | |
} | |
} | |
}; | |
@Test | |
public void name() { | |
// arrange | |
ArrayList<Integer> integers = Lists.newArrayList(1, 2, 3, 4); | |
// act | |
Collection<Integer> evenIntegers = Collections2.filter(integers, even); | |
// assert | |
assertThat(evenIntegers).containsOnly(2, 4); | |
assertThat(evenIntegers).excludes(6); | |
integers.add(6); | |
assertThat(evenIntegers).contains(6); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment