Skip to content

Instantly share code, notes, and snippets.

@markmaynard
markmaynard / threadsafetest.kt
Created June 8, 2019 15:21
Thread safe test
// Thread safe check
runBlocking {
val deferred = (1..20).map { n ->
GlobalScope.async {
val r = (1..10).random()
println("In ${n}")
try {
when {
r % 2 == 0 -> {
println("Create ${n} Thing1")
@markmaynard
markmaynard / ThreadSafeSetter.kt
Last active June 10, 2019 12:02
Thread safe setter
companion object {
@Volatile var myThing: SomeThing? = null
@Synchronized set(value: SomeThing?) {
val i = myThing
if (i != null) {
field = i
} else {
field = value
}
}
@markmaynard
markmaynard / sealedclassSomeThing.kt
Last active June 10, 2019 11:59
Sealed class for factory
sealed class SomeThing {
abstract fun run()
abstract fun play()
}
class Thing1: SomeThing() {
override fun run() = println("Run Fast!")
override fun play() = println("Play Hard!")
}
class Thing2: SomeThing() {
@markmaynard
markmaynard / fakeclassfunc.kt
Created June 8, 2019 14:31
Fake Class function
inline fun <reified T: SomeThing> ThingFactory(clazz: KClass<T>): ThingFactory? =
when(clazz) {
Thing1::class -> ThingFactory(ThingFactory.ThingType.Thing1Type)
Thing2::class -> ThingFactory(ThingFactory.ThingType.Thing2Type)
else -> throw RuntimeException("Unknown type: $clazz")
}
@markmaynard
markmaynard / enumfactory.kt
Last active June 10, 2019 11:49
Enum for class instantiation
interface ClassBackedEnum {
fun toClass(): Any
}
class ThingFactory private constructor() {
enum class ThingType(): ClassBackedEnum {
Thing1Type {
override fun toClass() = Thing1::class
},
Thing2Type {
@markmaynard
markmaynard / gensingletonfactory2.kt
Last active June 10, 2019 12:08
Generic Singleton Factory attempt 2
import kotlin.reflect.KClass
import kotlinx.coroutines.*
fun main() {
println("Welcome to Thingville, population 1")
val factory = ThingFactory(Thing1::class)
factory?.run() // "Run Fast!"
factory?.play() // "Run Hard!"
val factory2 = ThingFactory(Thing2::class) // Run Time Error: Already created as type: class Thing1
val factory3 = ThingFactory(NoThing::class) // Compile Time Error: None of the following functions can be called with the arguments supplied: public constructor ThingFactory(clazz: ThingFactory.ThingType) defined in ThingFactory public inline fun <reified T : SomeThing> ThingFactory(clazz: KClass<???>): ThingFactory?
@markmaynard
markmaynard / gsfpt1c.kt
Last active June 10, 2019 11:45
Generic Singleton Factory part 1 complete
val factory = ThingFactory()
factory.run<Thing1>() // "Run Fast!"
factory.play<Thing1>() // "Play Hard!"
factory.play<Thing2>() // "Play Hard!" ????
interface SomeThing {
fun run()
fun play()
}
class ThingFactory {
inline fun <reified T: Any> run() {
if (myThing == null) {
createThing<T>()
}
myThing!!.run()
}
inline fun <reified T: Any> play() {
if (myThing == null) {
@markmaynard
markmaynard / thingfactoryuse1.kt
Last active June 8, 2019 12:20
Using my first thing factor
val factory = ThingFactory()
factory.run<Thing1>() // "Run Fast!"
factory.play<Thing1>() // "Play Hard!"
factory.play<Thing2>() // "Play Hard!" ????
@markmaynard
markmaynard / ThingFactory.kt
Last active June 1, 2019 17:49
ThingFactory attempt number 1
class ThingFactory {
inline fun <reified T: Any> run() {
if (myThing == null) {
createThing<T>()
}
myThing!!.run()
}
inline fun <reified T: Any> play() {
if (myThing == null) {