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

com.objogate.wl.swing.matcher.IterableComponentsMatcher Maven / Gradle / Ivy

The newest version!
/**
 * Matches an iterable collection of Components against an array of Matchers.
 * Each Matcher has to match the component at its position in the iteration. 
 */
package com.objogate.wl.swing.matcher;

import java.awt.Component;
import java.util.Arrays;
import java.util.Iterator;

import javax.swing.JComponent;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public final class IterableComponentsMatcher extends TypeSafeMatcher> {
  private final Matcher[] matchers;

  public IterableComponentsMatcher(Matcher[] matchers) {
    this.matchers = matchers;
  }

  @Override public boolean matchesSafely(Iterable components) {
    Iterator iterator = components.iterator();
    for (Matcher matcher : matchers) {
      if (! isAMatch(matcher, iterator)) {
        return false;
      }
    }
    return ! iterator.hasNext();
  }

  public void describeTo(Description description) {
    description.appendList("with cells ", ", ", "", Arrays.asList(matchers));
  }

  private boolean isAMatch(Matcher matcher, Iterator iterator) {
    return iterator.hasNext() && matcher.matches(iterator.next());
  }

  public static Matcher> matching(final Matcher... matchers) {
    return new IterableComponentsMatcher(matchers);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy