package de.test.antennapod; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.Matcher; import java.util.concurrent.atomic.AtomicInteger; public class NthMatcher { public static Matcher first(final Matcher matcher) { return nth(matcher, 1); } public static Matcher second(final Matcher matcher) { return nth(matcher, 2); } private static Matcher nth(final Matcher matcher, final int index) { return new BaseMatcher() { AtomicInteger count = new AtomicInteger(0); @Override public boolean matches(final Object item) { if (matcher.matches(item)) { if (count.incrementAndGet() == index) { return true; } } return false; } @Override public void describeTo(final Description description) { description.appendText("should return first matching item"); } }; } }