1 @file:JvmName("SystemPropsKt")
2 @file:JvmMultifileClass
3
4 package kotlinx.coroutines.internal
5
6 import kotlin.jvm.*
7
8 /**
9 * Gets the system property indicated by the specified [property name][propertyName],
10 * or returns [defaultValue] if there is no property with that key.
11 *
12 * **Note: this function should be used in JVM tests only, other platforms use the default value.**
13 */
systemPropnull14 internal fun systemProp(
15 propertyName: String,
16 defaultValue: Boolean
17 ): Boolean = systemProp(propertyName)?.toBoolean() ?: defaultValue
18
19 /**
20 * Gets the system property indicated by the specified [property name][propertyName],
21 * or returns [defaultValue] if there is no property with that key. It also checks that the result
22 * is between [minValue] and [maxValue] (inclusively), throws [IllegalStateException] if it is not.
23 *
24 * **Note: this function should be used in JVM tests only, other platforms use the default value.**
25 */
26 internal fun systemProp(
27 propertyName: String,
28 defaultValue: Int,
29 minValue: Int = 1,
30 maxValue: Int = Int.MAX_VALUE
31 ): Int = systemProp(propertyName, defaultValue.toLong(), minValue.toLong(), maxValue.toLong()).toInt()
32
33 /**
34 * Gets the system property indicated by the specified [property name][propertyName],
35 * or returns [defaultValue] if there is no property with that key. It also checks that the result
36 * is between [minValue] and [maxValue] (inclusively), throws [IllegalStateException] if it is not.
37 *
38 * **Note: this function should be used in JVM tests only, other platforms use the default value.**
39 */
40 internal fun systemProp(
41 propertyName: String,
42 defaultValue: Long,
43 minValue: Long = 1,
44 maxValue: Long = Long.MAX_VALUE
45 ): Long {
46 val value = systemProp(propertyName) ?: return defaultValue
47 val parsed = value.toLongOrNull()
48 ?: error("System property '$propertyName' has unrecognized value '$value'")
49 if (parsed !in minValue..maxValue) {
50 error("System property '$propertyName' should be in range $minValue..$maxValue, but is '$parsed'")
51 }
52 return parsed
53 }
54
55 /**
56 * Gets the system property indicated by the specified [property name][propertyName],
57 * or returns [defaultValue] if there is no property with that key.
58 *
59 * **Note: this function should be used in JVM tests only, other platforms use the default value.**
60 */
systemPropnull61 internal fun systemProp(
62 propertyName: String,
63 defaultValue: String
64 ): String = systemProp(propertyName) ?: defaultValue
65
66 /**
67 * Gets the system property indicated by the specified [property name][propertyName],
68 * or returns `null` if there is no property with that key.
69 *
70 * **Note: this function should be used in JVM tests only, other platforms use the default value.**
71 */
72 internal expect fun systemProp(propertyName: String): String?
73