Skip to content

Commit 9f38b7c

Browse files
committed
Merge branch 'tomekl007-BAEL-382_kotlin'
2 parents 5d2abc9 + 4999547 commit 9f38b7c

7 files changed

Lines changed: 104 additions & 5 deletions

File tree

‎kotlin/pom.xml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,25 @@
3838
<version>${kotlin-maven-plugin.version}</version>
3939
<executions>
4040
<execution>
41+
<configuration>
42+
<sourceDirs>
43+
<source>src/main/java</source>
44+
<source>src/main/kotlin</source>
45+
</sourceDirs>
46+
</configuration>
4147
<id>compile</id>
4248
<goals>
4349
<goal>compile</goal>
4450
</goals>
4551
</execution>
4652

4753
<execution>
54+
<configuration>
55+
<sourceDirs>
56+
<source>src/test/java</source>
57+
<source>src/test/kotlin</source>
58+
</sourceDirs>
59+
</configuration>
4860
<id>test-compile</id>
4961
<goals>
5062
<goal>test-compile</goal>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.java;
2+
3+
public class StringUtils {
4+
public static String toUpperCase(String name) {
5+
return name.toUpperCase();
6+
}
7+
}

‎kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,41 @@ class ItemManager(val categoryId: String, val dbConnection: String) {
2424
fun makeAnalyisOfCategory(catId: String): Unit {
2525
val result = if (catId == "100") "Yes" else "No"
2626
println(result)
27+
`object`()
28+
}
29+
30+
fun sum(a: Int, b: Int): Int {
31+
return a + b
32+
}
2733

34+
fun `object`(): String {
35+
return "this is object"
2836
}
37+
2938
}
3039

3140
fun main(args: Array<String>) {
3241
val numbers = arrayOf("first", "second", "third", "fourth")
3342

34-
var concat = ""
3543
for (n in numbers) {
36-
concat += n
44+
println(n)
3745
}
3846

39-
var sum = 0
40-
for (i in 2..9) {
41-
sum += i
47+
for (i in 2..9 step 2) {
48+
println(i)
4249
}
4350

51+
val res = 1.rangeTo(10).map { it * 2 }
52+
println(res)
53+
4454
val firstName = "Tom"
4555
val secondName = "Mary"
4656
val concatOfNames = "$firstName + $secondName"
4757
println("Names: $concatOfNames")
58+
val sum = "four: ${2 + 2}"
4859

4960
val itemManager = ItemManager("cat_id", "db://connection")
61+
ItemManager(categoryId = "catId", dbConnection = "db://Connection")
5062
val result = "function result: ${itemManager.isFromSpecificCategory("1")}"
5163
println(result)
5264

@@ -63,4 +75,9 @@ fun main(args: Array<String>) {
6375
"Alice" -> println("Hi lady")
6476
}
6577

78+
val items = listOf(1, 2, 3, 4)
79+
80+
81+
val rwList = mutableListOf(1, 2, 3)
82+
rwList.add(5)
6683
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.kotlin
2+
3+
class MathematicsOperations {
4+
fun addTwoNumbers(a: Int, b: Int): Int {
5+
return a + b
6+
}
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.kotlin;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class JavaCallToKotlinTest {
8+
@Test
9+
public void givenKotlinClass_whenCallFromJava_shouldProduceResults() {
10+
//when
11+
int res = new MathematicsOperations().addTwoNumbers(2, 4);
12+
13+
//then
14+
assertEquals(6, res);
15+
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.kotlin
2+
3+
import com.baeldung.java.StringUtils
4+
import org.junit.Test
5+
import kotlin.test.assertEquals
6+
7+
8+
class KotlinScalaInteroperabilityTest {
9+
@Test
10+
fun givenLowercaseString_whenExecuteMethodFromJavaStringUtils_shouldReturnStringUppercase() {
11+
//given
12+
val name = "tom"
13+
14+
//when
15+
val res = StringUtils.toUpperCase(name)
16+
17+
//then
18+
assertEquals(res, "TOM")
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.kotlin
2+
3+
import org.junit.Test
4+
import kotlin.test.assertEquals
5+
6+
7+
class LambdaTest {
8+
@Test
9+
fun givenListOfNumber_whenDoingOperationsUsingLambda_shouldReturnProperResult() {
10+
//given
11+
val listOfNumbers = listOf(1, 2, 3)
12+
13+
//when
14+
val sum = listOfNumbers.reduce { a, b -> a + b }
15+
16+
//then
17+
assertEquals(6, sum)
18+
}
19+
}

0 commit comments

Comments
 (0)