-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathComparatorFunctionalInterfaceTest.java
69 lines (46 loc) · 1.76 KB
/
ComparatorFunctionalInterfaceTest.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package comparator;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import java.util.Comparator;
import org.hamcrest.Matcher;
import org.junit.Test;
public class ComparatorFunctionalInterfaceTest {
@Test
public void shouldCompareTwoGuidesByUsingComparator() throws Exception {
CraftCoderGuide mockito = new CraftCoderGuide("Mockito");
CraftCoderGuide java = new CraftCoderGuide("Java 8");
Comparator<CraftCoderGuide> comparator = new Comparator<CraftCoderGuide>() {
@Override
public int compare(CraftCoderGuide first, CraftCoderGuide second) {
return first.getName().compareTo(second.getName());
}
};
int compared = comparator.compare(mockito, java);
assertThat(compared, greaterThan(0));
}
@Test
public void shouldCompareTwoGuidesByUsingComparatorWithLambdaExpression() throws Exception {
CraftCoderGuide mockito = new CraftCoderGuide("Mockito");
CraftCoderGuide java = new CraftCoderGuide("Java 8");
Comparator<CraftCoderGuide> comparator = (first, second) -> first.getName().compareTo(second.getName());
int compared = comparator.compare(mockito, java);
assertThat(compared, greaterThan(0));
}
@Test
public void shouldCompareAndReverseTwoGuidesByUsingComparator() throws Exception {
CraftCoderGuide mockito = new CraftCoderGuide("Mockito");
CraftCoderGuide java = new CraftCoderGuide("Java 8");
Comparator<CraftCoderGuide> comparator = (first, second) -> first.getName().compareTo(second.getName());
int compared = comparator.reversed().compare(mockito, java);
assertThat(compared, lessThan(0));
}
}
class CraftCoderGuide {
private String name;
public CraftCoderGuide(String name) {
this.name = name;
}
public String getName() {
return name;
}
}