Skip to content

Commit

Permalink
Refactors to fix all the unchecked type warnings during compile
Browse files Browse the repository at this point in the history
  • Loading branch information
shayaantx committed Mar 5, 2022
1 parent 09d4331 commit e985961
Show file tree
Hide file tree
Showing 81 changed files with 2,106 additions and 1,457 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
</dependency>
<dependency>
<groupId>com.github.seratch</groupId>
<artifactId>jslack</artifactId>
<version>3.3.0</version>
<artifactId>jslack-lightning</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/botdarr/Application.java

This file was deleted.

11 changes: 8 additions & 3 deletions src/main/java/com/botdarr/BotdarrApplication.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.botdarr;

import com.botdarr.database.Bootstrap;
import com.botdarr.clients.ChatClientBootstrap;
import com.botdarr.database.DatabaseBootstrap;

public class BotdarrApplication {
public static void main(String[] args) throws Exception {
Bootstrap.init();
Config.getChatClientType().init();
// bootstrap the database
DatabaseBootstrap.init();

// boostrap the chat client/bot
ChatClientBootstrap chatClientBootstrap = Config.getChatClientBootstrap();
chatClientBootstrap.init();
}
}
49 changes: 22 additions & 27 deletions src/main/java/com/botdarr/Config.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.botdarr;

import com.botdarr.clients.ChatClientType;
import com.botdarr.commands.StatusCommand;
import com.botdarr.clients.ChatClientBootstrap;
import com.botdarr.clients.discord.DiscordBootstrap;
import com.botdarr.clients.matrix.MatrixBootstrap;
import com.botdarr.clients.slack.SlackBootstrap;
import com.botdarr.clients.telegram.TelegramBootstrap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.Strings;
Expand Down Expand Up @@ -34,22 +37,22 @@ private Config() {
properties = new Properties();
properties.load(input);

for (ChatClientType possibleChatClientType : ChatClientType.values()) {
if (possibleChatClientType.isConfigured(properties) && chatClientType != null) {
List<ChatClientBootstrap> availableBootstraps = Arrays.asList(
new DiscordBootstrap(),
new MatrixBootstrap(),
new SlackBootstrap(),
new TelegramBootstrap());
for (ChatClientBootstrap possibleChatClientType : availableBootstraps) {
if (possibleChatClientType.isConfigured(properties) && chatClientBootstrap != null) {
throw new RuntimeException("You cannot configure more than one chat client");
}
if (possibleChatClientType.isConfigured(properties)) {
chatClientType = possibleChatClientType;
chatClientBootstrap = possibleChatClientType;
}
}

if (chatClientType == null) {
String allChatClientTypes = Arrays.asList(ChatClientType.values())
.stream()
.sorted(Comparator.comparing(ChatClientType::getReadableName))
.map(ChatClientType::getReadableName)
.collect(Collectors.joining(", "));
throw new RuntimeException("You don't have " + allChatClientTypes + " configured, please configure one");
if (chatClientBootstrap == null) {
throw new RuntimeException("You don't have any chat clients configured, please configure one");
}

this.isRaddarrEnabled =
Expand Down Expand Up @@ -85,15 +88,7 @@ private Config() {

String configuredPrefix = properties.getProperty(Config.Constants.COMMAND_PREFIX);
if (!Strings.isEmpty(configuredPrefix)) {
if (configuredPrefix.length() > 1) {
throw new RuntimeException("Command prefix must be a single character");
}
if (chatClientType == ChatClientType.SLACK && configuredPrefix.equals("/")) {
throw new RuntimeException("Cannot use / command prefix in slack since /help command was deprecated by slack");
}
if (chatClientType == ChatClientType.MATRIX && configuredPrefix.equals("/")) {
throw new RuntimeException("Cannot use / command prefix in matrix since /help command is used by element by default");
}
chatClientBootstrap.validatePrefix(configuredPrefix);
}
} catch (Exception ex) {
LOGGER.error("Error loading properties file", ex);
Expand All @@ -117,8 +112,8 @@ public static boolean isLidarrEnabled() {
return getConfig().isLidarrEnabled;
}

public static ChatClientType getChatClientType() {
return getConfig().chatClientType;
public static ChatClientBootstrap getChatClientBootstrap() {
return getConfig().chatClientBootstrap;
}

public static List<String> getExistingItemBlacklistPaths() {
Expand All @@ -134,7 +129,7 @@ public static List<StatusEndPoint> getStatusEndpoints() {
if (splitEndpoint.length != 3) {
LOGGER.warn("Status endpoint not formatted correctly, usage - name:hostname:port, endpoint=" + endpoint);
}
statusEndPoints.add(new StatusEndPoint(splitEndpoint[0], splitEndpoint[1], Integer.valueOf(splitEndpoint[2])));
statusEndPoints.add(new StatusEndPoint(splitEndpoint[0], splitEndpoint[1], Integer.parseInt(splitEndpoint[2])));
}
}
StatusEndPoint endpoint;
Expand Down Expand Up @@ -166,7 +161,7 @@ public static int getTimeout() {
try {
return Integer.parseInt(Config.getProperty(Config.Constants.TIMEOUT));
} catch (NumberFormatException e) {
LOGGER.error("Error parsing timeout", e);
LOGGER.trace("Error parsing timeout, using default timeout", e);
}
return 5000;
}
Expand Down Expand Up @@ -355,7 +350,7 @@ public static final class Constants {
public static final String MAX_SHOW_REQUESTS_PER_USER = "max-show-requests-per-user";

/**
* The max of artist requests per user per configured threshold
* The max amount of artist requests per user per configured threshold
*/
public static final String MAX_ARTIST_REQUESTS_PER_USER = "max-artist-requests-per-user";

Expand Down Expand Up @@ -400,6 +395,6 @@ public static final class Constants {
private final boolean isRaddarrEnabled;
private final boolean isSonarrEnabled;
private final boolean isLidarrEnabled;
private ChatClientType chatClientType = null;
private ChatClientBootstrap chatClientBootstrap = null;
private static final Logger LOGGER = LogManager.getLogger();
}
44 changes: 21 additions & 23 deletions src/main/java/com/botdarr/api/AddStrategy.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package com.botdarr.api;

import com.botdarr.clients.ChatClientResponse;
import com.botdarr.clients.ChatClientResponseBuilder;
import com.botdarr.commands.responses.CommandResponse;
import com.botdarr.commands.responses.ErrorResponse;
import com.botdarr.commands.responses.InfoResponse;
import com.botdarr.utilities.ListUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public abstract class AddStrategy<T> {
public AddStrategy(ChatClientResponseBuilder<? extends ChatClientResponse> chatClientResponseBuilder, ContentType contentType) {
this.chatClientResponseBuilder = chatClientResponseBuilder;
public AddStrategy(ContentType contentType) {
this.contentDisplayName = contentType.getDisplayName();
}

public abstract List<T> lookupContent(String search) throws Exception;
public abstract List<T> lookupItemById(String id) throws Exception;
public abstract boolean doesItemExist(T content);
public abstract String getItemId(T item);
public abstract ChatClientResponse addContent(T content);
public abstract ChatClientResponse getResponse(T item);
public abstract CommandResponse addContent(T content);
public abstract CommandResponse getResponse(T item);

public ChatClientResponse addWithSearchId(String searchText, String id) {
public CommandResponse addWithSearchId(String searchText, String id) {
try {
List<T> items = lookupContent(searchText);
if (items.isEmpty()) {
Expand All @@ -32,39 +32,38 @@ public ChatClientResponse addWithSearchId(String searchText, String id) {
}
if (items.isEmpty()) {
LOGGER.warn("Search id " + id + "yielded no " + this.contentDisplayName + "s, stopping");
return chatClientResponseBuilder.createErrorMessage("No " + this.contentDisplayName + "s found");
return new ErrorResponse("No " + this.contentDisplayName + "s found");
}
for (T item : items) {
if (getItemId(item).equalsIgnoreCase(id)) {
if (doesItemExist(item)) {
return chatClientResponseBuilder.createErrorMessage(this.contentDisplayName + " already exists");
return new ErrorResponse(this.contentDisplayName + " already exists");
}
ChatClientResponse chatClientResponse = addContent(item);
return chatClientResponse;
return addContent(item);
}
}
return chatClientResponseBuilder.createErrorMessage("Could not find " + contentDisplayName + " with search text=" + searchText + " and id=" + id);
return new ErrorResponse("Could not find " + contentDisplayName + " with search text=" + searchText + " and id=" + id);
} catch (Throwable e) {
LOGGER.error("Error trying to add " + contentDisplayName, e);
return chatClientResponseBuilder.createErrorMessage("Error adding content, e=" + e.getMessage());
return new ErrorResponse("Error adding content, e=" + e.getMessage());
}
}

public List<ChatClientResponse> addWithSearchTitle(String searchText) {
public List<CommandResponse> addWithSearchTitle(String searchText) {
try {
List<T> items = lookupContent(searchText);
if (items.size() == 0) {
return Arrays.asList(chatClientResponseBuilder.createInfoMessage("No " + contentDisplayName + "s found"));
return Collections.singletonList(new InfoResponse("No " + contentDisplayName + "s found"));
}

if (items.size() == 1) {
T item = items.get(0);
if (doesItemExist(item)) {
return Arrays.asList(chatClientResponseBuilder.createErrorMessage(contentDisplayName + " already exists"));
return Collections.singletonList(new ErrorResponse(contentDisplayName + " already exists"));
}
return Arrays.asList(addContent(items.get(0)));
return Collections.singletonList(addContent(items.get(0)));
}
List<ChatClientResponse> restOfItems = new ArrayList<>();
List<CommandResponse> restOfItems = new ArrayList<>();
for (T item : items) {
if (doesItemExist(item)) {
//skip existing items
Expand All @@ -74,19 +73,18 @@ public List<ChatClientResponse> addWithSearchTitle(String searchText) {
}
if (restOfItems.size() > 1) {
restOfItems = ListUtils.subList(restOfItems, MAX_RESULTS_TO_SHOW);
restOfItems.add(0, chatClientResponseBuilder.createInfoMessage("Too many " + contentDisplayName + "s found, please narrow search or increase max results to show"));
restOfItems.add(0, new InfoResponse("Too many " + contentDisplayName + "s found, please narrow search or increase max results to show"));
}
if (restOfItems.size() == 0) {
return Arrays.asList(chatClientResponseBuilder.createInfoMessage("No new " + contentDisplayName + "s found, check existing " + contentDisplayName + "s"));
return Collections.singletonList(new InfoResponse("No new " + contentDisplayName + "s found, check existing " + contentDisplayName + "s"));
}
return restOfItems;
} catch (Throwable e) {
LOGGER.error("Error trying to add " + contentDisplayName, e);
return Arrays.asList(chatClientResponseBuilder.createErrorMessage("Error trying to add " + contentDisplayName + ", for search text=" + searchText + ", e=" + e.getMessage()));
return Collections.singletonList(new ErrorResponse("Error trying to add " + contentDisplayName + ", for search text=" + searchText + ", e=" + e.getMessage()));
}
}

private final ChatClientResponseBuilder<? extends ChatClientResponse> chatClientResponseBuilder;
private final String contentDisplayName;
private static Logger LOGGER = LogManager.getLogger();
private final int MAX_RESULTS_TO_SHOW = new ApiRequests().getMaxResultsToShow();
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/botdarr/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.botdarr.clients.ChatClient;
import com.botdarr.clients.ChatClientResponse;
import com.botdarr.clients.ChatClientResponseBuilder;
import com.botdarr.clients.discord.DiscordResponse;
import com.botdarr.commands.responses.CommandResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.Strings;
Expand All @@ -26,12 +28,7 @@ public interface Api {
/**
* Gets all the in-progress downloads
*/
List<ChatClientResponse> downloads();

/**
* Notifications that are sent every 10 minutes
*/
void sendPeriodicNotifications(ChatClient chatClient);
List<CommandResponse> downloads();

/**
* Data cached from jda directly in the api
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/botdarr/api/ApiRequestType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum ApiRequestType {
SHOW(Config.Constants.MAX_SHOW_REQUESTS_PER_USER),
ARTIST(Config.Constants.MAX_ARTIST_REQUESTS_PER_USER);

private ApiRequestType(String configProperty) {
ApiRequestType(String configProperty) {
this.maxRequestsPerUserProperty = configProperty;
}

Expand All @@ -29,7 +29,7 @@ public boolean isConfigured() {
}

public int getMaxRequestsAllowed() {
return Integer.valueOf(Config.getProperty(maxRequestsPerUserProperty));
return Integer.parseInt(Config.getProperty(maxRequestsPerUserProperty));
}

private final String maxRequestsPerUserProperty;
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/botdarr/api/ApiRequests.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public int getMaxDownloadsToShow() {
String maxDownloadsToShow = Config.getProperty(Config.Constants.MAX_DOWNLOADS_TO_SHOW);
if (!Strings.isEmpty(maxDownloadsToShow)) {
try {
return Integer.valueOf(maxDownloadsToShow);
return Integer.parseInt(maxDownloadsToShow);
} catch (NumberFormatException e) {
LOGGER.error("Invalid max downloads to show configuration", e);
}
Expand All @@ -29,7 +29,7 @@ public int getMaxResultsToShow() {
String maxResultsToShow = Config.getProperty(Config.Constants.MAX_RESULTS_TO_SHOW);
if (!Strings.isEmpty(maxResultsToShow)) {
try {
return Integer.valueOf(maxResultsToShow);
return Integer.parseInt(maxResultsToShow);
} catch (NumberFormatException e) {
LOGGER.error("Invalid max results to show configuration", e);
}
Expand All @@ -48,8 +48,7 @@ public boolean checkRequestLimits(ApiRequestType requestType) {
try {
ApiRequestThreshold.valueOf(maxRequestsThreshold);
} catch (Exception e) {
String allRequestThresholds = Arrays.asList(ApiRequestThreshold.values())
.stream()
String allRequestThresholds = Arrays.stream(ApiRequestThreshold.values())
.sorted(Comparator.comparing(ApiRequestThreshold::getReadableName))
.map(ApiRequestThreshold::getReadableName)
.collect(Collectors.joining(", "));
Expand Down
Loading

0 comments on commit e985961

Please sign in to comment.