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

io.github.dailystruggle.rtp.common.selection.region.ChunkSet Maven / Gradle / Ivy

There is a newer version: 2.0.15
Show newest version
package io.github.dailystruggle.rtp.common.selection.region;

import io.github.dailystruggle.rtp.common.serverSide.substitutions.RTPChunk;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

public final class ChunkSet {
    public final List> chunks;
    public final CompletableFuture complete;

    public ChunkSet(List> chunks, CompletableFuture complete) {
        this.chunks = chunks;
        this.complete = complete;

        AtomicLong count = new AtomicLong();
        Semaphore countAccess = new Semaphore(1);
        chunks.forEach(rtpChunkCompletableFuture -> rtpChunkCompletableFuture.thenAccept(rtpChunk -> {
            try {
                countAccess.acquire();
                long i = count.incrementAndGet();
                if (i == chunks.size()) {
                    this.complete.complete(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                countAccess.release();
            }
        }));
    }

    public void keep(boolean keep) {
        chunks.forEach(chunk -> {
            if (chunk.isDone()) {
                try {
                    RTPChunk rtpChunk = chunk.get();
                    rtpChunk.keep(keep);
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            } else {
                chunk.thenAccept(chunk1 -> chunk1.keep(keep));
            }
        });
    }

    public void whenComplete(Consumer consumer) {
        if (complete.isDone()) {
            try {
                consumer.accept(complete.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
            return;
        }

        complete.thenAccept(consumer);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy