Skip to content

Commit 8948e4e

Browse files
Merge branch 'Baeldung:master' into master
2 parents c8d16ac + 73e7420 commit 8948e4e

File tree

279 files changed

+5446
-1939
lines changed

Some content is hidden

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

279 files changed

+5446
-1939
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Building the project
99

1010
To build the entire repository with only Unit Tests enabled run:
1111

12-
`mvn clean install`
12+
`mvn clean install -Pdefault`
1313

1414
or if we want to build the entire repository with Integration Tests enabled, we can do:
1515

core-kotlin-companion/pom.xml

Lines changed: 1 addition & 7 deletions
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"
33
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-
>
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-companion</artifactId>
87
<name>core-kotlin-companion</name>
@@ -14,9 +13,4 @@
1413
<version>1.0.0-SNAPSHOT</version>
1514
</parent>
1615

17-
18-
19-
<properties>
20-
</properties>
21-
2216
</project>
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
## Relevant Articles
1+
### Relevant Articles
22
- [Determine Operating System in Kotlin](https://www.baeldung.com/kotlin/operating-system-identify)
33
- [Kotlin when vs. Java switch Statement](https://www.baeldung.com/kotlin/when-vs-java-switch)
44
- [Kotlin AES Encryption and Decryption](https://www.baeldung.com/kotlin/advanced-encryption-standard)
5+
- [Cloning an Object in Kotlin](https://www.baeldung.com/kotlin/clone-object)
6+
- [Assignment in While Expression in Kotlin](https://www.baeldung.com/kotlin/while-variable-assignment)
7+
- [Breadth-First Search Algorithm in Kotlin](https://www.baeldung.com/kotlin/bfs-graphs)
8+
- [Calling Private Methods of a Class From Outside the Class](https://www.baeldung.com/kotlin/private-methods-outside-class)
9+
- [Pass a Function as Parameter to Another in Kotlin](https://www.baeldung.com/kotlin/function-parameter-high-order)
10+
- [Accessing Methods Outside Companion Object in Kotlin](https://www.baeldung.com/kotlin/companion-object-external-methods)
11+
- [Accessing Private Java Fields via Kotlin Extension Functions](https://www.baeldung.com/kotlin/extension-functions-private-fields)
12+
- [Variable Shadowing in Kotlin](https://www.baeldung.com/kotlin/variable-shadowing)
13+
- More articles: [[<-- prev]](../core-kotlin-9)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
55
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
<artifactId>core-kotlin-10</artifactId>
8+
<name>core-kotlin-10</name>
9+
<packaging>jar</packaging>
610

711
<parent>
812
<groupId>com.baeldung</groupId>
913
<artifactId>core-kotlin-modules</artifactId>
1014
<version>1.0.0-SNAPSHOT</version>
1115
</parent>
1216

13-
<modelVersion>4.0.0</modelVersion>
14-
<artifactId>core-kotlin-10</artifactId>
15-
1617
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung;
2+
3+
public class StringWrapper {
4+
private final String source;
5+
6+
public StringWrapper(String source) {
7+
this.source = source;
8+
}
9+
10+
public boolean containsIgnoreCase(String other) {
11+
if (source == null) {
12+
return false;
13+
}
14+
15+
return source.toLowerCase().contains(other.toLowerCase());
16+
}
17+
18+
public String getSource() {
19+
return source;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.access_private_members_from_extension_func
2+
3+
class Main {
4+
fun String?.containsIgnoreCase(target: String) : Boolean {
5+
if (this == null) {
6+
return false
7+
}
8+
9+
val loadedClass = String::class
10+
val valueField = loadedClass.java.getDeclaredField("value")
11+
valueField?.isAccessible = true
12+
val actualValue = valueField?.get(this) as? ByteArray
13+
14+
println(actualValue?.contentToString())
15+
16+
return this.lowercase().contains(target.lowercase())
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.bfs
2+
3+
fun bfs(graph: Map<Int, List<Int>>, start: Int): Set<Int> {
4+
val visited = mutableSetOf<Int>()
5+
val queue = ArrayDeque<Int>()
6+
queue.add(start)
7+
while (queue.isNotEmpty()) {
8+
val vertex = queue.removeFirst()
9+
if (vertex !in visited) {
10+
visited.add(vertex)
11+
graph[vertex]?.let { neighbors -> queue.addAll(neighbors.filterNot { it in visited }) }
12+
}
13+
}
14+
return visited
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.bfs
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
6+
class BfsUnitTest {
7+
@Test
8+
fun `test BFS traversal order`() {
9+
val graph = mapOf(
10+
1 to listOf(2, 3, 4),
11+
2 to listOf(5, 6),
12+
3 to listOf(),
13+
4 to listOf(7, 8),
14+
5 to listOf(),
15+
6 to listOf(),
16+
7 to listOf(),
17+
8 to listOf()
18+
)
19+
20+
val traversalOrder = bfs(graph, 1)
21+
22+
23+
// Level 1
24+
val levelOne = listOf(traversalOrder.first())
25+
assertThat(levelOne).containsExactly(1)
26+
27+
// Level 2
28+
val levelTwo = traversalOrder.drop(1).take(3)
29+
assertThat(levelTwo).containsExactlyInAnyOrder(2, 3, 4)
30+
31+
// Level 3
32+
val levelThree = traversalOrder.drop(4)
33+
assertThat(levelThree).containsExactlyInAnyOrder(5, 6, 7, 8)
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.baeldung.functionAsParameter
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
6+
fun joinByOperation(theList: List<String>, operation: (List<String>) -> String): String {
7+
return operation(theList)
8+
}
9+
10+
class MessageParser {
11+
fun joinWithoutPlaceholder(segments: List<String>): String {
12+
return segments.joinToString(separator = " ").replace(" [SPACE] ", " ")
13+
}
14+
15+
companion object {
16+
fun simplyJoin(segments: List<String>): String {
17+
return segments.joinToString(separator = " ")
18+
}
19+
}
20+
}
21+
22+
object ParserInObject {
23+
fun joinWithoutComma(segments: List<String>): String {
24+
return segments.joinToString(separator = " ") { it.replace(",", "") }
25+
}
26+
}
27+
28+
fun decrypt(segments: List<String>): String {
29+
return segments.reversed().joinToString(separator = " ") { it.reversed() }
30+
}
31+
32+
class FunctionAsParameterUnitTest {
33+
34+
@Test
35+
fun `when passing lambda as parameters then get expected result`() {
36+
val input = listOf("a b c", "d e f", "x y z")
37+
val result1 = joinByOperation(input) { theList ->
38+
theList.joinToString(separator = " ") { str -> str.reversed() }.replace(" ", ", ")
39+
}
40+
assertEquals("c, b, a, f, e, d, z, y, x", result1)
41+
42+
val result2 = joinByOperation(input) { theList ->
43+
theList.reversed().joinToString(separator = " ") { str -> str }.uppercase()
44+
}
45+
assertEquals("X Y Z D E F A B C", result2)
46+
47+
}
48+
49+
@Test
50+
fun `when passing instance function ref as parameters then get expected result`() {
51+
val messageParser = MessageParser()
52+
val input = listOf("a [SPACE] b [SPACE] c", "d [SPACE] e [SPACE] f", "x [SPACE] y [SPACE] z")
53+
val result = joinByOperation(input, messageParser::joinWithoutPlaceholder)
54+
assertEquals("a b c d e f x y z", result)
55+
}
56+
57+
@Test
58+
fun `when passing companion object function ref as parameters then get expected result`() {
59+
val input = listOf("a b c", "d e f", "x y z")
60+
val result = joinByOperation(input, MessageParser::simplyJoin)
61+
assertEquals("a b c d e f x y z", result)
62+
}
63+
64+
@Test
65+
fun `when passing object function ref as parameters then get expected result`() {
66+
val input = listOf("a, b, c", "d, e, f", "x, y, z")
67+
val result = joinByOperation(input, ParserInObject::joinWithoutComma)
68+
assertEquals("a b c d e f x y z", result)
69+
}
70+
71+
@Test
72+
fun `when passing top-level function ref as parameters then get expected result`() {
73+
val input = listOf("z y x", "f e d", "c b a")
74+
val result = joinByOperation(input, ::decrypt)
75+
assertEquals("a b c d e f x y z", result)
76+
}
77+
78+
@Test
79+
fun `when passing variable with function type as parameter then get expected result`() {
80+
val input = listOf("a, b, c", "d, e, f", "x, y, z")
81+
82+
val funRef = ParserInObject::joinWithoutComma
83+
val resultFunRef = joinByOperation(input, funRef)
84+
assertEquals("a b c d e f x y z", resultFunRef)
85+
86+
val funLambda = { theList: List<String> -> theList.reversed().joinToString(separator = ", ") { str -> str }.uppercase() }
87+
val resultFunLambda = joinByOperation(input, funLambda)
88+
assertEquals("X, Y, Z, D, E, F, A, B, C", resultFunLambda)
89+
}
90+
}

0 commit comments

Comments
 (0)