All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hamcrest.collection.IsCollectionWithSize Maven / Gradle / Ivy

There is a newer version: 3.0
Show newest version
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 sizeMatcher) {
      super(sizeMatcher, "a collection with size", "collection size");
    }

    @Override
    protected Integer featureValueOf(Collection 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 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 matcher = equalTo(size); return IsCollectionWithSize.hasSize(matcher); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy