summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/NthMatcher.java
blob: 3f2b83a26c3219b535ce72e40144b37b77a78a16 (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
34
35
36
37
38
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> second(final Matcher<T> matcher) {
        return nth(matcher, 2);
    }

    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)) {
                    if (count.incrementAndGet() == index) {
                        return true;
                    }
                }
                return false;
            }

            @Override
            public void describeTo(final Description description) {
                description.appendText("should return first matching item");
            }
        };
    }
}