Merge branch 'tomekl007-BAEL-382_kotlin'

This commit is contained in:
Pedja 2017-01-27 15:12:21 +01:00
commit 9f38b7c681
7 changed files with 105 additions and 6 deletions

View File

@ -38,6 +38,12 @@
<version>${kotlin-maven-plugin.version}</version>
<executions>
<execution>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>src/main/kotlin</source>
</sourceDirs>
</configuration>
<id>compile</id>
<goals>
<goal>compile</goal>
@ -45,6 +51,12 @@
</execution>
<execution>
<configuration>
<sourceDirs>
<source>src/test/java</source>
<source>src/test/kotlin</source>
</sourceDirs>
</configuration>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>

View File

@ -0,0 +1,7 @@
package com.baeldung.java;
public class StringUtils {
public static String toUpperCase(String name) {
return name.toUpperCase();
}
}

View File

@ -24,29 +24,41 @@ class ItemManager(val categoryId: String, val dbConnection: String) {
fun makeAnalyisOfCategory(catId: String): Unit {
val result = if (catId == "100") "Yes" else "No"
println(result)
`object`()
}
fun sum(a: Int, b: Int): Int {
return a + b
}
fun `object`(): String {
return "this is object"
}
}
fun main(args: Array<String>) {
val numbers = arrayOf("first", "second", "third", "fourth")
var concat = ""
for (n in numbers) {
concat += n
println(n)
}
var sum = 0
for (i in 2..9) {
sum += i
for (i in 2..9 step 2) {
println(i)
}
val res = 1.rangeTo(10).map { it * 2 }
println(res)
val firstName = "Tom"
val secondName = "Mary"
val concatOfNames = "$firstName + $secondName"
println("Names: $concatOfNames")
val sum = "four: ${2 + 2}"
val itemManager = ItemManager("cat_id", "db://connection")
ItemManager(categoryId = "catId", dbConnection = "db://Connection")
val result = "function result: ${itemManager.isFromSpecificCategory("1")}"
println(result)
@ -63,4 +75,9 @@ fun main(args: Array<String>) {
"Alice" -> println("Hi lady")
}
val items = listOf(1, 2, 3, 4)
val rwList = mutableListOf(1, 2, 3)
rwList.add(5)
}

View File

@ -0,0 +1,7 @@
package com.baeldung.kotlin
class MathematicsOperations {
fun addTwoNumbers(a: Int, b: Int): Int {
return a + b
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.kotlin;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class JavaCallToKotlinTest {
@Test
public void givenKotlinClass_whenCallFromJava_shouldProduceResults() {
//when
int res = new MathematicsOperations().addTwoNumbers(2, 4);
//then
assertEquals(6, res);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.kotlin
import com.baeldung.java.StringUtils
import org.junit.Test
import kotlin.test.assertEquals
class KotlinScalaInteroperabilityTest {
@Test
fun givenLowercaseString_whenExecuteMethodFromJavaStringUtils_shouldReturnStringUppercase() {
//given
val name = "tom"
//when
val res = StringUtils.toUpperCase(name)
//then
assertEquals(res, "TOM")
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.kotlin
import org.junit.Test
import kotlin.test.assertEquals
class LambdaTest {
@Test
fun givenListOfNumber_whenDoingOperationsUsingLambda_shouldReturnProperResult() {
//given
val listOfNumbers = listOf(1, 2, 3)
//when
val sum = listOfNumbers.reduce { a, b -> a + b }
//then
assertEquals(6, sum)
}
}