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

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

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

import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import lombok.NonNull;
import lombok.SneakyThrows;

public class TrackedFutureStringFormatter {

    private TrackedFutureStringFormatter() {}

    public static  String format(TrackedFuture f) {
        return format(f, x -> null);
    }

    public static  String format(
        TrackedFuture f,
        @NonNull Function, String> resultFormatter
    ) {
        return f.walkParentsAsStream()
            .map(kvp -> stringFormatFutureWithDiagnostics(f, kvp, resultFormatter))
            .collect(Collectors.joining("<-"));
    }

    @SneakyThrows
    protected static  String stringFormatFutureWithDiagnostics(
        TrackedFuture f,
        @NonNull TrackedFuture tf,
        @NonNull Function, String> resultFormatter
    ) {
        var diagnosticInfo = tf.diagnosticSupplier.get();
        var isDone = tf.isDone();
        return "["
            + System.identityHashCode(tf)
            + "] "
            + diagnosticInfo
            + (isDone
                ? "[" + Optional.ofNullable(resultFormatter.apply(tf)).orElse("^") + "]"
                : Optional.ofNullable(tf.innerComposedPendingCompletableFutureReference)
                    .map(r -> (TrackedFuture) r.get())
                    .map(df -> " --[[" + format(df, resultFormatter) + " ]] ")
                    .orElse("[…]"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy