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

com.github.karamelsoft.flow.api.Flow Maven / Gradle / Ivy

package com.github.karamelsoft.flow.api;

import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * Interface that represents a functional flow.
 *
 * @author Frederic Gendebien ([email protected]).
 */
public interface Flow {

    /**
     * Gets the {@link Optional} value of the current {@link Flow}.
     *
     * @return an {@link Optional} wrapping the value.
     */
    Optional value();

    /**
     * Gets the value of the current {@link Flow} or specify a default.
     *
     * @param defaultValue to use.
     *
     * @return
     *  the current {@link Flow} value if the current {@link Flow} is active.
     *  The given default value otherwise.
     */
    T orElse(T defaultValue);

    /**
     * Gets the value of the current {@link Flow} or the {@link Supplier} value.
     *
     * @param supplier to use.
     *
     * @return
     *  the current {@link Flow} value if the current {@link Flow} is active.
     *  The given {@link Supplier} value otherwise.
     */
    T orElseGet(Supplier supplier);

    /**
     * Continues a new {@link Flow} with the given value.
     *
     * @param  the resulting {@link Flow} type.
     * @param value is the value to continue with.
     *
     * @return a new {@link Flow} instance.
     */
     Flow continueWith(U value);

    /**
     * Continues a new {@link Flow} with the given {@link Supplier}.
     *
     * @param  the resulting {@link Flow} type.
     * @param supplier is the {@link Supplier} to continue with.
     *
     * @return a new {@link Flow} instance.
     */
     Flow continueWith(Supplier supplier);

    /**
     * Executes a given {@link Script} on the current {@link Flow}.
     *
     * @param  the resulting {@link Flow} type.
     * @param script {@link Script} on this current {@link Flow}.
     *
     * @return the current {@link Flow} instance.
     */
     Flow continueApplying(final Script script);

    /**
     * Continues a new {@link Flow} with the given value if and only if
     * the current {@link Flow} has no defined value.
     *
     * @param value is the value to continue with.
     *
     * @return  a new {@link Flow} instance if the current {@link Flow} is inactive.
     *          The current {@link Flow} otherwise.
     */
    Flow orContinueWith(T value);

    /**
     * Continues a new {@link Flow} with the given {@link Supplier} if and only if
     * the current {@link Flow} has no defined value.
     *
     * @param supplier is the {@link Supplier} to continue with.
     *
     * @return  a new {@link Flow} instance if the current {@link Flow} is inactive.
     *          The current {@link Flow} otherwise.
     */
    Flow orContinueWith(Supplier supplier);

    /**
     * Filters the current {@link Flow} with a given {@link Predicate}.
     *
     * @param predicate is the {@link Predicate} of the filtering.
     *
     * @return the current {@link Flow} instance if the predicate is true, an inactive instance
     * otherwise.
     */
    Flow filter(Predicate predicate);

    /**
     * Maps the current value with the given {@link Function} if and only if
     * the current {@link Flow} has no defined value.
     *
     * @param  the resulting {@link Flow} type.
     * @param function is the {@link Function} used to map.
     *
     * @return the resulting {@link Flow} new instance.
     */
     Flow map(Function function);

    /**
     * Executes a given {@link Consumer} if and only if the current {@link Flow} is active.
     *
     * @param consumer is the {@link Consumer} to apply.
     *
     * @return the current {@link Flow} instance
     */
    Flow apply(Consumer consumer);

    /**
     * Splits the current {@link Flow} into several flows.
     *
     * @return an instance of {@link Choice}.
     */
    Choice choice();

    /**
     * Aggreagates the current {@link Flow} with another given {@link Flow} using a given {@link BiFunction}
     * as aggregate function.
     *
     * @param  the resulting {@link Flow} type.
     * @param  the resource {@link Flow} type.
     * @param resource is the {@link Flow} to aggregate the current {@link Flow} with.
     * @param function is the {@link BiFunction} used to aggregate both {@link Flow}.
     *
     * @return the resulting {@link Flow} new instance.
     */
     Flow aggregateWith(Flow resource, BiFunction function);

    /**
     * Executes a given {@link Runnable} if and only if this current {@link Flow} is active.
     *
     * @param runnable is the {@link Runnable} to execute.
     *
     * @return the current {@link Flow} instance.
     */
    Flow execute(final Runnable runnable);

    /**
     * Executes a given {@link Runnable} if and only if the current {@link Flow} has no defined value.
     *
     * @param runnable is the {@link Runnable} to execute.
     *
     * @return the current {@link Flow} instance.
     */
    Flow orExecute(Runnable runnable);
}