All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opensearch.migrations.replay.util.TextTrackedFuture Maven / Gradle / Ivy

There is a newer version: 0.2.0.4
Show newest version
package org.opensearch.migrations.replay.util;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Stream;

import lombok.NonNull;

public class TextTrackedFuture extends TrackedFuture {

    public TextTrackedFuture(String diagnosticLabel) {
        this(new CompletableFuture<>(), () -> diagnosticLabel);
    }

    public TextTrackedFuture(Supplier diagnosticSupplier) {
        this(new CompletableFuture<>(), diagnosticSupplier);
    }

    public TextTrackedFuture(@NonNull CompletableFuture future, Supplier diagnosticSupplier) {
        super(future, diagnosticSupplier);
    }

    public TextTrackedFuture(@NonNull CompletableFuture future, String diagnosticLabel) {
        super(future, () -> diagnosticLabel);
    }

    public static  TextTrackedFuture failedFuture(Throwable e, Supplier diagnosticSupplier) {
        return new TextTrackedFuture<>(CompletableFuture.failedFuture(e), diagnosticSupplier);
    }

    public static  TextTrackedFuture completedFuture(U v, Supplier diagnosticSupplier) {
        return new TextTrackedFuture<>(CompletableFuture.completedFuture(v), diagnosticSupplier);
    }

    public static  TextTrackedFuture allOf(
        TrackedFuture[] allRemainingWorkArray,
        Supplier diagnosticSupplier
    ) {
        return allOf(Arrays.stream(allRemainingWorkArray), diagnosticSupplier);
    }

    public static  TextTrackedFuture allOf(
        Stream> allRemainingWorkStream,
        Supplier diagnosticSupplier
    ) {
        return new TextTrackedFuture<>(
            CompletableFuture.allOf(allRemainingWorkStream.map(tcf -> tcf.future).toArray(CompletableFuture[]::new)),
            diagnosticSupplier
        );

    }
}