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

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

There is a newer version: 3.0
Show newest version
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 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 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