Skip to content

Commit 6474258

Browse files
authored
Merge branch 'master' into JAVA-39735
2 parents aab6239 + dfb6940 commit 6474258

File tree

337 files changed

+9457
-689
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

337 files changed

+9457
-689
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,5 @@ apache-fop/src/test/resources/output_html2fo.pdf
9393
apache-fop/src/test/resources/output_jtidy.pdf
9494

9595
!ktlint-custom-1.0.0-SNAPSHOT.jar
96-
!**/gradle-wrapper.jar
96+
!**/gradle-wrapper.jar
97+
/kotlin-testing/.jqwik-database

core-kotlin-companion/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-kotlin-companion</artifactId>
77
<name>core-kotlin-companion</name>

core-kotlin-modules/core-kotlin-10/pom.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5-
http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
65
<modelVersion>4.0.0</modelVersion>
76
<artifactId>core-kotlin-10</artifactId>
87
<name>core-kotlin-10</name>
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
### Relevant Articles
1+
### Relevant Articles
2+
- [Convert a Data Class to ByteBuffer in Kotlin](https://www.baeldung.com/kotlin/convert-data-class-to-bytebuffer)

core-kotlin-modules/core-kotlin-11/pom.xml

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5-
http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
65
<modelVersion>4.0.0</modelVersion>
76
<artifactId>core-kotlin-11</artifactId>
87
<name>core-kotlin-11</name>
@@ -59,7 +58,6 @@
5958
</build>
6059

6160
<properties>
62-
<kotlinx.serialization.version>1.4.1</kotlinx.serialization.version>
6361
<kryo.version>2.24.0</kryo.version>
6462
</properties>
6563
</project>

core-kotlin-modules/core-kotlin-2/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-kotlin-2</artifactId>
77
<name>core-kotlin-2</name>

core-kotlin-modules/core-kotlin-3/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
55
http://maven.apache.org/xsd/maven-4.0.0.xsd">
66
<modelVersion>4.0.0</modelVersion>
77
<artifactId>core-kotlin-3</artifactId>

core-kotlin-modules/core-kotlin-4/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns = "http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-kotlin-4</artifactId>
77
<name>core-kotlin-4</name>

core-kotlin-modules/core-kotlin-5/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns = "http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-kotlin-5</artifactId>
77
<name>core-kotlin-5</name>

core-kotlin-modules/core-kotlin-6/pom.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5-
http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
65
<modelVersion>4.0.0</modelVersion>
76
<artifactId>core-kotlin-6</artifactId>
87
<name>core-kotlin-6</name>

core-kotlin-modules/core-kotlin-7/pom.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5-
http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
65
<modelVersion>4.0.0</modelVersion>
76
<artifactId>core-kotlin-7</artifactId>
87
<name>core-kotlin-7</name>

core-kotlin-modules/core-kotlin-8/pom.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5-
http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
65
<modelVersion>4.0.0</modelVersion>
76
<artifactId>core-kotlin-8</artifactId>
87
<name>core-kotlin-8</name>

core-kotlin-modules/core-kotlin-9/pom.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5-
http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
65
<modelVersion>4.0.0</modelVersion>
76
<artifactId>core-kotlin-9</artifactId>
87
<name>core-kotlin-9</name>

core-kotlin-modules/core-kotlin-advanced-2/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-kotlin-advanced-2</artifactId>
77
<name>core-kotlin-advanced-2</name>

core-kotlin-modules/core-kotlin-advanced-3/pom.xml

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-kotlin-advanced-3</artifactId>
77
<name>core-kotlin-advanced-3</name>
@@ -19,24 +19,18 @@
1919
<artifactId>kotlinx-html-jvm</artifactId>
2020
<version>${kotlinx-html-jvm.version}</version>
2121
</dependency>
22-
2322
<dependency>
2423
<groupId>org.jetbrains.kotlinx</groupId>
2524
<artifactId>kotlinx-serialization-json</artifactId>
2625
<version>${serialization.version}</version>
2726
</dependency>
28-
2927
<dependency>
3028
<groupId>io.github.classgraph</groupId>
3129
<artifactId>classgraph</artifactId>
3230
<version>4.8.112</version>
3331
</dependency>
3432
</dependencies>
3533

36-
<properties>
37-
<kotlinx-html-jvm.version>0.7.5</kotlinx-html-jvm.version>
38-
</properties>
39-
4034
<build>
4135
<plugins>
4236
<plugin>
@@ -70,8 +64,11 @@
7064
</execution>
7165
</executions>
7266
</plugin>
73-
7467
</plugins>
7568
</build>
7669

70+
<properties>
71+
<kotlinx-html-jvm.version>0.7.5</kotlinx-html-jvm.version>
72+
</properties>
73+
7774
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[Combine 2 Kotlin State Flows Into a Single State Flow](https://www.baeldung.com/kotlin/combine-state-flows)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-kotlin-advanced-4</artifactId>
7+
<name>core-kotlin-advanced-4</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>core-kotlin-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.jetbrains.kotlinx</groupId>
19+
<artifactId>kotlinx-coroutines-test</artifactId>
20+
<version>${kotlinx-coroutines.version}</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.jetbrains.kotlinx</groupId>
25+
<artifactId>kotlinx-coroutines-core</artifactId>
26+
<version>${kotlinx-coroutines.version}</version>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.jetbrains.kotlin</groupId>
31+
<artifactId>kotlin-scripting-jsr223</artifactId>
32+
<version>${kotlin-scripting.version}</version>
33+
</dependency>
34+
</dependencies>
35+
36+
<profiles>
37+
<profile>
38+
<id>integration-test</id>
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-surefire-plugin</artifactId>
44+
<version>2.22.2</version>
45+
<configuration>
46+
<includes>
47+
<include>**/*IntegrationTest.kt</include>
48+
</includes>
49+
</configuration>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
</profile>
54+
</profiles>
55+
56+
<properties>
57+
<kotlin-scripting.version>1.8.0</kotlin-scripting.version>
58+
</properties>
59+
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.baeldung.combineTwoStateFlows
2+
3+
import kotlinx.coroutines.*
4+
import kotlinx.coroutines.flow.*
5+
import kotlinx.coroutines.test.runTest
6+
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.Test
8+
9+
class CombineTwoStateFlowsUnitTest {
10+
11+
@Test
12+
fun `merge two stateflows using combine operator`() = runTest {
13+
val stateFlow1 = MutableStateFlow(5)
14+
val stateFlow2 = MutableStateFlow("Hi")
15+
16+
val combinedStateFlow = combine(stateFlow1, stateFlow2) { value1, value2 ->
17+
value1 to value2
18+
}
19+
20+
val result = combinedStateFlow.first()
21+
22+
assertEquals(Pair(5, "Hi"), result)
23+
}
24+
25+
@Test
26+
fun `combine operator retains previous value when one flow emits`() = runTest {
27+
val stateFlow1 = MutableStateFlow(5)
28+
val stateFlow2 = MutableStateFlow("Hi")
29+
30+
val combinedStateFlow = combine(stateFlow1, stateFlow2) { value1, value2 ->
31+
value1 to value2
32+
}
33+
34+
val emissions = mutableListOf<Pair<Int, String>>()
35+
36+
val job = launch {
37+
combinedStateFlow.collect {
38+
emissions.add(it)
39+
}
40+
}
41+
42+
assertEquals(Pair(5, "Hi"), combinedStateFlow.first())
43+
44+
stateFlow1.value = 11
45+
assertEquals(Pair(11, "Hi"), combinedStateFlow.first())
46+
47+
stateFlow2.value = "World"
48+
assertEquals(Pair(11, "World"), combinedStateFlow.first())
49+
50+
51+
job.cancel()
52+
53+
assertEquals(listOf(Pair(5, "Hi"), Pair(11, "Hi"), Pair(11, "World")), emissions)
54+
}
55+
56+
@Test
57+
fun `merge two stateflows using zip operator`() = runTest {
58+
val stateFlow1 = MutableStateFlow(5)
59+
val stateFlow2 = MutableStateFlow("Hi")
60+
61+
val zippedStateFlow = stateFlow1.zip(stateFlow2) { value1, value2 ->
62+
value1 to value2
63+
}
64+
65+
val result = zippedStateFlow.first()
66+
assertEquals(Pair(5, "Hi"), result)
67+
}
68+
69+
@Test
70+
fun `zip operator only emits when both flows emit`() = runTest {
71+
val stateFlow1 = MutableStateFlow(5)
72+
val stateFlow2 = MutableStateFlow("Hi")
73+
74+
val zippedStateFlow = stateFlow1.zip(stateFlow2) { value1, value2 ->
75+
value1 to value2
76+
}
77+
78+
val emissions = mutableListOf<Pair<Int, String>>()
79+
80+
val job = launch {
81+
zippedStateFlow.collect {
82+
emissions.add(it)
83+
}
84+
}
85+
86+
delay(100)
87+
stateFlow1.value = 11
88+
89+
delay(100)
90+
stateFlow2.value = "Hey"
91+
92+
delay(100)
93+
stateFlow1.value = 12
94+
95+
delay(100)
96+
stateFlow2.value = "Hello"
97+
98+
delay(100)
99+
100+
assertEquals(listOf(Pair(5, "Hi"), Pair(11, "Hey"), Pair(12, "Hello")), emissions)
101+
102+
job.cancel()
103+
}
104+
}

0 commit comments

Comments
 (0)