graphql.execution.instrumentation.SimpleInstrumentationContext Maven / Gradle / Ivy
package graphql.execution.instrumentation;
import graphql.PublicApi;
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 final BiConsumer codeToRunOnComplete;
private final Consumer> codeToRunOnDispatch;
public SimpleInstrumentationContext() {
this(null, null);
}
private SimpleInstrumentationContext(Consumer> codeToRunOnDispatch, BiConsumer codeToRunOnComplete) {
this.codeToRunOnComplete = codeToRunOnComplete;
this.codeToRunOnDispatch = codeToRunOnDispatch;
}
@Override
public void onDispatched(CompletableFuture result) {
if (codeToRunOnDispatch != null) {
codeToRunOnDispatch.accept(result);
}
}
@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(Consumer> 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);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy