package me.shawlaf.banmanager.core.async;

import java.util.function.Consumer;

public abstract class SimpleFuture<F> {

    private F result;
    private Consumer<F> whenDone = f -> {
    };

    public boolean isDone() {
        return result != null;
    }

    public void done(F result) {
        whenDone.accept(result);
        this.result = result;
    }

    public final SimpleFuture<F> get() {
        Multithreading.runAsync(this::run);
        return this;
    }

    protected abstract void run();

    public void waitUntilDone() { // Locks the thread until execution is done
        while (! isDone())
            continue;
    }

    public SimpleFuture<F> whenDone(Consumer<F> whenDone) {
        if (isDone())
            whenDone.accept(result);

        this.whenDone = whenDone;
        return this;
    }

}
