Added more Utils extensions.

This commit is contained in:
Erik C. Thauvin 2021-05-15 01:26:31 -07:00
parent 8098a4855d
commit 2de9dc66bd
5 changed files with 35 additions and 15 deletions

View file

@ -616,7 +616,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
name = nickname name = nickname
ircServer = p.getProperty("server", Constants.DEFAULT_SERVER) 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 this.channel = channel
logsDir = logsDirPath logsDir = logsDirPath

View file

@ -45,6 +45,7 @@ import java.time.LocalDateTime
import java.time.ZoneId import java.time.ZoneId
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.util.Date import java.util.Date
import java.util.Properties
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import java.util.stream.Collectors import java.util.stream.Collectors
@ -119,12 +120,8 @@ object Utils {
* Returns a property as an int. * Returns a property as an int.
*/ */
@JvmStatic @JvmStatic
fun getIntProperty(property: String?, def: Int): Int { fun Properties.getIntProperty(key: String, defaultValue: Int): Int {
return try { return this.getProperty(key)?.toIntOrDefault(defaultValue) ?: defaultValue
property?.toInt() ?: def
} catch (nfe: NumberFormatException) {
def
}
} }
/** /**
@ -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. * Returns the specified date as an ISO local date string.
*/ */

View file

@ -34,10 +34,10 @@ package net.thauvin.erik.mobibot.commands.tell
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.buildCmdSyntax 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.helpFormat
import net.thauvin.erik.mobibot.Utils.plural import net.thauvin.erik.mobibot.Utils.plural
import net.thauvin.erik.mobibot.Utils.reverseColor 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.Utils.toUtcDateTime
import net.thauvin.erik.mobibot.commands.AbstractCommand import net.thauvin.erik.mobibot.commands.AbstractCommand
import net.thauvin.erik.mobibot.commands.links.View 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) { override fun setProperty(key: String, value: String) {
super.setProperty(key, value) super.setProperty(key, value)
if (MAX_DAYS_PROP == key) { if (MAX_DAYS_PROP == key) {
maxDays = getIntProperty(value, maxDays) maxDays = value.toIntOrDefault(maxDays)
} else if (MAX_SIZE_PROP == key) { } else if (MAX_SIZE_PROP == key) {
maxSize = getIntProperty(value, maxSize) maxSize = value.toIntOrDefault(maxSize)
} }
} }

View file

@ -41,6 +41,7 @@ import net.thauvin.erik.mobibot.Utils.obfuscate
import net.thauvin.erik.mobibot.Utils.plural import net.thauvin.erik.mobibot.Utils.plural
import net.thauvin.erik.mobibot.Utils.reverseColor import net.thauvin.erik.mobibot.Utils.reverseColor
import net.thauvin.erik.mobibot.Utils.toDir 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.toIsoLocalDate
import net.thauvin.erik.mobibot.Utils.toUtcDateTime import net.thauvin.erik.mobibot.Utils.toUtcDateTime
import net.thauvin.erik.mobibot.Utils.today import net.thauvin.erik.mobibot.Utils.today
@ -57,6 +58,7 @@ import java.io.IOException
import java.net.URL import java.net.URL
import java.time.LocalDateTime import java.time.LocalDateTime
import java.util.Calendar import java.util.Calendar
import java.util.Properties
/** /**
* The `Utils Test` class. * The `Utils Test` class.
@ -102,8 +104,11 @@ class UtilsTest {
@Test @Test
fun testGetIntProperty() { fun testGetIntProperty() {
assertThat(getIntProperty("10", 1)).describedAs("getIntProperty(10, 1)").isEqualTo(10) val p = Properties()
assertThat(getIntProperty("a", 1)).describedAs("getIntProperty(a, 1)").isEqualTo(1) 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 @Test
@ -151,6 +156,12 @@ class UtilsTest {
.isEqualTo("https://erik.thauvin.net/") .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 @Test
fun testUnescapeXml() { fun testUnescapeXml() {
assertThat(unescapeXml("<a name="test & ''">")).isEqualTo( assertThat(unescapeXml("<a name="test & ''">")).isEqualTo(

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle #Generated by the Semver Plugin for Gradle
#Fri May 14 21:44:23 PDT 2021 #Sat May 15 01:24:55 PDT 2021
version.buildmeta=787 version.buildmeta=791
version.major=0 version.major=0
version.minor=8 version.minor=8
version.patch=0 version.patch=0
version.prerelease=beta version.prerelease=beta
version.project=mobibot version.project=mobibot
version.semver=0.8.0-beta+787 version.semver=0.8.0-beta+791