Move util method to sequence CompletableFuture's to new class 'UtilFuture'

This commit is contained in:
Keir Nellyer 2016-06-09 23:41:17 +01:00
parent fe06fba406
commit 22dbcbf90d
2 changed files with 26 additions and 11 deletions

View File

@ -0,0 +1,24 @@
package mineplex.core.common.util;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class UtilFuture
{
/**
* Returns a {@link CompletableFuture} which will complete when supplied futures have completed.
* This is a workaround for {@link CompletableFuture#anyOf(CompletableFuture[])} returning void.
*
* @param futures the futures to wait to complete
* @param <T> the return type of the supplied futures
* @return a future which will complete when all supplied futures have completed
*/
public static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> futures)
{
CompletableFuture<Void> futuresCompletedFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
return futuresCompletedFuture.thenApply(v ->
futures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
}
}

View File

@ -11,7 +11,6 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.stream.Collectors;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
@ -25,6 +24,7 @@ import mineplex.core.common.jsonchat.JsonMessage;
import mineplex.core.common.util.BukkitFuture;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilFuture;
import mineplex.core.portal.Portal;
import mineplex.core.preferences.PreferencesManager;
import mineplex.core.report.command.ReportHandlerNotification;
@ -230,21 +230,12 @@ public class ReportManager
public CompletableFuture<Integer> calculatePriority(Report report)
{
// todo take into account age of report
return sequence(_reportProfileRepository.getProfiles(report.getReporterIds())).thenApply(reportProfiles -> reportProfiles.stream()
return UtilFuture.sequence(_reportProfileRepository.getProfiles(report.getReporterIds())).thenApply(reportProfiles -> reportProfiles.stream()
.mapToInt(profile -> profile.getReputation(report.getCategory()))
.map(operand -> operand * 100)
.sum());
}
// todo move to utility class
private static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> futures)
{
CompletableFuture<Void> futuresCompletedFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
return futuresCompletedFuture.thenApply(v ->
futures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
}
public void publishChatSnap(Report report, boolean updateChat)
{
// todo is this the best way to store Future's?