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.
// ============================================================================
// 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.task;
import static nullablej.nullable.Nullable.nullable;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
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.list.FuncList;
import functionalj.promise.DeferAction;
import functionalj.promise.DeferActionBuilder;
import functionalj.promise.Promise;
import functionalj.promise.RaceResult;
import functionalj.result.Result;
import lombok.val;
public class Tasks {
public static class TaskValue implements Task {
private final DATA value;
public TaskValue(DATA value) {
this.value = value;
}
@Override
public DeferAction createAction() {
return DeferAction.ofValue(value);
}
public String toString() {
return "Task(" + value + ")";
}
}
public static class TaskSupplier extends DeferActionBuilder implements Task {
public TaskSupplier(Func0 supplier) {
super(supplier);
}
@Override
public DeferAction createAction() {
return build();
}
public String toString() {
return "Task(()->" + supplier() + ")";
}
}
public static class TaskResult implements Task {
private final Result result;
public TaskResult(Result result) {
this.result = result;
}
@Override
public DeferAction createAction() {
val action = DeferAction.of((Class)null);
action.start();
if (result.isValue())
action.complete(result.getValue());
else action.fail (result.getException());
return action;
}
public String toString() {
return "Task(" + result + ")";
}
}
public static class TaskPromise implements Task {
private final Promise promise;
public TaskPromise(Promise promise) {
this.promise = promise;
}
@Override
public DeferAction createAction() {
val action = DeferAction.of((Class)null, ()->{
if (promise != null)
promise.start();
});
if (promise != null) {
promise
.eavesdrop(result -> {
val pendingAction = action.start();
if (result.isValue())
pendingAction.complete(result.getValue());
else pendingAction.fail (result.getException());
});
}
return action;
}
public String toString() {
return "Task(" + promise + ")";
}
}
public static class IOInstance implements Task {
private final String toString;
private final Supplier> createAction;
public IOInstance(String toString, Supplier> createAction) {
this.toString = (toString != null) ? toString : "Task@" + hashCode();
this.createAction = Objects.requireNonNull(createAction);
}
@Override
public DeferAction createAction() {
return createAction.get();
}
public String toString() {
return toString;
}
}
public static class TaskPeek