org.hamcrest.collection.IsIterableWithSize Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hamcrest-library Show documentation
Show all versions of hamcrest-library Show documentation
A library of Hamcrest matchers - deprecated, please use "hamcrest" instead
package org.hamcrest.collection;
import static org.hamcrest.core.IsEqual.equalTo;
import java.util.Iterator;
import org.hamcrest.Factory;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
public class IsIterableWithSize extends FeatureMatcher, Integer> {
public IsIterableWithSize(Matcher super Integer> sizeMatcher) {
super(sizeMatcher, "an iterable with size", "iterable size");
}
@Override
protected Integer featureValueOf(Iterable actual) {
int size = 0;
for (Iterator iterator = actual.iterator(); iterator.hasNext(); iterator.next()) {
size++;
}
return size;
}
/**
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
* examined {@link Iterable} yields an item count that satisfies the specified
* matcher.
*
* For example:
* assertThat(Arrays.asList("foo", "bar"), iterableWithSize(equalTo(2)))
*
* @param sizeMatcher
* a matcher for the number of items that should be yielded by an examined {@link Iterable}
*/
@Factory
public static Matcher> iterableWithSize(Matcher super Integer> sizeMatcher) {
return new IsIterableWithSize(sizeMatcher);
}
/**
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
* examined {@link Iterable} yields an item count that is equal to the specified
* size
argument.
*
* For example:
* assertThat(Arrays.asList("foo", "bar"), iterableWithSize(2))
*
* @param size
* the number of items that should be yielded by an examined {@link Iterable}
*/
@Factory
public static Matcher> iterableWithSize(int size) {
return iterableWithSize(equalTo(size));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy