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

functionalj.promise.DeferAction 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.promise;

import static functionalj.function.Func.carelessly;
import static functionalj.function.Func.f;
import static functionalj.list.FuncList.listOf;
import static functionalj.promise.RaceResult.Race;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Predicate;

import functionalj.environments.AsyncRunner;
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.FuncUnit0;
import functionalj.function.FuncUnit1;
import functionalj.function.NamedExpression;
import functionalj.list.FuncList;
import functionalj.pipeable.Pipeable;
import functionalj.result.Result;
import lombok.val;

@SuppressWarnings("javadoc")
public class DeferAction extends UncompletedAction implements Pipeable> {
    
    public static  DeferAction createNew() {
        return of((Class)null);
    }
    public static  DeferAction createNew(OnStart onStart) {
        return of((Class)null, onStart);
    }
    public static  DeferAction of(Class clzz) {
        return new DeferAction();
    }
    public static  DeferAction of(Class clzz, OnStart onStart) {
        return new DeferAction(null, onStart);
    }
    
    public static  DeferAction ofValue(D value) {
        val action = new DeferAction();
        action.getPromise().makeComplete(value);
        return action;
    }
    
    public static DeferAction from(FuncUnit0 runnable) {
        return DeferActionConfig.current.value().createBuilder(runnable).build();
    }
    public static  DeferAction from(Func0 supplier) {
        return DeferActionConfig.current.value().createBuilder(supplier).build();
    }
    public static  DeferAction from(CompletableFuture completableFucture) {
        val action = DeferAction.of((Class)null);
        val pending = action.start();
        
        completableFucture.handle((value, exception) -> {
            if (exception != null) {
                if (exception instanceof Exception)
                     pending.fail((Exception)exception);
                else pending.fail(new RuntimeException("CompletableFuture completed with failure: ", exception));
            } else {
                pending.complete(value);
            }
            
            return null;
        });
        return action;
    }
    
    public static PendingAction run(FuncUnit0 runnable) {
        return from(runnable)
                .start();
    }
    public static  PendingAction run(Func0 supplier) {
        return from(supplier)
                .start();
    }
    
    @SafeVarargs
    public static  RaceResult AnyOf(StartableAction ... actions) {
        return Race(FuncList.of(actions));
    }
    
    public static  RaceResult AnyOf(List> actions) {
        return Race(actions);
    }
    @SafeVarargs
    public static  RaceResult race(StartableAction ... actions) {
        return Race(FuncList.of(actions));
    }
    
    public static  RaceResult race(List> actions) {
        return Race(actions);
    }
    
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static  DeferAction from(
            NamedExpression> promise1,
            NamedExpression> promise2,
            Func2                merger) {
        val merge = (Func1)f((FuncList results)-> {
            val result1 = (Result)results.get(0);
            val result2 = (Result)results.get(1);
            val mergedResult = Result.ofResults(result1, result2, merger);
            return (Result)mergedResult;
        });
        val promises = listOf(promise1, promise2);
        val combiner = new CombineResult(promises, merge);
        val action   = combiner.getDeferAction();
        return action;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static  DeferAction from(
            NamedExpression> promise1,
            NamedExpression> promise2,
            NamedExpression> promise3,
            Func3            merger) {
        val merge = (Func1)f((FuncList results)-> {
            val result1 = (Result)results.get(0);
            val result2 = (Result)results.get(1);
            val result3 = (Result)results.get(2);
            val mergedResult = Result.ofResults(result1, result2, result3, merger);
            return (Result)mergedResult;
        });
        val promises = listOf(promise1, promise2, promise3);
        val combiner = new CombineResult(promises, merge);
        val action   = combiner.getDeferAction();
        return action;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static  DeferAction from(
            NamedExpression> promise1,
            NamedExpression> promise2,
            NamedExpression> promise3,
            NamedExpression> promise4,
            Func4        merger) {
        val merge = (Func1)f((FuncList results)-> {
            val result1 = (Result)results.get(0);
            val result2 = (Result)results.get(1);
            val result3 = (Result)results.get(2);
            val result4 = (Result)results.get(3);
            val mergedResult = Result.ofResults(result1, result2, result3, result4, merger);
            return (Result)mergedResult;
        });
        val promises = listOf(promise1, promise2, promise3, promise4);
        val combiner = new CombineResult(promises, merge);
        val action   = combiner.getDeferAction();
        return action;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static  DeferAction from(
            NamedExpression> promise1,
            NamedExpression> promise2,
            NamedExpression> promise3,
            NamedExpression> promise4,
            NamedExpression> promise5,
            Func5    merger) {
        val merge = (Func1)f((FuncList results)-> {
            val result1 = (Result)results.get(0);
            val result2 = (Result)results.get(1);
            val result3 = (Result)results.get(2);
            val result4 = (Result)results.get(3);
            val result5 = (Result)results.get(4);
            val mergedResult = Result.ofResults(result1, result2, result3, result4, result5, merger);
            return (Result)mergedResult;
        });
        val promises = listOf(promise1, promise2, promise3, promise4, promise5);
        val combiner = new CombineResult(promises, merge);
        val action   = combiner.getDeferAction();
        return action;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static  DeferAction from(
            NamedExpression>  promise1,
            NamedExpression>  promise2,
            NamedExpression>  promise3,
            NamedExpression>  promise4,
            NamedExpression>  promise5,
            NamedExpression>  promise6,
            Func6 merger) {
        val merge = (Func1)f((FuncList results)-> {
            val result1 = (Result)results.get(0);
            val result2 = (Result)results.get(1);
            val result3 = (Result)results.get(2);
            val result4 = (Result)results.get(3);
            val result5 = (Result)results.get(4);
            val result6 = (Result)results.get(5);
            val mergedResult = Result.ofResults(result1, result2, result3, result4, result5, result6, merger);
            return (Result)mergedResult;
        });
        val promises = listOf(promise1, promise2, promise3, promise4, promise5, promise6);
        val combiner = new CombineResult(promises, merge);
        val action   = combiner.getDeferAction();
        return action;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static  DeferAction from(
            Func1, D> merger,
            NamedExpression> ... promises) {
        val merge = f((Result[] results)-> {
            val resultList = listOf(results).map(Result::get);
            val mergedResult = merger.apply(resultList);
            return (Result)mergedResult;
        });
        val promiseList = listOf(promises);
        val combiner    = new CombineResult(promiseList, merge);
        val action      = combiner.getDeferAction();
        return action;
    }
    
    
    public static  DeferAction create(
            boolean     interruptOnCancel,
            Func0    supplier,
            Runnable    onStart,
            AsyncRunner runner) {
        return DeferActionCreator.current.value()
                .create(supplier, onStart, interruptOnCancel, runner);
    }
    
    private final Runnable task;
    
    private final DeferAction parent;
    
    DeferAction() {
        this(null, (OnStart)null);
    }
    DeferAction(DeferAction parent, Promise promise) {
        super(promise);
        this.parent = parent;
        this.task   = null;
    }
    DeferAction(Runnable task, OnStart onStart) {
        super(onStart);
        this.parent = null;
        this.task   = task;
    }
    
    public PendingAction start() {
        if (parent != null) {
            parent.start();
        } else {
            val isStarted = promise.start();
            
            if (!isStarted && (task != null))
                carelessly(task);
        }
        return new PendingAction<>(promise);
    }
    
    public HasPromise __data() throws Exception {
        return this;
    }
    
    //== Subscription ==
    
    public DeferAction use(Consumer> consumer) {
        carelessly(()->{
            consumer.accept(promise);
        });
        
        return this;
    }
    
    public DeferAction abortNoSubsriptionAfter(Wait wait) {
        promise.abortNoSubscriptionAfter(wait);
        return this;
    }
    
    public DeferAction subscribe(FuncUnit1 resultConsumer) {
        promise.subscribe(Wait.forever(), resultConsumer);
        return this;
    }
    
    public final DeferAction subscribe(Wait wait, FuncUnit1 resultConsumer) {
        promise.subscribe(wait, resultConsumer);
        return this;
    }
    
    public DeferAction onComplete(FuncUnit1> resultConsumer) {
        promise.onComplete(Wait.forever(), resultConsumer);
        return this;
    }
    
    public DeferAction onComplete(Wait wait, FuncUnit1> resultConsumer) {
        promise.onComplete(wait, resultConsumer);
        return this;
    }
    
    public DeferAction onComplete(
            FuncUnit1>       resultConsumer,
            FuncUnit1> subscriptionConsumer) {
        val subscription = promise.onComplete(Wait.forever(), resultConsumer);
        carelessly(() -> subscriptionConsumer.accept(subscription));
        return this;
    }
    
    public DeferAction onComplete(
            Wait                          wait,
            FuncUnit1>       resultConsumer,
            FuncUnit1> subscriptionConsumer) {
        val subscription = promise.onComplete(wait, resultConsumer);
        carelessly(() -> subscriptionConsumer.accept(subscription));
        return this;
    }
    
    public DeferAction eavesdrop(FuncUnit1> resultConsumer) {
        promise.eavesdrop(resultConsumer);
        return this;
    }
    
    public DeferAction eavesdrop(Wait wait, FuncUnit1> resultConsumer) {
        promise.eavesdrop(wait, resultConsumer);
        return this;
    }
    
    //== Functional ==
    
    public final DeferAction filter(Predicate predicate) {
        val newPromise = promise.filter(predicate);
        return new DeferAction(this, newPromise);
    }
    
    public final DeferAction peek(FuncUnit1 peeker) {
        val newPromise = promise.peek(peeker);
        return new DeferAction(this, (Promise)newPromise);
    }
    
    @SuppressWarnings("unchecked")
    public final  DeferAction map(Func1 mapper) {
        val newPromise = promise.map(mapper);
        val newAction  = new DeferAction(this, (Promise)newPromise);
        return newAction;
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public final  DeferAction flatMap(Func1> mapper) {
        return chain((Func1)mapper);
    }
    
    public final  DeferAction chain(Func1> mapper) {
        val newPromise = promise.chain(mapper);
        return new DeferAction(this, (Promise)newPromise);
    }
    
    // TODO - Other F-M-FM methods.
    
}