Skip to content

Commit 3e42850

Browse files
committed
Move systemProp function to the common module; only the JVM platform implements it properly while others always use provided default value.
1 parent b6f5b2c commit 3e42850

File tree

4 files changed

+84
-35
lines changed

4 files changed

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

5+
@file:JvmName("SystemPropsKt")
6+
@file:JvmMultifileClass
7+
58
package kotlinx.coroutines.internal
69

710
// number of processors at startup for consistent prop initialization
811
internal val AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors()
912

10-
internal fun systemProp(
13+
internal actual fun systemProp(
1114
propertyName: String
1215
): String? =
1316
try {
1417
System.getProperty(propertyName)
1518
} catch (e: SecurityException) {
1619
null
1720
}
18-
19-
internal fun systemProp(
20-
propertyName: String,
21-
defaultValue: Boolean
22-
): Boolean =
23-
try {
24-
System.getProperty(propertyName)?.toBoolean() ?: defaultValue
25-
} catch (e: SecurityException) {
26-
defaultValue
27-
}
28-
29-
internal fun systemProp(
30-
propertyName: String,
31-
defaultValue: Int,
32-
minValue: Int = 1,
33-
maxValue: Int = Int.MAX_VALUE
34-
): Int
35-
= systemProp(propertyName, defaultValue.toLong(), minValue.toLong(), maxValue.toLong()).toInt()
36-
37-
internal fun systemProp(
38-
propertyName: String,
39-
defaultValue: Long,
40-
minValue: Long = 1,
41-
maxValue: Long = Long.MAX_VALUE
42-
): Long {
43-
val value = systemProp(propertyName) ?: return defaultValue
44-
val parsed = value.toLongOrNull()
45-
?: error("System property '$propertyName' has unrecognized value '$value'")
46-
if (parsed !in minValue..maxValue) {
47-
error("System property '$propertyName' should be in range $minValue..$maxValue, but is '$parsed'")
48-
}
49-
return parsed
50-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.internal
6+
7+
internal actual fun systemProp(propertyName: String): String? = null

0 commit comments

Comments
 (0)