Skip to content

Commit 32ad05a

Browse files
authored
Merge pull request eugenp#951 from tomekl007/BAEL-382_kotlin
BAEL-382 kotlin
2 parents 543c6ee + 9512edb commit 32ad05a

8 files changed

Lines changed: 205 additions & 0 deletions

File tree

‎kotlin/pom.xml‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>kotlin</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.jetbrains.kotlin</groupId>
14+
<artifactId>kotlin-stdlib</artifactId>
15+
<version>${kotlin-stdlib.version}</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.jetbrains.kotlin</groupId>
19+
<artifactId>kotlin-test-junit</artifactId>
20+
<version>${kotlin-test-junit.version}</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>${junit.version}</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
33+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
34+
<plugins>
35+
<plugin>
36+
<artifactId>kotlin-maven-plugin</artifactId>
37+
<groupId>org.jetbrains.kotlin</groupId>
38+
<version>${kotlin-maven-plugin.version}</version>
39+
<executions>
40+
<execution>
41+
<id>compile</id>
42+
<goals>
43+
<goal>compile</goal>
44+
</goals>
45+
</execution>
46+
47+
<execution>
48+
<id>test-compile</id>
49+
<goals>
50+
<goal>test-compile</goal>
51+
</goals>
52+
</execution>
53+
</executions>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
<properties>
59+
<junit.version>4.12</junit.version>
60+
<kotlin-test-junit.version>1.0.4</kotlin-test-junit.version>
61+
<kotlin-stdlib.version>1.0.4</kotlin-stdlib.version>
62+
<kotlin-maven-plugin.version>1.0.4</kotlin-maven-plugin.version>
63+
</properties>
64+
65+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.kotlin
2+
3+
fun main(args: Array<String>){
4+
println("hello word")
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.kotlin
2+
3+
open class Item(val id: String, val name: String = "unknown_name") {
4+
open fun getIdOfItem(): String {
5+
return id
6+
}
7+
}
8+
9+
class ItemWithCategory(id: String, name: String, val categoryId: String) : Item(id, name) {
10+
override fun getIdOfItem(): String {
11+
return id + name
12+
}
13+
}
14+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.kotlin
2+
3+
import java.util.*
4+
5+
class ItemService {
6+
fun findItemNameForId(id: String): Item? {
7+
val itemId = UUID.randomUUID().toString()
8+
return Item(itemId, "name-$itemId")
9+
}
10+
}
11+
12+
class ItemManager(val categoryId: String, val dbConnection: String) {
13+
var email = ""
14+
15+
constructor(categoryId: String, dbConnection: String, email: String)
16+
: this(categoryId, dbConnection) {
17+
this.email = email
18+
}
19+
20+
fun isFromSpecificCategory(catId: String): Boolean {
21+
return categoryId == catId
22+
}
23+
24+
fun makeAnalyisOfCategory(catId: String): Unit {
25+
val result = if (catId == "100") "Yes" else "No"
26+
println(result)
27+
28+
}
29+
}
30+
31+
fun main(args: Array<String>) {
32+
val numbers = arrayOf("first", "second", "third", "fourth")
33+
34+
var concat = ""
35+
for (n in numbers) {
36+
concat += n
37+
}
38+
39+
var sum = 0
40+
for (i in 2..9) {
41+
sum += i
42+
}
43+
44+
val firstName = "Tom"
45+
val secondName = "Mary"
46+
val concatOfNames = "$firstName + $secondName"
47+
println("Names: $concatOfNames")
48+
49+
val itemManager = ItemManager("cat_id", "db://connection")
50+
val result = "function result: ${itemManager.isFromSpecificCategory("1")}"
51+
println(result)
52+
53+
val number = 2
54+
if (number < 10) {
55+
println("number less that 10")
56+
} else if (number > 10) {
57+
println("number is greater that 10")
58+
}
59+
60+
val name = "John"
61+
when (name) {
62+
"John" -> println("Hi man")
63+
"Alice" -> println("Hi lady")
64+
}
65+
66+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.kotlin
2+
3+
import java.util.concurrent.ThreadLocalRandom
4+
5+
class ListExtension {
6+
fun <T> List<T>.random(): T? {
7+
if (this.isEmpty()) return null
8+
return get(ThreadLocalRandom.current().nextInt(count()))
9+
}
10+
11+
fun <T> getRandomElementOfList(list: List<T>): T? {
12+
return list.random()
13+
}
14+
}
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 org.junit.Test
4+
import kotlin.test.assertNotNull
5+
6+
class ItemServiceTest {
7+
@Test
8+
fun givenItemId_whenGetForOptionalItem_shouldMakeActionOnNonNullValue() {
9+
//given
10+
val id = "item_id"
11+
val itemService = ItemService()
12+
13+
//when
14+
val result = itemService.findItemNameForId(id)
15+
16+
//then
17+
assertNotNull(result?.let { it -> it.id })
18+
assertNotNull(result!!.id)
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 com.baeldung.kotlin.ListExtension
4+
import org.junit.Test
5+
import kotlin.test.assertTrue
6+
7+
class ListExtensionTest {
8+
@Test
9+
fun givenList_whenExecuteExtensionFunctionOnList_shouldReturnRandomElementOfList(){
10+
//given
11+
val elements = listOf("a", "b", "c")
12+
13+
//when
14+
val result = ListExtension().getRandomElementOfList(elements)
15+
16+
//then
17+
assertTrue(elements.contains(result))
18+
}
19+
}

‎pom.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
<module>jsoup</module>
6969
<module>junit5</module>
7070

71+
<module>kotlin</module>
72+
7173
<module>log-mdc</module>
7274
<module>log4j</module>
7375
<module>log4j2</module>

0 commit comments

Comments
 (0)