2323import com .google .inject .TypeLiteral ;
2424import io .vavr .Tuple ;
2525import io .vavr .Tuple2 ;
26- import org .assertj .core .api .Assertions ;
2726import org .junit .jupiter .api .DynamicTest ;
2827import org .junit .jupiter .api .Test ;
2928import org .junit .jupiter .api .TestFactory ;
3029
3130import java .util .Comparator ;
31+ import java .util .List ;
3232import java .util .stream .Stream ;
3333
3434import static org .assertj .core .api .Assertions .assertThat ;
3535
3636class TestCompareUtil {
3737
38+ private static final Person BOB = new Person ("Bob" , 42 );
39+ private static final Person FRED = new Person ("Fred" , 37 );
40+ private static final Person NADINE = new Person ("Nadine" , 37 );
41+ private static final Person JANE = new Person ("Jane" , 57 );
42+
3843 @ Test
3944 void testStringCompare () {
4045 assertThat (CompareUtil .compareString (null , null )).isEqualTo (0 );
@@ -74,7 +79,7 @@ Stream<DynamicTest> getNullSafeCaseInsensitiveComparator() {
7479 if (compareResult != 0 ) {
7580 // Test the reverse
7681 final int compareResult2 = comparator .compare (wrapper2 , wrapper1 );
77- Assertions . assertThat (compareResult2 )
82+ assertThat (compareResult2 )
7883 .isEqualTo (compareResult * -1 );
7984 }
8085 return CompareResult .of (compareResult );
@@ -90,6 +95,136 @@ Stream<DynamicTest> getNullSafeCaseInsensitiveComparator() {
9095 .build ();
9196 }
9297
98+ @ Test
99+ void testCombine1 () {
100+ final List <Person > people = List .of (BOB , FRED , NADINE , JANE );
101+
102+ final Comparator <Person > comparator = CompareUtil .combine (null , Comparator .comparing (Person ::name ));
103+ final List <Person > sorted = people .stream ()
104+ .sorted (comparator )
105+ .toList ();
106+
107+ assertThat (sorted )
108+ .containsExactly (BOB , FRED , JANE , NADINE );
109+ }
110+
111+ @ Test
112+ void testCombine2 () {
113+ final List <Person > people = List .of (BOB , FRED , NADINE , JANE );
114+
115+ final Comparator <Person > comparator = CompareUtil .combine (Comparator .comparing (Person ::name ), null );
116+ final List <Person > sorted = people .stream ()
117+ .sorted (comparator )
118+ .toList ();
119+
120+ assertThat (sorted )
121+ .containsExactly (BOB , FRED , JANE , NADINE );
122+ }
123+
124+ @ Test
125+ void testCombine3 () {
126+ final List <Person > people = List .of (BOB , FRED , NADINE , JANE );
127+
128+ final Comparator <Person > comparator = CompareUtil .combine (
129+ Comparator .comparingInt (Person ::age ),
130+ Comparator .comparing (Person ::name ));
131+
132+ final List <Person > sorted = people .stream ()
133+ .sorted (comparator )
134+ .toList ();
135+
136+ assertThat (sorted )
137+ .containsExactly (FRED , NADINE , BOB , JANE );
138+ }
139+
140+ @ Test
141+ void testCombine4 () {
142+ //noinspection ConstantValue
143+ final Comparator <Person > comparator = CompareUtil .combine (null , null );
144+ assertThat (comparator )
145+ .isNull ();
146+ }
147+
148+ @ Test
149+ void testReverse () {
150+ final List <Person > people = List .of (BOB , FRED , NADINE , JANE );
151+ final Comparator <Person > comparator = CompareUtil .reverseIf (Comparator .comparing (Person ::name ), false );
152+
153+ final List <Person > sorted = people .stream ()
154+ .sorted (comparator )
155+ .toList ();
156+
157+ assertThat (sorted )
158+ .containsExactly (BOB , FRED , JANE , NADINE );
159+ }
160+
161+ @ Test
162+ void testReverse2 () {
163+ final List <Person > people = List .of (BOB , FRED , NADINE , JANE );
164+ final Comparator <Person > comparator = CompareUtil .reverseIf (Comparator .comparing (Person ::name ), true );
165+
166+ final List <Person > sorted = people .stream ()
167+ .sorted (comparator )
168+ .toList ();
169+
170+ assertThat (sorted )
171+ .containsExactly (NADINE , JANE , FRED , BOB );
172+ }
173+
174+ @ Test
175+ void testReverse3 () {
176+ //noinspection ConstantValue
177+ final Comparator <Person > comparator = CompareUtil .reverseIf (null , true );
178+ assertThat (comparator )
179+ .isNull ();
180+ }
181+
182+ @ Test
183+ void testReverse4 () {
184+ //noinspection ConstantValue
185+ final Comparator <Person > comparator = CompareUtil .reverseIf (null , false );
186+ assertThat (comparator )
187+ .isNull ();
188+ }
189+
190+ @ Test
191+ void testNoOpComparator () {
192+ final List <Person > people = List .of (BOB , FRED , NADINE , JANE , BOB , NADINE );
193+ final Comparator <Person > comparator = CompareUtil .noOpComparator ();
194+
195+ final List <Person > sorted = people .stream ()
196+ .sorted (comparator )
197+ .toList ();
198+
199+ assertThat (sorted )
200+ .containsExactly (BOB , FRED , NADINE , JANE , BOB , NADINE );
201+ }
202+
203+ @ Test
204+ void testNonNull () {
205+ final List <Person > people = List .of (BOB , FRED , NADINE , JANE , BOB , NADINE );
206+ final Comparator <Person > comparator = CompareUtil .nonNull (Comparator .comparing (Person ::name ));
207+
208+ final List <Person > sorted = people .stream ()
209+ .sorted (comparator )
210+ .toList ();
211+
212+ assertThat (sorted )
213+ .containsExactly (BOB , BOB , FRED , JANE , NADINE , NADINE );
214+ }
215+
216+ @ Test
217+ void testNonNull2 () {
218+ final List <Person > people = List .of (BOB , FRED , NADINE , JANE , BOB , NADINE );
219+ final Comparator <Person > comparator = CompareUtil .nonNull (null );
220+
221+ final List <Person > sorted = people .stream ()
222+ .sorted (comparator )
223+ .toList ();
224+
225+ assertThat (sorted )
226+ .containsExactly (BOB , FRED , NADINE , JANE , BOB , NADINE );
227+ }
93228
94229 // --------------------------------------------------------------------------------
95230
@@ -101,6 +236,10 @@ private static Wrapper of(final String val) {
101236 }
102237 }
103238
239+
240+ // --------------------------------------------------------------------------------
241+
242+
104243 private enum CompareResult {
105244 LESS_THAN ,
106245 EQUAL ,
@@ -117,4 +256,12 @@ private static CompareResult of(final int compareResult) {
117256 }
118257 }
119258 }
259+
260+
261+ // --------------------------------------------------------------------------------
262+
263+
264+ private record Person (String name , int age ) {
265+
266+ }
120267}
0 commit comments