org.hamcrest.collection.IsCollectionWithSize 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 org.hamcrest.Factory;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
import java.util.Collection;
import static org.hamcrest.core.IsEqual.equalTo;
/**
* Matches if collection size satisfies a nested matcher.
*/
public class IsCollectionWithSize extends FeatureMatcher, Integer> {
public IsCollectionWithSize(Matcher super Integer> sizeMatcher) {
super(sizeMatcher, "a collection with size", "collection size");
}
@Override
protected Integer featureValueOf(Collection extends E> actual) {
return actual.size();
}
/**
* Creates a matcher for {@link java.util.Collection}s that matches when the size()
method returns
* a value that satisfies the specified matcher.
*
* For example:
* assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)))
*
* @param sizeMatcher
* a matcher for the size of an examined {@link java.util.Collection}
*/
@Factory
public static Matcher> hasSize(Matcher super Integer> sizeMatcher) {
return new IsCollectionWithSize(sizeMatcher);
}
/**
* Creates a matcher for {@link java.util.Collection}s that matches when the size()
method returns
* a value equal to the specified size
.
*
* For example:
* assertThat(Arrays.asList("foo", "bar"), hasSize(2))
*
* @param size
* the expected size of an examined {@link java.util.Collection}
*/
@Factory
public static Matcher> hasSize(int size) {
Matcher super Integer> matcher = equalTo(size);
return IsCollectionWithSize.hasSize(matcher);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy