[BAEL-19885] - Move articles out of core-kotlin part4
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.contract
|
||||
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
|
||||
@ExperimentalContracts
|
||||
inline fun <R> myRun(block: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return block()
|
||||
}
|
||||
|
||||
@ExperimentalContracts
|
||||
fun callsInPlace() {
|
||||
val i: Int
|
||||
myRun {
|
||||
i = 1 // Without contract initialization is forbidden due to possible re-assignment
|
||||
}
|
||||
println(i) // Without contract variable might be uninitialized
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.baeldung.contract
|
||||
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.contract
|
||||
|
||||
data class Request(val arg: String)
|
||||
|
||||
class Service {
|
||||
|
||||
@ExperimentalContracts
|
||||
fun process(request: Request?) {
|
||||
validate(request)
|
||||
println(request.arg)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ExperimentalContracts
|
||||
private fun validate(request: Request?) {
|
||||
contract {
|
||||
returns() implies (request != null)
|
||||
}
|
||||
if (request == null) {
|
||||
throw IllegalArgumentException("Undefined request")
|
||||
}
|
||||
}
|
||||
|
||||
data class MyEvent(val message: String)
|
||||
|
||||
@ExperimentalContracts
|
||||
fun processEvent(event: Any?) {
|
||||
if (isInterested(event)) {
|
||||
println(event.message) // Compiler makes smart cast here with the help of contract
|
||||
}
|
||||
}
|
||||
|
||||
@ExperimentalContracts
|
||||
fun isInterested(event: Any?): Boolean {
|
||||
contract {
|
||||
returns(true) implies (event is MyEvent)
|
||||
}
|
||||
return event is MyEvent
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.datamapping
|
||||
|
||||
data class User(
|
||||
val firstName: String,
|
||||
val lastName: String,
|
||||
val street: String,
|
||||
val houseNumber: String,
|
||||
val phone: String,
|
||||
val age: Int,
|
||||
val password: String)
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.datamapping
|
||||
|
||||
import kotlin.reflect.full.memberProperties
|
||||
|
||||
fun User.toUserView() = UserView(
|
||||
name = "$firstName $lastName",
|
||||
address = "$street $houseNumber",
|
||||
telephone = phone,
|
||||
age = age
|
||||
)
|
||||
|
||||
fun User.toUserViewReflection() = with(::UserView) {
|
||||
val propertiesByName = User::class.memberProperties.associateBy { it.name }
|
||||
callBy(parameters.associate { parameter ->
|
||||
parameter to when (parameter.name) {
|
||||
UserView::name.name -> "$firstName $lastName"
|
||||
UserView::address.name -> "$street $houseNumber"
|
||||
UserView::telephone.name -> phone
|
||||
else -> propertiesByName[parameter.name]?.get(this@toUserViewReflection)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.datamapping
|
||||
|
||||
data class UserView(
|
||||
val name: String,
|
||||
val address: String,
|
||||
val telephone: String,
|
||||
val age: Int
|
||||
)
|
||||
@@ -1,114 +0,0 @@
|
||||
package com.baeldung.kotlin.dsl
|
||||
|
||||
abstract class Condition {
|
||||
|
||||
fun and(initializer: Condition.() -> Unit) {
|
||||
addCondition(And().apply(initializer))
|
||||
}
|
||||
|
||||
fun or(initializer: Condition.() -> Unit) {
|
||||
addCondition(Or().apply(initializer))
|
||||
}
|
||||
|
||||
infix fun String.eq(value: Any?) {
|
||||
addCondition(Eq(this, value))
|
||||
}
|
||||
|
||||
protected abstract fun addCondition(condition: Condition)
|
||||
}
|
||||
|
||||
open class CompositeCondition(private val sqlOperator: String) : Condition() {
|
||||
private val conditions = mutableListOf<Condition>()
|
||||
|
||||
override fun addCondition(condition: Condition) {
|
||||
conditions += condition
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return if (conditions.size == 1) {
|
||||
conditions.first().toString()
|
||||
} else {
|
||||
conditions.joinToString(prefix = "(", postfix = ")", separator = " $sqlOperator ") {
|
||||
"$it"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class And : CompositeCondition("and")
|
||||
|
||||
class Or : CompositeCondition("or")
|
||||
|
||||
class Eq(private val column: String, private val value: Any?) : Condition() {
|
||||
|
||||
init {
|
||||
if (value != null && value !is Number && value !is String) {
|
||||
throw IllegalArgumentException("Only <null>, numbers and strings values can be used in the 'where' clause")
|
||||
}
|
||||
}
|
||||
|
||||
override fun addCondition(condition: Condition) {
|
||||
throw IllegalStateException("Can't add a nested condition to the sql 'eq'")
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return when (value) {
|
||||
null -> "$column is null"
|
||||
is String -> "$column = '$value'"
|
||||
else -> "$column = $value"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SqlSelectBuilder {
|
||||
|
||||
private val columns = mutableListOf<String>()
|
||||
private lateinit var table: String
|
||||
private var condition: Condition? = null
|
||||
|
||||
fun select(vararg columns: String) {
|
||||
if (columns.isEmpty()) {
|
||||
throw IllegalArgumentException("At least one column should be defined")
|
||||
}
|
||||
if (this.columns.isNotEmpty()) {
|
||||
throw IllegalStateException("Detected an attempt to re-define columns to fetch. Current columns list: "
|
||||
+ "${this.columns}, new columns list: $columns")
|
||||
}
|
||||
this.columns.addAll(columns)
|
||||
}
|
||||
|
||||
fun from(table: String) {
|
||||
this.table = table
|
||||
}
|
||||
|
||||
fun where(initializer: Condition.() -> Unit) {
|
||||
condition = And().apply(initializer)
|
||||
}
|
||||
|
||||
fun build(): String {
|
||||
if (!::table.isInitialized) {
|
||||
throw IllegalStateException("Failed to build an sql select - target table is undefined")
|
||||
}
|
||||
return toString()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
val columnsToFetch =
|
||||
if (columns.isEmpty()) {
|
||||
"*"
|
||||
} else {
|
||||
columns.joinToString(", ")
|
||||
}
|
||||
val conditionString =
|
||||
if (condition == null) {
|
||||
""
|
||||
} else {
|
||||
" where $condition"
|
||||
}
|
||||
return "select $columnsToFetch from $table$conditionString"
|
||||
}
|
||||
}
|
||||
|
||||
fun query(initializer: SqlSelectBuilder.() -> Unit): SqlSelectBuilder {
|
||||
return SqlSelectBuilder().apply(initializer)
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
|
||||
open class LoggerAsExtensionOnAny {
|
||||
val logger = logger()
|
||||
|
||||
fun log(s: String) {
|
||||
logger().info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
}
|
||||
|
||||
class ExtensionSubclass : LoggerAsExtensionOnAny()
|
||||
|
||||
fun <T : Any> T.logger(): Logger = getLogger(getClassForLogging(javaClass))
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsExtensionOnAny().log("test")
|
||||
ExtensionSubclass().log("sub")
|
||||
"foo".logger().info("foo")
|
||||
1.logger().info("uh-oh!")
|
||||
SomeOtherClass().logger()
|
||||
}
|
||||
|
||||
class SomeOtherClass {
|
||||
fun logger(): String {
|
||||
return "foo"
|
||||
}
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
interface Logging
|
||||
|
||||
inline fun <reified T : Logging> T.logger(): Logger =
|
||||
//Wrong logger name!
|
||||
//LoggerFactory.getLogger(javaClass.name + " w/interface")
|
||||
LoggerFactory.getLogger(getClassForLogging(T::class.java).name + " w/interface")
|
||||
|
||||
open class LoggerAsExtensionOnMarkerInterface : Logging {
|
||||
companion object : Logging {
|
||||
val logger = logger()
|
||||
}
|
||||
|
||||
fun log(s: String) {
|
||||
logger().info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
}
|
||||
|
||||
class MarkerExtensionSubclass : LoggerAsExtensionOnMarkerInterface()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsExtensionOnMarkerInterface().log("test")
|
||||
MarkerExtensionSubclass().log("sub")
|
||||
"foo".logger().info("foo")
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
open class LoggerAsProperty {
|
||||
private val logger = getLogger(javaClass)
|
||||
|
||||
fun log(s: String) {
|
||||
logger.info(s)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class PropertySubclass : LoggerAsProperty()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsProperty().log("test")
|
||||
PropertySubclass().log("sub")
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
import kotlin.properties.ReadOnlyProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
open class LoggerAsPropertyDelegate {
|
||||
private val lazyLogger by lazyLogger()
|
||||
protected val logger by LoggerDelegate()
|
||||
private val logger2 = logger
|
||||
|
||||
companion object {
|
||||
private val lazyLoggerComp by lazyLogger()
|
||||
private val loggerComp by LoggerDelegate()
|
||||
}
|
||||
|
||||
open fun log(s: String) {
|
||||
logger.info(s)
|
||||
logger2.info(s)
|
||||
lazyLogger.info(s)
|
||||
loggerComp.info(s)
|
||||
lazyLoggerComp.info(s)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DelegateSubclass : LoggerAsPropertyDelegate() {
|
||||
override fun log(s: String) {
|
||||
logger.info("-- in sub")
|
||||
super.log(s)
|
||||
}
|
||||
}
|
||||
|
||||
fun lazyLogger(forClass: Class<*>): Lazy<Logger> =
|
||||
lazy { getLogger(getClassForLogging(forClass)) }
|
||||
|
||||
fun <T : Any> T.lazyLogger(): Lazy<Logger> = lazyLogger(javaClass)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsPropertyDelegate().log("test")
|
||||
DelegateSubclass().log("sub")
|
||||
}
|
||||
|
||||
class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
||||
override fun getValue(thisRef: R, property: KProperty<*>) =
|
||||
getLogger(getClassForLogging(thisRef.javaClass))
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
open class LoggerInCompanionObject {
|
||||
companion object {
|
||||
private val loggerWithExplicitClass = getLogger(LoggerInCompanionObject::class.java)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
private val loggerWithWrongClass = getLogger(javaClass)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
private val logger = getLogger(javaClass.enclosingClass)
|
||||
}
|
||||
|
||||
fun log(s: String) {
|
||||
loggerWithExplicitClass.info(s)
|
||||
loggerWithWrongClass.info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
|
||||
class Inner {
|
||||
companion object {
|
||||
private val loggerWithExplicitClass = getLogger(Inner::class.java)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
@JvmStatic
|
||||
private val loggerWithWrongClass = getLogger(javaClass)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
@JvmStatic
|
||||
private val logger = getLogger(javaClass.enclosingClass)
|
||||
}
|
||||
|
||||
fun log(s: String) {
|
||||
loggerWithExplicitClass.info(s)
|
||||
loggerWithWrongClass.info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class CompanionSubclass : LoggerInCompanionObject()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerInCompanionObject().log("test")
|
||||
LoggerInCompanionObject.Inner().log("test")
|
||||
CompanionSubclass().log("sub")
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import kotlin.reflect.full.companionObject
|
||||
|
||||
fun getLogger(forClass: Class<*>): Logger = LoggerFactory.getLogger(forClass)
|
||||
|
||||
fun <T : Any> getClassForLogging(javaClass: Class<T>): Class<*> {
|
||||
return javaClass.enclosingClass?.takeIf {
|
||||
it.kotlin.companionObject?.java == javaClass
|
||||
} ?: javaClass
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.thread
|
||||
|
||||
class SimpleRunnable: Runnable {
|
||||
|
||||
override fun run() {
|
||||
println("${Thread.currentThread()} has run.")
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.thread
|
||||
|
||||
class SimpleThread: Thread() {
|
||||
|
||||
override fun run() {
|
||||
println("${Thread.currentThread()} has run.")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user