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<E> extends FeatureMatcher<Iterable<E>, Integer> {

    public IsIterableWithSize(Matcher<? super Integer> sizeMatcher) {
        super(sizeMatcher, "an iterable with size", "iterable size");
    }
    

    @Override
    protected Integer featureValueOf(Iterable<E> actual) {
      int size = 0;
      for (Iterator<E> iterator = actual.iterator(); iterator.hasNext(); iterator.next()) {
        size++;
      }
      return size;
    }

    
Creates a matcher for Iterables that matches when a single pass over the examined Iterable yields an item count that satisfies the specified matcher.

For example:
assertThat(Arrays.asList("foo", "bar"), iterableWithSize(equalTo(2)))
Params:
  • sizeMatcher – a matcher for the number of items that should be yielded by an examined Iterable
/** * 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. * <p/> * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), iterableWithSize(equalTo(2)))</pre> * * @param sizeMatcher * a matcher for the number of items that should be yielded by an examined {@link Iterable} */
@Factory public static <E> Matcher<Iterable<E>> iterableWithSize(Matcher<? super Integer> sizeMatcher) { return new IsIterableWithSize<E>(sizeMatcher); }
Creates a matcher for Iterables that matches when a single pass over the examined Iterable yields an item count that is equal to the specified size argument.

For example:
assertThat(Arrays.asList("foo", "bar"), iterableWithSize(2))
Params:
  • size – the number of items that should be yielded by an examined Iterable
/** * 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 * <code>size</code> argument. * <p/> * For example: * <pre>assertThat(Arrays.asList("foo", "bar"), iterableWithSize(2))</pre> * * @param size * the number of items that should be yielded by an examined {@link Iterable} */
@Factory public static <E> Matcher<Iterable<E>> iterableWithSize(int size) { return iterableWithSize(equalTo(size)); } }