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

graphql.language.Document Maven / Gradle / Ivy

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


import graphql.Internal;
import graphql.PublicApi;
import graphql.util.TraversalControl;
import graphql.util.TraverserContext;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@PublicApi
public class Document extends AbstractNode {

    private final List definitions;

    @Internal
    protected Document(List definitions, SourceLocation sourceLocation, List comments) {
        super(sourceLocation, comments);
        this.definitions = definitions;
    }

    /**
     * alternative to using a Builder for convenience
     *
     * @param definitions the definitions that make up this document
     */
    public Document(List definitions) {
        this(definitions, null, new ArrayList<>());
    }

    public List getDefinitions() {
        return definitions;
    }


    @Override
    public List getChildren() {
        return new ArrayList<>(definitions);
    }


    @Override
    public boolean isEqualTo(Node o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        return true;
    }

    @Override
    public Document deepCopy() {
        return new Document(deepCopy(definitions), getSourceLocation(), getComments());
    }

    @Override
    public String toString() {
        return "Document{" +
                "definitions=" + definitions +
                '}';
    }

    @Override
    public TraversalControl accept(TraverserContext context, NodeVisitor visitor) {
        return visitor.visitDocument(this, context);
    }

    public static Builder newDocument() {
        return new Builder();
    }

    public Document transform(Consumer builderConsumer) {
        Builder builder = new Builder(this);
        builderConsumer.accept(builder);
        return builder.build();
    }

    public static final class Builder implements NodeBuilder {
        private List definitions = new ArrayList<>();
        private SourceLocation sourceLocation;
        private List comments = new ArrayList<>();

        private Builder() {
        }

        private Builder(Document existing) {
            this.sourceLocation = existing.getSourceLocation();
            this.comments = existing.getComments();
            this.definitions = existing.getDefinitions();
        }

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

        public Builder definition(Definition definition) {
            this.definitions.add(definition);
            return this;
        }

        public Builder sourceLocation(SourceLocation sourceLocation) {
            this.sourceLocation = sourceLocation;
            return this;
        }

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

        public Document build() {
            Document document = new Document(definitions, sourceLocation, comments);
            return document;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy