2020-07-31 16:37:01 +02:00
|
|
|
/*
|
|
|
|
* This file is part of PlaceholderAPI
|
|
|
|
*
|
|
|
|
* PlaceholderAPI
|
|
|
|
* Copyright (c) 2015 - 2020 PlaceholderAPI Team
|
|
|
|
*
|
|
|
|
* PlaceholderAPI free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PlaceholderAPI is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-07-27 03:02:55 +02:00
|
|
|
package me.clip.placeholderapi.util;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.function.BiConsumer;
|
|
|
|
import java.util.stream.Collector;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Stream;
|
2020-08-01 04:52:07 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2020-07-27 03:02:55 +02:00
|
|
|
|
2020-08-01 04:52:07 +02:00
|
|
|
public final class Futures {
|
2020-07-27 03:02:55 +02:00
|
|
|
|
2020-08-01 04:52:07 +02:00
|
|
|
private Futures() {}
|
2020-07-27 03:02:55 +02:00
|
|
|
|
|
|
|
|
2020-08-01 04:52:07 +02:00
|
|
|
public static <T> void onMainThread(@NotNull final Plugin plugin,
|
|
|
|
@NotNull final CompletableFuture<T> future,
|
|
|
|
@NotNull final BiConsumer<T, Throwable> consumer) {
|
|
|
|
future.whenComplete((value, exception) -> {
|
|
|
|
if (Bukkit.isPrimaryThread()) {
|
|
|
|
consumer.accept(value, exception);
|
|
|
|
} else {
|
|
|
|
Bukkit.getScheduler().runTask(plugin, () -> consumer.accept(value, exception));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-07-27 03:02:55 +02:00
|
|
|
|
|
|
|
|
2020-08-01 04:52:07 +02:00
|
|
|
@NotNull
|
|
|
|
public static <T> Collector<CompletableFuture<T>, ?, CompletableFuture<List<T>>> collector() {
|
|
|
|
return Collectors.collectingAndThen(Collectors.toList(), Futures::of);
|
|
|
|
}
|
2020-07-27 03:02:55 +02:00
|
|
|
|
|
|
|
|
2020-08-01 04:52:07 +02:00
|
|
|
@NotNull
|
|
|
|
public static <T> CompletableFuture<List<T>> of(
|
|
|
|
@NotNull final Stream<CompletableFuture<T>> futures) {
|
|
|
|
return of(futures.collect(Collectors.toList()));
|
|
|
|
}
|
2020-07-27 03:02:55 +02:00
|
|
|
|
2020-08-01 04:52:07 +02:00
|
|
|
@NotNull
|
|
|
|
public static <T> CompletableFuture<List<T>> of(
|
|
|
|
@NotNull final Collection<CompletableFuture<T>> futures) {
|
|
|
|
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
|
|
|
|
.thenApplyAsync($ -> awaitCompletion(futures));
|
|
|
|
}
|
2020-07-27 03:02:55 +02:00
|
|
|
|
2020-08-01 04:52:07 +02:00
|
|
|
@NotNull
|
|
|
|
private static <T> List<T> awaitCompletion(
|
|
|
|
@NotNull final Collection<CompletableFuture<T>> futures) {
|
|
|
|
return futures.stream().map(CompletableFuture::join).collect(Collectors.toList());
|
|
|
|
}
|
2020-07-27 03:02:55 +02:00
|
|
|
|
|
|
|
}
|