Skip to content

Commit d7eaa00

Browse files
pauljervispivovarit
authored andcommitted
Bael 1601 kotlin (eugenp#4016)
* add article files * update kotlin version in pom * IntelliJ added binary folder to all the .gitignores * IntelliJ added binary folder to all the .gitignores * update test function names * Expand tests and add more code for more meaningful content within the article. Remove core-kotlin from parent pom again for eclipse to work. * adjust for issues with custom-pmd * rename test * Revert "IntelliJ added binary folder to all the .gitignores" This reverts commit 0850a2a. * Revert "IntelliJ added binary folder to all the .gitignores" This reverts commit 2b17cf2.
1 parent c80befe commit d7eaa00

9 files changed

Lines changed: 298 additions & 11 deletions

File tree

‎core-kotlin/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

‎core-kotlin/pom.xml‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
</repositories>
2222

2323
<dependencies>
24+
<dependency>
25+
<groupId>org.apache.commons</groupId>
26+
<artifactId>commons-math3</artifactId>
27+
<version>${commons-math3.version}</version>
28+
</dependency>
2429
<dependency>
2530
<groupId>org.junit.jupiter</groupId>
2631
<artifactId>junit-jupiter-engine</artifactId>
@@ -33,20 +38,14 @@
3338
<version>${junit.platform.version}</version>
3439
<scope>test</scope>
3540
</dependency>
36-
<dependency>
37-
<groupId>junit</groupId>
38-
<artifactId>junit</artifactId>
39-
<version>${junit4.version}</version>
40-
<scope>test</scope>
41-
</dependency>
4241
<dependency>
4342
<groupId>org.jetbrains.kotlin</groupId>
4443
<artifactId>kotlin-stdlib</artifactId>
4544
<version>${kotlin-stdlib.version}</version>
4645
</dependency>
4746
<dependency>
4847
<groupId>org.jetbrains.kotlin</groupId>
49-
<artifactId>kotlin-stdlib-jre8</artifactId>
48+
<artifactId>kotlin-stdlib-jdk8</artifactId>
5049
<version>${kotlin-stdlib.version}</version>
5150
</dependency>
5251
<dependency>
@@ -194,14 +193,16 @@
194193

195194
<properties>
196195
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
197-
<kotlin-maven-plugin.version>1.1.2</kotlin-maven-plugin.version>
198-
<kotlin-test-junit.version>1.1.2</kotlin-test-junit.version>
199-
<kotlin-stdlib.version>1.1.2</kotlin-stdlib.version>
200-
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
196+
<kotlin-maven-plugin.version>1.2.31</kotlin-maven-plugin.version>
197+
<kotlin-test-junit.version>1.2.31</kotlin-test-junit.version>
198+
<kotlin-stdlib.version>1.2.31</kotlin-stdlib.version>
199+
<kotlin-reflect.version>1.2.31</kotlin-reflect.version>
201200
<kotlinx.version>0.15</kotlinx.version>
202201
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
203202
<kodein.version>4.1.0</kodein.version>
204203

204+
<commons-math3.version>3.6.1</commons-math3.version>
205+
205206
<junit.jupiter.version>5.0.0</junit.jupiter.version>
206207
<junit.platform.version>1.0.0</junit.platform.version>
207208
<junit.vintage.version>4.12.0</junit.vintage.version>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.filter
2+
3+
import org.junit.jupiter.api.Assertions.assertIterableEquals
4+
import org.junit.jupiter.api.Test
5+
6+
internal class ChunkedTest {
7+
8+
@Test
9+
fun givenDNAFragmentString_whenChunking_thenProduceListOfChunks() {
10+
val dnaFragment = "ATTCGCGGCCGCCAA"
11+
12+
val fragments = dnaFragment.chunked(3)
13+
14+
assertIterableEquals(listOf("ATT", "CGC", "GGC", "CGC", "CAA"), fragments)
15+
}
16+
17+
@Test
18+
fun givenDNAString_whenChunkingWithTransformer_thenProduceTransformedList() {
19+
val codonTable = mapOf("ATT" to "Isoleucine", "CAA" to "Glutamine", "CGC" to "Arginine", "GGC" to "Glycine")
20+
val dnaFragment = "ATTCGCGGCCGCCAA"
21+
22+
val proteins = dnaFragment.chunked(3) { codon ->
23+
codonTable[codon.toString()] ?: error("Unknown codon")
24+
}
25+
26+
assertIterableEquals(listOf("Isoleucine", "Arginine", "Glycine", "Arginine", "Glutamine"), proteins)
27+
}
28+
29+
@Test
30+
fun givenListOfValues_whenChunking_thenProduceListOfArrays() {
31+
val whole = listOf(1, 4, 7, 4753, 2, 34, 62, 76, 5868, 0)
32+
val chunks = whole.chunked(6)
33+
34+
val expected = listOf(listOf(1, 4, 7, 4753, 2, 34), listOf(62, 76, 5868, 0))
35+
36+
assertIterableEquals(expected, chunks)
37+
}
38+
39+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.filter
2+
3+
import org.junit.jupiter.api.Assertions.assertIterableEquals
4+
import org.junit.jupiter.api.Test
5+
6+
internal class DistinctTest {
7+
data class SmallClass(val key: String, val num: Int)
8+
9+
@Test
10+
fun givenArrayOfSomeDuplicateValues_whenApplyingDistinct_thenReturnListOfNoDuplicateValues() {
11+
val array = arrayOf(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9)
12+
val result = array.distinct()
13+
val expected = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
14+
15+
assertIterableEquals(expected, result)
16+
}
17+
18+
@Test
19+
fun givenArrayOfClassObjects_whenApplyingDistinctOnClassProperty_thenReturnListDistinctOnThatValue() {
20+
21+
val original = arrayOf(
22+
SmallClass("key1", 1),
23+
SmallClass("key2", 2),
24+
SmallClass("key3", 3),
25+
SmallClass("key4", 3),
26+
SmallClass("er", 9),
27+
SmallClass("er", 10),
28+
SmallClass("er", 11))
29+
30+
val actual = original.distinctBy { it.key }
31+
32+
val expected = listOf(
33+
SmallClass("key1", 1),
34+
SmallClass("key2", 2),
35+
SmallClass("key3", 3),
36+
SmallClass("key4", 3),
37+
SmallClass("er", 9))
38+
39+
40+
assertIterableEquals(expected, actual)
41+
}
42+
43+
@Test
44+
fun givenArrayOfClassObjects_whenApplyingComplicatedSelector_thenReturnFirstElementToMatchEachSelectorValue() {
45+
val array = arrayOf(
46+
SmallClass("key1", 1),
47+
SmallClass("key2", 2),
48+
SmallClass("key3", 3),
49+
SmallClass("key4", 3),
50+
SmallClass("er", 9),
51+
SmallClass("er", 10),
52+
SmallClass("er", 11),
53+
SmallClass("er", 11),
54+
SmallClass("er", 91),
55+
SmallClass("blob", 22),
56+
SmallClass("dob", 27),
57+
SmallClass("high", 201_434_314))
58+
59+
val actual = array.distinctBy { Math.floor(it.num / 10.0) }
60+
61+
val expected = listOf(
62+
SmallClass("key1", 1),
63+
SmallClass("er", 10),
64+
SmallClass("er", 91),
65+
SmallClass("blob", 22),
66+
SmallClass("high", 201_434_314))
67+
68+
assertIterableEquals(expected, actual)
69+
}
70+
71+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.filter
2+
3+
import org.junit.jupiter.api.Assertions.assertIterableEquals
4+
import org.junit.jupiter.api.Test
5+
6+
internal class DropTest {
7+
8+
@Test
9+
fun whenDroppingFirstTwoItemsOfArray_thenTwoLess() {
10+
val array = arrayOf(1, 2, 3, 4)
11+
val result = array.drop(2)
12+
val expected = listOf(3, 4)
13+
14+
assertIterableEquals(expected, result)
15+
}
16+
17+
@Test
18+
fun whenDroppingMoreItemsOfArray_thenEmptyList() {
19+
val array = arrayOf(1, 2, 3, 4)
20+
val result = array.drop(5)
21+
val expected = listOf<Int>()
22+
23+
assertIterableEquals(expected, result)
24+
}
25+
26+
@Test
27+
fun givenArray_whenDroppingLastElement_thenReturnListWithoutLastElement() {
28+
val array = arrayOf("1", "2", "3", "4")
29+
val result = array.dropLast(1)
30+
val expected = listOf("1", "2", "3")
31+
32+
assertIterableEquals(expected, result)
33+
}
34+
35+
@Test
36+
fun givenArrayOfFloats_whenDroppingLastUntilPredicateIsFalse_thenReturnSubsetListOfFloats() {
37+
val array = arrayOf(1f, 1f, 1f, 1f, 1f, 2f, 1f, 1f, 1f)
38+
val result = array.dropLastWhile { it == 1f }
39+
val expected = listOf(1f, 1f, 1f, 1f, 1f, 2f)
40+
41+
assertIterableEquals(expected, result)
42+
}
43+
44+
@Test
45+
fun givenList_whenDroppingMoreThanAvailable_thenThrowException() {
46+
val list = listOf('a', 'e', 'i', 'o', 'u')
47+
val result = list.drop(6)
48+
val expected: List<String> = listOf()
49+
50+
assertIterableEquals(expected, result)
51+
}
52+
53+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.filter
2+
3+
import org.apache.commons.math3.primes.Primes
4+
import org.junit.jupiter.api.Assertions.assertIterableEquals
5+
import org.junit.jupiter.api.Test
6+
import kotlin.test.assertTrue
7+
8+
internal class FilterTest {
9+
10+
@Test
11+
fun givenAscendingValueMap_whenFilteringOnValue_ThenReturnSubsetOfMap() {
12+
val originalMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3)
13+
val filteredMap = originalMap.filter { it.value < 2 }
14+
val expectedMap = mapOf("key1" to 1)
15+
16+
assertTrue { expectedMap == filteredMap }
17+
}
18+
19+
@Test
20+
fun givenSeveralCollections_whenFilteringToAccumulativeList_thenListContainsAllContents() {
21+
val array1 = arrayOf(90, 92, 93, 94, 92, 95, 93)
22+
val array2 = sequenceOf(51, 31, 83, 674_506_111, 256_203_161, 15_485_863)
23+
val list1 = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
24+
val primes = mutableListOf<Int>()
25+
26+
val expected = listOf(2, 3, 5, 7, 31, 83, 15_485_863, 256_203_161, 674_506_111)
27+
28+
val primeCheck = { num: Int -> Primes.isPrime(num) }
29+
30+
array1.filterTo(primes, primeCheck)
31+
list1.filterTo(primes, primeCheck)
32+
array2.filterTo(primes, primeCheck)
33+
34+
primes.sort()
35+
36+
assertIterableEquals(expected, primes)
37+
}
38+
39+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.filter
2+
3+
import org.junit.jupiter.api.Assertions.assertIterableEquals
4+
import org.junit.jupiter.api.Assertions.assertThrows
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SliceTest {
8+
9+
@Test
10+
fun whenSlicingAnArrayWithDotRange_ThenListEqualsTheSlice() {
11+
val original = arrayOf(1, 2, 3, 2, 1)
12+
val actual = original.slice(1..3)
13+
val expected = listOf(2, 3, 2)
14+
15+
assertIterableEquals(expected, actual)
16+
}
17+
18+
@Test
19+
fun whenSlicingAnArrayWithDownToRange_thenListMadeUpOfReverseSlice() {
20+
val original = arrayOf(1, 2, 3, 2, 1)
21+
val actual = original.slice(3 downTo 0)
22+
val expected = listOf(2, 3, 2, 1)
23+
24+
assertIterableEquals(expected, actual)
25+
}
26+
27+
@Test
28+
fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() {
29+
val original = arrayOf(12, 3, 34, 4)
30+
val actual = original.slice(3..8)
31+
val expected = listOf(4, null, null, null, null, null)
32+
33+
assertIterableEquals(expected, actual)
34+
}
35+
36+
@Test
37+
fun whenSlicingBeyondRangeOfArrayWithStep_thenOutOfBoundsException() {
38+
assertThrows(ArrayIndexOutOfBoundsException::class.java) {
39+
val original = arrayOf(12, 3, 34, 4)
40+
original.slice(3..8 step 2)
41+
}
42+
}
43+
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.filter
2+
3+
import org.junit.jupiter.api.Assertions.assertIterableEquals
4+
import org.junit.jupiter.api.Test
5+
6+
internal class TakeTest {
7+
8+
@Test
9+
fun `given array of alternating types, when predicating on 'is String', then produce list of array up until predicate is false`() {
10+
val originalArray = arrayOf("val1", 2, "val3", 4, "val5", 6)
11+
val actualList = originalArray.takeWhile { it is String }
12+
val expectedList = listOf("val1")
13+
14+
assertIterableEquals(expectedList, actualList)
15+
}
16+
17+
@Test
18+
fun `given array of alternating types, when taking 4 items, then produce list of first 4 items`() {
19+
val originalArray = arrayOf("val1", 2, "val3", 4, "val5", 6)
20+
val actualList = originalArray.take(4)
21+
val expectedList = listOf("val1", 2, "val3", 4)
22+
23+
println(originalArray.drop(4))
24+
println(actualList)
25+
26+
assertIterableEquals(expectedList, actualList)
27+
}
28+
29+
@Test
30+
fun `when taking more items than available, then return all elements`() {
31+
val originalArray = arrayOf(1, 2)
32+
val actual = originalArray.take(10)
33+
val expected = listOf(1, 2)
34+
35+
assertIterableEquals(expected, actual)
36+
}
37+
38+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<module>core-java</module>
6565
<module>core-java-io</module>
6666
<module>core-java-8</module>
67+
<!--<module>core-kotlin</module>-->
6768
<module>core-groovy</module>
6869
<module>core-java-concurrency</module>
6970

0 commit comments

Comments
 (0)