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

graphql.incremental.IncrementalExecutionResultImpl Maven / Gradle / Ivy

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

import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.ExperimentalApi;
import org.jetbrains.annotations.Nullable;
import org.reactivestreams.Publisher;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@ExperimentalApi
public class IncrementalExecutionResultImpl extends ExecutionResultImpl implements IncrementalExecutionResult {
    private final boolean hasNext;
    private final List incremental;
    private final Publisher incrementalItemPublisher;

    private IncrementalExecutionResultImpl(Builder builder) {
        super(builder);
        this.hasNext = builder.hasNext;
        this.incremental = builder.incremental;
        this.incrementalItemPublisher = builder.incrementalItemPublisher;
    }

    @Override
    public boolean hasNext() {
        return this.hasNext;
    }

    @Nullable
    @Override
    public List getIncremental() {
        return this.incremental;
    }

    @Override
    public Publisher getIncrementalItemPublisher() {
        return incrementalItemPublisher;
    }

    /**
     * @return a {@link Builder} that can be used to create an instance of {@link IncrementalExecutionResultImpl}
     */
    public static Builder newIncrementalExecutionResult() {
        return new Builder();
    }

    public static Builder fromExecutionResult(ExecutionResult executionResult) {
        return new Builder().from(executionResult);
    }

    @Override
    public IncrementalExecutionResult transform(Consumer> builderConsumer) {
        var builder = fromExecutionResult(this);
        builderConsumer.accept(builder);
        return builder.build();
    }

    @Override
    public Map toSpecification() {
        Map map = new LinkedHashMap<>(super.toSpecification());
        map.put("hasNext", hasNext);

        if (this.incremental != null) {
            map.put("incremental",
                    this.incremental.stream()
                            .map(IncrementalPayload::toSpecification)
                            .collect(Collectors.toCollection(LinkedList::new))
            );
        }

        return map;
    }

    public static class Builder extends ExecutionResultImpl.Builder {
        private boolean hasNext = true;
        public List incremental;
        private Publisher incrementalItemPublisher;

        public Builder hasNext(boolean hasNext) {
            this.hasNext = hasNext;
            return this;
        }

        public Builder incremental(List incremental) {
            this.incremental = incremental;
            return this;
        }

        public Builder incrementalItemPublisher(Publisher incrementalItemPublisher) {
            this.incrementalItemPublisher = incrementalItemPublisher;
            return this;
        }

        public Builder from(IncrementalExecutionResult incrementalExecutionResult) {
            super.from(incrementalExecutionResult);
            this.hasNext = incrementalExecutionResult.hasNext();
            this.incremental = incrementalExecutionResult.getIncremental();
            this.incrementalItemPublisher = incrementalExecutionResult.getIncrementalItemPublisher();
            return this;
        }

        public IncrementalExecutionResult build() {
            return new IncrementalExecutionResultImpl(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy