forked from eclipse-collections/eclipse-collections-kata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTop25MethodsTest.java
More file actions
373 lines (303 loc) · 12.4 KB
/
Top25MethodsTest.java
File metadata and controls
373 lines (303 loc) · 12.4 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright (c) 2022 The Bank of New York Mellon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.topmethodskata;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.primitive.MutableCharBag;
import org.eclipse.collections.api.factory.Bags;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.Sets;
import org.eclipse.collections.api.factory.primitive.IntLists;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.ParallelListIterable;
import org.eclipse.collections.api.list.primitive.ImmutableIntList;
import org.eclipse.collections.api.map.primitive.ImmutableObjectLongMap;
import org.eclipse.collections.api.multimap.list.ImmutableListMultimap;
import org.eclipse.collections.api.partition.list.PartitionImmutableList;
import org.eclipse.collections.api.set.ImmutableSet;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.impl.factory.Multimaps;
import org.eclipse.collections.impl.utility.LazyIterate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
public class Top25MethodsTest
{
private final ImmutableList<String> fruitNames = Fruit.toLowerCaseList();
private final ImmutableSet<String> onlyBanana = Sets.immutable.with(Fruit.BANANA.toLowerCase());
@Test
@Tag("KATA")
public void with()
{
// Fix by replacing the empty list with a List of lowercase fruit names
MutableList<String> fruit = Lists.mutable.empty();
var expected = Fruit.toLowerCaseList();
Assertions.assertEquals(expected, fruit);
}
@Test
@Tag("KATA")
public void collect()
{
// Fix by collecting the names of the fruit to uppercase
ImmutableList<String> uppercase = this.fruitNames;
var expectedUppercase = Fruit.ALL.collect(Object::toString);
Assertions.assertEquals(expectedUppercase, uppercase);
}
@Test
@Tag("KATA")
public void of()
{
// Fix by replacing the empty set with a Set of just a banana
MutableSet<String> onlyBanana = Sets.mutable.empty();
var expected = Set.of(Fruit.BANANA.toLowerCase());
Assertions.assertEquals(expected, onlyBanana);
}
@Test
@Tag("KATA")
public void select()
{
// Select the fruitNames that are contained in the set of onlyBanana
ImmutableList<String> justBanana = this.fruitNames;
var expected = List.of(Fruit.BANANA.toLowerCase());
Assertions.assertEquals(expected, justBanana);
}
@Test
@Tag("KATA")
public void reject()
{
// Reject the fruitNames that are contained in the set of onlyBanana
ImmutableList<String> notBanana = this.fruitNames;
var expected = List.of("apple", "apricot", "blueberry", "clementine");
Assertions.assertEquals(expected, notBanana);
}
@Test
@Tag("KATA")
public void count()
{
// Count the fruitNames that are contained in the set of onlyBanana
int countBanana = 0;
// Count the fruitNames that are contained in the list of fruitNames
int countAll = 0;
Assertions.assertEquals(1, countBanana);
Assertions.assertEquals(5, countAll);
}
@Test
@Tag("KATA")
public void anySatisfy()
{
// Are any of the fruitNames contained in the set of onlyBanana?
boolean anyBanana = false;
// Are any of the fruitNames an empty String?
boolean anyEmpty = true;
Assertions.assertTrue(anyBanana);
Assertions.assertFalse(anyEmpty);
}
@Test
@Tag("KATA")
public void allSatisfy()
{
// Are all of the fruitNames contained in the set of onlyBanana?
boolean allBanana = true;
// Are all of the fruitNames lowercase?
boolean allLowercase = false;
Assertions.assertFalse(allBanana);
Assertions.assertTrue(allLowercase);
}
@Test
@Tag("KATA")
public void noneSatisfy()
{
// Are none of the fruitNames contained in the set of onlyBanana?
boolean noneBanana = true;
// Are none of the fruitNames empty Strings?
boolean noneEmpty = false;
Assertions.assertFalse(noneBanana);
Assertions.assertTrue(noneEmpty);
}
@Test
@Tag("KATA")
public void groupBy()
{
// Group the fruitNames by the first character in the name
ImmutableListMultimap<Character, String> multimap = Multimaps.immutable.list.empty();
Assertions.assertEquals(List.of("apple", "apricot"), multimap.get('a'));
Assertions.assertEquals(List.of("banana", "blueberry"), multimap.get('b'));
Assertions.assertEquals(List.of("clementine"), multimap.get('c'));
}
@Test
@Tag("KATA")
public void countBy()
{
// Group the fruitNames by the first character in the name
ImmutableBag<Character> firstLetterCounts = Bags.immutable.empty();
Assertions.assertEquals(2, firstLetterCounts.occurrencesOf('a'));
Assertions.assertEquals(2, firstLetterCounts.occurrencesOf('b'));
Assertions.assertEquals(1, firstLetterCounts.occurrencesOf('c'));
}
@Test
@Tag("KATA")
public void makeString()
{
// Make the fruitNames into a String that starts with "(", ends with ")", and is separated by ","
String fruitString = this.fruitNames.toString();
Assertions.assertEquals("(apple,apricot,banana,blueberry,clementine)", fruitString);
}
@Test
@Tag("KATA")
public void toImmutable()
{
MutableList<String> mutableFruit =
Lists.mutable.with("apple", "apricot", "banana", "blueberry", "clementine");
// Convert the MutableList of fruit names above into an ImmutableList
ImmutableList<String> immutableFruit = Lists.immutable.empty();
Assertions.assertEquals(mutableFruit, immutableFruit);
}
@Test
@Tag("KATA")
public void asLazy()
{
// Convert this.fruitNames into a LazyIterable
LazyIterable<String> lazyFruit = LazyIterate.adapt(List.of());
Assertions.assertEquals(5, lazyFruit.size());
// Note: LazyIterable does not get exhausted, so you can keep using it.
Assertions.assertEquals(5, lazyFruit.size());
// Is an ImmutableList equal to a LazyIterable? If not, how can you convert the LazyIterable to a List?
Assertions.assertEquals(Fruit.ALL.collect(Object::toString), lazyFruit.collect(String::toUpperCase));
}
@Test
@Tag("KATA")
public void containsBy()
{
// Does this.fruitNames contain APPLE if you compare each element By uppercase?
boolean hasApple = this.fruitNames.contains("APPLE");
// Does this.fruitNames contain TOMATO if you compare each element By uppercase?
boolean hasTomato = this.fruitNames.contains("TOMATO");
Assertions.assertTrue(hasApple);
Assertions.assertFalse(hasTomato);
}
@Test
@Tag("KATA")
public void detectWith()
{
// Do any of the this.fruitNames start with the letter b? Which one is first?
String banana = this.fruitNames.selectWith(String::startsWith, "").getFirst();
// Do any of the this.fruitNames start with the letter d?
String none = this.fruitNames.selectWith(String::startsWith, "").getFirst();
Assertions.assertEquals("banana", banana);
Assertions.assertNull(none);
}
@Test
@Tag("KATA")
public void detectWithIfNone()
{
// Do any of the this.fruitNames start with the letter b? Which one is first? Return "apple" if none match.
String banana = "";
// Do any of the this.fruitNames start with the letter d? Which one is first? Return "banana" if none match.
String stillBanana = "";
Assertions.assertEquals("banana", banana);
Assertions.assertEquals("banana", stillBanana);
}
@Test
@Tag("KATA")
public void injectInto()
{
// Use injectInto with a String builder to append all of the fruitNames together in a String.
StringBuilder stringBuilder = null;
String mixedFruitString = stringBuilder.toString();
Assertions.assertEquals("appleapricotbananablueberryclementine", mixedFruitString);
}
@Test
@Tag("KATA")
public void partition()
{
// Partition the fruitNames based on the length of a name greater than 6
PartitionImmutableList<String> partitionFruit = null;
ImmutableList<String> selected = partitionFruit.getSelected();
ImmutableList<String> rejected = partitionFruit.getRejected();
var expectedSelected = Lists.mutable.with("apricot", "blueberry", "clementine");
var expectedRejected = Lists.mutable.with("apple", "banana");
Assertions.assertEquals(expectedSelected, selected);
Assertions.assertEquals(expectedRejected, rejected);
}
@Test
@Tag("KATA")
public void chunk()
{
// Chunk the fruitNames in batches of size 2
RichIterable<RichIterable<String>> chunkFruit = null;
Assertions.assertEquals(3, chunkFruit.size());
var expectedFirst = Lists.mutable.with("apple", "apricot");
var expectedLast = Lists.mutable.with("clementine");
Assertions.assertEquals(expectedFirst, chunkFruit.getFirst());
Assertions.assertEquals(expectedLast, chunkFruit.getLast());
}
@Test
@Tag("KATA")
public void sumByInt()
{
// Sum the length of the fruitNames by the first character of each item
ImmutableObjectLongMap<Character> sumLengthsByFirstCharacter = null;
Assertions.assertEquals(12, sumLengthsByFirstCharacter.get('a'));
Assertions.assertEquals(15, sumLengthsByFirstCharacter.get('b'));
Assertions.assertEquals(10, sumLengthsByFirstCharacter.get('c'));
}
@Test
@Tag("KATA")
public void collectInt()
{
// Collect the lengths of the fruitNames as int values
ImmutableIntList lengths = null;
var expected = IntLists.immutable.with(5, 7, 6, 9, 10);
Assertions.assertEquals(expected, lengths);
}
@Test
@Tag("KATA")
public void flatCollectChar()
{
// Flat collect all of the char values in the fruitNames into a mutable CharBag
MutableCharBag charCounts = null;
Assertions.assertEquals(5, charCounts.occurrencesOf('a'));
Assertions.assertEquals(3, charCounts.occurrencesOf('b'));
Assertions.assertEquals(2, charCounts.occurrencesOf('c'));
}
@Test
@Tag("KATA")
public void asParallel()
{
ExecutorService executorService = Executors.newFixedThreadPool(4);
// Convert the fruitNames into a ParallelListIterable using the executorService and a batch size of 1
ParallelListIterable<String> parallelFruit = null;
ParallelListIterable<String> parallelSelect =
parallelFruit.select(this.onlyBanana::contains);
MutableList<String> onlyBananaList = parallelSelect.toList();
MutableSet<String> onlyBananaSet = parallelSelect.toSet();
var expectedList = Lists.mutable.with("banana");
var expectedSet = Sets.mutable.with("banana");
Assertions.assertEquals(expectedList, onlyBananaList);
Assertions.assertEquals(expectedSet, onlyBananaSet);
}
@Test
@Tag("KATA")
public void distinct()
{
MutableList<String> duplicateFruit =
Lists.mutable.with("apple", "apple", "banana", "banana");
// Find the distinct list of fruitNames from duplicateFruit
MutableList<String> distinctFruit = duplicateFruit;
var expected = Lists.mutable.with("apple", "banana");
Assertions.assertEquals(expected, distinctFruit);
}
}