From 2de9dc66bd906fae0765dea970ffd04f5c33a0ee Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 15 May 2021 01:26:31 -0700 Subject: [PATCH] Added more Utils extensions. --- .../net/thauvin/erik/mobibot/Mobibot.kt | 2 +- .../kotlin/net/thauvin/erik/mobibot/Utils.kt | 21 +++++++++++++------ .../erik/mobibot/commands/tell/Tell.kt | 6 +++--- .../net/thauvin/erik/mobibot/UtilsTest.kt | 15 +++++++++++-- version.properties | 6 +++--- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt index af81bc8..c6390f0 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt @@ -616,7 +616,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert name = nickname ircServer = p.getProperty("server", Constants.DEFAULT_SERVER) - ircPort = getIntProperty(p.getProperty("port"), Constants.DEFAULT_PORT) + ircPort = p.getIntProperty("port", Constants.DEFAULT_PORT) this.channel = channel logsDir = logsDirPath diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt index 3a07ac3..f55ff61 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt @@ -45,6 +45,7 @@ import java.time.LocalDateTime import java.time.ZoneId import java.time.format.DateTimeFormatter import java.util.Date +import java.util.Properties import java.util.concurrent.TimeUnit import java.util.stream.Collectors @@ -119,12 +120,8 @@ object Utils { * Returns a property as an int. */ @JvmStatic - fun getIntProperty(property: String?, def: Int): Int { - return try { - property?.toInt() ?: def - } catch (nfe: NumberFormatException) { - def - } + fun Properties.getIntProperty(key: String, defaultValue: Int): Int { + return this.getProperty(key)?.toIntOrDefault(defaultValue) ?: defaultValue } /** @@ -203,6 +200,18 @@ object Utils { } } + /** + * Converts a string to an int. + */ + @JvmStatic + fun String.toIntOrDefault(defaultValue: Int): Int { + return try { + this.toInt() + } catch (e: NumberFormatException) { + defaultValue + } + } + /** * Returns the specified date as an ISO local date string. */ diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt index 72fd262..cd59eac 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/Tell.kt @@ -34,10 +34,10 @@ package net.thauvin.erik.mobibot.commands.tell import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.buildCmdSyntax -import net.thauvin.erik.mobibot.Utils.getIntProperty import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.plural import net.thauvin.erik.mobibot.Utils.reverseColor +import net.thauvin.erik.mobibot.Utils.toIntOrDefault import net.thauvin.erik.mobibot.Utils.toUtcDateTime import net.thauvin.erik.mobibot.commands.AbstractCommand import net.thauvin.erik.mobibot.commands.links.View @@ -162,9 +162,9 @@ class Tell(bot: Mobibot) : AbstractCommand(bot) { override fun setProperty(key: String, value: String) { super.setProperty(key, value) if (MAX_DAYS_PROP == key) { - maxDays = getIntProperty(value, maxDays) + maxDays = value.toIntOrDefault(maxDays) } else if (MAX_SIZE_PROP == key) { - maxSize = getIntProperty(value, maxSize) + maxSize = value.toIntOrDefault(maxSize) } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt index 0a77475..2dd8dd5 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt @@ -41,6 +41,7 @@ import net.thauvin.erik.mobibot.Utils.obfuscate import net.thauvin.erik.mobibot.Utils.plural import net.thauvin.erik.mobibot.Utils.reverseColor import net.thauvin.erik.mobibot.Utils.toDir +import net.thauvin.erik.mobibot.Utils.toIntOrDefault import net.thauvin.erik.mobibot.Utils.toIsoLocalDate import net.thauvin.erik.mobibot.Utils.toUtcDateTime import net.thauvin.erik.mobibot.Utils.today @@ -57,6 +58,7 @@ import java.io.IOException import java.net.URL import java.time.LocalDateTime import java.util.Calendar +import java.util.Properties /** * The `Utils Test` class. @@ -102,8 +104,11 @@ class UtilsTest { @Test fun testGetIntProperty() { - assertThat(getIntProperty("10", 1)).describedAs("getIntProperty(10, 1)").isEqualTo(10) - assertThat(getIntProperty("a", 1)).describedAs("getIntProperty(a, 1)").isEqualTo(1) + val p = Properties() + p["one"] = "1" + p["two"] = "foo" + assertThat(p.getIntProperty("one", 1)).describedAs("getIntProperty(one)").isEqualTo(1) + assertThat(p.getIntProperty("two", 2)).describedAs("getIntProperty(two)").isEqualTo(2) } @Test @@ -151,6 +156,12 @@ class UtilsTest { .isEqualTo("https://erik.thauvin.net/") } + @Test + fun testToIntOrDefault() { + assertThat("10".toIntOrDefault(1)).describedAs("toIntOrDefault(10, 1)").isEqualTo(10) + assertThat("a".toIntOrDefault(2)).describedAs("toIntOrDefault(a, 2)").isEqualTo(2) + } + @Test fun testUnescapeXml() { assertThat(unescapeXml("<a name="test & ''">")).isEqualTo( diff --git a/version.properties b/version.properties index 5c5888f..a15c017 100644 --- a/version.properties +++ b/version.properties @@ -1,9 +1,9 @@ #Generated by the Semver Plugin for Gradle -#Fri May 14 21:44:23 PDT 2021 -version.buildmeta=787 +#Sat May 15 01:24:55 PDT 2021 +version.buildmeta=791 version.major=0 version.minor=8 version.patch=0 version.prerelease=beta version.project=mobibot -version.semver=0.8.0-beta+787 +version.semver=0.8.0-beta+791