Skip to content

Commit a6e5082

Browse files
authored
Merge branch 'master' into master
2 parents c508a36 + d7752e7 commit a6e5082

File tree

22 files changed

+791
-1
lines changed

22 files changed

+791
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.underscoreoperator
2+
// Before Kotlin 1.7
3+
class Box<T>(value: T) {
4+
// Some class implementation
5+
}
6+
fun main() {
7+
val intBox: Box<Int> = Box(42)
8+
val anyBox: Box<Any> = Box("Some value")
9+
}
10+
fun main1(args: Array<String>) {
11+
val (_, second) = "ignored" to "hello"
12+
println(second)
13+
}
14+
fun mainTwo(args: Array<String>) {
15+
val list = listOf("hi")
16+
val list2 = list.mapIndexed { _, item -> item }
17+
println(list2)
18+
}
19+
inline fun <T> printElementInfo(element: T) {
20+
println("Element: $element")
21+
}
22+
fun mainForHOF() {
23+
printElementInfo<_>(42)
24+
printElementInfo<_>("Hello")
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package iterateListAndAddItem
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
6+
7+
fun byForEach(list: List<String>): List<String> {
8+
val result = mutableListOf<String>()
9+
list.forEach {
10+
result += it
11+
if (it.length > 1) {
12+
result += "<- a long one"
13+
}
14+
}
15+
return result.toList()
16+
}
17+
18+
fun byBuildList(list: List<String>) =
19+
buildList {
20+
list.forEach {
21+
this += it
22+
if (it.length > 1) {
23+
this += "<- a long one"
24+
}
25+
}
26+
}
27+
28+
fun addBeforeByBuildList(list: List<String>) =
29+
buildList {
30+
list.forEach {
31+
if (it.length > 1) {
32+
this += "a long one ->"
33+
}
34+
this += it
35+
}
36+
}
37+
38+
fun byListIterator(list: MutableList<String>) {
39+
val it = list.listIterator()
40+
for (e in it) {
41+
if (e.length > 1) {
42+
it.add("<- a long one")
43+
}
44+
}
45+
}
46+
47+
fun addBeforeByListIterator(list: MutableList<String>) {
48+
val it = list.listIterator()
49+
for (e in it) {
50+
if (e.length > 1) {
51+
it.previous()
52+
it.add("a long one ->")
53+
it.next()
54+
}
55+
}
56+
}
57+
58+
class IterateListAndAddElementUnitTest {
59+
val myList = listOf("ab", "a", "cd", "c", "xyz")
60+
61+
@Test
62+
fun `when using byLoop() then get expected result`() {
63+
assertEquals(
64+
listOf("ab", "<- a long one", "a", "cd", "<- a long one", "c", "xyz", "<- a long one"),
65+
byForEach(myList)
66+
)
67+
}
68+
69+
@Test
70+
fun `when using buildList() then get expected result`() {
71+
assertEquals(
72+
listOf("ab", "<- a long one", "a", "cd", "<- a long one", "c", "xyz", "<- a long one"),
73+
byBuildList(myList)
74+
)
75+
//insert before the target
76+
assertEquals(
77+
listOf("a long one ->", "ab", "a", "a long one ->", "cd", "c", "a long one ->", "xyz"),
78+
addBeforeByBuildList(myList)
79+
)
80+
}
81+
82+
@Test
83+
fun `given mutableList, when using listIterator to append elements then get expected result`() {
84+
val myMutableList = mutableListOf("ab", "a", "cd", "c", "xyz")
85+
byListIterator(myMutableList)
86+
assertEquals(
87+
listOf("ab", "<- a long one", "a", "cd", "<- a long one", "c", "xyz", "<- a long one"),
88+
myMutableList
89+
)
90+
}
91+
92+
@Test
93+
fun `given mutableList, when using listIterator to prepend elements then get expected result`() {
94+
val myMutableList = mutableListOf("ab", "a", "cd", "c", "xyz")
95+
addBeforeByListIterator(myMutableList)
96+
assertEquals(
97+
listOf("a long one ->", "ab", "a", "a long one ->", "cd", "c", "a long one ->", "xyz"),
98+
myMutableList
99+
)
100+
}
101+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package listOfLists
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
import kotlin.test.assertTrue
6+
7+
class ListOfListsUnitTest {
8+
9+
@Test
10+
fun `Creates an immutable list of immutable lists using listOf()`() {
11+
val listOfLists = listOf(listOf(1, 2), listOf(3, 4), listOf(5, 6))
12+
13+
assertEquals(3, listOfLists.size)
14+
assertEquals(listOf(1, 2), listOfLists[0])
15+
assertEquals(listOf(3, 4), listOfLists[1])
16+
assertEquals(listOf(5, 6), listOfLists[2])
17+
}
18+
19+
@Test
20+
fun `Creates an immutable list of mutable lists using List constructor`() {
21+
val listOfLists = List(3) { MutableList<Int>(2) {0} }
22+
23+
assertEquals(3, listOfLists.size)
24+
assertEquals(listOf(0, 0), listOfLists[0])
25+
assertEquals(listOf(0, 0), listOfLists[1])
26+
assertEquals(listOf(0, 0), listOfLists[2])
27+
}
28+
29+
@Test
30+
fun `Creates an immutable list of immutable lists using map method`() {
31+
val listOfLists = (0..2).map { _ -> (0..1).map { 0 } }
32+
33+
assertEquals(3, listOfLists.size)
34+
assertEquals(listOf(0, 0), listOfLists[0])
35+
assertEquals(listOf(0, 0), listOfLists[1])
36+
assertEquals(listOf(0, 0), listOfLists[2])
37+
}
38+
39+
@Test
40+
fun `Creates an immutable list of mutable lists using map method`() {
41+
val listOfMutableLists = (0..2).map { mutableListOf<Int>() }
42+
43+
assertEquals(3, listOfMutableLists.size)
44+
assertTrue(listOfMutableLists.all { it.isEmpty() })
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package removeNullAndEmptyValues
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class RemoveNullAndEmptyValuesUnitTest {
7+
8+
@Test
9+
fun `remove null and empty values from list via list iteration`() {
10+
val listWithNullsAndEmpty: MutableList<String?> = mutableListOf("A", null, "", "C", null, "E")
11+
val listWithoutNullsAndEmpty = removeValuesViaIteration(listWithNullsAndEmpty)
12+
assertEquals(listOf("A", "C", "E"), listWithoutNullsAndEmpty)
13+
}
14+
15+
@Test
16+
fun `remove null and empty values from list using filterNotNull and filterNot methods`() {
17+
val listWithNullsAndEmpty: List<String?> = listOf("A", null, "", "C", null, "E")
18+
val listWithoutNulls: List<String> = listWithNullsAndEmpty.filterNotNull()
19+
20+
val listWithoutNullsAndEmpty: List<String> = listWithoutNulls.filterNot { it.isEmpty() }
21+
22+
assertEquals(listOf("A", "", "C", "E"), listWithoutNulls)
23+
assertEquals(listOf("A", "C", "E"), listWithoutNullsAndEmpty)
24+
}
25+
26+
@Test
27+
fun `remove null and empty values from list using removeIf method`() {
28+
val listWithNullsAndEmpty: MutableList<String?> = mutableListOf("A", null, "", "C", null, "E")
29+
listWithNullsAndEmpty.removeIf { it == null || it.isEmpty() }
30+
31+
assertEquals(listOf("A", "C", "E"), listWithNullsAndEmpty)
32+
}
33+
34+
fun removeValuesViaIteration(listWithNullsAndEmpty: MutableList<String?>): List<String> {
35+
val iterator = listWithNullsAndEmpty.iterator()
36+
while (iterator.hasNext()) {
37+
val element = iterator.next()
38+
if (element == null || element.isEmpty()) {
39+
iterator.remove()
40+
}
41+
}
42+
return listWithNullsAndEmpty as List<String>
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.extensionOfEnum
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.enums.EnumEntries
5+
import kotlin.test.assertEquals
6+
7+
enum class Level {
8+
A, B, C, D, E
9+
}
10+
11+
enum class WorkingDay(val n: Int) {
12+
MON(1), TUE(2), WED(3), THU(4), FRI(5);
13+
}
14+
15+
16+
class EnumExtensionFunctionsUnitTest {
17+
18+
@Test
19+
fun `when extending enum Array, then get expected result`() {
20+
fun <E : Enum<E>> Array<E>.joinTheirNames(): String {
21+
return joinToString { it.name }
22+
}
23+
24+
assertEquals("A, B, C, D, E", Level.values().joinTheirNames())
25+
assertEquals("MON, TUE, WED, THU, FRI", WorkingDay.values().joinTheirNames())
26+
}
27+
28+
@Test
29+
fun `when extending EnumEntries, then get expected result`() {
30+
fun EnumEntries<*>.joinTheirNames(): String {
31+
return joinToString { it.name }
32+
}
33+
34+
assertEquals("A, B, C, D, E", Level.entries.joinTheirNames())
35+
assertEquals("MON, TUE, WED, THU, FRI", WorkingDay.entries.joinTheirNames())
36+
}
37+
38+
39+
inline fun <reified E : Enum<E>> Enum.Companion.joinTheirNames(): String {
40+
return enumValues<E>().joinToString { it.name }
41+
}
42+
43+
@Test
44+
fun `when extending Enum's Companion object, then get expected result`() {
45+
assertEquals("A, B, C, D, E", Enum.joinTheirNames<Level>())
46+
assertEquals("MON, TUE, WED, THU, FRI", Enum.joinTheirNames<WorkingDay>())
47+
}
48+
49+
inline fun <reified E : Enum<E>> joinEnumNames(): String {
50+
return enumValues<E>().joinToString { it.name }
51+
}
52+
53+
@Test
54+
fun `when using a generic function, then get expected result`() {
55+
assertEquals("A, B, C, D, E", joinEnumNames<Level>())
56+
assertEquals("MON, TUE, WED, THU, FRI", joinEnumNames<WorkingDay>())
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.sortalphabetically
2+
3+
import org.junit.Test
4+
import kotlin.test.assertEquals
5+
6+
class SortStringAlphabeticallyUnitTest {
7+
8+
private fun sortStringWithCharArrayAndSorted(input : String) : String{
9+
return input.toCharArray().sorted().joinToString("")
10+
}
11+
12+
private fun sortStringWithCharArrayAnddistinct(input : String) : String {
13+
return input.toCharArray().sorted().distinct().joinToString("")
14+
}
15+
16+
private fun sortStringWithtoCharArrayAndCompareBy(input: String) : String {
17+
val vowels = setOf('a', 'e', 'i', 'o', 'u')
18+
return input.toCharArray().sortedWith(compareBy<Char> { it in vowels }
19+
.thenBy { it }
20+
).joinToString("")
21+
}
22+
23+
@Test
24+
fun `using toCharArray and then sorted`() {
25+
val inputString = "laibkcedfgjh"
26+
assertEquals("abcdefghijkl", sortStringWithCharArrayAndSorted(inputString))
27+
}
28+
29+
@Test
30+
fun `using sorted and distinct`() {
31+
val inputString = "lakibkcekdghfgjhh"
32+
assertEquals("abcdefghijkl", sortStringWithCharArrayAnddistinct(inputString))
33+
}
34+
35+
@Test
36+
fun `using compareBy`() {
37+
val inputString = "laibkcedfgjh"
38+
assertEquals("bcdfghjklaei", sortStringWithtoCharArrayAndCompareBy(inputString))
39+
}
40+
41+
// If we use extension function
42+
private fun String.sortStringWithCompareBy() : String {
43+
val vowels = setOf('a', 'e', 'i', 'o', 'u')
44+
return toCharArray().sortedWith(compareBy<Char> { it in vowels }
45+
.thenBy { it }
46+
).joinToString("")
47+
}
48+
49+
private fun String.sortAsc() = toCharArray().sorted().joinToString("")
50+
51+
@Test
52+
fun `simplify compareBy with extension`() {
53+
val inputString = "laibkcedfgjh"
54+
assertEquals("bcdfghjklaei", inputString.sortStringWithCompareBy())
55+
}
56+
57+
@Test
58+
fun `simplify toCharArray and sorted with extension`() {
59+
assertEquals("abcde", "cdbae".sortAsc())
60+
}
61+
}

kotlin-ktor/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ This module contains articles about Kotlin Libraries.
44

55
### Relevant articles:
66
- [An Introduction to Running GraphQL in Kotlin and Ktor](https://www.baeldung.com/kotlin/graphql-ktor)
7+
- [Creating a Web Application With Ktor and Thymeleaf](https://www.baeldung.com/kotlin/ktor-thymeleaf-web-application)
78
- [A Look at the Ktor Client](https://www.baeldung.com/kotlin/ktor-client)

kotlin-ktor/build.gradle.kts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ description = "Example usage of Gradle plugin to generate GraphQL Kotlin Client"
44

55
val graphQLKotlinVersion = "7.0.1"
66
val ktorVersion = "2.3.5"
7+
val logbackVersion = "1.4.14"
8+
val kotlinTestUnit = "1.9.10"
9+
val seleniumVersion = "4.16.1"
710

811
repositories {
912
mavenCentral()
@@ -18,16 +21,21 @@ dependencies {
1821
implementation("io.ktor", "ktor-server-core", ktorVersion)
1922
implementation("io.ktor", "ktor-server-netty", ktorVersion)
2023
implementation("io.ktor", "ktor-server-websockets", ktorVersion)
24+
implementation("io.ktor","ktor-server-status-pages", ktorVersion)
25+
implementation("io.ktor", "ktor-server-thymeleaf-jvm", ktorVersion)
2126
implementation("io.ktor", "ktor-client-auth", ktorVersion)
2227
implementation("io.ktor", "ktor-client-websockets", ktorVersion)
2328
implementation("io.ktor", "ktor-serialization-jackson", ktorVersion)
2429
implementation("io.ktor", "ktor-client-content-negotiation", ktorVersion)
2530

31+
implementation("ch.qos.logback","logback-classic", logbackVersion)
2632

2733
testImplementation("io.ktor", "ktor-client-mock", ktorVersion)
2834
testImplementation("io.ktor", "ktor-server-tests", ktorVersion)
2935
testImplementation("io.ktor", "ktor-serialization-kotlinx-json", ktorVersion)
30-
testImplementation("org.jetbrains.kotlin", "kotlin-test-junit", "1.9.10")
36+
testImplementation("org.jetbrains.kotlin", "kotlin-test-junit", kotlinTestUnit)
37+
testImplementation("org.seleniumhq.selenium", "selenium-java", seleniumVersion)
38+
3139
}
3240

3341
plugins {

0 commit comments

Comments
 (0)