forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
systemProp
function to the common module; only the JVM platfor…
…m implements it properly while others always use provided default value.
- Loading branch information
Showing
4 changed files
with
84 additions
and
35 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
kotlinx-coroutines-core/common/src/internal/SystemProps.common.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
@file:JvmName("SystemPropsKt") | ||
@file:JvmMultifileClass | ||
|
||
package kotlinx.coroutines.internal | ||
|
||
import kotlin.jvm.* | ||
|
||
/** | ||
* Gets the system property indicated by the specified [property name][propertyName], | ||
* or returns [defaultValue] if there is no property with that key. | ||
* | ||
* **Note: this function should be used in JVM tests only, other platforms use the default value.** | ||
*/ | ||
internal fun systemProp( | ||
propertyName: String, | ||
defaultValue: Boolean | ||
): Boolean = systemProp(propertyName)?.toBoolean() ?: defaultValue | ||
|
||
/** | ||
* Gets the system property indicated by the specified [property name][propertyName], | ||
* or returns [defaultValue] if there is no property with that key. It also checks that the result | ||
* is between [minValue] and [maxValue] (inclusively), throws [IllegalStateException] if it is not. | ||
* | ||
* **Note: this function should be used in JVM tests only, other platforms use the default value.** | ||
*/ | ||
internal fun systemProp( | ||
propertyName: String, | ||
defaultValue: Int, | ||
minValue: Int = 1, | ||
maxValue: Int = Int.MAX_VALUE | ||
): Int = systemProp(propertyName, defaultValue.toLong(), minValue.toLong(), maxValue.toLong()).toInt() | ||
|
||
/** | ||
* Gets the system property indicated by the specified [property name][propertyName], | ||
* or returns [defaultValue] if there is no property with that key. It also checks that the result | ||
* is between [minValue] and [maxValue] (inclusively), throws [IllegalStateException] if it is not. | ||
* | ||
* **Note: this function should be used in JVM tests only, other platforms use the default value.** | ||
*/ | ||
internal fun systemProp( | ||
propertyName: String, | ||
defaultValue: Long, | ||
minValue: Long = 1, | ||
maxValue: Long = Long.MAX_VALUE | ||
): Long { | ||
val value = systemProp(propertyName) ?: return defaultValue | ||
val parsed = value.toLongOrNull() | ||
?: error("System property '$propertyName' has unrecognized value '$value'") | ||
if (parsed !in minValue..maxValue) { | ||
error("System property '$propertyName' should be in range $minValue..$maxValue, but is '$parsed'") | ||
} | ||
return parsed | ||
} | ||
|
||
/** | ||
* Gets the system property indicated by the specified [property name][propertyName], | ||
* or returns `null` if there is no property with that key. | ||
* | ||
* **Note: this function should be used in JVM tests only, other platforms use the default value.** | ||
*/ | ||
internal expect fun systemProp(propertyName: String): String? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.internal | ||
|
||
internal actual fun systemProp(propertyName: String): String? = null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,20 @@ | ||
/* | ||
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
@file:JvmName("SystemPropsKt") | ||
@file:JvmMultifileClass | ||
|
||
package kotlinx.coroutines.internal | ||
|
||
// number of processors at startup for consistent prop initialization | ||
internal val AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors() | ||
|
||
internal fun systemProp( | ||
internal actual fun systemProp( | ||
propertyName: String | ||
): String? = | ||
try { | ||
System.getProperty(propertyName) | ||
} catch (e: SecurityException) { | ||
null | ||
} | ||
|
||
internal fun systemProp( | ||
propertyName: String, | ||
defaultValue: Boolean | ||
): Boolean = | ||
try { | ||
System.getProperty(propertyName)?.toBoolean() ?: defaultValue | ||
} catch (e: SecurityException) { | ||
defaultValue | ||
} | ||
|
||
internal fun systemProp( | ||
propertyName: String, | ||
defaultValue: Int, | ||
minValue: Int = 1, | ||
maxValue: Int = Int.MAX_VALUE | ||
): Int | ||
= systemProp(propertyName, defaultValue.toLong(), minValue.toLong(), maxValue.toLong()).toInt() | ||
|
||
internal fun systemProp( | ||
propertyName: String, | ||
defaultValue: Long, | ||
minValue: Long = 1, | ||
maxValue: Long = Long.MAX_VALUE | ||
): Long { | ||
val value = systemProp(propertyName) ?: return defaultValue | ||
val parsed = value.toLongOrNull() | ||
?: error("System property '$propertyName' has unrecognized value '$value'") | ||
if (parsed !in minValue..maxValue) { | ||
error("System property '$propertyName' should be in range $minValue..$maxValue, but is '$parsed'") | ||
} | ||
return parsed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.internal | ||
|
||
internal actual fun systemProp(propertyName: String): String? = null |