All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tschuchort.compiletesting.DefaultPropertyDelegate.kt Maven / Gradle / Ivy

package com.tschuchort.compiletesting

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

@Suppress("MemberVisibilityCanBePrivate")
internal class DefaultPropertyDelegate(private val createDefault: () -> T) : ReadWriteProperty {
    val hasDefaultValue
        @Synchronized get() = (value == DEFAULT)

    private var value: Any? = DEFAULT
    val defaultValue by lazy { createDefault() }

    @Synchronized
    override operator fun getValue(thisRef: R, property: KProperty<*>): T {
        @Suppress("UNCHECKED_CAST")
        return if(hasDefaultValue)
            defaultValue
        else
            value as T
    }

    @Synchronized
    override operator fun setValue(thisRef: R, property: KProperty<*>, value: T) {
        this.value = value
    }

    companion object {
        private object DEFAULT
    }
}

internal fun  default(createDefault: () -> T) = DefaultPropertyDelegate(createDefault)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy