comparison src/name/blackcap/clipman/Misc.kt @ 47:19d9da731c43

Recoded; cleaned up root namespace, removed race conditions.
author David Barts <n5jrn@me.com>
date Sun, 12 Apr 2020 14:31:06 -0700
parents 33fbe3a78d84
children 3f8409470fdf
comparison
equal deleted inserted replaced
46:88066346f129 47:19d9da731c43
6 import java.awt.Dimension 6 import java.awt.Dimension
7 import java.awt.Toolkit 7 import java.awt.Toolkit
8 import java.nio.charset.Charset 8 import java.nio.charset.Charset
9 import javax.swing.* 9 import javax.swing.*
10 import javax.swing.text.JTextComponent 10 import javax.swing.text.JTextComponent
11 import kotlin.annotation.*
12 import kotlin.properties.ReadWriteProperty
13 import kotlin.reflect.*
11 14
12 /** 15 /**
13 * Name of the character set (encoding) we preferentially use for many 16 * Name of the character set (encoding) we preferentially use for many
14 * things. 17 * things.
15 */ 18 */
16 val CHARSET_NAME = "UTF-8" 19 val CHARSET_NAME = "UTF-8"
17 val CHARSET = Charset.forName(CHARSET_NAME) 20 val CHARSET = Charset.forName(CHARSET_NAME)
18 21
19 /** 22 /**
20 * Allows a val to have lateinit functionality. It is an error to attempt 23 * Delegate that makes a var that can only be set once. This is commonly
21 * to retrieve this object's value unless it has been set, and it is an 24 * needed in Swing, because some vars inevitably need to be declared at
22 * error to attempt to set the value of an already-set object. 25 * outer levels but initialized in the Swing event dispatch thread.
26 *
23 * @param &lt;T&gt; type of the associated value 27 * @param &lt;T&gt; type of the associated value
24 */ 28 */
25 class LateInit<T> { 29 class SetOnceImpl<T>: ReadWriteProperty<Any?,T> {
26 private var _v: T? = null 30 private var setOnceValue: T? = null
27 31
28 /** 32 override operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
29 * The value associated with this object. The name of this property is 33 if (setOnceValue == null) {
30 * deliberately short. 34 throw RuntimeException("${property.name} has not been initialized")
31 */
32 var v: T
33 get() {
34 if (_v == null) {
35 throw IllegalStateException("cannot retrieve un-initialized value")
36 } else { 35 } else {
37 return _v!! 36 return setOnceValue!!
38 } 37 }
39 } 38 }
40 @Synchronized set(value) { 39
41 if (_v != null) { 40 @Synchronized
42 throw IllegalStateException("cannot set already-initialized value") 41 override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T): Unit {
42 if (setOnceValue != null) {
43 throw RuntimeException("${property.name} has already been initialized")
43 } 44 }
44 _v = value 45 setOnceValue = value
45 } 46 }
46 } 47 }
48
49 /**
50 * Normal way to create a setOnce var:
51 * var something: SomeType by setOnce()
52 */
53 fun <T> setOnce(): SetOnceImpl<T> = SetOnceImpl<T>()
47 54
48 /** 55 /**
49 * Run something in the Swing thread, asynchronously. 56 * Run something in the Swing thread, asynchronously.
50 * @param block lambda containing code to run 57 * @param block lambda containing code to run
51 */ 58 */
97 return jMenuItem 104 return jMenuItem
98 } 105 }
99 } 106 }
100 return null 107 return null
101 } 108 }
102