27
27
import java .util .Map ;
28
28
import java .util .Properties ;
29
29
import java .util .Set ;
30
+ import java .util .SortedSet ;
31
+ import java .util .TreeSet ;
32
+ import java .util .Vector ;
30
33
31
34
import org .junit .jupiter .api .Test ;
32
35
@@ -176,6 +179,31 @@ void findFirstMatch() {
176
179
assertThat (CollectionUtils .findFirstMatch (source , candidates )).isEqualTo ("def" );
177
180
}
178
181
182
+ @ Test
183
+ void findValueOfType () {
184
+ List <Integer > integerList = new ArrayList <>();
185
+ integerList .add (1 );
186
+ assertThat (CollectionUtils .findValueOfType (integerList , Integer .class )).isEqualTo (1 );
187
+
188
+ Set <Integer > integerSet = new HashSet <>();
189
+ integerSet .add (2 );
190
+ assertThat (CollectionUtils .findValueOfType (integerSet , Integer .class )).isEqualTo (2 );
191
+ }
192
+
193
+ @ Test
194
+ void findValueOfTypeWithEmptyCollection () {
195
+ List <Integer > emptyList = new ArrayList <>();
196
+ assertThat (CollectionUtils .findValueOfType (emptyList , Integer .class )).isNull ();
197
+ }
198
+
199
+ @ Test
200
+ void findValueOfTypeWithMoreThanOneValue () {
201
+ List <Integer > integerList = new ArrayList <>();
202
+ integerList .add (1 );
203
+ integerList .add (2 );
204
+ assertThat (CollectionUtils .findValueOfType (integerList , Integer .class )).isNull ();
205
+ }
206
+
179
207
@ Test
180
208
void hasUniqueObject () {
181
209
List <String > list = new ArrayList <>();
@@ -210,6 +238,96 @@ void hasUniqueObject() {
210
238
assertThat (CollectionUtils .hasUniqueObject (list )).isFalse ();
211
239
}
212
240
241
+ @ Test
242
+ void findCommonElementType () {
243
+ List <Integer > integerList = new ArrayList <>();
244
+ integerList .add (1 );
245
+ integerList .add (2 );
246
+
247
+ assertThat (CollectionUtils .findCommonElementType (integerList )).isEqualTo (Integer .class );
248
+ }
249
+
250
+ @ Test
251
+ void findCommonElementTypeWithEmptyCollection () {
252
+ List <Integer > emptyList = new ArrayList <>();
253
+ assertThat (CollectionUtils .findCommonElementType (emptyList )).isNull ();
254
+ }
255
+
256
+ @ Test
257
+ void findCommonElementTypeWithDifferentElementType () {
258
+ List <Object > list = new ArrayList <>();
259
+ list .add (1 );
260
+ list .add ("foo" );
261
+ assertThat (CollectionUtils .findCommonElementType (list )).isNull ();
262
+ }
263
+
264
+ @ Test
265
+ void firstElementWithSet () {
266
+ Set <Integer > set = new HashSet <>();
267
+ set .add (17 );
268
+ set .add (3 );
269
+ set .add (2 );
270
+ set .add (1 );
271
+ assertThat (CollectionUtils .firstElement (set )).isEqualTo (17 );
272
+ }
273
+
274
+ @ Test
275
+ void firstElementWithSortedSet () {
276
+ SortedSet <Integer > sortedSet = new TreeSet <>();
277
+ sortedSet .add (17 );
278
+ sortedSet .add (3 );
279
+ sortedSet .add (2 );
280
+ sortedSet .add (1 );
281
+ assertThat (CollectionUtils .firstElement (sortedSet )).isEqualTo (1 );
282
+ }
283
+
284
+ @ Test
285
+ void firstElementWithList () {
286
+ List <Integer > list = new ArrayList <>();
287
+ list .add (1 );
288
+ list .add (2 );
289
+ list .add (3 );
290
+ assertThat (CollectionUtils .firstElement (list )).isEqualTo (1 );
291
+ }
292
+
293
+ @ Test
294
+ void lastElementWithSet () {
295
+ Set <Integer > set = new HashSet <>();
296
+ set .add (17 );
297
+ set .add (3 );
298
+ set .add (2 );
299
+ set .add (1 );
300
+ assertThat (CollectionUtils .lastElement (set )).isEqualTo (3 );
301
+ }
302
+
303
+ @ Test
304
+ void lastElementWithSortedSet () {
305
+ SortedSet <Integer > sortedSet = new TreeSet <>();
306
+ sortedSet .add (17 );
307
+ sortedSet .add (3 );
308
+ sortedSet .add (2 );
309
+ sortedSet .add (1 );
310
+ assertThat (CollectionUtils .lastElement (sortedSet )).isEqualTo (17 );
311
+ }
312
+
313
+ @ Test
314
+ void lastElementWithList () {
315
+ List <Integer > list = new ArrayList <>();
316
+ list .add (1 );
317
+ list .add (2 );
318
+ list .add (3 );
319
+ assertThat (CollectionUtils .lastElement (list )).isEqualTo (3 );
320
+ }
321
+
322
+ @ Test
323
+ void toArray () {
324
+ Vector <String > vector = new Vector <>();
325
+ vector .add ("foo" );
326
+ vector .add ("bar" );
327
+ Enumeration <String > enumeration = vector .elements ();
328
+ assertThat (CollectionUtils .toArray (enumeration , new String []{})).containsExactly ("foo" , "bar" );
329
+ }
330
+
213
331
@ Test
214
332
void conversionOfEmptyMap () {
215
333
MultiValueMap <String , String > asMultiValueMap = CollectionUtils .toMultiValueMap (new HashMap <>());
@@ -234,6 +352,22 @@ void changesValueByReference() {
234
352
assertThat (asMultiValueMap ).containsKey ("key" );
235
353
}
236
354
355
+ @ Test
356
+ void compositeMap () {
357
+ Map <String , String > first = new HashMap <>();
358
+ first .put ("key1" , "value1" );
359
+ first .put ("key2" , "value2" );
360
+
361
+ Map <String , String > second = new HashMap <>();
362
+ second .put ("key3" , "value3" );
363
+ second .put ("key4" , "value4" );
364
+
365
+ Map <String , String > compositeMap = CollectionUtils .compositeMap (first , second );
366
+
367
+ assertThat (compositeMap ).containsKeys ("key1" , "key2" , "key3" , "key4" );
368
+ assertThat (compositeMap ).containsValues ("value1" , "value2" , "value3" , "value4" );
369
+ }
370
+
237
371
238
372
private static final class Instance {
239
373
0 commit comments