comparison src/name/blackcap/exifwasher/Misc.kt @ 21:539c3641c539

Input type to SetOnceImpl cannot be nullable.
author David Barts <n5jrn@me.com>
date Sun, 12 Apr 2020 18:46:57 -0700
parents 965435b85a69
children cd2ca4727b7f
comparison
equal deleted inserted replaced
20:965435b85a69 21:539c3641c539
21 /** 21 /**
22 * Delegate that makes a var that can only be set once. This is commonly 22 * Delegate that makes a var that can only be set once. This is commonly
23 * needed in Swing, because some vars inevitably need to be declared at 23 * needed in Swing, because some vars inevitably need to be declared at
24 * outer levels but initialized in the Swing event dispatch thread. 24 * outer levels but initialized in the Swing event dispatch thread.
25 */ 25 */
26 class SetOnceImpl<T>: ReadWriteProperty<Any?,T> { 26 class SetOnceImpl<T: Any>: ReadWriteProperty<Any?,T> {
27 private var setOnceValue: T? = null 27 private var setOnceValue: T? = null
28 28
29 override operator fun getValue(thisRef: Any?, property: KProperty<*>): T { 29 override operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
30 if (setOnceValue == null) { 30 if (setOnceValue == null) {
31 throw RuntimeException("${property.name} has not been initialized") 31 throw RuntimeException("${property.name} has not been initialized")
41 } 41 }
42 setOnceValue = value 42 setOnceValue = value
43 } 43 }
44 } 44 }
45 45
46 fun <T> setOnce(): SetOnceImpl<T> = SetOnceImpl<T>() 46 fun <T: Any> setOnce(): SetOnceImpl<T> = SetOnceImpl<T>()
47 47
48 /** 48 /**
49 * Run something in the Swing thread, asynchronously. 49 * Run something in the Swing thread, asynchronously.
50 * @param block lambda containing code to run 50 * @param block lambda containing code to run
51 */ 51 */