Merge branch 'BAEL-382_kotlin' of https://github.com/tomekl007/tutorials into tomekl007-BAEL-382_kotlin

This commit is contained in:
Pedja 2017-01-27 14:45:57 +01:00
commit a765ad26ec
6 changed files with 93 additions and 6 deletions

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)
}
}