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

functionalj.task.Task Maven / Gradle / Ivy

// ============================================================================
// Copyright (c) 2017-2019 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.task;

import java.util.List;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.function.Supplier;

import functionalj.function.Func0;
import functionalj.function.Func1;
import functionalj.function.Func2;
import functionalj.function.Func3;
import functionalj.function.Func4;
import functionalj.function.Func5;
import functionalj.function.Func6;
import functionalj.function.FuncUnit1;
import functionalj.function.Named;
import functionalj.list.FuncList;
import functionalj.promise.DeferAction;
import functionalj.promise.DeferActionBuilder;
import functionalj.promise.Promise;
import functionalj.result.Result;
import functionalj.task.Tasks.TaskCached;
import functionalj.task.Tasks.TaskCachedFor;
import functionalj.task.Tasks.TaskChain;
import functionalj.task.Tasks.TaskFilter;
import functionalj.task.Tasks.TaskMap;
import functionalj.task.Tasks.TaskMerge2;
import functionalj.task.Tasks.TaskMerge3;
import functionalj.task.Tasks.TaskMerge4;
import functionalj.task.Tasks.TaskMerge5;
import functionalj.task.Tasks.TaskMerge6;
import functionalj.task.Tasks.TaskPeek;
import functionalj.task.Tasks.TaskPromise;
import functionalj.task.Tasks.TaskRace;
import functionalj.task.Tasks.TaskResult;
import functionalj.task.Tasks.TaskSupplier;
import functionalj.task.Tasks.TaskValue;

@FunctionalInterface
public interface Task {
    
    public DeferAction createAction();
    
    
    public static  Task ofValue(D value) {
        return new TaskValue<>(value);
    }
    public static  DeferActionBuilder from(Func0 supplier) {
        return new TaskSupplier<>(supplier);
    }
    public static  Task from(Result result) {
        return new TaskResult<>(result);
    }
    public static  Task from(Promise promise) {
        return new TaskPromise<>(promise);
    }
    
    public static  Task from(
            Task           io1,
            Task           io2,
            Func2 merger) {
        return new TaskMerge2<>(io1, io2, merger);
    }
    public static  Task from(
            Task               io1,
            Task               io2,
            Task               io3,
            Func3 merger) {
        return new TaskMerge3<>(io1, io2, io3, merger);
    }
    public static  Task from(
            Task                   io1,
            Task                   io2,
            Task                   io3,
            Task                   io4,
            Func4 merger) {
        return new TaskMerge4<>(io1, io2, io3, io4, merger);
    }
    public static  Task from(
            Task                       io1,
            Task                       io2,
            Task                       io3,
            Task                       io4,
            Task                       io5,
            Func5 merger) {
        return new TaskMerge5<>(io1, io2, io3, io4, io5, merger);
    }
    public static  Task from(
            Task                           io1,
            Task                           io2,
            Task                           io3,
            Task                           io4,
            Task                           io5,
            Task                           io6,
            Func6 merger) {
        return new TaskMerge6<>(io1, io2, io3, io4, io5, io6, merger);
    }
    
    @SafeVarargs
    public static  Task firstOf(Task ... ios) {
        return new TaskRace(FuncList.from(ios));
    }
    public static  Task firstFrom(List> ios) {
        return new TaskRace(FuncList.from(ios));
    }
    
    public static  Task doUntil(Task body, Predicate> breakCondition) {
        return new Tasks.TaskDoUntil(body, breakCondition);
    }
    
    // TODO - Do while that has Task as a way to check if the loop should still continue.
    
    public default Task peek(FuncUnit1 keeper) {
        return new TaskPeek<>(this, keeper);
    }
    
    public default  Task map(Func1 mapper) {
        return new TaskMap<>(this, mapper);
    }
    
    public default  Task flatMap(Func1> mapper) {
        return new TaskChain<>(this, mapper);
    }
    
    public default  Task chain(Func1> mapper) {
        return new TaskChain<>(this, mapper);
    }
    
    public default Task filter(Predicate predicate) {
        return new TaskFilter<>(this, predicate);
    }
    
    public default  Task cached() {
        return new TaskCached<>(this);
    }
    public default  Task cached(Supplier theContext) {
        return cached(theContext, Named.BiPredicate("when-change", (S o, S n)->!Objects.equals(o, n)));
    }
    public default  Task cached(Supplier contextSupplier, BiPredicate staleChecker) {
        return new TaskCachedFor<>(this, contextSupplier, staleChecker);
    }
    
}