From 89f64cd9f52b4c3cd153f15020a1ba60b19506bf Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 17 Jul 2016 02:35:05 -0700 Subject: [PATCH] Extensive javadoc cleanup. Started to convert to the java.time --- build.gradle | 2 + .../net/thauvin/erik/mobibot/ReleaseInfo.java | 2 +- .../net/thauvin/erik/mobibot/Commands.java | 4 +- .../thauvin/erik/mobibot/DeliciousPoster.java | 2 +- .../net/thauvin/erik/mobibot/EntriesMgr.java | 8 +- .../thauvin/erik/mobibot/EntryComment.java | 10 +- .../net/thauvin/erik/mobibot/EntryLink.java | 86 +++++------ .../net/thauvin/erik/mobibot/FeedReader.java | 22 +-- .../net/thauvin/erik/mobibot/Mobibot.java | 143 +++++------------- .../java/net/thauvin/erik/mobibot/Tell.java | 42 ++--- .../net/thauvin/erik/mobibot/TellMessage.java | 24 +-- .../thauvin/erik/mobibot/TellMessagesMgr.java | 15 +- .../thauvin/erik/mobibot/TwitterOAuth.java | 4 +- .../java/net/thauvin/erik/mobibot/Utils.java | 69 ++++++--- .../erik/mobibot/modules/AbstractModule.java | 4 +- .../thauvin/erik/mobibot/modules/Calc.java | 4 +- .../mobibot/modules/CurrencyConverter.java | 18 +-- .../thauvin/erik/mobibot/modules/Dice.java | 4 +- .../erik/mobibot/modules/GoogleSearch.java | 25 +-- .../thauvin/erik/mobibot/modules/Joke.java | 8 +- .../thauvin/erik/mobibot/modules/Lookup.java | 10 +- .../thauvin/erik/mobibot/modules/Ping.java | 12 +- .../erik/mobibot/modules/StockQuote.java | 8 +- .../thauvin/erik/mobibot/modules/Twitter.java | 13 +- .../net/thauvin/erik/mobibot/modules/War.java | 14 +- .../thauvin/erik/mobibot/modules/Weather.java | 16 +- .../erik/mobibot/modules/WorldTime.java | 62 +++----- 27 files changed, 252 insertions(+), 379 deletions(-) diff --git a/build.gradle b/build.gradle index b58a31c..1c4552c 100644 --- a/build.gradle +++ b/build.gradle @@ -87,6 +87,8 @@ compileJava { javadoc { options.tags = ['created'] + options.author = true + options.links('http://www.jibble.org/javadocs/pircbot/', 'http://docs.oracle.com/javase/8/docs/api/') } jar { diff --git a/src/generated/java/net/thauvin/erik/mobibot/ReleaseInfo.java b/src/generated/java/net/thauvin/erik/mobibot/ReleaseInfo.java index 65a0955..28f2e24 100644 --- a/src/generated/java/net/thauvin/erik/mobibot/ReleaseInfo.java +++ b/src/generated/java/net/thauvin/erik/mobibot/ReleaseInfo.java @@ -14,7 +14,7 @@ import java.util.Date; */ public final class ReleaseInfo { private final static String buildmeta = "008"; - private final static Date date = new Date(1468718052505L); + private final static Date date = new Date(1468747036593L); private final static int major = 0; private final static int minor = 7; private final static int patch = 0; diff --git a/src/main/java/net/thauvin/erik/mobibot/Commands.java b/src/main/java/net/thauvin/erik/mobibot/Commands.java index 4ec43ba..5a41e43 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Commands.java +++ b/src/main/java/net/thauvin/erik/mobibot/Commands.java @@ -34,11 +34,11 @@ package net.thauvin.erik.mobibot; /** * The commands, keywords and arguments. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2014-04-26 * @since 1.0 */ -final class Commands { +final public class Commands { /** * The add (back)log command. */ diff --git a/src/main/java/net/thauvin/erik/mobibot/DeliciousPoster.java b/src/main/java/net/thauvin/erik/mobibot/DeliciousPoster.java index 181a2f4..401b049 100644 --- a/src/main/java/net/thauvin/erik/mobibot/DeliciousPoster.java +++ b/src/main/java/net/thauvin/erik/mobibot/DeliciousPoster.java @@ -38,7 +38,7 @@ import javax.swing.*; /** * The class to handle posts to del.icio.us. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created Mar 5, 2005 * @since 1.0 */ diff --git a/src/main/java/net/thauvin/erik/mobibot/EntriesMgr.java b/src/main/java/net/thauvin/erik/mobibot/EntriesMgr.java index daa8890..77241e8 100644 --- a/src/main/java/net/thauvin/erik/mobibot/EntriesMgr.java +++ b/src/main/java/net/thauvin/erik/mobibot/EntriesMgr.java @@ -44,7 +44,7 @@ import java.util.List; /** * Manages the feed entries. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2014-04-28 * @since 1.0 */ @@ -64,9 +64,7 @@ final class EntriesMgr { */ public static final String XML_EXT = ".xml"; - /** - * The maximum number of backlogs to keep. - */ + // The maximum number of backlogs to keep. private static final int MAX_BACKLOGS = 10; /** @@ -129,7 +127,7 @@ final class EntriesMgr { try (InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(file)))) { final SyndFeed feed = input.build(reader); - today = Utils.ISO_SDF.format(feed.getPublishedDate()); + today = Utils.isoLocalDate(feed.getPublishedDate()); final List items = feed.getEntries(); SyndEntry item; diff --git a/src/main/java/net/thauvin/erik/mobibot/EntryComment.java b/src/main/java/net/thauvin/erik/mobibot/EntryComment.java index 826deac..f8aaee1 100644 --- a/src/main/java/net/thauvin/erik/mobibot/EntryComment.java +++ b/src/main/java/net/thauvin/erik/mobibot/EntryComment.java @@ -38,19 +38,15 @@ import java.util.Date; /** * The class used to store comments associated to a specific entry. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created Jan 31, 2004 * @since 1.0 */ public class EntryComment implements Serializable { - /** - * The serial version UID. - */ + // The serial version UID. static final long serialVersionUID = 6957415292233553224L; - /** - * The creation date. - */ + // The creation date. private final Date date = Calendar.getInstance().getTime(); private String comment = ""; diff --git a/src/main/java/net/thauvin/erik/mobibot/EntryLink.java b/src/main/java/net/thauvin/erik/mobibot/EntryLink.java index 2be2d11..81fe20e 100644 --- a/src/main/java/net/thauvin/erik/mobibot/EntryLink.java +++ b/src/main/java/net/thauvin/erik/mobibot/EntryLink.java @@ -43,14 +43,12 @@ import java.util.concurrent.CopyOnWriteArrayList; /** * The class used to store link entries. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created Jan 31, 2004 * @since 1.0 */ public class EntryLink implements Serializable { - /** - * The serial version UID. - */ + // The serial version UID. static final long serialVersionUID = 3676245542270899086L; // The link's comments @@ -282,10 +280,44 @@ public class EntryLink implements Serializable { /** * Sets the tags. * - * @param tags The tags. + * @param tags The space-delimited tags. */ - private void setTags(final List tags) { - this.tags.addAll(tags); + public final void setTags(final String tags) { + if (tags != null) { + final String[] parts = tags.replaceAll(", ", " ").replaceAll(",", " ").split(" "); + + SyndCategoryImpl tag; + String part; + char mod; + + for (final String rawPart : parts) { + part = rawPart.trim(); + + if (part.length() >= 2) { + tag = new SyndCategoryImpl(); + tag.setName(part.substring(1).toLowerCase()); + + mod = part.charAt(0); + + if (mod == '-') { + // Don't remove the channel tag, if any. + if (!tag.getName().equals(channel.substring(1))) { + this.tags.remove(tag); + } + } else if (mod == '+') { + if (!this.tags.contains(tag)) { + this.tags.add(tag); + } + } else { + tag.setName(part.trim().toLowerCase()); + + if (!this.tags.contains(tag)) { + this.tags.add(tag); + } + } + } + } + } } /** @@ -340,44 +372,10 @@ public class EntryLink implements Serializable { /** * Sets the tags. * - * @param tags The space-delimited tags. + * @param tags The tags. */ - public final void setTags(final String tags) { - if (tags != null) { - final String[] parts = tags.replaceAll(", ", " ").replaceAll(",", " ").split(" "); - - SyndCategoryImpl tag; - String part; - char mod; - - for (final String rawPart : parts) { - part = rawPart.trim(); - - if (part.length() >= 2) { - tag = new SyndCategoryImpl(); - tag.setName(part.substring(1).toLowerCase()); - - mod = part.charAt(0); - - if (mod == '-') { - // Don't remove the channel tag, if any. - if (!tag.getName().equals(channel.substring(1))) { - this.tags.remove(tag); - } - } else if (mod == '+') { - if (!this.tags.contains(tag)) { - this.tags.add(tag); - } - } else { - tag.setName(part.trim().toLowerCase()); - - if (!this.tags.contains(tag)) { - this.tags.add(tag); - } - } - } - } - } + private void setTags(final List tags) { + this.tags.addAll(tags); } /** diff --git a/src/main/java/net/thauvin/erik/mobibot/FeedReader.java b/src/main/java/net/thauvin/erik/mobibot/FeedReader.java index ee8b4a5..92808e4 100644 --- a/src/main/java/net/thauvin/erik/mobibot/FeedReader.java +++ b/src/main/java/net/thauvin/erik/mobibot/FeedReader.java @@ -44,34 +44,24 @@ import java.util.List; /** * Reads a RSS feed. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created Feb 1, 2004 * @since 1.0 */ class FeedReader implements Runnable { - /** - * The maximum number of feed items to display. - */ + // The maximum number of feed items to display. private static final int MAX_ITEMS = 5; - /** - * The tab indent (4 spaces). - */ + // The tab indent (4 spaces). private static final String TAB_INDENT = " "; - /** - * The bot. - */ + // The bot. private final Mobibot bot; - /** - * The nick of the person who sent the message. - */ + // The nick of the person who sent the message. private final String sender; - /** - * The URL to fetch. - */ + // The URL to fetch. private final String url; /** diff --git a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java index 4eb851d..e2b0e74 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java +++ b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java @@ -43,12 +43,14 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.*; +import java.time.Clock; +import java.time.LocalDateTime; import java.util.*; /** * Implements the #mobitopia bot. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created Jan 31, 2004 * @since 1.0 */ @@ -59,72 +61,48 @@ public class Mobibot extends PircBot { */ public static final int CONNECT_TIMEOUT = 5000; - /** - * The empty title string. - */ + // The empty title string. static final String NO_TITLE = "No Title"; - /** - * The default port. - */ + // The default port. private static final int DEFAULT_PORT = 6667; - /** - * The info strings. - */ + // The info strings. private static final String[] INFO_STRS = { ReleaseInfo.getProject() + " v" + ReleaseInfo.getVersion() + " by Erik C. Thauvin (erik@thauvin.net)", "http://www.mobitopia.org/mobibot/" }; - /** - * The link match string. - */ + // The link match string. private static final String LINK_MATCH = "^[hH][tT][tT][pP](|[sS])://.*"; - /** - * The default maximum number of entries to display. - */ + // The default maximum number of entries to display. private static final int MAX_ENTRIES = 8; - /** - * The default maximum recap entries. - */ + // The default maximum recap entries. private static final int MAX_RECAP = 10; - /** - * The maximum number of times the bot will try to reconnect, if disconnected. - */ + // The maximum number of times the bot will try to reconnect, if disconnected. private static final int MAX_RECONNECT = 10; - /** - * The number of milliseconds to delay between consecutive messages. - */ + // The number of milliseconds to delay between consecutive messages. private static final long MESSAGE_DELAY = 1000L; - /** - * The modules. - */ + // The modules. private static final List MODULES = new ArrayList<>(0); - /** - * The start time. - */ + // The start time. private static final long START_TIME = System.currentTimeMillis(); - /** - * The tags/categories marker. - */ + // The tags/categories marker. private static final String TAGS_MARKER = "tags:"; - /** - * The version strings. - */ + // The version strings. private static final String[] VERSION_STRS = { "Version: " + ReleaseInfo.getVersion() + " (" - + Utils.ISO_SDF.format(ReleaseInfo.getBuildDate()) + ')', + + Utils.isoLocalDate(ReleaseInfo.getBuildDate()) + ')', "Platform: " + System.getProperty("os.name") + " (" @@ -147,109 +125,67 @@ public class Mobibot extends PircBot { + ')' }; - /** - * The tell object. - */ + // The tell object. private static Tell tell; - /** - * The main channel. - */ + // The main channel. private final String channel; - /** - * The commands list. - */ + // The commands list. private final List commandsList = new ArrayList<>(); - /** - * The entries array. - */ + // The entries array. private final List entries = new ArrayList<>(0); - /** - * The history/backlogs array. - */ + // The history/backlogs array. private final List history = new ArrayList<>(0); - /** - * The ignored nicks array. - */ + // The ignored nicks array. private final List ignoredNicks = new ArrayList<>(0); - /** - * The IRC port. - */ + // The IRC port. private final int ircPort; - /** - * The IRC server. - */ + // The IRC server. private final String ircServer; - /** - * The logger. - */ + // The logger. private final Log4JLogger logger = new Log4JLogger(Mobibot.class.getPackage().getName()); - /** - * The logger default level. - */ + // The logger default level. private final Level loggerLevel; - /** - * The log directory. - */ + // The log directory. private final String logsDir; - /** - * The recap array. - */ + // The recap array. private final List recap = new ArrayList<>(0); - /** - * The backlogs URL. - */ + // The backlogs URL. private String backLogsUrl = ""; - /** - * The default tags/categories. - */ + // The default tags/categories. private String defaultTags = ""; - /** - * The del.icio.us posts handler. - */ + // The del.icio.us posts handler. private DeliciousPoster delicious = null; - /** - * The feed URL. - */ + // The feed URL. private String feedURL = ""; - /** - * The ident message. - */ + // The ident message. private String identMsg = ""; - /** - * The ident nick. - */ + // The ident nick. private String identNick = ""; - /** - * The NickServ ident password. - */ + // The NickServ ident password. private String identPwd = ""; - /** - * Today's date. - */ + // Today's date. private String today = Utils.today(); - /** - * The weblog URL. - */ + // The weblog URL. private String weblogUrl = ""; /** @@ -280,9 +216,6 @@ public class Mobibot extends PircBot { // Set the logger level loggerLevel = logger.getLogger().getLevel(); - // Initialization - Utils.UTC_SDF.setTimeZone(TimeZone.getTimeZone("UTC")); - // Load the current entries, if any. try { today = EntriesMgr.loadEntries(this.logsDir + EntriesMgr.CURRENT_XML, this.channel, entries); @@ -1600,7 +1533,7 @@ public class Mobibot extends PircBot { * @param isAction Set to true if the message is an action. */ private void storeRecap(final String sender, final String message, final boolean isAction) { - recap.add(Utils.UTC_SDF.format(Calendar.getInstance().getTime()) + " -> " + sender + (isAction ? " " : ": ") + recap.add(Utils.utcDateTime(LocalDateTime.now(Clock.systemUTC())) + " -> " + sender + (isAction ? " " : ": ") + message); if (recap.size() > MAX_RECAP) { diff --git a/src/main/java/net/thauvin/erik/mobibot/Tell.java b/src/main/java/net/thauvin/erik/mobibot/Tell.java index fd8858c..b34a47a 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Tell.java +++ b/src/main/java/net/thauvin/erik/mobibot/Tell.java @@ -37,7 +37,7 @@ import java.util.concurrent.CopyOnWriteArrayList; /** * The Tell command. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2016-07-02 * @since 1.0 */ @@ -57,44 +57,28 @@ public class Tell { */ public static final String TELL_DEL_KEYWORD = "del"; - /** - * The default maximum number of days to keep messages. - */ + // The default maximum number of days to keep messages. private static final int DEFAULT_TELL_MAX_DAYS = 7; - /** - * The default message max queue size. - */ + // The default message max queue size. private static final int DEFAULT_TELL_MAX_SIZE = 50; - /** - * The serialized object file extension. - */ + // The serialized object file extension. private static final String SER_EXT = ".ser"; - /** - * The bot instance. - */ + // The bot instance. final private Mobibot bot; - /** - * The maximum number of days to keep messages. - */ + // The maximum number of days to keep messages. final private int maxDays; - /** - * The message maximum queue size. - */ + // The message maximum queue size. final private int maxSize; - /** - * The messages queue. - */ + // The messages queue. private final List messages = new CopyOnWriteArrayList<>(); - /** - * The serialized object file. - */ + // The serialized object file. private final String serializedObject; public Tell(final Mobibot bot, final String maxDays, final String maxSize) { @@ -114,7 +98,7 @@ public class Tell { /** * Cleans the messages queue. * - * @return True if the queue was cleaned. + * @return true if the queue was cleaned. */ private boolean clean() { if (bot.getLogger().isDebugEnabled()) { @@ -182,14 +166,14 @@ public class Tell { if (message.isReceived()) { bot.send(sender, Utils.bold(message.getSender()) + " --> " + Utils.bold(message.getRecipient()) - + " [" + Utils.UTC_SDF.format(message.getReceived()) + ", ID: " + + " [" + Utils.utcDateTime(message.getReceived()) + ", ID: " + message.getId() + ", DELIVERED]", true); } else { bot.send(sender, Utils.bold(message.getSender()) + " --> " + Utils.bold(message.getRecipient()) - + " [" + Utils.UTC_SDF.format(message.getQueued()) + ", ID: " + + " [" + Utils.utcDateTime(message.getQueued()) + ", ID: " + message.getId() + ", QUEUED]", true); } @@ -326,7 +310,7 @@ public class Tell { "Your message " + Utils.reverseColor("[ID " + message.getId() + ']') + " was sent to " + Utils.bold(message.getRecipient()) + " on " - + Utils.UTC_SDF.format(message.getReceived()), + + Utils.utcDateTime(message.getReceived()), true); message.setIsNotified(); diff --git a/src/main/java/net/thauvin/erik/mobibot/TellMessage.java b/src/main/java/net/thauvin/erik/mobibot/TellMessage.java index dc64bc0..db54230 100644 --- a/src/main/java/net/thauvin/erik/mobibot/TellMessage.java +++ b/src/main/java/net/thauvin/erik/mobibot/TellMessage.java @@ -32,26 +32,27 @@ package net.thauvin.erik.mobibot; import java.io.Serializable; -import java.util.Calendar; -import java.util.Date; +import java.time.Clock; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; /** * The TellMessage class. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2014-04-24 * @since 1.0 */ public class TellMessage implements Serializable { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 2L; private final String id; private final String message; - final private Date queued; + final private LocalDateTime queued; private final String recipient; private final String sender; private boolean isNotified; private boolean isReceived; - private Date received; + private LocalDateTime received; /** * Create a new message. @@ -65,9 +66,8 @@ public class TellMessage implements Serializable { this.recipient = recipient; this.message = message; - this.queued = Calendar.getInstance().getTime(); - this.id = Utils.TIMESTAMP_SDF.format(this.queued); - + this.queued = LocalDateTime.now(Clock.systemUTC()); + this.id = this.queued.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); } /** @@ -93,7 +93,7 @@ public class TellMessage implements Serializable { * * @return true if the message is queued. */ - public Date getQueued() { + public LocalDateTime getQueued() { return queued; } @@ -102,7 +102,7 @@ public class TellMessage implements Serializable { * * @return true if the message has been received. */ - public Date getReceived() { + public LocalDateTime getReceived() { return received; } @@ -173,7 +173,7 @@ public class TellMessage implements Serializable { * Sets the received flag. */ public void setIsReceived() { - received = Calendar.getInstance().getTime(); + received = LocalDateTime.now(Clock.systemUTC()); isReceived = true; } } \ No newline at end of file diff --git a/src/main/java/net/thauvin/erik/mobibot/TellMessagesMgr.java b/src/main/java/net/thauvin/erik/mobibot/TellMessagesMgr.java index 37e0346..d170e37 100644 --- a/src/main/java/net/thauvin/erik/mobibot/TellMessagesMgr.java +++ b/src/main/java/net/thauvin/erik/mobibot/TellMessagesMgr.java @@ -34,15 +34,15 @@ package net.thauvin.erik.mobibot; import org.apache.commons.logging.impl.Log4JLogger; import java.io.*; +import java.time.Clock; +import java.time.LocalDateTime; import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; import java.util.List; /** * The Tell Messages Manager. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2014-04-26 * @since 1.0 */ @@ -65,15 +65,12 @@ final class TellMessagesMgr { * @return True if the queue was cleaned. */ public static boolean clean(final List tellMessages, final int tellMaxDays) { - final Calendar maxDate = Calendar.getInstance(); - final Date today = new Date(); + final LocalDateTime today = LocalDateTime.now(Clock.systemUTC()); boolean cleaned = false; for (final TellMessage message : tellMessages) { - maxDate.setTime(message.getQueued()); - maxDate.add(Calendar.DATE, tellMaxDays); - - if (maxDate.getTime().before(today)) { + final LocalDateTime maxDate = message.getQueued().plusDays(tellMaxDays); + if (maxDate.isBefore(today)) { tellMessages.remove(message); cleaned = true; } diff --git a/src/main/java/net/thauvin/erik/mobibot/TwitterOAuth.java b/src/main/java/net/thauvin/erik/mobibot/TwitterOAuth.java index 5f3b492..dcaa667 100644 --- a/src/main/java/net/thauvin/erik/mobibot/TwitterOAuth.java +++ b/src/main/java/net/thauvin/erik/mobibot/TwitterOAuth.java @@ -21,8 +21,8 @@ import java.io.InputStreamReader; *

* and follow the prompts/instructions. * - * @author Erik C. Thauvin - * @author http://twitter4j.org/en/code-examples.html#oauth + * @author Erik C. Thauvin + * @author Twitter4J * @created Sep 13, 2010 * @since 1.0 */ diff --git a/src/main/java/net/thauvin/erik/mobibot/Utils.java b/src/main/java/net/thauvin/erik/mobibot/Utils.java index 99586fc..903dec7 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Utils.java +++ b/src/main/java/net/thauvin/erik/mobibot/Utils.java @@ -34,30 +34,19 @@ package net.thauvin.erik.mobibot; import org.jibble.pircbot.Colors; import java.io.File; -import java.text.SimpleDateFormat; -import java.util.Calendar; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.Date; /** * Miscellaneous utilities class. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2014-04-26 * @since 1.0 */ final public class Utils { - /** - * The ISO (YYYY-MM-DD) simple date format. - */ - public static final SimpleDateFormat ISO_SDF = new SimpleDateFormat("yyyy-MM-dd"); - /** - * The timestamp simple date format. - */ - public static final SimpleDateFormat TIMESTAMP_SDF = new SimpleDateFormat("yyyyMMddHHmmss"); - /** - * The UTC (yyyy-MM-dd HH:mm) simple date format. - */ - public static final SimpleDateFormat UTC_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm"); - /** * Disables the default constructor. * @@ -225,12 +214,33 @@ final public class Utils { return false; } + /** + * Returns the specified date as an ISO local date string. + * + * @param date The date. + * @return The date in {@link DateTimeFormatter#ISO_LOCAL_DATE} format. + */ + public static String isoLocalDate(final Date date) { + return isoLocalDate(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault())); + } + + /** + * Returns the specified date as an ISO local date string. + * + * @param date The date. + * @return The date in {@link DateTimeFormatter#ISO_LOCAL_DATE} format. + */ + public static String isoLocalDate(final LocalDateTime date) { + return date.format(DateTimeFormatter.ISO_LOCAL_DATE); + } + /** * Returns the plural form of a word, if count > 1. * * @param count The count. * @param word The word. * @param plural The plural word. + * @return The plural string. */ public static String plural(final long count, final String word, final String plural) { if (count > 1) { @@ -253,10 +263,10 @@ final public class Utils { /** * Returns today's date. * - * @return Today's date in {@link #ISO_SDF ISO} format. + * @return Today's date in {@link DateTimeFormatter#ISO_LOCAL_DATE} format. */ public static String today() { - return ISO_SDF.format(Calendar.getInstance().getTime()); + return isoLocalDate(LocalDateTime.now()); } /** @@ -275,4 +285,25 @@ final public class Utils { return s; } -} + + /** + * Returns the specified date formatted as yyyy-MM-dd HH:mm + * + * @param date The date. + * @return The fromatted date. + */ + public static String utcDateTime(final Date date) { + return utcDateTime(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault())); + } + + /** + * Returns the specified date formatted as yyyy-MM-dd HH:mm + * + * @param date The date. + * @return The fromatted date. + */ + public static String utcDateTime(final LocalDateTime date) { + return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); + } + +} \ No newline at end of file diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.java b/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.java index 678ac99..46b73d2 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/AbstractModule.java @@ -39,7 +39,7 @@ import java.util.*; /** * The Module abstract class. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2016-07-01 * @since 1.0 */ @@ -110,7 +110,7 @@ public abstract class AbstractModule { } /** - * Returns true if the module responds to private messages. + * Returns true if the module responds to private messages. * * @return true or false */ diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Calc.java b/src/main/java/net/thauvin/erik/mobibot/modules/Calc.java index 233ae79..0cd95b8 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Calc.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Calc.java @@ -41,7 +41,7 @@ import java.text.DecimalFormat; /** * The Calc module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2016-07-01 * @since 1.0 */ @@ -49,7 +49,7 @@ public class Calc extends AbstractModule { /** * The Calc command. */ - private static final String CALC_CMD = "calc"; + public static final String CALC_CMD = "calc"; /** * The default constructor. diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java index e66b343..d393361 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java @@ -50,7 +50,7 @@ import java.util.TreeMap; /** * The CurrentConverter module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created Feb 11, 2004 * @since 1.0 */ @@ -58,26 +58,20 @@ final public class CurrencyConverter extends AbstractModule { /** * The currency command. */ - private static final String CURRENCY_CMD = "currency"; + public static final String CURRENCY_CMD = "currency"; /** * The rates keyword. */ - private static final String CURRENCY_RATES_KEYWORD = "rates"; + public static final String CURRENCY_RATES_KEYWORD = "rates"; - /** - * The exchange rates. - */ + // The exchange rates. private static final Map EXCHANGE_RATES = new TreeMap<>(); - /** - * The exchange rates table URL. - */ + // The exchange rates table URL. private static final String EXCHANGE_TABLE_URL = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"; - /** - * The last exchange rates table publication date. - */ + // The last exchange rates table publication date. private static String pubDate = ""; /** diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Dice.java b/src/main/java/net/thauvin/erik/mobibot/modules/Dice.java index b13c989..b80d1e0 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Dice.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Dice.java @@ -39,7 +39,7 @@ import java.util.Random; /** * The Dice module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2014-04-28 * @since 1.0 */ @@ -47,7 +47,7 @@ final public class Dice extends AbstractModule { /** * The dice command. */ - private final String DICE_CMD = "dice"; + public static final String DICE_CMD = "dice"; /** * The default constructor. diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java index 85f2bec..b30b796 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java @@ -45,29 +45,22 @@ import java.net.URLEncoder; /** * The GoogleSearch module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created Feb 7, 2004 * @since 1.0 */ final public class GoogleSearch extends AbstractModule { - /** - * The Google API Key property. - */ - private static final String GOOGLE_API_KEY_PROP = "google-api-key"; - /** * The google command. */ - private static final String GOOGLE_CMD = "google"; + public static final String GOOGLE_CMD = "google"; - /** - * The Google Custom Search Engine ID property. - */ + // The Google API Key property. + private static final String GOOGLE_API_KEY_PROP = "google-api-key"; + // The Google Custom Search Engine ID property. private static final String GOOGLE_CSE_KEY_PROP = "google-cse-cx"; - /** - * The tab indent (4 spaces). - */ + // The tab indent (4 spaces). private static final String TAB_INDENT = " "; /** @@ -82,11 +75,7 @@ final public class GoogleSearch extends AbstractModule { @Override public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { if (isEnabled() && args.length() > 0) { - if (args.length() > 0) { - new Thread(() -> run(bot, sender, args)).start(); - } else { - helpResponse(bot, sender, args, isPrivate); - } + new Thread(() -> run(bot, sender, args)).start(); } else { helpResponse(bot, sender, args, isPrivate); } diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java b/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java index d945f85..cdbcdaf 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java @@ -43,7 +43,7 @@ import java.net.URLConnection; /** * The Joke module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2014-04-20 * @since 1.0 */ @@ -51,11 +51,9 @@ final public class Joke extends AbstractModule { /** * The joke command. */ - private static final String JOKE_CMD = "joke"; + public static final String JOKE_CMD = "joke"; - /** - * The ICNDB URL. - */ + // The ICNDB URL. private static final String JOKE_URL = "http://api.icndb.com/jokes/random?escape=javascript&exclude=[explicit]&limitTo=[nerdy]"; diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.java b/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.java index cf67caa..3712111 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.java @@ -41,19 +41,17 @@ import java.net.UnknownHostException; /** * The Lookup module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2014-04-26 * @since 1.0 */ final public class Lookup extends AbstractModule { /** - * THe lookup command. + * The lookup command. */ - private static final String LOOKUP_CMD = "lookup"; + public static final String LOOKUP_CMD = "lookup"; - /** - * The whois host. - */ + // The whois host. private static final String WHOIS_HOST = "whois.arin.net"; /** diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Ping.java b/src/main/java/net/thauvin/erik/mobibot/modules/Ping.java index dad186e..46a4af1 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Ping.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Ping.java @@ -40,14 +40,17 @@ import java.util.Random; /** * The Ping module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2016-07-02 * @since 1.0 */ public class Ping extends AbstractModule { /** - * The ping responses. + * The ping command. */ + public static final String PING_CMD = "ping"; + + // The ping responses. private static final List PINGS = Arrays.asList( "is barely alive.", @@ -63,11 +66,6 @@ public class Ping extends AbstractModule { "is saving energy: apathetic mode activated.", "is busy. Go away!"); - /** - * The ping command. - */ - private static final String PING_CMD = "ping"; - /** * The default constructor. */ diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java index 6d4096a..f8cffbe 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java @@ -41,7 +41,7 @@ import java.io.IOException; /** * The StockQuote module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created Feb 7, 2004 * @since 1.0 */ @@ -49,11 +49,9 @@ final public class StockQuote extends AbstractModule { /** * The quote command. */ - private static final String STOCK_CMD = "stock"; + public static final String STOCK_CMD = "stock"; - /** - * The Yahoo! stock quote URL. - */ + // The Yahoo! stock quote URL. private static final String YAHOO_URL = "http://finance.yahoo.com/d/quotes.csv?&f=snl1d1t1c1oghv&e=.csv&s="; /** diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java b/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java index 05e41d7..4a6f0cc 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Twitter.java @@ -39,21 +39,22 @@ import twitter4j.conf.ConfigurationBuilder; /** * The Twitter module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created Sept 10, 2008 * @since 1.0 */ final public class Twitter extends AbstractModule { + /** + * The twitter command. + */ + public final static String TWITTER_CMD = "twitter"; + + // The property keys. private final static String CONSUMER_KEY_PROP = "twitter-consumerKey"; private final static String CONSUMER_SECRET_PROP = "twitter-consumerSecret"; private final static String TOKEN_PROP = "twitter-token"; private final static String TOKEN_SECRET_PROP = "twitter-tokenSecret"; - /** - * The twitter command. - */ - private final static String TWITTER_CMD = "twitter"; - /** * Creates a new {@link Twitter} instance. */ diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/War.java b/src/main/java/net/thauvin/erik/mobibot/modules/War.java index ab07d74..e13cc2c 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/War.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/War.java @@ -39,25 +39,21 @@ import java.util.Random; /** * The War module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2014-04-28 * @since 1.0 */ final public class War extends AbstractModule { /** - * The war command. + * The war command */ - private static final String WAR_CMD = "war"; + public static final String WAR_CMD = "war"; - /** - * The deck of card. - */ + // The deck of card. private static final String[] WAR_DECK = new String[]{"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"}; - /** - * The suits for the deck of card. - */ + // The suits for the deck of card. private static final String[] WAR_SUITS = new String[]{"Hearts", "Spades", "Diamonds", "Clubs"}; /** diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Weather.java b/src/main/java/net/thauvin/erik/mobibot/modules/Weather.java index 2611d2f..1e55805 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Weather.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Weather.java @@ -49,20 +49,16 @@ import java.util.Date; */ final public class Weather extends AbstractModule { /** - * The decimal number format. + * The weather command. */ + public static final String WEATHER_CMD = "weather"; + + // The decimal number format. private static final DecimalFormat NUMBER_FORMAT = new DecimalFormat("0.##"); - /** - * The URL where the stations are listed. - */ + // The URL where the stations are listed. private static final String STATIONS_URL = "http://www.rap.ucar.edu/weather/surface/stations.txt"; - /** - * THe weather command. - */ - private static final String WEATHER_CMD = "weather"; - /** * Creates a new {@link Weather} instance. */ @@ -101,7 +97,7 @@ final public class Weather extends AbstractModule { bot.send(sender, "At: " - + Utils.UTC_SDF.format(metar.getDate()) + + Utils.utcDateTime(metar.getDate()) + " UTC (" + (((new Date()).getTime() - metar.getDate().getTime()) / 1000L / 60L) + " minutes ago)", diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.java b/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.java index 87263d4..746382e 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.java @@ -34,6 +34,9 @@ package net.thauvin.erik.mobibot.modules; import net.thauvin.erik.mobibot.Mobibot; import java.text.SimpleDateFormat; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoField; import java.util.Calendar; import java.util.Map; import java.util.TimeZone; @@ -42,29 +45,23 @@ import java.util.TreeMap; /** * The WorldTime module. * - * @author Erik C. Thauvin + * @author Erik C. Thauvin * @created 2014-04-27 * @since 1.0 */ final public class WorldTime extends AbstractModule { - /** - * The beats (Internet Time) keyword. - */ - private static final String BEATS_KEYWORD = ".beats"; - - /** - * The supported countries. - */ - private static final Map COUNTRIES_MAP = new TreeMap<>(); - /** * The time command. */ - private static final String TIME_CMD = "time"; + public static final String TIME_CMD = "time"; - /** - * The date/time format. - */ + // The beats (Internet Time) keyword. + private static final String BEATS_KEYWORD = ".beats"; + + // The supported countries. + private static final Map COUNTRIES_MAP = new TreeMap<>(); + + // The date/time format. private static final SimpleDateFormat TIME_SDF = new SimpleDateFormat("'The time is 'HH:mm' on 'EEEE, d MMMM yyyy' in '"); @@ -136,7 +133,7 @@ final public class WorldTime extends AbstractModule { COUNTRIES_MAP.put("BEATS", BEATS_KEYWORD); for (final String tz : TimeZone.getAvailableIDs()) { - if (!tz.contains("/") && tz.length() == 3 & !COUNTRIES_MAP.containsKey(tz)) { + if (!tz.contains("/") && tz.length() == 3 && !COUNTRIES_MAP.containsKey(tz)) { COUNTRIES_MAP.put(tz, tz); } } @@ -161,8 +158,8 @@ final public class WorldTime extends AbstractModule { response = ("The current Internet Time is: " + internetTime() + ' ' + BEATS_KEYWORD); } else { TIME_SDF.setTimeZone(TimeZone.getTimeZone(tz)); - response = TIME_SDF.format(Calendar.getInstance().getTime()) + tz.substring(tz.indexOf('/') + 1) - .replace('_', ' '); + response = TIME_SDF.format(Calendar.getInstance().getTime()) + + tz.substring(tz.indexOf('/') + 1).replace('_', ' '); } } else { isInvalidTz = true; @@ -195,31 +192,10 @@ final public class WorldTime extends AbstractModule { * @return The Internet Time string. */ private String internetTime() { - final Calendar gc = Calendar.getInstance(); - - final int offset = (gc.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)); - int hh = gc.get(Calendar.HOUR_OF_DAY); - final int mm = gc.get(Calendar.MINUTE); - final int ss = gc.get(Calendar.SECOND); - - hh -= offset; // GMT - hh += 1; // BMT - - long beats = Math.round(Math.floor((double) ((((hh * 3600) + (mm * 60) + ss) * 1000) / 86400))); - - if (beats >= 1000) { - beats -= (long) 1000; - } else if (beats < 0) { - beats += (long) 1000; - } - - if (beats < 10) { - return ("@00" + beats); - } else if (beats < 100) { - return ("@0" + beats); - } - - return ('@' + String.valueOf(beats)); + final ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("UTC+01:00")); + final int beats = (int) ((zdt.get(ChronoField.SECOND_OF_MINUTE) + (zdt.get(ChronoField.MINUTE_OF_HOUR) * 60) + + (zdt.get(ChronoField.HOUR_OF_DAY) * 3600)) / 86.4); + return String.format("%c%03d", '@', beats); } @Override