Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
functionalj.promise.DeferAction Maven / Gradle / Ivy
// ============================================================================
// Copyright (c) 2017-2021 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.io.PrintStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
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;
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.FuncUnit2;
import functionalj.function.NamedExpression;
import functionalj.list.FuncList;
import functionalj.pipeable.Pipeable;
import functionalj.result.Result;
import functionalj.result.ResultStatus;
import functionalj.result.ValidationException;
import functionalj.tuple.Tuple2;
import functionalj.validator.Validator;
import lombok.val;
@SuppressWarnings({ "unchecked", "rawtypes" })
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 defer(FuncUnit0 runnable) {
return DeferAction.from(runnable);
}
public static DeferAction defer(Func0 supplier) {
return DeferAction.from(supplier);
}
public static DeferAction defer(CompletableFuture completableFucture) {
return DeferAction.from(completableFucture);
}
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 DeferAction.from(runnable)
.start();
}
public static PendingAction run(Func0 supplier) {
return DeferAction.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);
}
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;
}
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;
}
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;
}
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;
}
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;
}
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 super DATA> predicate) {
val newPromise = promise.filter(predicate);
return new DeferAction(this, newPromise);
}
public final DeferAction peek(FuncUnit1 super DATA> peeker) {
val newPromise = promise.peek(peeker);
return new DeferAction(this, (Promise)newPromise);
}
public final DeferAction map(Func1 super DATA, ? extends TARGET> mapper) {
val newPromise = promise.map(mapper);
val newAction = new DeferAction(this, (Promise)newPromise);
return newAction;
}
public final DeferAction flatMap(Func1 super DATA, ? extends HasPromise extends TARGET>> mapper) {
return chain((Func1)mapper);
}
public final DeferAction chain(Func1> mapper) {
val newPromise = promise.chain(mapper);
return new DeferAction(this, (Promise)newPromise);
}
//== Status ==
public DeferAction ifStatusRun(ResultStatus status, Runnable runnable) {
return new DeferAction(this, promise.ifStatusRun(status, runnable));
}
public DeferAction ifStatusAccept(ResultStatus status, Consumer super DATA> consumer) {
return new DeferAction(this, promise.ifStatusAccept(status, consumer));
}
public DeferAction whenStatusUse(ResultStatus status, DATA fallbackValue) {
return new DeferAction(this, promise.whenStatusUse(status, fallbackValue));
}
public DeferAction whenStatusGet(ResultStatus status, Supplier extends DATA> fallbackSupplier) {
return new DeferAction(this, promise.whenStatusGet(status, fallbackSupplier));
}
public DeferAction whenStatusApply(ResultStatus status, BiFunction recoverFunction) {
return new DeferAction(this, promise.whenStatusApply(status, recoverFunction));
}
//== Validation ==
public DeferAction validateNotNull() {
return new DeferAction(this, promise.validateNotNull());
}
public DeferAction validateNotNull(String message) {
return new DeferAction(this, promise.validateNotNull(message));
}
public DeferAction validateUnavailable() {
return new DeferAction(this, promise.validateUnavailable());
}
public DeferAction validateNotReady() {
return new DeferAction(this, promise.validateNotReady());
}
public DeferAction validateResultCancelled() {
return new DeferAction(this, promise.validateResultCancelled());
}
public DeferAction validateResultNotExist() {
return new DeferAction(this, promise.validateResultNotExist());
}
public DeferAction validateNoMoreResult() {
return new DeferAction(this, promise.validateNoMoreResult());
}
public DeferAction validate(String stringFormat, Predicate super DATA> validChecker) {
return new DeferAction(this, promise.validate(stringFormat, validChecker));
}
public DeferAction validate(String stringFormat, Func1 super DATA, T> mapper, Predicate super T> validChecker) {
return new DeferAction(this, promise.validate(stringFormat, mapper, validChecker));
}
public DeferAction validate(Validator validator) {
return new DeferAction(this, promise.validate(validator));
}
public DeferAction>> validate(Validator super DATA> ... validators) {
return new DeferAction>>(this, promise.validate(validators));
}
public DeferAction>> validate(List> validators) {
return new DeferAction>>(this, promise.validate(validators));
}
public DeferAction ensureNotNull() {
return new DeferAction(this, promise.ensureNotNull());
}
// Alias of whenNotPresentUse
public DeferAction otherwise(DATA elseValue) {
return new DeferAction(this, promise.otherwise(elseValue));
}
// Alias of whenNotPresentGet
public DeferAction otherwiseGet(Supplier extends DATA> elseSupplier) {
return new DeferAction(this, promise.otherwiseGet(elseSupplier));
}
public DeferAction printException() {
return new DeferAction(this, promise.printException());
}
public DeferAction printException(PrintStream printStream) {
return new DeferAction(this, promise.printException(printStream));
}
public DeferAction printException(PrintWriter printWriter) {
return new DeferAction(this, promise.printException(printWriter));
}
//== Peek ==
public DeferAction peek(Class clzz, Consumer super T> theConsumer) {
return new DeferAction(this, promise.peek(clzz, theConsumer));
}
public DeferAction peek(Predicate super DATA> selector, Consumer super DATA> theConsumer) {
return new DeferAction(this, promise.peek(selector, theConsumer));
}
public DeferAction peek(Function super DATA, T> mapper, Consumer super T> theConsumer) {
return new DeferAction(this, promise.peek(mapper, theConsumer));
}
public DeferAction peek(Function super DATA, T> mapper, Predicate super T> selector, Consumer super T> theConsumer) {
return new DeferAction(this, promise.peek(mapper, selector, theConsumer));
}
//== If+When ==
public DeferAction useData(FuncUnit2 processor) {
return new DeferAction(this, promise.useData(processor));
}
public DeferAction whenComplete(FuncUnit2 processor) {
return new DeferAction(this, promise.whenComplete(processor));
}
public DeferAction whenComplete(FuncUnit1> processor) {
return new DeferAction(this, promise.whenComplete(processor));
}
//== Present ==
public DeferAction ifPresent(Runnable runnable) {
return new DeferAction(this, promise.ifPresent(runnable));
}
public DeferAction ifPresent(Consumer super DATA> consumer) {
return new DeferAction(this, promise.ifPresent(consumer));
}
//== Absent ==
public DeferAction ifAbsent(Runnable runnable) {
return new DeferAction(this, promise.ifAbsent(runnable));
}
public DeferAction ifAbsent(Consumer super DATA> consumer) {
return new DeferAction(this, promise.ifAbsent(consumer));
}
public DeferAction ifAbsent(BiConsumer super DATA, ? super Exception> consumer) {
return new DeferAction(this, promise.ifAbsent(consumer));
}
public DeferAction whenAbsentUse(DATA fallbackValue) {
return new DeferAction(this, promise.whenAbsentUse(fallbackValue));
}
public DeferAction whenAbsentGet(Supplier extends DATA> fallbackSupplier) {
return new DeferAction(this, promise.whenAbsentGet(fallbackSupplier));
}
public DeferAction whenAbsentApply(BiFunction