Merge branch 'tomekl007-BAEL-382_kotlin'
This commit is contained in:
		
						commit
						9f38b7c681
					
				| @ -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> | ||||
|  | ||||
							
								
								
									
										7
									
								
								kotlin/src/main/java/com/baeldung/java/StringUtils.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								kotlin/src/main/java/com/baeldung/java/StringUtils.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | ||||
| package com.baeldung.java; | ||||
| 
 | ||||
| public class StringUtils { | ||||
|     public static String toUpperCase(String name) { | ||||
|         return name.toUpperCase(); | ||||
|     } | ||||
| } | ||||
| @ -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) | ||||
| } | ||||
| @ -0,0 +1,7 @@ | ||||
| package com.baeldung.kotlin | ||||
| 
 | ||||
| class MathematicsOperations { | ||||
|     fun addTwoNumbers(a: Int, b: Int): Int { | ||||
|         return a + b | ||||
|     } | ||||
| } | ||||
| @ -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); | ||||
| 
 | ||||
|     } | ||||
| } | ||||
| @ -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") | ||||
|     } | ||||
| } | ||||
							
								
								
									
										19
									
								
								kotlin/src/test/kotlin/com/baeldung/kotlin/LambdaTest.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								kotlin/src/test/kotlin/com/baeldung/kotlin/LambdaTest.kt
									
									
									
									
									
										Normal 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) | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user