From 19894dce1fbb2c64f55d9c2ce9d453ed020441b7 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 8 Dec 2020 12:29:47 -0800 Subject: [PATCH] Changed with(x) to x.apply --- config/detekt/baseline.xml | 2 +- src/main/java/net/thauvin/erik/mobibot/Addons.kt | 4 ++-- src/main/java/net/thauvin/erik/mobibot/FeedReader.kt | 2 +- src/main/java/net/thauvin/erik/mobibot/PinboardUtils.kt | 2 +- src/main/java/net/thauvin/erik/mobibot/Utils.kt | 3 +-- .../java/net/thauvin/erik/mobibot/commands/ChannelFeed.kt | 2 +- src/main/java/net/thauvin/erik/mobibot/commands/Cycle.kt | 2 +- src/main/java/net/thauvin/erik/mobibot/commands/Debug.kt | 2 +- src/main/java/net/thauvin/erik/mobibot/commands/Modules.kt | 2 +- .../java/net/thauvin/erik/mobibot/commands/tell/Tell.kt | 2 +- .../java/net/thauvin/erik/mobibot/entries/EntriesMgr.kt | 4 ++-- .../java/net/thauvin/erik/mobibot/entries/EntriesUtils.kt | 2 +- .../java/net/thauvin/erik/mobibot/modules/AbstractModule.kt | 2 +- .../net/thauvin/erik/mobibot/modules/CurrencyConverter.kt | 2 +- src/main/java/net/thauvin/erik/mobibot/modules/Dice.kt | 2 +- .../java/net/thauvin/erik/mobibot/modules/GoogleSearch.kt | 2 +- src/main/java/net/thauvin/erik/mobibot/modules/Lookup.kt | 4 ++-- .../net/thauvin/erik/mobibot/modules/RockPaperScissors.kt | 6 +++--- .../java/net/thauvin/erik/mobibot/modules/StockQuote.kt | 4 ++-- src/main/java/net/thauvin/erik/mobibot/modules/Twitter.kt | 6 +++--- src/main/java/net/thauvin/erik/mobibot/modules/Weather2.kt | 4 ++-- src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.kt | 2 +- 22 files changed, 31 insertions(+), 32 deletions(-) diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index bfccac4..65149d6 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -56,7 +56,7 @@ NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand, props: Properties) NestedBlockDepth:Comment.kt$Comment$override fun commandResponse( sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean ) NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) - NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$override fun helpResponse(sender: String, isPrivate: Boolean) : Boolean + NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$override fun helpResponse(sender: String, isPrivate: Boolean): Boolean NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$ @JvmStatic fun convertCurrency(query: String): Message NestedBlockDepth:EntriesMgr.kt$EntriesMgr$ @Throws(IOException::class, FeedException::class) fun loadEntries(file: String, channel: String, entries: MutableList<EntryLink>): String NestedBlockDepth:EntriesMgr.kt$EntriesMgr$ fun saveEntries( bot: Mobibot, entries: List<EntryLink>, history: MutableList<String>, isDayBackup: Boolean ) diff --git a/src/main/java/net/thauvin/erik/mobibot/Addons.kt b/src/main/java/net/thauvin/erik/mobibot/Addons.kt index 147aea0..a9b91b0 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Addons.kt +++ b/src/main/java/net/thauvin/erik/mobibot/Addons.kt @@ -49,7 +49,7 @@ class Addons { * Add a module with properties. */ fun add(module: AbstractModule, props: Properties) { - with(module) { + module.apply { if (hasProperties()) { propertyKeys.forEach { setProperty(it, props.getProperty(it, "")) @@ -68,7 +68,7 @@ class Addons { * Add a command with properties. */ fun add(command: AbstractCommand, props: Properties) { - with(command) { + command.apply { if (properties.isNotEmpty()) { properties.keys.forEach { setProperty(it, props.getProperty(it, "")) diff --git a/src/main/java/net/thauvin/erik/mobibot/FeedReader.kt b/src/main/java/net/thauvin/erik/mobibot/FeedReader.kt index 23b124a..0d437ef 100644 --- a/src/main/java/net/thauvin/erik/mobibot/FeedReader.kt +++ b/src/main/java/net/thauvin/erik/mobibot/FeedReader.kt @@ -54,7 +54,7 @@ class FeedReader( * Fetches the Feed's items. */ override fun run() { - with(bot) { + bot.apply { try { val input = SyndFeedInput() XmlReader(URL(url)).use { reader -> diff --git a/src/main/java/net/thauvin/erik/mobibot/PinboardUtils.kt b/src/main/java/net/thauvin/erik/mobibot/PinboardUtils.kt index addf76b..ad6cda7 100644 --- a/src/main/java/net/thauvin/erik/mobibot/PinboardUtils.kt +++ b/src/main/java/net/thauvin/erik/mobibot/PinboardUtils.kt @@ -80,7 +80,7 @@ object PinboardUtils { @JvmStatic fun updatePin(poster: PinboardPoster, ircServer: String, oldUrl: String, entry: EntryLink) = runBlocking { val update = GlobalScope.async { - with(entry) { + entry.apply { if (oldUrl != link) { poster.deletePin(oldUrl) poster.addPin( diff --git a/src/main/java/net/thauvin/erik/mobibot/Utils.kt b/src/main/java/net/thauvin/erik/mobibot/Utils.kt index 7bf1580..a165328 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Utils.kt +++ b/src/main/java/net/thauvin/erik/mobibot/Utils.kt @@ -38,7 +38,6 @@ import java.io.BufferedReader import java.io.File import java.io.IOException import java.io.InputStreamReader -import java.lang.NumberFormatException import java.net.URL import java.net.URLEncoder import java.nio.charset.StandardCharsets @@ -245,7 +244,7 @@ object Utils { val minutes = TimeUnit.MILLISECONDS.toMinutes(uptime) - TimeUnit.HOURS.toMinutes( TimeUnit.MILLISECONDS.toHours(uptime) ) - with(info) { + info.apply { if (years > 0) { append(years).append(plural(years, " year ", " years ")) } diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/ChannelFeed.kt b/src/main/java/net/thauvin/erik/mobibot/commands/ChannelFeed.kt index 3eee724..f183340 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/ChannelFeed.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/ChannelFeed.kt @@ -61,7 +61,7 @@ class ChannelFeed(bot: Mobibot, channel: String) : AbstractCommand(bot) { isOp: Boolean, isPrivate: Boolean ) { - with(properties[FEED_PROP]) { + properties[FEED_PROP].apply { if (!isNullOrBlank()) { Thread(FeedReader(bot, sender, this)).start() } else { diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/Cycle.kt b/src/main/java/net/thauvin/erik/mobibot/commands/Cycle.kt index 49832f2..6390b3f 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/Cycle.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/Cycle.kt @@ -51,7 +51,7 @@ class Cycle(bot: Mobibot) : AbstractCommand(bot) { isOp: Boolean, isPrivate: Boolean ) { - with(bot) { + bot.apply { if (isOp) { send("$sender has just asked me to leave. I'll be back!") sleep(wait) diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/Debug.kt b/src/main/java/net/thauvin/erik/mobibot/commands/Debug.kt index fa6b462..54b8d6e 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/Debug.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/Debug.kt @@ -46,7 +46,7 @@ class Debug(bot: Mobibot) : AbstractCommand(bot) { override fun commandResponse(sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean) { if (isOp) { - with(bot) { + bot.apply { if (logger.isDebugEnabled) { Configurator.setLevel(logger.name, loggerLevel) } else { diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/Modules.kt b/src/main/java/net/thauvin/erik/mobibot/commands/Modules.kt index a9493fa..d42aa33 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/Modules.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/Modules.kt @@ -52,7 +52,7 @@ class Modules(bot: Mobibot) : AbstractCommand(bot) { isOp: Boolean, isPrivate: Boolean ) { - with(bot) { + bot.apply { if (isOp) { if (modulesNames.isEmpty()) { send(sender, "There are no enabled modules.", isPrivate) diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/tell/Tell.kt b/src/main/java/net/thauvin/erik/mobibot/commands/tell/Tell.kt index 5627285..be1fb89 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/tell/Tell.kt +++ b/src/main/java/net/thauvin/erik/mobibot/commands/tell/Tell.kt @@ -33,9 +33,9 @@ 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.buildCmdSyntax import net.thauvin.erik.mobibot.Utils.plural import net.thauvin.erik.mobibot.Utils.reverseColor import net.thauvin.erik.mobibot.Utils.utcDateTime diff --git a/src/main/java/net/thauvin/erik/mobibot/entries/EntriesMgr.kt b/src/main/java/net/thauvin/erik/mobibot/entries/EntriesMgr.kt index 823d2ab..d4a63ac 100644 --- a/src/main/java/net/thauvin/erik/mobibot/entries/EntriesMgr.kt +++ b/src/main/java/net/thauvin/erik/mobibot/entries/EntriesMgr.kt @@ -103,7 +103,7 @@ object EntriesMgr { val items = feed.entries var entry: EntryLink for (i in items.indices.reversed()) { - with(items[i]) { + items[i].apply { entry = EntryLink( link, title, @@ -156,7 +156,7 @@ object EntriesMgr { var buff: StringBuilder var comment: EntryComment for (i in entries.size - 1 downTo 0) { - with(entries[i]) { + entries[i].apply { buff = StringBuilder() .append("Posted by ") .append(nick) diff --git a/src/main/java/net/thauvin/erik/mobibot/entries/EntriesUtils.kt b/src/main/java/net/thauvin/erik/mobibot/entries/EntriesUtils.kt index 5f83aa2..edbd5ac 100644 --- a/src/main/java/net/thauvin/erik/mobibot/entries/EntriesUtils.kt +++ b/src/main/java/net/thauvin/erik/mobibot/entries/EntriesUtils.kt @@ -61,7 +61,7 @@ object EntriesUtils { buff.append("[+").append(entry.comments.size).append(']') } buff.append(' ') - with(entry) { + entry.apply { if (Constants.NO_TITLE == title) { buff.append(title) } else { diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.kt b/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.kt index 4fe3d8d..726f5e2 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.kt @@ -74,7 +74,7 @@ abstract class AbstractModule(val bot: Mobibot) { /** * Responds with the module's help. */ - open fun helpResponse(sender: String, isPrivate: Boolean) : Boolean { + open fun helpResponse(sender: String, isPrivate: Boolean): Boolean { for (h in help) { bot.send(sender, Utils.buildCmdSyntax(h, bot.nick, isPrivateMsgEnabled && isPrivate), isPrivate) } diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt index 6d9587a..07e5bca 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt @@ -93,7 +93,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) { } } - override fun helpResponse(sender: String, isPrivate: Boolean) : Boolean { + override fun helpResponse(sender: String, isPrivate: Boolean): Boolean { bot.apply { if (EXCHANGE_RATES.isEmpty()) { try { diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Dice.kt b/src/main/java/net/thauvin/erik/mobibot/modules/Dice.kt index fb20e9d..ace71ae 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Dice.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Dice.kt @@ -49,7 +49,7 @@ class Dice(bot: Mobibot) : AbstractModule(bot) { val playerRoll = roll() val total = roll.first + roll.second val playerTotal = playerRoll.first + playerRoll.second - with(bot) { + bot.apply { send( channel, "$sender rolled two dice: ${Utils.bold(playerRoll.first)} and ${Utils.bold(playerRoll.second)}" diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.kt b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.kt index bbaec32..de38941 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.kt @@ -50,7 +50,7 @@ class GoogleSearch(bot: Mobibot) : ThreadedModule(bot) { * Searches Google. */ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) { - with(bot) { + bot.apply { if (args.isNotBlank()) { try { val results = searchGoogle( diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.kt b/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.kt index 71a3037..31dbc9f 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.kt @@ -50,7 +50,7 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) { isPrivate: Boolean ) { if (args.matches("(\\S.)+(\\S)+".toRegex())) { - with(bot) { + bot.apply { try { lookup(args).split(',').forEach { send(it.trim()) @@ -142,7 +142,7 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) { fun whois(query: String, host: String): List { val whoisClient = WhoisClient() val lines: List - with(whoisClient) { + whoisClient.apply { try { defaultTimeout = Constants.CONNECT_TIMEOUT connect(host) diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/RockPaperScissors.kt b/src/main/java/net/thauvin/erik/mobibot/modules/RockPaperScissors.kt index e22496a..11071c2 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/RockPaperScissors.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/RockPaperScissors.kt @@ -42,13 +42,13 @@ import kotlin.random.Random */ class RockPaperScissors(bot: Mobibot) : AbstractModule(bot) { init { - with(commands) { + commands.apply { add(Hands.ROCK.name.toLowerCase()) add(Hands.PAPER.name.toLowerCase()) add(Hands.SCISSORS.name.toLowerCase()) } - with(help) { + help.apply { add("To play Rock Paper Scissors:") add( Utils.helpFormat( @@ -96,7 +96,7 @@ class RockPaperScissors(bot: Mobibot) : AbstractModule(bot) { override fun commandResponse(sender: String, cmd: String, args: String, isPrivate: Boolean) { val hand = Hands.valueOf(cmd.toUpperCase()) val botHand = Hands.values()[Random.nextInt(0, Hands.values().size)] - with(bot) { + bot.apply { when { hand == botHand -> { send("${Utils.green(hand.name)} vs. ${Utils.green(botHand.name)}") diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.kt b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.kt index a43eca2..101e387 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.kt @@ -50,7 +50,7 @@ class StockQuote(bot: Mobibot) : ThreadedModule(bot) { * Returns the specified stock quote from Alpha Avantage. */ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) { - with(bot) { + bot.apply { if (args.isNotBlank()) { try { val messages = getQuote(args, properties[ALPHAVANTAGE_API_KEY_PROP]) @@ -128,7 +128,7 @@ class StockQuote(bot: Mobibot) : ThreadedModule(bot) { val messages = mutableListOf() var response: String try { - with(messages) { + messages.apply { // Search for symbol/keywords response = Utils.urlReader( URL( diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.kt b/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.kt index beb3793..4535329 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.kt @@ -86,7 +86,7 @@ class Twitter(bot: Mobibot) : ThreadedModule(bot) { * Send a notification to the registered Twitter handle. */ fun notification(msg: String) { - with(bot) { + bot.apply { if (isEnabled && !handle.isNullOrBlank()) { Thread { try { @@ -120,7 +120,7 @@ class Twitter(bot: Mobibot) : ThreadedModule(bot) { * Post an entry to twitter. */ fun postEntry(index: Int) { - with(bot) { + bot.apply { if (isAutoPost && hasEntry(index) && LinksMgr.entries.size >= index) { val entry = LinksMgr.entries[index] val msg = "${entry.title} ${entry.link} via ${entry.nick} on $channel" @@ -157,7 +157,7 @@ class Twitter(bot: Mobibot) : ThreadedModule(bot) { * Posts to twitter. */ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean) { - with(bot) { + bot.apply { try { send( sender, diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.kt b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.kt index a6133b4..3ee4532 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.kt @@ -124,7 +124,7 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) { messages.add( PublicMessage("City: ${cwd.cityName} [${country.toUpperCase()}]") ) - with(cwd.mainData) { + cwd.mainData.apply { if (this != null) { if (hasTemp()) { messages.add(PublicMessage("Temperature: ${getTemps(temp)}")) @@ -135,7 +135,7 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) { } } if (cwd.hasWindData()) { - with(cwd.windData) { + cwd.windData.apply { if (this != null && hasSpeed() && speed != null) { messages.add(NoticeMessage("Wind: ${wind(speed!!)}")) } diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.kt b/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.kt index ed7ad74..487cba1 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.kt @@ -188,7 +188,7 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) { args: String, isPrivate: Boolean ) { - with(bot) { + bot.apply { if (args.isEmpty()) { send(sender, "The supported countries/zones are: ", isPrivate) sendList(sender, ArrayList(COUNTRIES_MAP.keys), 17, isPrivate = false)