package com.android.systemui.util import java.lang.ref.SoftReference import java.lang.ref.WeakReference import java.util.concurrent.atomic.AtomicReference import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty /** * Creates a Kotlin idiomatic weak reference. * * Usage: * ``` * var weakReferenceObj: Object? by weakReference(null) * weakReferenceObj = Object() * ``` */ fun weakReference(obj: T? = null): ReadWriteProperty { return object : ReadWriteProperty { var weakRef = WeakReference(obj) override fun getValue(thisRef: Any?, property: KProperty<*>): T? { return weakRef.get() } override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) { weakRef = WeakReference(value) } } } /** * Creates a Kotlin idiomatic soft reference. * * Usage: * ``` * var softReferenceObj: Object? by softReference(null) * softReferenceObj = Object() * ``` */ fun softReference(obj: T? = null): ReadWriteProperty { return object : ReadWriteProperty { var softRef = SoftReference(obj) override fun getValue(thisRef: Any?, property: KProperty<*>): T? { return softRef.get() } override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) { softRef = SoftReference(value) } } } /** * Creates a nullable Kotlin idiomatic [AtomicReference]. * * Usage: * ``` * var atomicReferenceObj: Object? by nullableAtomicReference(null) * atomicReferenceObj = Object() * ``` */ fun nullableAtomicReference(obj: T? = null): ReadWriteProperty { return object : ReadWriteProperty { val t = AtomicReference(obj) override fun getValue(thisRef: Any?, property: KProperty<*>): T? { return t.get() } override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) { t.set(value) } } }