Added call to super() in constructor (PMD)

This commit is contained in:
Erik C. Thauvin 2019-04-27 02:53:30 -07:00
parent bc4399f798
commit 1ba412d1f0
15 changed files with 125 additions and 88 deletions

View file

@ -54,6 +54,7 @@ public class Calc extends AbstractModule {
* The default constructor. * The default constructor.
*/ */
public Calc() { public Calc() {
super();
commands.add(CALC_CMD); commands.add(CALC_CMD);
} }

View file

@ -52,6 +52,7 @@ public final class Dice extends AbstractModule {
* The default constructor. * The default constructor.
*/ */
public Dice() { public Dice() {
super();
commands.add(DICE_CMD); commands.add(DICE_CMD);
} }

View file

@ -49,6 +49,7 @@ import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
/** /**
* The GoogleSearch module. * The GoogleSearch module.
@ -71,11 +72,46 @@ public final class GoogleSearch extends ThreadedModule {
* Creates a new {@link GoogleSearch} instance. * Creates a new {@link GoogleSearch} instance.
*/ */
public GoogleSearch() { public GoogleSearch() {
super();
commands.add(GOOGLE_CMD); commands.add(GOOGLE_CMD);
properties.put(GOOGLE_API_KEY_PROP, ""); properties.put(GOOGLE_API_KEY_PROP, "");
properties.put(GOOGLE_CSE_KEY_PROP, ""); properties.put(GOOGLE_CSE_KEY_PROP, "");
} }
/**
* {@inheritDoc}
*/
@Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
if (isEnabled()) {
bot.send(sender, "To search Google:");
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + GOOGLE_CMD + " <query>"));
} else {
bot.send(sender, "The Google search module is disabled.");
}
}
/**
* Searches Google.
*/
@Override
void run(final Mobibot bot, final String sender, final String query) {
if (Utils.isValidString(query)) {
try {
final List<Message> results = searchGoogle(query, properties.get(GOOGLE_API_KEY_PROP),
properties.get(GOOGLE_CSE_KEY_PROP));
for (final Message msg : results) {
bot.send(sender, msg);
}
} catch (ModuleException e) {
bot.getLogger().warn(e.getDebugMessage(), e);
bot.send(sender, e.getMessage());
}
} else {
helpResponse(bot, sender, query, true);
}
}
/** /**
* Performs a search on Google. * Performs a search on Google.
* *
@ -85,8 +121,9 @@ public final class GoogleSearch extends ThreadedModule {
* @return The {@link Message} array containing the search results. * @return The {@link Message} array containing the search results.
* @throws ModuleException If an error occurs while searching. * @throws ModuleException If an error occurs while searching.
*/ */
@SuppressFBWarnings(value = {"URLCONNECTION_SSRF_FD", "REC_CATCH_EXCEPTION"}) @SuppressFBWarnings({"URLCONNECTION_SSRF_FD", "REC_CATCH_EXCEPTION"})
static ArrayList<Message> searchGoogle(final String query, final String apiKey, final String cseKey) @SuppressWarnings(("PMD.AvoidInstantiatingObjectsInLoops"))
static List<Message> searchGoogle(final String query, final String apiKey, final String cseKey)
throws ModuleException { throws ModuleException {
if (!Utils.isValidString(apiKey) || !Utils.isValidString(cseKey)) { if (!Utils.isValidString(apiKey) || !Utils.isValidString(cseKey)) {
throw new ModuleException(Utils.capitalize(GOOGLE_CMD) + " is disabled. The API keys are missing."); throw new ModuleException(Utils.capitalize(GOOGLE_CMD) + " is disabled. The API keys are missing.");
@ -134,37 +171,4 @@ public final class GoogleSearch extends ThreadedModule {
throw new ModuleException("Invalid query."); throw new ModuleException("Invalid query.");
} }
} }
/**
* {@inheritDoc}
*/
@Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
if (isEnabled()) {
bot.send(sender, "To search Google:");
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + GOOGLE_CMD + " <query>"));
} else {
bot.send(sender, "The Google search module is disabled.");
}
}
/**
* Searches Google.
*/
void run(final Mobibot bot, final String sender, final String query) {
if (Utils.isValidString(query)) {
try {
final ArrayList<Message> results = searchGoogle(query, properties.get(GOOGLE_API_KEY_PROP),
properties.get(GOOGLE_CSE_KEY_PROP));
for (final Message msg : results) {
bot.send(sender, msg);
}
} catch (ModuleException e) {
bot.getLogger().warn(e.getDebugMessage(), e);
bot.send(sender, e.getMessage());
}
} else {
helpResponse(bot, sender, query, true);
}
}
} }

View file

@ -62,6 +62,7 @@ public final class Joke extends ThreadedModule {
* Creates a new {@link Joke} instance. * Creates a new {@link Joke} instance.
*/ */
public Joke() { public Joke() {
super();
commands.add(JOKE_CMD); commands.add(JOKE_CMD);
} }
@ -107,6 +108,7 @@ public final class Joke extends ThreadedModule {
/** /**
* Returns a random joke from <a href="http://www.icndb.com/">The Internet Chuck Norris Database</a>. * Returns a random joke from <a href="http://www.icndb.com/">The Internet Chuck Norris Database</a>.
*/ */
@Override
void run(final Mobibot bot, final String sender, final String arg) { void run(final Mobibot bot, final String sender, final String arg) {
try { try {
bot.send(Utils.cyan(randomJoke().getMessage())); bot.send(Utils.cyan(randomJoke().getMessage()));

View file

@ -60,6 +60,7 @@ public final class Lookup extends AbstractModule {
* The default constructor. * The default constructor.
*/ */
public Lookup() { public Lookup() {
super();
commands.add(LOOKUP_CMD); commands.add(LOOKUP_CMD);
} }

View file

@ -46,11 +46,6 @@ import java.util.List;
* @since 1.0 * @since 1.0
*/ */
public class Ping extends AbstractModule { public class Ping extends AbstractModule {
/**
* The ping command.
*/
private static final String PING_CMD = "ping";
/** /**
* The ping responses. * The ping responses.
*/ */
@ -68,11 +63,16 @@ public class Ping extends AbstractModule {
"is hibernating.", "is hibernating.",
"is saving energy: apathetic mode activated.", "is saving energy: apathetic mode activated.",
"is busy. Go away!"); "is busy. Go away!");
/**
* The ping command.
*/
private static final String PING_CMD = "ping";
/** /**
* The default constructor. * The default constructor.
*/ */
public Ping() { public Ping() {
super();
commands.add(PING_CMD); commands.add(PING_CMD);
} }

View file

@ -70,6 +70,7 @@ public final class StockQuote extends ThreadedModule {
* Creates a new {@link StockQuote} instance. * Creates a new {@link StockQuote} instance.
*/ */
public StockQuote() { public StockQuote() {
super();
commands.add(STOCK_CMD); commands.add(STOCK_CMD);
properties.put(ALPHAVANTAGE_API_KEY_PROP, ""); properties.put(ALPHAVANTAGE_API_KEY_PROP, "");
} }

View file

@ -60,6 +60,7 @@ public final class Twitter extends ThreadedModule {
* Creates a new {@link Twitter} instance. * Creates a new {@link Twitter} instance.
*/ */
public Twitter() { public Twitter() {
super();
commands.add(TWITTER_CMD); commands.add(TWITTER_CMD);
properties.put(CONSUMER_SECRET_PROP, ""); properties.put(CONSUMER_SECRET_PROP, "");
properties.put(CONSUMER_KEY_PROP, ""); properties.put(CONSUMER_KEY_PROP, "");

View file

@ -57,6 +57,7 @@ public final class War extends AbstractModule {
* The default constructor. * The default constructor.
*/ */
public War() { public War() {
super();
commands.add(WAR_CMD); commands.add(WAR_CMD);
} }

View file

@ -69,6 +69,7 @@ public class Weather2 extends ThreadedModule {
* Creates a new {@link Weather2} instance. * Creates a new {@link Weather2} instance.
*/ */
public Weather2() { public Weather2() {
super();
commands.add(WEATHER_CMD); commands.add(WEATHER_CMD);
properties.put(OWM_API_KEY_PROP, ""); properties.put(OWM_API_KEY_PROP, "");
} }
@ -183,11 +184,6 @@ public class Weather2 extends ThreadedModule {
return messages; return messages;
} }
private static String wind(final Double w) {
final double kmh = w * 1.60934;
return Math.round(w) + " mph, " + Math.round(kmh) + " km/h";
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -224,4 +220,9 @@ public class Weather2 extends ThreadedModule {
helpResponse(bot, sender, args, true); helpResponse(bot, sender, args, true);
} }
} }
private static String wind(final Double w) {
final double kmh = w * 1.60934;
return Math.round(w) + " mph, " + Math.round(kmh) + " km/h";
}
} }

View file

@ -54,12 +54,14 @@ import java.util.TreeMap;
* @created 2014-04-27 * @created 2014-04-27
* @since 1.0 * @since 1.0
*/ */
@SuppressWarnings("PMD.UseConcurrentHashMap")
public final class WorldTime extends AbstractModule { public final class WorldTime extends AbstractModule {
// The beats (Internet Time) keyword. // The beats (Internet Time) keyword.
private static final String BEATS_KEYWORD = ".beats"; private static final String BEATS_KEYWORD = ".beats";
// The supported countries. // The supported countries.
private static final Map<String, String> COUNTRIES_MAP; private static final Map<String, String> COUNTRIES_MAP;
/** /**
* The time command. * The time command.
*/ */
@ -155,52 +157,10 @@ public final class WorldTime extends AbstractModule {
* Creates a new {@link WorldTime} instance. * Creates a new {@link WorldTime} instance.
*/ */
public WorldTime() { public WorldTime() {
super();
commands.add(TIME_CMD); commands.add(TIME_CMD);
} }
/**
* Returns the current Internet (beat) Time.
*
* @return The Internet Time string.
*/
private static String internetTime() {
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);
}
/**
* Returns the world time.
*
* <ul>
* <li>PST</li>
* <li>BEATS</li>
* </ul>
*
* @param query The query.
* @return The {@link Message} containing the world time.
*/
@SuppressFBWarnings(value = "STT_STRING_PARSING_A_FIELD")
static Message worldTime(final String query) {
final String tz = (COUNTRIES_MAP.get((query.substring(query.indexOf(' ') + 1).trim().toUpperCase())));
final String response;
if (tz != null) {
if (BEATS_KEYWORD.equals(tz)) {
response = ("The current Internet Time is: " + internetTime() + ' ' + BEATS_KEYWORD);
} else {
response = ZonedDateTime.now().withZoneSameInstant(ZoneId.of(tz)).format(
DateTimeFormatter.ofPattern("'The time is 'HH:mm' on 'EEEE, d MMMM yyyy' in '"))
+ tz.substring(tz.indexOf('/') + 1).replace('_', ' ');
}
} else {
return new ErrorMessage("The supported countries/zones are: " + COUNTRIES_MAP.keySet());
}
return new PublicMessage(response);
}
/** /**
* Responds with the current time in the specified timezone/country. * Responds with the current time in the specified timezone/country.
* *
@ -243,4 +203,48 @@ public final class WorldTime extends AbstractModule {
public boolean isPrivateMsgEnabled() { public boolean isPrivateMsgEnabled() {
return true; return true;
} }
/**
* Returns the current Internet (beat) Time.
*
* @return The Internet Time string.
*/
private static String internetTime() {
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);
}
/**
* Returns the world time.
*
* <ul>
* <li>PST</li>
* <li>BEATS</li>
* </ul>
*
* @param query The query.
* @return The {@link Message} containing the world time.
*/
@SuppressFBWarnings("STT_STRING_PARSING_A_FIELD")
static Message worldTime(final String query) {
final String tz = (COUNTRIES_MAP.get((query.substring(query.indexOf(' ') + 1).trim()
.toUpperCase(Constants.LOCALE))));
final String response;
if (tz != null) {
if (BEATS_KEYWORD.equals(tz)) {
response = ("The current Internet Time is: " + internetTime() + ' ' + BEATS_KEYWORD);
} else {
response = ZonedDateTime.now().withZoneSameInstant(ZoneId.of(tz)).format(
DateTimeFormatter.ofPattern("'The time is 'HH:mm' on 'EEEE, d MMMM yyyy' in '"))
+ tz.substring(tz.indexOf('/') + 1).replace('_', ' ');
}
} else {
return new ErrorMessage("The supported countries/zones are: " + COUNTRIES_MAP.keySet());
}
return new PublicMessage(response);
}
} }

View file

@ -46,6 +46,7 @@ public class ErrorMessage extends Message {
* @param message The error message. * @param message The error message.
*/ */
public ErrorMessage(final String message) { public ErrorMessage(final String message) {
super();
this.setMessage(message); this.setMessage(message);
this.setError(true); this.setError(true);
this.setNotice(true); this.setNotice(true);
@ -59,6 +60,7 @@ public class ErrorMessage extends Message {
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public ErrorMessage(final String message, final String color) { public ErrorMessage(final String message, final String color) {
super();
this.setMessage(message); this.setMessage(message);
this.setError(true); this.setError(true);
this.setNotice(true); this.setNotice(true);

View file

@ -46,6 +46,7 @@ public class NoticeMessage extends Message {
* @param message The notice's message. * @param message The notice's message.
*/ */
public NoticeMessage(final String message) { public NoticeMessage(final String message) {
super();
this.setMessage(message); this.setMessage(message);
this.setNotice(true); this.setNotice(true);
} }
@ -57,6 +58,7 @@ public class NoticeMessage extends Message {
* @param color The color. * @param color The color.
*/ */
public NoticeMessage(final String message, final String color) { public NoticeMessage(final String message, final String color) {
super();
this.setMessage(message); this.setMessage(message);
this.setNotice(true); this.setNotice(true);
this.setColor(color); this.setColor(color);

View file

@ -42,10 +42,18 @@ package net.thauvin.erik.mobibot.msg;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class PrivateMessage extends Message { public class PrivateMessage extends Message {
public PrivateMessage(final String message) { public PrivateMessage(final String message) {
super();
this.setMessage(message); this.setMessage(message);
} }
/**
* Creates a new private message.
*
* @param message The message.
* @param color The message color.
*/
public PrivateMessage(final String message, final String color) { public PrivateMessage(final String message, final String color) {
super();
this.setMessage(message); this.setMessage(message);
this.setColor(color); this.setColor(color);
} }

View file

@ -41,11 +41,19 @@ package net.thauvin.erik.mobibot.msg;
*/ */
public class PublicMessage extends Message { public class PublicMessage extends Message {
public PublicMessage(final String message) { public PublicMessage(final String message) {
super();
this.setMessage(message); this.setMessage(message);
} }
/**
* Creates a new public message.
*
* @param message The message.
* @param color The message color.
*/
@SuppressWarnings("unused") @SuppressWarnings("unused")
public PublicMessage(final String message, final String color) { public PublicMessage(final String message, final String color) {
super();
this.setMessage(message); this.setMessage(message);
this.setColor(color); this.setColor(color);
} }