summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/NthMatcher.java
blob: 933d4faff7f417ae18c00dcbdf9f35eccb7535fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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 <T> Matcher<T> first(final Matcher<T> matcher) {
        return nth(matcher, 1);
    }

    public static <T> Matcher<T> nth(final Matcher<T> matcher, final int index) {
        return new BaseMatcher<T>() {
            AtomicInteger count = new AtomicInteger(0);

            @Override
            public boolean matches(final Object item) {
                if (matcher.matches(item)) {
                    return count.incrementAndGet() == index;
                }
                return false;
            }

            @Override
            public void describeTo(final Description description) {
                description.appendText("Item #" + index + " ");
                description.appendDescriptionOf(matcher);
            }
        };
    }
}