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

graphql.execution.instrumentation.SimpleInstrumentationContext Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.execution.instrumentation;

import graphql.PublicApi;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * A simple implementation of {@link InstrumentationContext}
 */
@PublicApi
public class SimpleInstrumentationContext implements InstrumentationContext {

    private static final InstrumentationContext NO_OP = new InstrumentationContext() {
        @Override
        public void onDispatched() {
        }

        @Override
        public void onCompleted(Object result, Throwable t) {
        }
    };

    /**
     * A context that does nothing
     *
     * @param  the type needed
     *
     * @return a context that does nothing
     */
    @SuppressWarnings("unchecked")
    public static  InstrumentationContext noOp() {
        return (InstrumentationContext) NO_OP;
    }

    /**
     * This creates a no-op {@link InstrumentationContext} if the one pass in is null
     *
     * @param nullableContext a {@link InstrumentationContext} that can be null
     * @param              for two
     *
     * @return a non null {@link InstrumentationContext} that maybe a no-op
     */
    @NotNull
    public static  InstrumentationContext nonNullCtx(InstrumentationContext nullableContext) {
        return nullableContext == null ? noOp() : nullableContext;
    }

    private final BiConsumer codeToRunOnComplete;
    private final Runnable codeToRunOnDispatch;

    public SimpleInstrumentationContext() {
        this(null, null);
    }

    private SimpleInstrumentationContext(Runnable codeToRunOnDispatch, BiConsumer codeToRunOnComplete) {
        this.codeToRunOnComplete = codeToRunOnComplete;
        this.codeToRunOnDispatch = codeToRunOnDispatch;
    }

    @Override
    public void onDispatched() {
        if (codeToRunOnDispatch != null) {
            codeToRunOnDispatch.run();
        }
    }

    @Override
    public void onCompleted(T result, Throwable t) {
        if (codeToRunOnComplete != null) {
            codeToRunOnComplete.accept(result, t);
        }
    }

    /**
     * Allows for the more fluent away to return an instrumentation context that runs the specified
     * code on instrumentation step dispatch.
     *
     * @param codeToRun the code to run on dispatch
     * @param        the generic type
     *
     * @return an instrumentation context
     */
    public static  SimpleInstrumentationContext whenDispatched(Runnable codeToRun) {
        return new SimpleInstrumentationContext<>(codeToRun, null);
    }

    /**
     * Allows for the more fluent away to return an instrumentation context that runs the specified
     * code on instrumentation step completion.
     *
     * @param codeToRun the code to run on completion
     * @param        the generic type
     *
     * @return an instrumentation context
     */
    public static  SimpleInstrumentationContext whenCompleted(BiConsumer codeToRun) {
        return new SimpleInstrumentationContext<>(null, codeToRun);
    }

    public static  BiConsumer completeInstrumentationCtxCF(
            InstrumentationContext instrumentationContext) {
        return (result, throwable) -> {
            nonNullCtx(instrumentationContext).onCompleted(result, throwable);
        };
    }

}