From c30f905153c143ef26137377fa8583f9fd8b8c37 Mon Sep 17 00:00:00 2001 From: Sidd Date: Thu, 27 Jan 2022 21:54:01 -0800 Subject: [PATCH] fix: make commandPrefixes non-final in builders --- .../java/com/github/twitch4j/chat/TwitchChat.java | 4 ++-- .../github/twitch4j/chat/TwitchChatBuilder.java | 7 ++++--- .../com/github/twitch4j/TwitchClientBuilder.java | 6 +++--- .../github/twitch4j/TwitchClientPoolBuilder.java | 6 +++--- .../com/github/twitch4j/TwitchClientTest.java | 15 +++++++++++++++ 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/chat/src/main/java/com/github/twitch4j/chat/TwitchChat.java b/chat/src/main/java/com/github/twitch4j/chat/TwitchChat.java index 30c1eacdd..ab51af3c6 100644 --- a/chat/src/main/java/com/github/twitch4j/chat/TwitchChat.java +++ b/chat/src/main/java/com/github/twitch4j/chat/TwitchChat.java @@ -279,13 +279,13 @@ public class TwitchChat implements ITwitchChat { * @param maxJoinRetries Maximum join retries per channel * @param chatJoinTimeout Minimum milliseconds to wait after a join attempt */ - public TwitchChat(EventManager eventManager, CredentialManager credentialManager, OAuth2Credential chatCredential, String baseUrl, boolean sendCredentialToThirdPartyHost, List commandPrefixes, Integer chatQueueSize, Bucket ircMessageBucket, Bucket ircWhisperBucket, Bucket ircJoinBucket, Bucket ircAuthBucket, ScheduledThreadPoolExecutor taskExecutor, long chatQueueTimeout, ProxyConfig proxyConfig, boolean autoJoinOwnChannel, boolean enableMembershipEvents, Collection botOwnerIds, boolean removeChannelOnJoinFailure, int maxJoinRetries, long chatJoinTimeout) { + public TwitchChat(EventManager eventManager, CredentialManager credentialManager, OAuth2Credential chatCredential, String baseUrl, boolean sendCredentialToThirdPartyHost, Collection commandPrefixes, Integer chatQueueSize, Bucket ircMessageBucket, Bucket ircWhisperBucket, Bucket ircJoinBucket, Bucket ircAuthBucket, ScheduledThreadPoolExecutor taskExecutor, long chatQueueTimeout, ProxyConfig proxyConfig, boolean autoJoinOwnChannel, boolean enableMembershipEvents, Collection botOwnerIds, boolean removeChannelOnJoinFailure, int maxJoinRetries, long chatJoinTimeout) { this.eventManager = eventManager; this.credentialManager = credentialManager; this.chatCredential = chatCredential; this.baseUrl = baseUrl; this.sendCredentialToThirdPartyHost = sendCredentialToThirdPartyHost; - this.commandPrefixes = commandPrefixes; + this.commandPrefixes = new ArrayList<>(commandPrefixes); this.botOwnerIds = botOwnerIds; this.ircCommandQueue = new ArrayBlockingQueue<>(chatQueueSize, true); this.ircMessageBucket = ircMessageBucket; diff --git a/chat/src/main/java/com/github/twitch4j/chat/TwitchChatBuilder.java b/chat/src/main/java/com/github/twitch4j/chat/TwitchChatBuilder.java index eb619fd11..f537f773a 100644 --- a/chat/src/main/java/com/github/twitch4j/chat/TwitchChatBuilder.java +++ b/chat/src/main/java/com/github/twitch4j/chat/TwitchChatBuilder.java @@ -23,12 +23,11 @@ import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; -import java.util.List; +import java.util.Set; import java.util.concurrent.ScheduledThreadPoolExecutor; /** @@ -110,7 +109,9 @@ public class TwitchChatBuilder { /** * IRC Command Handlers */ - protected final List commandPrefixes = new ArrayList<>(); + @Setter + @Accessors(chain = true) + protected Set commandPrefixes = new HashSet<>(); /** * Size of the ChatQueue diff --git a/twitch4j/src/main/java/com/github/twitch4j/TwitchClientBuilder.java b/twitch4j/src/main/java/com/github/twitch4j/TwitchClientBuilder.java index 6ddbfcdb9..b96f5874b 100644 --- a/twitch4j/src/main/java/com/github/twitch4j/TwitchClientBuilder.java +++ b/twitch4j/src/main/java/com/github/twitch4j/TwitchClientBuilder.java @@ -124,7 +124,7 @@ public class TwitchClientBuilder { /** * IRC Command Handlers */ - protected final Set commandPrefixes = new HashSet<>(); + protected Set commandPrefixes = new HashSet<>(); /** * Enabled: PubSub @@ -412,10 +412,10 @@ public TwitchClient build() { .withScheduledThreadPoolExecutor(scheduledThreadPoolExecutor) .withBaseUrl(chatServer) .withChatQueueTimeout(chatQueueTimeout) - .withCommandTriggers(commandPrefixes) .withProxyConfig(proxyConfig) - .setBotOwnerIds(botOwnerIds) .withMaxJoinRetries(chatMaxJoinRetries) + .setBotOwnerIds(botOwnerIds) + .setCommandPrefixes(commandPrefixes) .build(); } diff --git a/twitch4j/src/main/java/com/github/twitch4j/TwitchClientPoolBuilder.java b/twitch4j/src/main/java/com/github/twitch4j/TwitchClientPoolBuilder.java index 3caa990e6..39d014fb4 100644 --- a/twitch4j/src/main/java/com/github/twitch4j/TwitchClientPoolBuilder.java +++ b/twitch4j/src/main/java/com/github/twitch4j/TwitchClientPoolBuilder.java @@ -139,7 +139,7 @@ public class TwitchClientPoolBuilder { /** * IRC Command Handlers */ - protected final Set commandPrefixes = new HashSet<>(); + protected Set commandPrefixes = new HashSet<>(); /** * Enabled: PubSub @@ -438,7 +438,7 @@ public TwitchClientPool build() { .withBaseUrl(chatServer) .withChatQueueTimeout(chatQueueTimeout) .withMaxJoinRetries(chatMaxJoinRetries) - .withCommandTriggers(commandPrefixes) + .setCommandPrefixes(commandPrefixes) .setBotOwnerIds(botOwnerIds) ) .build(); @@ -455,10 +455,10 @@ public TwitchClientPool build() { .withScheduledThreadPoolExecutor(scheduledThreadPoolExecutor) .withBaseUrl(chatServer) .withChatQueueTimeout(chatQueueTimeout) - .withCommandTriggers(commandPrefixes) .withProxyConfig(proxyConfig) .withMaxJoinRetries(chatMaxJoinRetries) .setBotOwnerIds(botOwnerIds) + .setCommandPrefixes(commandPrefixes) .build(); } diff --git a/twitch4j/src/test/java/com/github/twitch4j/TwitchClientTest.java b/twitch4j/src/test/java/com/github/twitch4j/TwitchClientTest.java index b2176d80e..e3c4c4f8c 100644 --- a/twitch4j/src/test/java/com/github/twitch4j/TwitchClientTest.java +++ b/twitch4j/src/test/java/com/github/twitch4j/TwitchClientTest.java @@ -9,8 +9,10 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; + import java.util.concurrent.ScheduledThreadPoolExecutor; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @Slf4j @@ -34,6 +36,19 @@ public void buildTwitch4J() { .build(); } + @Test + @DisplayName("Ensure builder calls don't reset command triggers") + public void buildCommandPrefix() { + TwitchClientBuilder b1 = TwitchClientBuilder.builder().withEnableChat(true); + assertTrue(b1.getCommandPrefixes().isEmpty()); + + TwitchClientBuilder b2 = b1.withCommandTrigger("!"); + assertEquals(1, b2.getCommandPrefixes().size()); + + TwitchClientBuilder b3 = b2.withEnablePubSub(true); + assertEquals(1, b3.getCommandPrefixes().size()); + } + @Test @DisplayName("Test if the Twitch4J ThreadPool is closed on shutdown") public void testScheduledThreadPoolExecutorShutdown() {