org.opensearch.migrations.replay.util.TrackedFutureStringFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trafficReplayer Show documentation
Show all versions of trafficReplayer Show documentation
Everything opensearch migrations
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("[…]"));
}
}