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

io.descoped.dc.api.PageContext Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package io.descoped.dc.api;

import io.descoped.dc.api.context.ExecutionContext;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

public class PageContext {

    private final List expectedPositions;
    private final List nextPositionVariableNames;
    private final Map nextPositionMap; // variableName, positionValue
    private final List parallelFutures = new ArrayList<>();
    private final CompletionInfo completionInfo = new CompletionInfo();
    private final AtomicBoolean endOfStream = new AtomicBoolean(false);
    private final AtomicReference failureCause = new AtomicReference<>();

    private PageContext(List expectedPositions, List nextPositionVariableNames, Map nextPositionMap) {
        this.expectedPositions = expectedPositions;
        this.nextPositionVariableNames = nextPositionVariableNames;
        this.nextPositionMap = nextPositionMap;
    }

    public static PageContext createEndOfStream() {
        PageContext pageContext = new PageContext(new LinkedList<>(), new LinkedList<>(), new LinkedHashMap<>());
        pageContext.endOfStream.set(true);
        return pageContext;
    }

    public List expectedPositions() {
        return expectedPositions;
    }

    public List nextPositionVariableNames() {
        return nextPositionVariableNames;
    }

    public Map nextPositionMap() {
        return nextPositionMap;
    }

    public void addFuture(CompletableFuture future) {
        parallelFutures.add(new Entry(future));
    }

    public List parallelPageEntries() {
        return parallelFutures;
    }

    public List> parallelFutures() {
        return parallelFutures.stream().map(Entry::future).collect(Collectors.toList());
    }

    public CompletionInfo completionInfo() {
        return completionInfo;
    }

    public void incrementQueueCount() {
        completionInfo.queueCount.incrementAndGet();
    }

    public void incrementCompletionCount() {
        completionInfo.completedCount.incrementAndGet();
    }

    public boolean isEndOfStream() {
        return endOfStream.get();
    }

    public void setEndOfStream(boolean endOfStream) {
        this.endOfStream.set(endOfStream);
    }

    public AtomicReference failureCause() {
        return failureCause;
    }

    public boolean isFailure() {
        return failureCause.get() != null;
    }

    public PageContext failure(Throwable cause) {
        failureCause.set(cause);
        return this;
    }

    public Throwable getFailureCause() {
        return failureCause.get();
    }

    public static class Entry {
        private CompletableFuture future;

        public Entry(CompletableFuture future) {
            this.future = future;
        }

        public CompletableFuture future() {
            return future;
        }
    }

    public static class CompletionInfo {
        final AtomicLong queueCount = new AtomicLong(0);
        final AtomicLong completedCount = new AtomicLong(0);

        public AtomicLong queueCount() {
            return queueCount;
        }

        public AtomicLong completedCount() {
            return completedCount;
        }
    }

    public static class Builder {
        private List expectedPositions;
        private Map nextPositionMap = new LinkedHashMap<>();
        private List nextPositionVariableNames;

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

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

        public List nextPositionVariableNames() {
            return nextPositionVariableNames;
        }

        public Builder addNextPosition(String variableName, String position) {
            nextPositionMap.put(variableName, position);
            return this;
        }

        public PageContext build() {
            return new PageContext(expectedPositions, nextPositionVariableNames, nextPositionMap);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy