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
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

View file

@ -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.
*/

View file

@ -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)
}
}

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.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(

View file

@ -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