[BAEL-19882] - Move articles out of core-kotlin part1
This commit is contained in:
@@ -24,12 +24,8 @@ This module contains articles about core Kotlin.
|
||||
- [Try-with-resources in Kotlin](https://www.baeldung.com/kotlin-try-with-resources)
|
||||
- [Regular Expressions in Kotlin](https://www.baeldung.com/kotlin-regular-expressions)
|
||||
- [Objects in Kotlin](https://www.baeldung.com/kotlin-objects)
|
||||
- [Reading from a File in Kotlin](https://www.baeldung.com/kotlin-read-file)
|
||||
- [Guide to Kotlin @JvmField](https://www.baeldung.com/kotlin-jvm-field-annotation)
|
||||
- [Filtering Kotlin Collections](https://www.baeldung.com/kotlin-filter-collection)
|
||||
- [Writing to a File in Kotlin](https://www.baeldung.com/kotlin-write-file)
|
||||
- [Lambda Expressions in Kotlin](https://www.baeldung.com/kotlin-lambda-expressions)
|
||||
- [Kotlin String Templates](https://www.baeldung.com/kotlin-string-template)
|
||||
- [Working with Enums in Kotlin](https://www.baeldung.com/kotlin-enum)
|
||||
- [Create a Java and Kotlin Project with Maven](https://www.baeldung.com/kotlin-maven-java-project)
|
||||
- [Reflection with Kotlin](https://www.baeldung.com/kotlin-reflection)
|
||||
@@ -41,7 +37,6 @@ This module contains articles about core Kotlin.
|
||||
- [Fuel HTTP Library with Kotlin](https://www.baeldung.com/kotlin-fuel)
|
||||
- [Introduction to Kovenant Library for Kotlin](https://www.baeldung.com/kotlin-kovenant)
|
||||
- [Converting Kotlin Data Class from JSON using GSON](https://www.baeldung.com/kotlin-json-convert-data-class)
|
||||
- [Concatenate Strings in Kotlin](https://www.baeldung.com/kotlin-concatenate-strings)
|
||||
- [Kotlin return, break, continue Keywords](https://www.baeldung.com/kotlin-return-break-continue)
|
||||
- [Mapping of Data Objects in Kotlin](https://www.baeldung.com/kotlin-data-objects)
|
||||
- [Initializing Arrays in Kotlin](https://www.baeldung.com/kotlin-initialize-array)
|
||||
@@ -50,7 +45,6 @@ This module contains articles about core Kotlin.
|
||||
- [Guide to Sorting in Kotlin](https://www.baeldung.com/kotlin-sort)
|
||||
- [Dependency Injection for Kotlin with Injekt](https://www.baeldung.com/kotlin-dependency-injection-with-injekt)
|
||||
- [Implementing a Binary Tree in Kotlin](https://www.baeldung.com/kotlin-binary-tree)
|
||||
- [Generate a Random Alphanumeric String in Kotlin](https://www.baeldung.com/kotlin-random-alphanumeric-string)
|
||||
- [Kotlin Contracts](https://www.baeldung.com/kotlin-contracts)
|
||||
- [Operator Overloading in Kotlin](https://www.baeldung.com/kotlin-operator-overloading)
|
||||
- [Inline Classes in Kotlin](https://www.baeldung.com/kotlin-inline-classes)
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.filesystem
|
||||
|
||||
import java.io.File
|
||||
|
||||
class FileReader {
|
||||
|
||||
fun readFileLineByLineUsingForEachLine(fileName: String) = File(fileName).forEachLine { println(it) }
|
||||
|
||||
fun readFileAsLinesUsingUseLines(fileName: String): List<String> = File(fileName)
|
||||
.useLines { it.toList() }
|
||||
|
||||
fun readFileAsLinesUsingBufferedReader(fileName: String): List<String> = File(fileName).bufferedReader().readLines()
|
||||
|
||||
fun readFileAsLinesUsingReadLines(fileName: String): List<String> = File(fileName).readLines()
|
||||
|
||||
fun readFileAsTextUsingInputStream(fileName: String) =
|
||||
File(fileName).inputStream().readBytes().toString(Charsets.UTF_8)
|
||||
|
||||
fun readFileDirectlyAsText(fileName: String): String = File(fileName).readText(Charsets.UTF_8)
|
||||
|
||||
fun readFileUsingGetResource(fileName: String) = this::class.java.getResource(fileName).readText(Charsets.UTF_8)
|
||||
|
||||
fun readFileAsLinesUsingGetResourceAsStream(fileName: String) = this::class.java.getResourceAsStream(fileName).bufferedReader().readLines()
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.filesystem
|
||||
|
||||
import java.io.File
|
||||
|
||||
class FileWriter {
|
||||
|
||||
fun writeFileUsingPrintWriter(fileName: String, fileContent: String) =
|
||||
File(fileName).printWriter().use { out -> out.print(fileContent) }
|
||||
|
||||
fun writeFileUsingBufferedWriter(fileName: String, fileContent: String) =
|
||||
File(fileName).bufferedWriter().use { out -> out.write(fileContent) }
|
||||
|
||||
fun writeFileDirectly(fileName: String, fileContent: String) =
|
||||
File(fileName).writeText(fileContent)
|
||||
|
||||
fun writeFileDirectlyAsBytes(fileName: String, fileContent: String) =
|
||||
File(fileName).writeBytes(fileContent.toByteArray())
|
||||
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
class JvmSample(text:String) {
|
||||
@JvmField
|
||||
val sampleText:String = text
|
||||
}
|
||||
|
||||
class CompanionSample {
|
||||
companion object {
|
||||
@JvmField val MAX_LIMIT = 20
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
package com.baeldung.stringtemplates
|
||||
|
||||
/**
|
||||
* Example of a useful function defined in Kotlin String class
|
||||
*/
|
||||
fun padExample(): String {
|
||||
return "Hello".padEnd(10, '!')
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a simple string template usage
|
||||
*/
|
||||
fun simpleTemplate(n: Int): String {
|
||||
val message = "n = $n"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a string template with a simple expression
|
||||
*/
|
||||
fun templateWithExpression(n: Int): String {
|
||||
val message = "n + 1 = ${n + 1}"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a string template with expression containing some logic
|
||||
*/
|
||||
fun templateWithLogic(n: Int): String {
|
||||
val message = "$n is ${if (n > 0) "positive" else "not positive"}"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of nested string templates
|
||||
*/
|
||||
fun nestedTemplates(n: Int): String {
|
||||
val message = "$n is ${if (n > 0) "positive" else if (n < 0) "negative and ${if (n % 2 == 0) "even" else "odd"}" else "zero"}"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of joining array's element into a string with a default separator
|
||||
*/
|
||||
fun templateJoinArray(): String {
|
||||
val numbers = listOf(1, 1, 2, 3, 5, 8)
|
||||
val message = "first Fibonacci numbers: ${numbers.joinToString()}"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of escaping the dollar sign
|
||||
*/
|
||||
fun notAStringTemplate(): String {
|
||||
val message = "n = \$n"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a simple triple quoted string
|
||||
*/
|
||||
fun showFilePath(): String {
|
||||
val path = """C:\Repository\read.me"""
|
||||
return path
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a multiline string
|
||||
*/
|
||||
fun showMultiline(): String {
|
||||
val receipt = """Item 1: $1.00
|
||||
Item 2: $0.50"""
|
||||
return receipt
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a multiline string with indentation
|
||||
*/
|
||||
fun showMultilineIndent(): String {
|
||||
val receipt = """Item 1: $1.00
|
||||
>Item 2: $0.50""".trimMargin(">")
|
||||
return receipt
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a triple quoted string with a not-working escape sequence
|
||||
*/
|
||||
fun showTripleQuotedWrongEscape(): String {
|
||||
val receipt = """Item 1: $1.00\nItem 2: $0.50"""
|
||||
return receipt
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a triple quoted string with a correctly working escape sequence
|
||||
*/
|
||||
|
||||
fun showTripleQuotedCorrectEscape(): String {
|
||||
val receipt = """Item 1: $1.00${"\n"}Item 2: $0.50"""
|
||||
return receipt
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println(padExample())
|
||||
println(simpleTemplate(10))
|
||||
println(templateWithExpression(5))
|
||||
println(templateWithLogic(7))
|
||||
println(nestedTemplates(-5))
|
||||
println(templateJoinArray())
|
||||
println(notAStringTemplate())
|
||||
println(showFilePath())
|
||||
println(showMultiline())
|
||||
println(showMultilineIndent())
|
||||
println(showTripleQuotedWrongEscape())
|
||||
println(showTripleQuotedCorrectEscape())
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package com.baeldung.filesystem
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
internal class FileReaderTest {
|
||||
|
||||
private val fileName = "src/test/resources/Kotlin.in"
|
||||
|
||||
private val fileReader = FileReader()
|
||||
|
||||
@Test
|
||||
fun whenReadFileLineByLineUsingForEachLine_thenCorrect() {
|
||||
fileReader.readFileLineByLineUsingForEachLine(fileName)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileAsLinesUsingUseLines_thenCorrect() {
|
||||
val lines = fileReader.readFileAsLinesUsingUseLines(fileName)
|
||||
|
||||
assertTrue { lines.contains("1. Concise") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileAsLinesUsingBufferedReader_thenCorrect() {
|
||||
val lines = fileReader.readFileAsLinesUsingBufferedReader(fileName)
|
||||
|
||||
assertTrue { lines.contains("2. Safe") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileAsLinesUsingReadLines_thenCorrect() {
|
||||
val lines = fileReader.readFileAsLinesUsingReadLines(fileName)
|
||||
|
||||
assertTrue { lines.contains("3. Interoperable") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileAsTextUsingInputStream_thenCorrect() {
|
||||
val text = fileReader.readFileAsTextUsingInputStream(fileName)
|
||||
|
||||
assertTrue { text.contains("4. Tool-friendly") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadDirectlyAsText_thenCorrect() {
|
||||
val text = fileReader.readFileDirectlyAsText(fileName)
|
||||
|
||||
assertTrue { text.contains("Hello to Kotlin") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileAsTextUsingGetResource_thenCorrect() {
|
||||
val text = fileReader.readFileUsingGetResource("/Kotlin.in")
|
||||
|
||||
assertTrue { text.contains("1. Concise") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileUsingGetResourceAsStream_thenCorrect() {
|
||||
val lines = fileReader.readFileAsLinesUsingGetResourceAsStream("/Kotlin.in")
|
||||
|
||||
assertTrue { lines.contains("3. Interoperable") }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.baeldung.filesystem
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
internal class FileWriterTest {
|
||||
|
||||
private val fileName = "src/test/resources/Kotlin.out"
|
||||
|
||||
private val fileContent = "Kotlin\nConcise, Safe, Interoperable, Tool-friendly"
|
||||
|
||||
private val fileWriter = FileWriter()
|
||||
|
||||
@Test
|
||||
fun whenWrittenWithPrintWriter_thenCorrect() {
|
||||
fileWriter.writeFileUsingPrintWriter(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenWrittenWithBufferedWriter_thenCorrect() {
|
||||
fileWriter.writeFileUsingBufferedWriter(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenWrittenDirectly_thenCorrect() {
|
||||
fileWriter.writeFileDirectly(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenWrittenDirectlyAsBytes_thenCorrect() {
|
||||
fileWriter.writeFileDirectlyAsBytes(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class JvmSampleTest {
|
||||
|
||||
var sample = ""
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
sample = JvmSample("Hello!").sampleText
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenField_whenCheckValue_thenMatchesValue() {
|
||||
assertTrue(sample == "Hello!")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenStaticVariable_whenCheckValue_thenMatchesValue() {
|
||||
// Sample when is treated as a static variable
|
||||
assertTrue(CompanionSample.MAX_LIMIT == 20)
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class StringConcatenationTest {
|
||||
|
||||
@Test
|
||||
fun givenTwoStrings_concatenateWithTemplates_thenEquals() {
|
||||
val a = "Hello"
|
||||
val b = "Baeldung"
|
||||
val c = "$a $b"
|
||||
|
||||
assertEquals("Hello Baeldung", c)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenTwoStrings_concatenateWithPlusOperator_thenEquals() {
|
||||
val a = "Hello"
|
||||
val b = "Baeldung"
|
||||
val c = a + " " + b
|
||||
|
||||
assertEquals("Hello Baeldung", c)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenTwoStrings_concatenateWithStringBuilder_thenEquals() {
|
||||
val a = "Hello"
|
||||
val b = "Baeldung"
|
||||
|
||||
val builder = StringBuilder()
|
||||
builder.append(a).append(" ").append(b)
|
||||
|
||||
val c = builder.toString()
|
||||
|
||||
assertEquals("Hello Baeldung", c)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenTwoStrings_concatenateWithPlusMethod_thenEquals() {
|
||||
val a = "Hello"
|
||||
val b = "Baeldung"
|
||||
val c = a.plus(" ").plus(b)
|
||||
|
||||
assertEquals("Hello Baeldung", c)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
import org.apache.commons.lang3.RandomStringUtils
|
||||
import org.junit.Before
|
||||
import org.junit.jupiter.api.BeforeAll
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.security.SecureRandom
|
||||
import java.util.concurrent.ThreadLocalRandom
|
||||
import kotlin.experimental.and
|
||||
import kotlin.streams.asSequence
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
const val STRING_LENGTH = 10
|
||||
const val ALPHANUMERIC_REGEX = "[a-zA-Z0-9]+"
|
||||
|
||||
class RandomStringUnitTest {
|
||||
private val charPool : List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
|
||||
|
||||
@Test
|
||||
fun givenAStringLength_whenUsingJava_thenReturnAlphanumericString() {
|
||||
var randomString = ThreadLocalRandom.current()
|
||||
.ints(STRING_LENGTH.toLong(), 0, charPool.size)
|
||||
.asSequence()
|
||||
.map(charPool::get)
|
||||
.joinToString("")
|
||||
|
||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||
assertEquals(STRING_LENGTH, randomString.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenAStringLength_whenUsingKotlin_thenReturnAlphanumericString() {
|
||||
var randomString = (1..STRING_LENGTH).map { i -> kotlin.random.Random.nextInt(0, charPool.size) }
|
||||
.map(charPool::get)
|
||||
.joinToString("")
|
||||
|
||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||
assertEquals(STRING_LENGTH, randomString.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenAStringLength_whenUsingApacheCommon_thenReturnAlphanumericString() {
|
||||
var randomString = RandomStringUtils.randomAlphanumeric(STRING_LENGTH)
|
||||
|
||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||
assertEquals(STRING_LENGTH, randomString.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenAStringLength_whenUsingRandomForBytes_thenReturnAlphanumericString() {
|
||||
val random = SecureRandom()
|
||||
val bytes = ByteArray(STRING_LENGTH)
|
||||
random.nextBytes(bytes)
|
||||
|
||||
var randomString = (0..bytes.size - 1).map { i ->
|
||||
charPool.get((bytes[i] and 0xFF.toByte() and (charPool.size-1).toByte()).toInt())
|
||||
}.joinToString("")
|
||||
|
||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||
assertEquals(STRING_LENGTH, randomString.length)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
Hello to Kotlin. Its:
|
||||
1. Concise
|
||||
2. Safe
|
||||
3. Interoperable
|
||||
4. Tool-friendly
|
||||
@@ -1,2 +0,0 @@
|
||||
Kotlin
|
||||
Concise, Safe, Interoperable, Tool-friendly
|
||||
Reference in New Issue
Block a user