Skip to content

Commit 5ed9458

Browse files
committed
resolve conflicts
2 parents 8df07bc + 47b9795 commit 5ed9458

File tree

33 files changed

+1306
-4
lines changed

33 files changed

+1306
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.addListToSet
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class AddListContentToSetUnitTest {
7+
8+
@Test
9+
fun `test adding list contents to set programmatically`() {
10+
val list = listOf("apple", "banana", "orange")
11+
val set = addListToSet(list)
12+
assertEquals(setOf("apple", "banana", "orange"), set)
13+
}
14+
15+
@Test
16+
fun `test adding list contents to set using addAll() method`() {
17+
val list = listOf("apple", "banana", "orange")
18+
val set = mutableSetOf<String>()
19+
set.addAll(list)
20+
assertEquals(setOf("apple", "banana", "orange"), set)
21+
}
22+
23+
@Test
24+
fun `test adding list contents to set using toSet() method`() {
25+
val list = listOf("apple", "banana", "orange")
26+
val set = list.toSet()
27+
assertEquals(setOf("apple", "banana", "orange"), set)
28+
}
29+
30+
@Test
31+
fun `test adding list contents to set using plus operator`() {
32+
val set = setOf<String>()
33+
val list = listOf("apple", "banana", "orange")
34+
val newSet = set + list
35+
assertEquals(setOf("apple", "banana", "orange"), newSet)
36+
}
37+
38+
@Test
39+
fun `test adding list contents to set using union method`() {
40+
val set = setOf<String>()
41+
val list = listOf("apple", "banana", "orange")
42+
val newSet = set.union(list)
43+
assertEquals(setOf("apple", "banana", "orange"), newSet)
44+
}
45+
46+
fun addListToSet(list: List<String>): Set<String> {
47+
val set = mutableSetOf<String>()
48+
for (element in list) {
49+
set.add(element)
50+
}
51+
return set
52+
}
53+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Core Kotlin Concurrency
2+
3+
This module contains articles about concurrency in Kotlin.
4+
5+
### Relevant articles:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>core-kotlin-modules</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>core-kotlin-concurrency-3</artifactId>
11+
<packaging>jar</packaging>
12+
13+
<name>core-kotlin-concurrency-3</name>
14+
<url>http://maven.apache.org</url>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>3.8.1</version>
25+
<scope>test</scope>
26+
</dependency>
27+
</dependencies>
28+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.comparedelays
2+
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.runBlocking
5+
import kotlinx.coroutines.*
6+
7+
val lock = Object()
8+
9+
fun main() {
10+
runBlocking(Dispatchers.Default) {
11+
launch(Dispatchers.IO) {
12+
testWaitThread1()
13+
}
14+
launch(Dispatchers.IO) {
15+
testWaitThread2()
16+
}
17+
}
18+
}
19+
20+
fun testWaitThread1() = synchronized(lock) {
21+
lock.wait()
22+
println("Print first")
23+
}
24+
25+
fun testWaitThread2() = synchronized(lock) {
26+
println("Print second")
27+
lock.notify()
28+
}
29+
fun sleepMethod() {
30+
println("Thread 1 is sleeping...")
31+
Thread.sleep(2000) // Sleep for 2 seconds
32+
println("Thread 1 woke up!")
33+
}
34+
fun delayMethod() = runBlocking {
35+
println("Coroutine 1 is delaying...")
36+
delay(2000) // Delay for 2 seconds
37+
println("Coroutine 1 resumed!")
38+
}

core-kotlin-modules/core-kotlin-date-time/src/test/kotlin/com/baeldung/currentdatetime/CurrentDateTImeUnitTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CurrentDateTImeUnitTest {
5353

5454
val current = LocalDateTime.of(
5555
calendar.get(Calendar.YEAR),
56-
calendar.get(Calendar.MONTH),
56+
calendar.get(Calendar.MONTH) + 1,
5757
calendar.get(Calendar.DAY_OF_MONTH),
5858
calendar.get(Calendar.HOUR_OF_DAY),
5959
calendar.get(Calendar.MINUTE),
@@ -72,4 +72,4 @@ class CurrentDateTImeUnitTest {
7272

7373
Assertions.assertThat(current).isNotNull()
7474
}
75-
}
75+
}

core-kotlin-modules/core-kotlin-lang-4/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ This module contains articles about core features in the Kotlin language.
77
- [Difference Between !! and ? in Kotlin](https://www.baeldung.com/kotlin/nullability-operators-difference)
88
- [Using Kotlin when() Clause for <, <=, >=, and == Comparisons](https://www.baeldung.com/kotlin/when-clause-comparisons)
99
- [Difference Between “it” and “this” Keywords in Kotlin](https://www.baeldung.com/kotlin/it-vs-this-keywords)
10+
- [Data Objects in Kotlin](https://www.baeldung.com/kotlin/data-object-basics)
1011
- [[<-- prev]](/core-kotlin-modules/core-kotlin-lang-3)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.dataobjects
2+
3+
class DataObjectsExample {
4+
data class MyDataClass(val name: String)
5+
object MyRegularObject
6+
data object MyDataObject
7+
8+
// print
9+
fun printObjects() {
10+
println(MyDataClass("Jon")) // prints: MyDataClass(name=Jon)
11+
println(MyRegularObject.toString()) // prints: DataObjects$MyRegularObject@1b6d3586
12+
println(MyDataObject) // prints: MyDataObject
13+
}
14+
}

core-kotlin-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<module>core-kotlin-string-conversions</module>
6262
<module>core-kotlin-files</module>
6363
<module>core-kotlin-operators</module>
64+
<module>core-kotlin-concurrency-3</module>
6465
</modules>
6566

6667
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.kotlin.deserializestringobject
2+
3+
import com.fasterxml.jackson.databind.JsonNode
4+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
5+
import com.fasterxml.jackson.module.kotlin.readValue
6+
import com.google.gson.Gson
7+
import com.google.gson.JsonObject
8+
import kotlinx.serialization.json.Json
9+
import kotlinx.serialization.json.int
10+
import kotlinx.serialization.json.jsonObject
11+
import kotlinx.serialization.json.jsonPrimitive
12+
import org.junit.jupiter.api.Test
13+
import kotlin.test.assertEquals
14+
15+
class DeserializeStringObjectUnitTest {
16+
@Test
17+
fun `extract values from JSON string using Gson`(){
18+
val gson = Gson()
19+
val jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"
20+
val jsonObject = gson.fromJson(jsonString, JsonObject::class.java)
21+
val name = jsonObject.get("name").asString
22+
val city = jsonObject.get("city").asString
23+
val age = jsonObject.get("age").asInt
24+
25+
26+
assertEquals("John", name)
27+
assertEquals("New York", city)
28+
assertEquals(30, age)
29+
assertEquals("{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}", jsonObject.toString())
30+
}
31+
32+
@Test
33+
fun `extract values from JSON string using Kotlinx serialization`(){
34+
val json = Json { ignoreUnknownKeys = true }
35+
val jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"
36+
val jsonObject = json.parseToJsonElement(jsonString).jsonObject
37+
val name = jsonObject["name"]?.jsonPrimitive?.content
38+
val city = jsonObject["city"]?.jsonPrimitive?.content
39+
val age = jsonObject["age"]?.jsonPrimitive?.int
40+
41+
42+
assertEquals("John", name)
43+
assertEquals("New York", city)
44+
assertEquals(30, age)
45+
assertEquals("{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}", jsonObject.toString())
46+
}
47+
48+
@Test
49+
fun `extract values from JSON string using Jackson library`(){
50+
val objectMapper = jacksonObjectMapper()
51+
val jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"
52+
val jsonObject = objectMapper.readValue<JsonNode>(jsonString)
53+
val name = jsonObject.get("name").asText()
54+
val city = jsonObject.get("city").asText()
55+
val age = jsonObject.get("age").asInt()
56+
57+
assertEquals("John", name)
58+
assertEquals("New York", city)
59+
assertEquals(30, age)
60+
assertEquals("{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}", jsonObject.toString())
61+
}
62+
}

kotlin-ktor/build.gradle.kts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ dependencies {
1717
implementation("com.expediagroup", "graphql-kotlin-ktor-server", graphQLKotlinVersion)
1818
implementation("com.expediagroup", "graphql-kotlin-client-jackson", graphQLKotlinVersion)
1919

20+
implementation("io.ktor", "ktor-client-core", ktorVersion)
2021
implementation("io.ktor", "ktor-server-core", ktorVersion)
2122
implementation("io.ktor", "ktor-server-netty", ktorVersion)
2223
implementation("io.ktor", "ktor-server-websockets", ktorVersion)
2324
implementation("io.ktor","ktor-server-status-pages", ktorVersion)
24-
2525
implementation("io.ktor", "ktor-server-thymeleaf-jvm", ktorVersion)
26+
implementation("io.ktor", "ktor-client-auth", ktorVersion)
27+
implementation("io.ktor", "ktor-client-websockets", ktorVersion)
28+
implementation("io.ktor", "ktor-serialization-jackson", ktorVersion)
29+
implementation("io.ktor", "ktor-client-content-negotiation", ktorVersion)
30+
2631
implementation("ch.qos.logback","logback-classic", logbackVersion)
2732

33+
testImplementation("io.ktor", "ktor-client-mock", ktorVersion)
2834
testImplementation("io.ktor", "ktor-server-tests", ktorVersion)
29-
testImplementation("io.ktor", "ktor-client-content-negotiation", ktorVersion)
3035
testImplementation("io.ktor", "ktor-serialization-kotlinx-json", ktorVersion)
3136
testImplementation("org.jetbrains.kotlin", "kotlin-test-junit", kotlinTestUnit)
3237
testImplementation("org.seleniumhq.selenium", "selenium-java", seleniumVersion)

0 commit comments

Comments
 (0)