From 1097a8f2f8e7a09fee59a2aa9820cbb78584e6de Mon Sep 17 00:00:00 2001 From: Vankka Date: Sun, 23 Jan 2022 16:23:24 +0200 Subject: [PATCH] Convert things to CompletableFutureUtil --- .../ReceivedDiscordMessageCluster.java | 4 +-- .../common/discord/api/DiscordAPIImpl.java | 13 +++---- .../ReceivedDiscordMessageClusterImpl.java | 36 +++++++------------ .../message/ReceivedDiscordMessageImpl.java | 9 ++--- .../DiscordMessageMirroringModule.java | 16 ++++----- .../game/AbstractGameMessageModule.java | 7 ++-- 6 files changed, 30 insertions(+), 55 deletions(-) diff --git a/api/src/main/java/com/discordsrv/api/discord/api/entity/message/ReceivedDiscordMessageCluster.java b/api/src/main/java/com/discordsrv/api/discord/api/entity/message/ReceivedDiscordMessageCluster.java index a3e9b2e7..d0d3de70 100644 --- a/api/src/main/java/com/discordsrv/api/discord/api/entity/message/ReceivedDiscordMessageCluster.java +++ b/api/src/main/java/com/discordsrv/api/discord/api/entity/message/ReceivedDiscordMessageCluster.java @@ -25,7 +25,7 @@ package com.discordsrv.api.discord.api.entity.message; import org.jetbrains.annotations.NotNull; -import java.util.List; +import java.util.Set; import java.util.concurrent.CompletableFuture; /** @@ -38,7 +38,7 @@ public interface ReceivedDiscordMessageCluster { * @return the messages in this cluster */ @NotNull - List getMessages(); + Set getMessages(); /** * Deletes all the messages from this cluster, one request per message. diff --git a/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java b/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java index 3fe86f86..25de2557 100644 --- a/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java +++ b/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java @@ -41,6 +41,7 @@ import com.discordsrv.common.discord.api.entity.channel.DiscordTextChannelImpl; import com.discordsrv.common.discord.api.entity.guild.DiscordGuildImpl; import com.discordsrv.common.discord.api.entity.guild.DiscordRoleImpl; import com.discordsrv.common.function.CheckedSupplier; +import com.discordsrv.common.future.util.CompletableFutureUtil; import com.github.benmanes.caffeine.cache.AsyncCacheLoader; import com.github.benmanes.caffeine.cache.AsyncLoadingCache; import com.github.benmanes.caffeine.cache.Expiry; @@ -320,9 +321,7 @@ public class DiscordAPIImpl implements DiscordAPI { } public CompletableFuture notReady() { - CompletableFuture future = new CompletableFuture<>(); - future.completeExceptionally(new NotReadyException()); - return future; + return CompletableFutureUtil.failed(new NotReadyException()); } @Override @@ -398,11 +397,9 @@ public class DiscordAPIImpl implements DiscordAPI { return notReady(); } - CompletableFuture future = new CompletableFuture<>(); TextChannel textChannel = jda.getTextChannelById(channelId); if (textChannel == null) { - future.completeExceptionally(new IllegalArgumentException("Channel could not be found")); - return future; + return CompletableFutureUtil.failed(new IllegalArgumentException("Channel could not be found")); } return textChannel.retrieveWebhooks().submit().thenApply(webhooks -> { @@ -422,9 +419,7 @@ public class DiscordAPIImpl implements DiscordAPI { return hook; }).thenCompose(webhook -> { if (webhook != null) { - CompletableFuture completableFuture = new CompletableFuture<>(); - completableFuture.complete(webhook); - return completableFuture; + return CompletableFuture.completedFuture(webhook); } return textChannel.createWebhook("DiscordSRV").submit(); diff --git a/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageClusterImpl.java b/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageClusterImpl.java index 5130c8e7..06e09ec9 100644 --- a/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageClusterImpl.java +++ b/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageClusterImpl.java @@ -21,54 +21,44 @@ package com.discordsrv.common.discord.api.entity.message; import com.discordsrv.api.discord.api.entity.message.ReceivedDiscordMessage; import com.discordsrv.api.discord.api.entity.message.ReceivedDiscordMessageCluster; import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage; +import com.discordsrv.common.future.util.CompletableFutureUtil; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.concurrent.CompletableFuture; public class ReceivedDiscordMessageClusterImpl implements ReceivedDiscordMessageCluster { - private final List messages; + private final Set messages; - public ReceivedDiscordMessageClusterImpl(List messages) { + public ReceivedDiscordMessageClusterImpl(Set messages) { this.messages = messages; } @Override - public @NotNull List getMessages() { + public @NotNull Set getMessages() { return messages; } - @SuppressWarnings("unchecked") @Override public @NotNull CompletableFuture deleteAll() { - CompletableFuture[] futures = new CompletableFuture[messages.size()]; - for (int i = 0; i < messages.size(); i++) { - futures[i] = messages.get(i).delete(); + List> futures = new ArrayList<>(messages.size()); + for (ReceivedDiscordMessage message : messages) { + futures.add(message.delete()); } - return CompletableFuture.allOf(futures); + return CompletableFutureUtil.combine(futures).thenApply(v -> null); } - @SuppressWarnings("unchecked") @Override public @NotNull CompletableFuture editAll(SendableDiscordMessage newMessage) { - CompletableFuture[] futures = new CompletableFuture[messages.size()]; - for (int i = 0; i < messages.size(); i++) { - futures[i] = messages.get(i).edit(newMessage); + List> futures = new ArrayList<>(messages.size()); + for (ReceivedDiscordMessage message : messages) { + futures.add(message.edit(newMessage)); } - return CompletableFuture.allOf(futures) - .thenApply(v -> { - List messages = new ArrayList<>(); - for (CompletableFuture future : futures) { - // All the futures are done, so we're just going to get the results from all of them - messages.add( - future.join()); - } - - return new ReceivedDiscordMessageClusterImpl(messages); - }); + return CompletableFutureUtil.combine(futures).thenApply(ReceivedDiscordMessageClusterImpl::new); } } diff --git a/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java b/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java index 0ee9893a..90781c7c 100644 --- a/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java +++ b/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java @@ -44,6 +44,7 @@ import com.discordsrv.common.discord.api.entity.DiscordUserImpl; import com.discordsrv.common.discord.api.entity.channel.DiscordMessageChannelImpl; import com.discordsrv.common.discord.api.entity.guild.DiscordGuildMemberImpl; import com.discordsrv.common.function.OrDefault; +import com.discordsrv.common.future.util.CompletableFutureUtil; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageEmbed; @@ -260,9 +261,7 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple public @NotNull CompletableFuture delete() { DiscordTextChannel textChannel = discordSRV.discordAPI().getTextChannelById(channelId).orElse(null); if (textChannel == null) { - CompletableFuture future = new CompletableFuture<>(); - future.completeExceptionally(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL)); - return future; + return CompletableFutureUtil.failed(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL)); } return textChannel.deleteMessageById(getId(), fromSelf && getWebhookUsername().isPresent()); @@ -276,9 +275,7 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple DiscordTextChannel textChannel = discordSRV.discordAPI().getTextChannelById(channelId).orElse(null); if (textChannel == null) { - CompletableFuture future = new CompletableFuture<>(); - future.completeExceptionally(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL)); - return future; + return CompletableFutureUtil.failed(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL)); } return textChannel.editMessageById(getId(), message); diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java index 62ca14d6..d228d90a 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java @@ -37,6 +37,7 @@ import com.discordsrv.common.config.main.channels.MirroringConfig; import com.discordsrv.common.config.main.channels.base.BaseChannelConfig; import com.discordsrv.common.config.main.channels.base.IChannelConfig; import com.discordsrv.common.function.OrDefault; +import com.discordsrv.common.future.util.CompletableFutureUtil; import com.discordsrv.common.module.type.AbstractModule; import com.github.benmanes.caffeine.cache.Cache; @@ -134,17 +135,12 @@ public class DiscordMessageMirroringModule extends AbstractModule { } } - CompletableFuture.allOf(messageFutures.toArray(new CompletableFuture[0])).whenComplete((v2, t2) -> { - Set messages = new HashSet<>(); - for (CompletableFuture messageFuture : messageFutures) { - if (messageFuture.isCompletedExceptionally()) { - continue; - } - - messages.add(getReference(messageFuture.join())); + CompletableFutureUtil.combine(messageFutures).whenComplete((messages, t2) -> { + Set references = new HashSet<>(); + for (ReceivedDiscordMessage msg : messages) { + references.add(getReference(msg)); } - - mapping.put(getReference(message), messages); + mapping.put(getReference(message), references); }); }); } diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java index df8cc936..e195e722 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java @@ -40,10 +40,7 @@ import net.kyori.adventure.text.Component; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.CopyOnWriteArrayList; @@ -126,7 +123,7 @@ public abstract class AbstractGameMessageModule extends AbstractModule { - List messages = new ArrayList<>(); + Set messages = new LinkedHashSet<>(); for (Map.Entry, DiscordMessageChannel> entry : messageFutures.entrySet()) { CompletableFuture future = entry.getKey(); if (future.isCompletedExceptionally()) {