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

net.cassite.f.Flow Maven / Gradle / Ivy

package net.cassite.f;

import io.vertx.core.Future;
import org.jetbrains.annotations.NotNull;

import java.util.function.Supplier;

public class Flow {
    private Flow() {
    }

    public static  FlowStmt store(@NotNull Ptr ptr, @NotNull Supplier> f) {
        if (ptr == null)
            throw new NullPointerException();
        if (f == null)
            throw new NullPointerException();
        return new FlowStmt().store(ptr, f);
    }

    public static  FlowStmt exec(@NotNull Supplier> f) {
        if (f == null)
            throw new NullPointerException();
        return new FlowStmt().exec(f);
    }

    public static FlowStmt exec(@NotNull Runnable f) {
        if (f == null)
            throw new NullPointerException();
        return new FlowStmt().exec(f);
    }

    public static class FlowStmt {
        private Future fu = null;

        FlowStmt() {
        }

        public  FlowStmt store(@NotNull Ptr ptr, @NotNull Supplier> f) {
            if (ptr == null)
                throw new NullPointerException();
            if (f == null)
                throw new NullPointerException();
            return exec(() -> ptr.store(f.get()));
        }

        public  FlowStmt exec(@NotNull Supplier> f) {
            if (f == null)
                throw new NullPointerException();
            if (fu == null) {
                fu = f.get();
            } else {
                fu = fu.compose(v -> f.get());
            }
            return this;
        }

        public FlowStmt exec(@NotNull Runnable f) {
            if (f == null)
                throw new NullPointerException();
            return exec(() -> {
                f.run();
                return F.unit();
            });
        }

        public  Monad returnFuture(@NotNull Supplier> f) {
            if (f == null)
                throw new NullPointerException();
            return Monad.transform(fu.compose(v -> f.get()));
        }

        public  Monad returnValue(@NotNull Supplier f) {
            if (f == null)
                throw new NullPointerException();
            return Monad.transform(fu.map(v -> f.get()));
        }

        public  Monad returnPtr(@NotNull Ptr ptr) {
            if (ptr == null)
                throw new NullPointerException();
            return Monad.transform(fu.map(v -> ptr.get()));
        }

        public Monad returnNull() {
            return Monad.transform(fu.map(v -> Null.value));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy