1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package io.earcam.unexceptional;
20
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.equalTo;
23 import static org.hamcrest.Matchers.greaterThan;
24 import static org.hamcrest.Matchers.is;
25 import static org.hamcrest.Matchers.lessThan;
26 import static org.hamcrest.Matchers.not;
27
28 import java.io.IOException;
29
30 import org.junit.jupiter.api.Test;
31
32 public class CheckedComparatorTest {
33
34 private static class Compound {
35 int a;
36 int b;
37
38
39 public int a() throws Throwable
40 {
41 return a;
42 }
43
44
45 public int b() throws Throwable
46 {
47 return b;
48 }
49 }
50
51 private static class Compared {
52 String s;
53 double d;
54 long l;
55 Compound c;
56 Object amorphous;
57
58
59 public String s() throws Throwable
60 {
61 return s;
62 }
63
64
65 public double d() throws Exception
66 {
67 return d;
68 }
69
70
71 public long l() throws Exception
72 {
73 return l;
74 }
75
76
77 public Compound c() throws IOException
78 {
79 return c;
80 }
81
82
83 public String amorphous()
84 {
85 return amorphous.toString();
86 }
87 }
88
89 private final CheckedComparator<Compared, Throwable> comparator = CheckedComparator.comparing(Compared::s)
90 .thenComparingDouble(Compared::d)
91 .thenComparingLong(Compared::l)
92 .thenComparing(Compared::amorphous)
93 .thenComparing(Compared::c, CheckedComparator.comparingInt(Compound::a).thenComparingInt(Compound::b));
94
95
96 @Test
97 public void thenComparingGreaterThan()
98 {
99 Compared a = createCompared(21);
100 Compared b = createCompared(12);
101
102 int diff = Exceptional.applyAsInt(comparator::compare, a, b);
103 assertThat(diff, is(greaterThan(0)));
104 }
105
106
107
108
109 @Test
110 public void thenComparingArbitraryAmorphous()
111 {
112 Compared a = createCompared(21);
113 Compared b = createCompared(21);
114
115 b.amorphous = new Object();
116
117 int diff = Exceptional.applyAsInt(comparator::compare, a, b);
118 assertThat(diff, is(not(equalTo(0))));
119 }
120
121
122 Compared createCompared(int value)
123 {
124 Compared compared = new Compared();
125 compared.s = "s";
126 compared.d = 42D;
127 compared.l = 101L;
128 compared.c = new Compound();
129 compared.c.a = 1;
130 compared.c.b = value;
131 compared.amorphous = this;
132 return compared;
133 }
134
135
136 @Test
137 public void thenComparingLessThan()
138 {
139 Compared a = createCompared(12);
140 Compared b = createCompared(21);
141
142 int diff = Exceptional.applyAsInt(comparator::compare, a, b);
143 assertThat(diff, is(lessThan(0)));
144 }
145
146
147 @Test
148 public void thenComparingEqual()
149 {
150 Compared a = createCompared(21);
151 Compared b = createCompared(21);
152
153 int diff = Exceptional.applyAsInt(comparator::compare, a, b);
154 assertThat(diff, is(equalTo(0)));
155 }
156
157
158 @Test
159 public void reverse()
160 {
161 Compared a = createCompared(12);
162 Compared b = createCompared(21);
163
164 int diff = Exceptional.applyAsInt(comparator.reversed()::compare, a, b);
165 assertThat(diff, is(greaterThan(0)));
166 }
167 }