From ffd39a1fd11330ab60599fc9d12f562f5ef3eafb Mon Sep 17 00:00:00 2001 From: Nicholas Hubbard Date: Sat, 10 Feb 2024 13:35:16 -0500 Subject: [PATCH] chore: Clear up some minor deprecations --- src/main/kotlin/com/nhubbard/konfig/SizeInBytes.kt | 6 +++--- src/main/kotlin/com/nhubbard/konfig/Utils.kt | 6 +++--- .../com/nhubbard/konfig/source/DefaultLoaders.kt | 5 +++-- .../com/nhubbard/konfig/source/DefaultProviders.kt | 3 ++- src/main/kotlin/com/nhubbard/konfig/source/Loader.kt | 5 +++-- src/main/kotlin/com/nhubbard/konfig/source/Provider.kt | 10 +++++----- .../com/nhubbard/konfig/source/DefaultLoadersSpec.kt | 10 +++++----- 7 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/com/nhubbard/konfig/SizeInBytes.kt b/src/main/kotlin/com/nhubbard/konfig/SizeInBytes.kt index 28afe15d..a77b5660 100644 --- a/src/main/kotlin/com/nhubbard/konfig/SizeInBytes.kt +++ b/src/main/kotlin/com/nhubbard/konfig/SizeInBytes.kt @@ -123,12 +123,12 @@ data class SizeInBytes( ZEBIBYTES("zebi", Radix.KIBI, 7), YOBIBYTES("yobi", Radix.KIBI, 8); - internal val bytes: BigInteger = BigInteger.valueOf(radix.toInt().toLong()).pow(power) + val bytes: BigInteger = BigInteger.valueOf(radix.toInt().toLong()).pow(power) companion object { private val unitsMap = mutableMapOf().apply { - for (unit in MemoryUnit.values()) { + for (unit in MemoryUnit.entries) { put(unit.prefix + "byte", unit) put(unit.prefix + "bytes", unit) if (unit.prefix.isEmpty()) { @@ -137,7 +137,7 @@ data class SizeInBytes( put("", unit) // no unit specified means bytes } else { val first = unit.prefix.substring(0, 1) - val firstUpper = first.toUpperCase() + val firstUpper = first.uppercase() when (unit.radix) { Radix.KILO -> { if (unit.power == 1) { diff --git a/src/main/kotlin/com/nhubbard/konfig/Utils.kt b/src/main/kotlin/com/nhubbard/konfig/Utils.kt index 061173e8..40478e8a 100644 --- a/src/main/kotlin/com/nhubbard/konfig/Utils.kt +++ b/src/main/kotlin/com/nhubbard/konfig/Utils.kt @@ -47,13 +47,13 @@ fun String.notEmptyOr(default: String): String = if (isEmpty()) default else thi fun String.toLittleCase(): String { return if (this.all { it.isUpperCase() }) { - this.toLowerCase() + this.lowercase() } else { when (val firstLowerCaseIndex = this.indexOfFirst { it.isLowerCase() }) { -1, 0 -> this - 1 -> this[0].toLowerCase() + this.drop(1) + 1 -> this[0].lowercaseChar() + this.drop(1) else -> - this.substring(0, firstLowerCaseIndex - 1).toLowerCase() + + this.substring(0, firstLowerCaseIndex - 1).lowercase() + this.substring(firstLowerCaseIndex - 1) } } diff --git a/src/main/kotlin/com/nhubbard/konfig/source/DefaultLoaders.kt b/src/main/kotlin/com/nhubbard/konfig/source/DefaultLoaders.kt index eff3c768..7813baaf 100644 --- a/src/main/kotlin/com/nhubbard/konfig/source/DefaultLoaders.kt +++ b/src/main/kotlin/com/nhubbard/konfig/source/DefaultLoaders.kt @@ -27,6 +27,7 @@ import com.nhubbard.konfig.source.json.JsonProvider import com.nhubbard.konfig.source.properties.PropertiesProvider import kotlinx.coroutines.Dispatchers import java.io.File +import java.net.URI import java.net.URL import java.util.concurrent.TimeUnit import kotlin.coroutines.CoroutineContext @@ -293,7 +294,7 @@ class DefaultLoaders( * @return a child config containing values from specified url string * @throws UnsupportedExtensionException */ - fun url(url: String, optional: Boolean = this.optional): Config = url(URL(url), optional) + fun url(url: String, optional: Boolean = this.optional): Config = url(URI(url).toURL(), optional) /** * Returns a child config containing values from specified url, @@ -360,7 +361,7 @@ class DefaultLoaders( context: CoroutineContext = Dispatchers.Default, optional: Boolean = this.optional, onLoad: ((config: Config, source: Source) -> Unit)? = null - ): Config = watchUrl(URL(url), period, unit, context, optional, onLoad) + ): Config = watchUrl(URI(url).toURL(), period, unit, context, optional, onLoad) } /** diff --git a/src/main/kotlin/com/nhubbard/konfig/source/DefaultProviders.kt b/src/main/kotlin/com/nhubbard/konfig/source/DefaultProviders.kt index 2a21e84c..55af36fc 100644 --- a/src/main/kotlin/com/nhubbard/konfig/source/DefaultProviders.kt +++ b/src/main/kotlin/com/nhubbard/konfig/source/DefaultProviders.kt @@ -24,6 +24,7 @@ import com.nhubbard.konfig.source.env.EnvProvider import com.nhubbard.konfig.source.json.JsonProvider import com.nhubbard.konfig.source.properties.PropertiesProvider import java.io.File +import java.net.URI import java.net.URL /** @@ -157,7 +158,7 @@ object DefaultProviders { * @return a source from specified url string * @throws UnsupportedExtensionException */ - fun url(url: String, optional: Boolean = false): Source = url(URL(url), optional) + fun url(url: String, optional: Boolean = false): Source = url(URI(url).toURL(), optional) } /** diff --git a/src/main/kotlin/com/nhubbard/konfig/source/Loader.kt b/src/main/kotlin/com/nhubbard/konfig/source/Loader.kt index e375bc38..6153ef22 100644 --- a/src/main/kotlin/com/nhubbard/konfig/source/Loader.kt +++ b/src/main/kotlin/com/nhubbard/konfig/source/Loader.kt @@ -27,6 +27,7 @@ import kotlinx.coroutines.launch import java.io.File import java.io.InputStream import java.io.Reader +import java.net.URI import java.net.URL import java.nio.file.FileSystems import java.nio.file.StandardWatchEventKinds @@ -126,7 +127,7 @@ class Loader( } onLoad?.invoke(newConfig, source) val path = absoluteFile.toPath().parent - val isMac = "mac" in System.getProperty("os.name").toLowerCase() + val isMac = "mac" in System.getProperty("os.name").lowercase() val watcher = FileSystems.getDefault().newWatchService() path.register( watcher, @@ -316,7 +317,7 @@ class Loader( optional: Boolean = this.optional, onLoad: ((config: Config, source: Source) -> Unit)? = null ): Config = - watchUrl(URL(url), period, unit, context, optional, onLoad) + watchUrl(URI(url).toURL(), period, unit, context, optional, onLoad) /** * Returns a child config containing values from specified resource. diff --git a/src/main/kotlin/com/nhubbard/konfig/source/Provider.kt b/src/main/kotlin/com/nhubbard/konfig/source/Provider.kt index 66c30e13..766ae9c3 100644 --- a/src/main/kotlin/com/nhubbard/konfig/source/Provider.kt +++ b/src/main/kotlin/com/nhubbard/konfig/source/Provider.kt @@ -22,12 +22,12 @@ import com.nhubbard.konfig.source.json.JsonProvider import com.nhubbard.konfig.source.properties.PropertiesProvider import org.reflections.ReflectionUtils import org.reflections.Reflections -import org.reflections.scanners.SubTypesScanner -import org.reflections.scanners.TypeAnnotationsScanner +import org.reflections.scanners.Scanners import java.io.File import java.io.IOException import java.io.InputStream import java.io.Reader +import java.net.URI import java.net.URL import java.util.concurrent.ConcurrentHashMap @@ -163,7 +163,7 @@ interface Provider { * @param optional whether this source is optional * @return a new source from specified url string */ - fun url(url: String, optional: Boolean = false): Source = url(URL(url), optional) + fun url(url: String, optional: Boolean = false): Source = url(URI(url).toURL(), optional) /** * Returns a new source from specified resource. @@ -254,8 +254,8 @@ interface Provider { init { val reflections = Reflections( "com.nhubbard.konfig", - SubTypesScanner(), - TypeAnnotationsScanner() + Scanners.SubTypes, + Scanners.TypesAnnotated ) val providers = reflections.getSubTypesOf(Provider::class.java) .intersect(reflections.getTypesAnnotatedWith(RegisterExtension::class.java)) diff --git a/src/test/kotlin/com/nhubbard/konfig/source/DefaultLoadersSpec.kt b/src/test/kotlin/com/nhubbard/konfig/source/DefaultLoadersSpec.kt index 78bf7530..50308906 100644 --- a/src/test/kotlin/com/nhubbard/konfig/source/DefaultLoadersSpec.kt +++ b/src/test/kotlin/com/nhubbard/konfig/source/DefaultLoadersSpec.kt @@ -35,8 +35,8 @@ import org.jetbrains.spek.api.dsl.on import org.jetbrains.spek.subject.SubjectSpek import org.jetbrains.spek.subject.itBehavesLike import spark.Service -import java.net.URL -import java.util.UUID +import java.net.URI +import java.util.* import java.util.concurrent.TimeUnit object DefaultLoadersSpec : SubjectSpek({ @@ -93,7 +93,7 @@ object DefaultLoadersSpec : SubjectSpek({ service.port(0) service.get("/source.properties") { _, _ -> propertiesContent } service.awaitInitialization() - val config = subject.url(URL("http://localhost:${service.port()}/source.properties")) + val config = subject.url(URI("http://localhost:${service.port()}/source.properties").toURL()) it("should load as auto-detected URL format") { assertThat(config[item], equalTo("properties")) } @@ -213,7 +213,7 @@ object DefaultLoadersSpec : SubjectSpek({ service.get("/source.properties") { _, _ -> content } service.awaitInitialization() val url = "http://localhost:${service.port()}/source.properties" - val config = subject.watchUrl(URL(url), period = 1, unit = TimeUnit.SECONDS, context = Dispatchers.Sequential) + val config = subject.watchUrl(URI(url).toURL(), period = 1, unit = TimeUnit.SECONDS, context = Dispatchers.Sequential) val originalValue = config[item] content = propertiesContent.replace("properties", "newValue") runBlocking(Dispatchers.Sequential) { @@ -234,7 +234,7 @@ object DefaultLoadersSpec : SubjectSpek({ service.get("/source.properties") { _, _ -> content } service.awaitInitialization() val url = "http://localhost:${service.port()}/source.properties" - val config = subject.watchUrl(URL(url), context = Dispatchers.Sequential) + val config = subject.watchUrl(URI(url).toURL(), context = Dispatchers.Sequential) val originalValue = config[item] content = propertiesContent.replace("properties", "newValue") runBlocking(Dispatchers.Sequential) {