sonarcloud code improvements.

This commit is contained in:
Erik C. Thauvin 2019-04-01 21:59:23 -07:00
parent 20ff7ed62b
commit 6d3e3f2c6e
20 changed files with 139 additions and 156 deletions

2
sonar-project.properties Normal file
View file

@ -0,0 +1,2 @@
sonar.projectKey=ethauvin_semver
sonar.sourceEncoding=UTF-8

View file

@ -38,7 +38,7 @@ package net.thauvin.erik.mobibot;
* @created 2014-04-26
* @since 1.0
*/
final public class Commands {
public final class Commands {
/**
* The add (back)log command.
*/
@ -159,8 +159,7 @@ final public class Commands {
*
* @throws UnsupportedOperationException If the constructor is called.
*/
private Commands()
throws UnsupportedOperationException {
private Commands() {
throw new UnsupportedOperationException("Illegal constructor call.");
}
}

View file

@ -73,8 +73,7 @@ final class EntriesMgr {
*
* @throws UnsupportedOperationException If the constructor is called.
*/
private EntriesMgr()
throws UnsupportedOperationException {
private EntriesMgr() {
throw new UnsupportedOperationException("Illegal constructor call.");
}
@ -184,78 +183,76 @@ final class EntriesMgr {
}
if (Utils.isValidString(bot.getLogsDir()) && Utils.isValidString(bot.getWeblogUrl())) {
Writer fw = null;
try {
fw = new OutputStreamWriter(
new FileOutputStream(bot.getLogsDir() + CURRENT_XML), StandardCharsets.UTF_8);
final SyndFeedOutput output = new SyndFeedOutput();
SyndFeed rss = new SyndFeedImpl();
rss.setFeedType("rss_2.0");
rss.setTitle(bot.getChannel() + " IRC Links");
rss.setDescription("Links from " + bot.getIrcServer() + " on " + bot.getChannel());
rss.setLink(bot.getWeblogUrl());
rss.setPublishedDate(Calendar.getInstance().getTime());
rss.setLanguage("en");
EntryLink entry;
StringBuffer buff;
EntryComment comment;
final List<SyndEntry> items = new ArrayList<>(0);
SyndEntry item;
SyndContent description;
try (final Writer fw = new OutputStreamWriter(
new FileOutputStream(bot.getLogsDir() + CURRENT_XML), StandardCharsets.UTF_8)) {
rss.setFeedType("rss_2.0");
rss.setTitle(bot.getChannel() + " IRC Links");
rss.setDescription("Links from " + bot.getIrcServer() + " on " + bot.getChannel());
rss.setLink(bot.getWeblogUrl());
rss.setPublishedDate(Calendar.getInstance().getTime());
rss.setLanguage("en");
for (int i = (entries.size() - 1); i >= 0; --i) {
entry = entries.get(i);
EntryLink entry;
StringBuilder buff;
EntryComment comment;
buff = new StringBuffer(
"Posted by <b>" + entry.getNick() + "</b> on <a href=\"irc://" + bot.getIrcServer() + '/'
+ entry.getChannel() + "\"><b>" + entry.getChannel() + "</b></a>");
for (int i = (entries.size() - 1); i >= 0; --i) {
entry = entries.get(i);
if (entry.getCommentsCount() > 0) {
buff.append(" <br/><br/>");
buff = new StringBuilder(
"Posted by <b>" + entry.getNick() + "</b> on <a href=\"irc://" + bot.getIrcServer() + '/'
+ entry.getChannel() + "\"><b>" + entry.getChannel() + "</b></a>");
final EntryComment[] comments = entry.getComments();
if (entry.getCommentsCount() > 0) {
buff.append(" <br/><br/>");
for (int j = 0; j < comments.length; j++) {
comment = comments[j];
final EntryComment[] comments = entry.getComments();
if (j > 0) {
buff.append(" <br/>");
for (int j = 0; j < comments.length; j++) {
comment = comments[j];
if (j > 0) {
buff.append(" <br/>");
}
buff.append(comment.getNick()).append(": ").append(comment.getComment());
}
buff.append(comment.getNick()).append(": ").append(comment.getComment());
}
item = new SyndEntryImpl();
item.setLink(entry.getLink());
description = new SyndContentImpl();
description.setValue(buff.toString());
item.setDescription(description);
item.setTitle(entry.getTitle());
item.setPublishedDate(entry.getDate());
item.setAuthor(
bot.getChannel().substring(1) + '@' + bot.getIrcServer() + " (" + entry.getNick() + ')');
item.setCategories(entry.getTags());
items.add(item);
}
item = new SyndEntryImpl();
item.setLink(entry.getLink());
description = new SyndContentImpl();
description.setValue(buff.toString());
item.setDescription(description);
item.setTitle(entry.getTitle());
item.setPublishedDate(entry.getDate());
item.setAuthor(
bot.getChannel().substring(1) + '@' + bot.getIrcServer() + " (" + entry.getNick() + ')');
item.setCategories(entry.getTags());
rss.setEntries(items);
items.add(item);
if (bot.getLogger().isDebugEnabled()) {
bot.getLogger().debug("Writing the entries feed.");
}
output.output(rss, fw);
}
rss.setEntries(items);
if (bot.getLogger().isDebugEnabled()) {
bot.getLogger().debug("Writing the entries feed.");
}
final SyndFeedOutput output = new SyndFeedOutput();
output.output(rss, fw);
fw.close();
fw = new OutputStreamWriter(
try (final Writer fw = new OutputStreamWriter(
new FileOutputStream(
bot.getLogsDir() + bot.getToday() + XML_EXT), StandardCharsets.UTF_8);
output.output(rss, fw);
bot.getLogsDir() + bot.getToday() + XML_EXT), StandardCharsets.UTF_8)) {
output.output(rss, fw);
}
if (isDayBackup) {
if (Utils.isValidString(bot.getBacklogsUrl())) {
@ -266,54 +263,46 @@ final class EntriesMgr {
history.remove(0);
}
}
try (final Writer fw = new OutputStreamWriter(
new FileOutputStream(bot.getLogsDir() + NAV_XML), StandardCharsets.UTF_8)) {
rss = new SyndFeedImpl();
rss.setFeedType("rss_2.0");
rss.setTitle(bot.getChannel() + " IRC Links Backlogs");
rss.setDescription("Backlogs of Links from " + bot.getIrcServer() + " on " + bot.getChannel());
rss.setLink(bot.getBacklogsUrl());
rss.setPublishedDate(Calendar.getInstance().getTime());
fw.close();
fw = new OutputStreamWriter(
new FileOutputStream(bot.getLogsDir() + NAV_XML), StandardCharsets.UTF_8);
rss = new SyndFeedImpl();
rss.setFeedType("rss_2.0");
rss.setTitle(bot.getChannel() + " IRC Links Backlogs");
rss.setDescription("Backlogs of Links from " + bot.getIrcServer() + " on " + bot.getChannel());
rss.setLink(bot.getBacklogsUrl());
rss.setPublishedDate(Calendar.getInstance().getTime());
String date;
items.clear();
String date;
items.clear();
for (int i = (history.size() - 1); i >= 0; --i) {
date = history.get(i);
for (int i = (history.size() - 1); i >= 0; --i) {
date = history.get(i);
item = new SyndEntryImpl();
item.setLink(bot.getBacklogsUrl() + date + ".xml");
item.setTitle(date);
description = new SyndContentImpl();
description.setValue("Links for " + date);
item.setDescription(description);
item = new SyndEntryImpl();
item.setLink(bot.getBacklogsUrl() + date + ".xml");
item.setTitle(date);
description = new SyndContentImpl();
description.setValue("Links for " + date);
item.setDescription(description);
items.add(item);
}
items.add(item);
rss.setEntries(items);
if (bot.getLogger().isDebugEnabled()) {
bot.getLogger().debug("Writing the backlog feed.");
}
output.output(rss, fw);
}
rss.setEntries(items);
if (bot.getLogger().isDebugEnabled()) {
bot.getLogger().debug("Writing the backlog feed.");
}
output.output(rss, fw);
} else {
bot.getLogger().warn("Unable to generate the backlogs feed. No property configured.");
}
}
} catch (FeedException | IOException e) {
bot.getLogger().warn("Unable to generate the entries feed.", e);
} finally {
try {
if (fw != null) {
fw.close();
}
} catch (Exception ignore) {
; // Do nothing
}
}
} else {
bot.getLogger()

View file

@ -67,7 +67,7 @@ public class EntryComment implements Serializable {
*/
@SuppressWarnings("UnusedDeclaration")
protected EntryComment() {
; // Required for serialization.
// Required for serialization.
}
/**

View file

@ -266,14 +266,14 @@ public class EntryLink implements Serializable {
* @return The tags as a comma-delimited string.
*/
public final String getPinboardTags() {
final StringBuilder tags = new StringBuilder(nick);
final StringBuilder pinboardTags = new StringBuilder(nick);
for (final Object tag : this.tags) {
tags.append(',');
tags.append(((SyndCategoryImpl) tag).getName());
for (final SyndCategory tag : tags) {
pinboardTags.append(',');
pinboardTags.append(tag.getName());
}
return tags.toString();
return pinboardTags.toString();
}
/**

View file

@ -57,6 +57,7 @@ import java.util.*;
* @created Jan 31, 2004
* @since 1.0
*/
@SuppressWarnings("WeakerAccess")
@Version(properties = "version.properties", className = "ReleaseInfo")
public class Mobibot extends PircBot {
/**
@ -182,7 +183,6 @@ public class Mobibot extends PircBot {
* @param logsDirPath The path to the logs directory.
* @param p The bot's properties.
*/
@SuppressWarnings("WeakerAccess")
public Mobibot(final String nickname, final String channel, final String logsDirPath, final Properties p) {
System.getProperties().setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(CONNECT_TIMEOUT));
System.getProperties().setProperty("sun.net.client.defaultReadTimeout", String.valueOf(CONNECT_TIMEOUT));
@ -210,7 +210,7 @@ public class Mobibot extends PircBot {
today = Utils.today();
}
} catch (IOException ignore) {
; // Do nothing.
// Do nothing.
} catch (FeedException e) {
logger.error("An error occurred while parsing the '" + EntriesMgr.CURRENT_XML + "' file.", e);
}
@ -219,7 +219,7 @@ public class Mobibot extends PircBot {
try {
EntriesMgr.loadBacklogs(logsDir + EntriesMgr.NAV_XML, history);
} catch (IOException ignore) {
; // Do nothing.
// Do nothing.
} catch (FeedException e) {
logger.error("An error occurred while parsing the '" + EntriesMgr.NAV_XML + "' file.", e);
}
@ -335,30 +335,24 @@ public class Mobibot extends PircBot {
// Redirect the stdout and stderr
if (!line.hasOption(Commands.DEBUG_ARG.charAt(0))) {
PrintStream stdout = null;
try {
stdout = new PrintStream(new FileOutputStream(
logsDir + channel.substring(1) + '.' + Utils.today() + ".log", true));
try (final PrintStream stdout = new PrintStream(new FileOutputStream(
logsDir + channel.substring(1) + '.' + Utils.today() + ".log", true))) {
System.setOut(stdout);
} catch (IOException e) {
System.err.println("Unable to open output (stdout) log file.");
e.printStackTrace(System.err);
System.exit(1);
}
PrintStream stderr = null;
try {
stderr = new PrintStream(
new FileOutputStream(logsDir + nickname + ".err", true));
try (final PrintStream stderr = new PrintStream(
new FileOutputStream(logsDir + nickname + ".err", true))) {
System.setErr(stderr);
} catch (IOException e) {
System.err.println("Unable to open error (stderr) log file.");
e.printStackTrace(System.err);
System.exit(1);
}
System.setOut(stdout);
System.setErr(stderr);
}
// Create the bot
@ -378,7 +372,7 @@ public class Mobibot extends PircBot {
try {
Thread.sleep((long) (secs * 1000));
} catch (InterruptedException ignore) {
; // Do nothing
// Do nothing.
}
}
@ -387,7 +381,7 @@ public class Mobibot extends PircBot {
*
* @param action The action.
*/
final public void action(final String action) {
public final void action(final String action) {
action(ircChannel, action);
}
@ -580,7 +574,7 @@ public class Mobibot extends PircBot {
* @param help The help string.
* @return The indented help string.
*/
final public String helpIndent(final String help) {
public final String helpIndent(final String help) {
return helpIndent(help, true);
}
@ -624,7 +618,7 @@ public class Mobibot extends PircBot {
} else if (lcTopic.equals(Commands.VIEW_CMD)) {
send(sender, "To list or search the current URL posts:");
send(sender, helpIndent(getNick() + ": " + Commands.VIEW_CMD) + " [<start>] [<query>]");
} else if (lcTopic.equals(ircChannel.substring(1).toLowerCase())) {
} else if (lcTopic.equalsIgnoreCase(ircChannel.substring(1))) {
send(sender, "To list the last 5 posts from the channel's weblog:");
send(sender, helpIndent(getNick() + ": " + ircChannel.substring(1)));
} else if (lcTopic.equals(Commands.RECAP_CMD)) {
@ -1390,9 +1384,9 @@ public class Mobibot extends PircBot {
* @param isPrivate Set to <code>true</code> if the response should be sent as a private message.
*/
private void recapResponse(final String sender, final boolean isPrivate) {
if (recap.size() > 0) {
for (final String recap : recap) {
send(sender, recap, isPrivate);
if (recap.isEmpty()) {
for (final String r : recap) {
send(sender, r, isPrivate);
}
} else {
send(sender, "Sorry, nothing to recap.", true);
@ -1574,7 +1568,7 @@ public class Mobibot extends PircBot {
final int max = entries.size();
int i = 0;
if (!(lcArgs.length() > 0) && (max > MAX_ENTRIES)) {
if ((lcArgs.length() <= 0) && (max > MAX_ENTRIES)) {
i = max - MAX_ENTRIES;
}
@ -1598,7 +1592,7 @@ public class Mobibot extends PircBot {
i = 0;
}
} catch (NumberFormatException ignore) {
; // Do nothing
// Do nothing.
}
}

View file

@ -67,13 +67,13 @@ public class Tell {
private static final String SER_EXT = ".ser";
// The bot instance.
final private Mobibot bot;
private final Mobibot bot;
// The maximum number of days to keep messages.
final private int maxDays;
private final int maxDays;
// The message maximum queue size.
final private int maxSize;
private final int maxSize;
// The messages queue.
private final List<TellMessage> messages = new CopyOnWriteArrayList<>();
@ -139,13 +139,14 @@ public class Tell {
* @param cmds The commands string.
*/
public void response(final String sender, final String cmds) {
final String arrow = " --> ";
if (!Utils.isValidString(cmds)) {
helpResponse(sender);
} else if (cmds.startsWith(Commands.VIEW_CMD)) {
if (bot.isOp(sender) && cmds.equals(Commands.VIEW_CMD + ' ' + TELL_ALL_KEYWORD)) {
if (messages.size() > 0) {
if (messages.isEmpty()) {
for (final TellMessage message : messages) {
bot.send(sender, Utils.bold(message.getSender()) + " --> " + Utils.bold(message.getRecipient())
bot.send(sender, Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient())
+ " [ID: " + message.getId() + ", "
+ (message.isReceived() ? "DELIVERED" : "QUEUED") + ']',
true);
@ -165,14 +166,14 @@ public class Tell {
if (message.isReceived()) {
bot.send(sender,
Utils.bold(message.getSender()) + " --> " + Utils.bold(message.getRecipient())
Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient())
+ " [" + Utils.utcDateTime(message.getReceived()) + ", ID: "
+ message.getId() + ", DELIVERED]",
true);
} else {
bot.send(sender,
Utils.bold(message.getSender()) + " --> " + Utils.bold(message.getRecipient())
Utils.bold(message.getSender()) + arrow + Utils.bold(message.getRecipient())
+ " [" + Utils.utcDateTime(message.getQueued()) + ", ID: "
+ message.getId() + ", QUEUED]",
true);

View file

@ -47,7 +47,7 @@ public class TellMessage implements Serializable {
private static final long serialVersionUID = 2L;
private final String id;
private final String message;
final private LocalDateTime queued;
private final LocalDateTime queued;
private final String recipient;
private final String sender;
private boolean isNotified;
@ -176,4 +176,4 @@ public class TellMessage implements Serializable {
received = LocalDateTime.now(Clock.systemUTC());
isReceived = true;
}
}
}

View file

@ -52,8 +52,7 @@ final class TellMessagesMgr {
*
* @throws UnsupportedOperationException If the constructor is called.
*/
private TellMessagesMgr()
throws UnsupportedOperationException {
private TellMessagesMgr() {
throw new UnsupportedOperationException("Illegal constructor call.");
}
@ -98,7 +97,7 @@ final class TellMessagesMgr {
return ((List<TellMessage>) input.readObject());
}
} catch (FileNotFoundException ignore) {
; // Do nothing.
// Do nothing.
} catch (IOException e) {
logger.error("An IO error occurred loading the messages queue.", e);
} catch (Exception e) {

View file

@ -46,14 +46,13 @@ import java.util.Date;
* @created 2014-04-26
* @since 1.0
*/
final public class Utils {
public final class Utils {
/**
* Disables the default constructor.
*
* @throws UnsupportedOperationException If the constructor is called.
*/
private Utils()
throws UnsupportedOperationException {
private Utils() {
throw new UnsupportedOperationException("Illegal constructor call.");
}

View file

@ -90,4 +90,4 @@ public class Calc extends AbstractModule {
bot.send(sender, "To solve a mathematical calculation:");
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + CALC_CMD + " <calculation>"));
}
}
}

View file

@ -55,7 +55,7 @@ import java.util.TreeMap;
* @created Feb 11, 2004
* @since 1.0
*/
final public class CurrencyConverter extends AbstractModule {
public final class CurrencyConverter extends AbstractModule {
/**
* The currency command.
*/
@ -73,7 +73,7 @@ final public class CurrencyConverter extends AbstractModule {
private static final String EXCHANGE_TABLE_URL = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
// The last exchange rates table publication date.
private static String pubDate = "";
private String pubDate = "";
/**
* Creates a new {@link CurrencyConverter} instance.

View file

@ -43,7 +43,7 @@ import java.security.SecureRandom;
* @created 2014-04-28
* @since 1.0
*/
final public class Dice extends AbstractModule {
public final class Dice extends AbstractModule {
/**
* The dice command.
*/

View file

@ -51,7 +51,7 @@ import java.nio.charset.StandardCharsets;
* @created Feb 7, 2004
* @since 1.0
*/
final public class GoogleSearch extends AbstractModule {
public final class GoogleSearch extends AbstractModule {
/**
* The google command.
*/

View file

@ -48,7 +48,7 @@ import java.nio.charset.StandardCharsets;
* @created 2014-04-20
* @since 1.0
*/
final public class Joke extends AbstractModule {
public final class Joke extends AbstractModule {
/**
* The joke command.
*/

View file

@ -45,7 +45,7 @@ import java.net.UnknownHostException;
* @created 2014-04-26
* @since 1.0
*/
final public class Lookup extends AbstractModule {
public final class Lookup extends AbstractModule {
/**
* The lookup command.
*/

View file

@ -46,7 +46,7 @@ import java.io.IOException;
* @created Feb 7, 2004
* @since 1.0
*/
final public class StockQuote extends AbstractModule {
public final class StockQuote extends AbstractModule {
/**
* The quote command.
*/

View file

@ -43,17 +43,17 @@ import twitter4j.conf.ConfigurationBuilder;
* @created Sept 10, 2008
* @since 1.0
*/
final public class Twitter extends AbstractModule {
public final class Twitter extends AbstractModule {
/**
* The twitter command.
*/
public final static String TWITTER_CMD = "twitter";
public static final 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";
private static final String CONSUMER_KEY_PROP = "twitter-consumerKey";
private static final String CONSUMER_SECRET_PROP = "twitter-consumerSecret";
private static final String TOKEN_PROP = "twitter-token";
private static final String TOKEN_SECRET_PROP = "twitter-tokenSecret";
/**
* Creates a new {@link Twitter} instance.

View file

@ -43,7 +43,7 @@ import java.security.SecureRandom;
* @created 2014-04-28
* @since 1.0
*/
final public class War extends AbstractModule {
public final class War extends AbstractModule {
/**
* The war command
*/
@ -95,7 +95,7 @@ final public class War extends AbstractModule {
if (i < y) {
bot.action("lost.");
} else if (i > y) {
} else {
bot.action("wins.");
}
}

View file

@ -47,7 +47,7 @@ import java.util.TreeMap;
* @created 2014-04-27
* @since 1.0
*/
final public class WorldTime extends AbstractModule {
public final class WorldTime extends AbstractModule {
/**
* The time command.
*/