FutureChain .java

net.minecraft.util.FutureChain (class, 46 lines): members: append, executor, closed, head, close, LOGGER. imports: LogUtils.

net.minecraft.utilclass46 linescommon1.3 KB

Implements

API Surface (2)

<T> void append(final CompletableFuture<T> preparation, final Consumer<T> chainedTask)
void close()

Fields & Constants (4)

LOGGER
head
executor
closed

String literals (1)

"Chain link failed, continuing to next one"

Code Graph: Dependencies (1)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

FutureChain.java46 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import com.mojang.logging.LogUtils;
4
import java.util.concurrent.CancellationException;
5
import java.util.concurrent.CompletableFuture;
6
import java.util.concurrent.CompletionException;
7
import java.util.concurrent.Executor;
8
import java.util.function.Consumer;
9
import org.slf4j.Logger;
10
11
public class FutureChain implements TaskChainer, AutoCloseable {
12
	private static final Logger LOGGER = LogUtils.getLogger();
13
	private CompletableFuture<?> head = CompletableFuture.completedFuture(null);
14
	private final Executor executor;
15
	private volatile boolean closed;
16
17
	public FutureChain(final Executor executor) {
18
		this.executor = executor;
19
	}
20
21
	@Override
22
	public <T> void append(final CompletableFuture<T> preparation, final Consumer<T> chainedTask) {
23
		this.head = this.head.<T, Object>thenCombine(preparation, (ignored, value) -> value).thenAcceptAsync(value -> {
24
			if (!this.closed) {
25
				chainedTask.accept((T)value);
26
			}
27
		}, this.executor).exceptionally(t -> {
28
			if (t instanceof CompletionException c) {
29
				t = c.getCause();
30
			}
31
32
			if (t instanceof CancellationException c) {
33
				throw c;
34
			} else {
35
				LOGGER.error("Chain link failed, continuing to next one", t);
36
				return null;
37
			}
38
		});
39
	}
40
41
	@Override
42
	public void close() {
43
		this.closed = true;
44
	}
45
}
46

Decompiled Minecraft 26.3 source of FutureChain (net/minecraft/util/FutureChain.java) with Mojang official mappings, stored verbatim on the site.