From a1bc8b5fdeab2392f2ce443c3709404cb2e24b72 Mon Sep 17 00:00:00 2001 From: norangebit Date: Tue, 24 Mar 2020 21:43:20 +0100 Subject: [PATCH] add drill03 exercise 1 --- config/detekt/detekt.yml | 6 +-- .../kotlin/drills/drill03/exercise1/Main.kt | 17 +++++++++ .../kotlin/drills/drill03/exercise1/Math.kt | 11 ++++++ .../kotlin/util/active/ActiveObjectHandler.kt | 38 +++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/drills/drill03/exercise1/Main.kt create mode 100644 src/main/kotlin/drills/drill03/exercise1/Math.kt create mode 100644 src/main/kotlin/util/active/ActiveObjectHandler.kt diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml index 2227f6a..8a98267 100644 --- a/config/detekt/detekt.yml +++ b/config/detekt/detekt.yml @@ -239,7 +239,7 @@ formatting: active: true autoCorrect: true MultiLineIfElse: - active: true + active: false autoCorrect: true NoBlankLineBeforeRbrace: active: true @@ -401,7 +401,7 @@ performance: active: true excludes: "**/test/**,**/androidTest/**,**/*.Test.kt,**/*.Spec.kt,**/*.Spek.kt" SpreadOperator: - active: true + active: false excludes: "**/test/**,**/androidTest/**,**/*.Test.kt,**/*.Spec.kt,**/*.Spek.kt" UnnecessaryTemporaryInstantiation: active: true @@ -503,7 +503,7 @@ style: MagicNumber: active: true excludes: "**/test/**,**/androidTest/**,**/*.Test.kt,**/*.Spec.kt,**/*.Spek.kt" - ignoreNumbers: '-1,0,1,2' + ignoreNumbers: '-1,0,1,2,100,500,1000' ignoreHashCodeFunction: true ignorePropertyDeclaration: false ignoreLocalVariableDeclaration: false diff --git a/src/main/kotlin/drills/drill03/exercise1/Main.kt b/src/main/kotlin/drills/drill03/exercise1/Main.kt new file mode 100644 index 0000000..04df5ab --- /dev/null +++ b/src/main/kotlin/drills/drill03/exercise1/Main.kt @@ -0,0 +1,17 @@ +package drills.drill03.exercise1 + +import util.active.toActive +import kotlin.time.ExperimentalTime +import kotlin.time.measureTime + +@ExperimentalTime +fun main() { + val math = MathImpl().toActive(Math::class.java) + + val elapsed = measureTime { + for (i in 0 until 1000) + math.add(i, 0) + } + + println("Elapsed time: $elapsed") +} diff --git a/src/main/kotlin/drills/drill03/exercise1/Math.kt b/src/main/kotlin/drills/drill03/exercise1/Math.kt new file mode 100644 index 0000000..2e31df8 --- /dev/null +++ b/src/main/kotlin/drills/drill03/exercise1/Math.kt @@ -0,0 +1,11 @@ +package drills.drill03.exercise1 + +interface Math { + fun add(a: Int, b: Int) +} + +class MathImpl : Math { + override fun add(a: Int, b: Int) { + println("The sum is ${a + b}") + } +} diff --git a/src/main/kotlin/util/active/ActiveObjectHandler.kt b/src/main/kotlin/util/active/ActiveObjectHandler.kt new file mode 100644 index 0000000..ef17002 --- /dev/null +++ b/src/main/kotlin/util/active/ActiveObjectHandler.kt @@ -0,0 +1,38 @@ +package util.active + +import java.lang.reflect.InvocationHandler +import java.lang.reflect.Method +import java.lang.reflect.Proxy +import java.util.concurrent.LinkedBlockingQueue + +class ActiveObjectHandler(private val target: Any) : InvocationHandler { + private val queue = LinkedBlockingQueue() + + init { + Thread { + while (true) + queue.take().executeOn(target) + }.start() + } + + override fun invoke(proxy: Any?, method: Method?, args: Array?): Any { + if (method != null && args != null) + queue.add(Call(method, args)) + + return Unit + } + + data class Call(val method: Method, val args: Array) { + fun executeOn(target: Any) { + method.invoke(target, *args) + } + } +} + +fun Any.toActive(inter: Class): T { + return Proxy.newProxyInstance( + this::class.java.classLoader, + arrayOf(inter), + ActiveObjectHandler(this) + ) as T +}