forked from gmjonker/util
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsSortedMatcher.java
More file actions
47 lines (39 loc) · 1.12 KB
/
IsSortedMatcher.java
File metadata and controls
47 lines (39 loc) · 1.12 KB
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
39
40
41
42
43
44
45
46
47
package gmjonker.matchers;
import com.google.common.collect.Ordering;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.TypeSafeMatcher;
import java.util.List;
public class IsSortedMatcher<T extends Comparable> extends TypeSafeMatcher<List<T>>
{
private final boolean reverse;
public IsSortedMatcher(boolean reverse)
{
this.reverse = reverse;
}
@Override
public boolean matchesSafely(List<T> list)
{
if (reverse)
return Ordering.natural().reverse().isOrdered(list);
else
return Ordering.natural().isOrdered(list);
}
public void describeTo(Description description)
{
if (reverse)
description.appendText("a inversly sorted list");
else
description.appendText("a sorted list");
}
@Factory
public static <S extends Comparable> IsSortedMatcher<S> isSortedNaturally()
{
return new IsSortedMatcher<S>(false);
}
@Factory
public static <S extends Comparable> IsSortedMatcher<S> isSortedInversly()
{
return new IsSortedMatcher<S>(true);
}
}