comparison src/name/blackcap/exifwasher/Misc.kt @ 20:965435b85a69

Get rid of some stuttering.
author David Barts <n5jrn@me.com>
date Sun, 12 Apr 2020 11:46:12 -0700
parents 39b977021ea1
children 539c3641c539
comparison
equal deleted inserted replaced
19:39b977021ea1 20:965435b85a69
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 SetOnce<T: Any>: ReadWriteProperty<Any?,T> { 26 class SetOnceImpl<T>: 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")
40 throw RuntimeException("${property.name} has already been initialized") 40 throw RuntimeException("${property.name} has already been initialized")
41 } 41 }
42 setOnceValue = value 42 setOnceValue = value
43 } 43 }
44 } 44 }
45
46 fun <T> setOnce(): SetOnceImpl<T> = SetOnceImpl<T>()
45 47
46 /** 48 /**
47 * Run something in the Swing thread, asynchronously. 49 * Run something in the Swing thread, asynchronously.
48 * @param block lambda containing code to run 50 * @param block lambda containing code to run
49 */ 51 */