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

com.spring.boxes.dollar.support.graphql.instrument.NestFutureTask Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.dollar.support.graphql.instrument;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

@Slf4j
public class NestFutureTask {

    // 是否是query下的字段
    // 当前字段所表示的任务执行完毕、则其子实体(数组实体)所表示的任务则也一定完成
    private final boolean topNode;

    // for debug
    private final String path;

    // 当前节点是否是 list节点、并且不是叶子节点
    private final boolean list;

    // 当前节点是否是被 @node 注视的节点
    private final boolean letAnnotated;

    // 如果是list路径下的节点,则在最后一个节点计算完成前,将结果暂存在 tmpResult 中
    private final List tempResult = Collections.synchronizedList(new ArrayList<>());

    private final CompletableFuture future;

    private final NestFutureTask parent;

    private final List> subTaskList = new ArrayList<>();

    public void addSubTaskList(NestFutureTask subTask) {
        subTaskList.add(subTask);
    }

    public List> getSubTaskList() {
        return subTaskList;
    }

    private NestFutureTask(boolean topNode, String path, boolean list, boolean letAnnotated, CompletableFuture future, NestFutureTask parent) {
        this.topNode = topNode;
        this.path = path;
        this.list = list;
        this.letAnnotated = letAnnotated;
        this.future = future;
        this.parent = parent;
    }

    public boolean isLetAnnotated() {
        return letAnnotated;
    }

    public boolean isTopNode() {
        return topNode;
    }

    public String getPath() {
        return path;
    }

    public boolean isList() {
        return list;
    }

    public List getTempResult() {
        return tempResult;
    }

    public CompletableFuture getFuture() {
        return future;
    }

    public NestFutureTask getParent() {
        return parent;
    }

    public static  FutureTaskBuilder newBuilder() {
        return new FutureTaskBuilder();
    }

    public static class FutureTaskBuilder {

        private Boolean topNode;

        private String path;

        private Boolean list;

        private Boolean letAnnotated;

        private CompletableFuture future;

        private NestFutureTask parent;


        public FutureTaskBuilder isTopNode(boolean isTopNode) {
            this.topNode = isTopNode;
            return this;
        }

        public FutureTaskBuilder path(String path) {
            this.path = path;
            return this;
        }

        public FutureTaskBuilder isList(boolean val) {
            this.list = val;
            return this;
        }

        public FutureTaskBuilder isLetAnnotated(boolean val) {
            this.letAnnotated = val;
            return this;
        }

        public FutureTaskBuilder future(CompletableFuture future) {
            this.future = future;
            return this;
        }

        public FutureTaskBuilder parent(NestFutureTask parent) {
            this.parent = parent;
            return this;
        }

        public NestFutureTask build() {
            Objects.requireNonNull(topNode);
            Objects.requireNonNull(path);
            Objects.requireNonNull(list);
            Objects.requireNonNull(letAnnotated);
            Objects.requireNonNull(future);

            return new NestFutureTask(topNode, path, list, letAnnotated, future, parent);
        }
    }
}