Implemented appendIfMissing and replaceEach extension functions to remove dependencies on StringUtils.

This commit is contained in:
Erik C. Thauvin 2021-07-30 01:19:31 -07:00
parent d8930a9520
commit 1535290290
7 changed files with 143 additions and 74 deletions

View file

@ -34,11 +34,11 @@ package net.thauvin.erik.mobibot
import net.thauvin.erik.mobibot.PinboardUtils.addPin
import net.thauvin.erik.mobibot.PinboardUtils.deletePin
import net.thauvin.erik.mobibot.PinboardUtils.updatePin
import net.thauvin.erik.mobibot.Utils.appendIfMissing
import net.thauvin.erik.mobibot.Utils.buildCmdSyntax
import net.thauvin.erik.mobibot.Utils.colorize
import net.thauvin.erik.mobibot.Utils.getIntProperty
import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.Utils.toDir
import net.thauvin.erik.mobibot.Utils.toIsoLocalDate
import net.thauvin.erik.mobibot.Utils.today
import net.thauvin.erik.mobibot.commands.AddLog
@ -95,6 +95,7 @@ import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.jibble.pircbot.PircBot
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
@ -586,7 +587,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
}
val nickname = p.getProperty("nick", Mobibot::class.java.name.lowercase())
val channel = p.getProperty("channel")
val logsDir = p.getProperty("logs", ".").toDir()
val logsDir = p.getProperty("logs", ".").appendIfMissing(File.separatorChar)
// Redirect stdout and stderr
if (!commandLine.hasOption(Constants.DEBUG_ARG[0])) {
@ -656,14 +657,14 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
// Set the URLs
weblogUrl = p.getProperty("weblog", "")
backlogsUrl = p.getProperty("backlogs", weblogUrl).toDir(true)
backlogsUrl = p.getProperty("backlogs", weblogUrl).appendIfMissing('/')
// Load the current entries and backlogs, if any
try {
LinksMgr.startup(logsDir + EntriesMgr.CURRENT_XML, logsDir + EntriesMgr.NAV_XML, this.channel)
if (logger.isDebugEnabled) logger.debug("Last feed: ${LinksMgr.startDate}")
} catch (e: Exception) {
logger.error("An error occurred while loading the logs.", e)
if (logger.isErrorEnabled) logger.error("An error occurred while loading the logs.", e)
}
// Set the pinboard authentication

View file

@ -31,7 +31,6 @@
*/
package net.thauvin.erik.mobibot
import org.apache.commons.lang3.StringUtils
import org.jibble.pircbot.Colors
import org.jsoup.Jsoup
import java.io.BufferedReader
@ -56,6 +55,18 @@ import java.util.stream.Collectors
object Utils {
private val searchFlags = arrayOf("%c", "%n")
/**
* Appends a suffix to the end of the String if not present.
*/
@JvmStatic
fun String.appendIfMissing(suffix: Char) : String {
return if (this.last() != suffix) {
"$this${suffix}"
} else {
this
}
}
/**
* Makes the given int bold.
*/
@ -81,7 +92,7 @@ object Utils {
@JvmStatic
fun buildCmdSyntax(text: String, botNick: String, isPrivate: Boolean): String {
val replace = arrayOf(if (isPrivate) "/msg $botNick" else "$botNick:", botNick)
return StringUtils.replaceEach(text, searchFlags, replace)
return text.replaceEach(searchFlags, replace)
}
/**
@ -95,7 +106,7 @@ object Utils {
*/
@JvmStatic
fun colorize(s: String?, color: String): String {
return if (s.isNullOrBlank()) {
return if (s.isNullOrEmpty()) {
Colors.NORMAL
} else if (Colors.BOLD == color || Colors.REVERSE == color) {
color + s + color
@ -136,7 +147,8 @@ object Utils {
@JvmStatic
@JvmOverloads
fun helpFormat(help: String, isBold: Boolean = true, isIndent: Boolean = true): String {
return (if (isIndent) " " else "").plus(if (isBold) bold(help) else help)
val s = if (isBold) bold(help) else help
return if (isIndent) s.prependIndent() else s
}
/**
@ -145,21 +157,16 @@ object Utils {
@JvmStatic
fun String.obfuscate(): String {
return if (this.isNotBlank()) {
StringUtils.repeat('x', this.length)
"x".repeat(this.length)
} else this
}
/**
* Returns the plural form of a word, if count > 1.
*/
fun String.plural(count: Int, plural: String): String = this.plural(count.toLong(), plural)
/**
* Returns the plural form of a word, if count > 1.
*/
@JvmStatic
fun String.plural(count: Long, plural: String): String {
return if (count > 1) plural else this
fun String.plural(count: Long): String {
return if (count > 1) "${this}s" else this
}
/**
@ -168,6 +175,20 @@ object Utils {
@JvmStatic
fun red(s: String?): String = colorize(s, Colors.RED)
/**
* Replaces all occurrences of Strings within another String.
*/
@JvmStatic
fun String.replaceEach(search: Array<out String>, replace: Array<out String>): String {
var result = this
if (search.size == replace.size) {
search.forEachIndexed { i, s ->
result = result.replace(s, replace[i])
}
}
return result
}
/**
* Makes the given string reverse color.
*/
@ -180,26 +201,6 @@ object Utils {
@JvmStatic
fun today(): String = LocalDateTime.now().toIsoLocalDate()
/**
* Ensures that the given location (File/URL) has a trailing slash (`/`) to indicate a directory.
*/
@JvmStatic
fun String.toDir(isUrl: Boolean = false): String {
return if (isUrl) {
if (this.last() == '/') {
this
} else {
"$this/"
}
} else {
if (this.last() == File.separatorChar) {
this
} else {
this + File.separatorChar
}
}
}
/**
* Converts a string to an int.
*/
@ -266,21 +267,23 @@ object Utils {
with(info) {
if (years > 0) {
append(years).append(" year ".plural(years, " years "))
append(years).append(" year".plural(years)).append(' ')
}
if (months > 0) {
append(weeks).append(" month ".plural(months, " months "))
append(weeks).append(" month".plural(months)).append(' ')
}
if (weeks > 0) {
append(weeks).append(" week ".plural(weeks, " weeks "))
append(weeks).append(" week".plural(weeks)).append(' ')
}
if (days > 0) {
append(days).append(" day ".plural(days, " days "))
append(days).append(" day".plural(days)).append(' ')
}
if (hours > 0) {
append(hours).append(" hour ".plural(hours, " hours "))
append(hours).append(" hour".plural(hours)).append(' ')
}
append(minutes).append(" minute ".plural(minutes, " minutes"))
append(minutes).append(" minute".plural(minutes))
return toString()
}
}

View file

@ -122,7 +122,7 @@ class Tell(bot: Mobibot) : AbstractCommand(bot) {
helpFormat("%c $name <nick> <message>"),
"To view queued and sent messages:",
helpFormat("%c $name ${View.VIEW_CMD}"),
"Messages are kept for ${bold(maxDays)}" + " day.".plural(maxDays, " days.")
"Messages are kept for ${bold(maxDays)}" + " day".plural(maxDays.toLong()) + '.'
)
override val isOp: Boolean = false
override val isPublic: Boolean = isEnabled()

View file

@ -34,7 +34,6 @@ package net.thauvin.erik.mobibot.entries
import com.rometools.rome.feed.synd.SyndCategory
import com.rometools.rome.feed.synd.SyndCategoryImpl
import net.thauvin.erik.mobibot.commands.links.LinksMgr
import org.apache.commons.lang3.StringUtils
import java.io.Serializable
import java.util.Calendar
import java.util.Date
@ -144,9 +143,11 @@ class EntryLink : Serializable {
* Returns true if a string is contained in the link, title, or nick.
*/
fun matches(match: String?): Boolean {
return (StringUtils.containsIgnoreCase(link, match)
|| StringUtils.containsIgnoreCase(title, match)
|| StringUtils.containsIgnoreCase(nick, match))
return if (match.isNullOrEmpty()) {
false
} else {
link.contains(match, true) || title.contains(match, true) || nick.contains(match, true)
}
}
/**

View file

@ -40,7 +40,6 @@ import net.thauvin.erik.mobibot.Utils.today
import net.thauvin.erik.mobibot.msg.ErrorMessage
import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.PublicMessage
import org.apache.commons.lang3.StringUtils
import org.jdom2.JDOMException
import org.jdom2.input.SAXBuilder
import java.io.IOException
@ -193,7 +192,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
val rates = mutableListOf<String>()
for ((key, value) in EXCHANGE_RATES) {
@Suppress("MagicNumber")
rates.add(" $key: ${StringUtils.leftPad(value, 8)}")
rates.add(" $key: ${value.padStart(8)}")
}
return rates
}

View file

@ -32,7 +32,7 @@
package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Utils.obfuscate
import org.apache.commons.lang3.StringUtils
import net.thauvin.erik.mobibot.Utils.replaceEach
/**
* The `ModuleException` class.
@ -70,11 +70,11 @@ class ModuleException : Exception {
fun getSanitizedMessage(vararg sanitize: String): String {
val obfuscate = sanitize.map { it.obfuscate() }.toTypedArray()
return when {
cause != null -> {
cause.javaClass.name + ": " + StringUtils.replaceEach(cause.message, sanitize, obfuscate)
cause?.message != null -> {
cause.javaClass.name + ": " + cause.message!!.replaceEach(sanitize, obfuscate)
}
message != null -> {
message.javaClass.name + ": " + StringUtils.replaceEach(message, sanitize, obfuscate)
message.javaClass.name + ": " + message.replaceEach(sanitize, obfuscate)
}
else -> ""
}