functionalj.promise.DeferActionBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
// ============================================================================
// 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 java.util.Objects.requireNonNull;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import functionalj.environments.AsyncRunner;
import functionalj.function.Func0;
import functionalj.function.Func1;
import functionalj.function.FuncUnit0;
import functionalj.list.FuncList;
import functionalj.result.Result;
import functionalj.task.Task;
import lombok.val;
public class DeferActionBuilder implements Task {
public static RetryConfig Retry(int times) {
return new RetryConfig(times);
}
public static Function, DeferActionBuilder> InterruptOnCancel(boolean interruptOnCancel) {
return builder -> builder.interruptOnCancel(interruptOnCancel);
}
public static Function, DeferActionBuilder> OnStart(FuncUnit0 onStart) {
return builder -> builder.onStart(onStart);
}
public static Function, DeferActionBuilder> Runner(AsyncRunner runner) {
return builder -> builder.runner(runner);
}
// TODO - Add other configuration.
private static final FuncUnit0 DO_NOTHING = ()->{};
private final String toString;
private final Func0 supplier;
private boolean interruptOnCancel = true;
private FuncUnit0 onStart = DO_NOTHING;
private AsyncRunner runner = null;
@SuppressWarnings("unchecked")
private Retry retry = (Retry)Retry.noRetry;
public static final DeferActionBuilder from(FuncUnit0 runnable) {
return new DeferActionBuilder<>(runnable);
}
public static final DeferActionBuilder from(Func0 supplier) {
return new DeferActionBuilder<>(supplier);
}
public static final DeferActionBuilder from(String toString, FuncUnit0 runnable) {
return new DeferActionBuilder<>(toString, runnable);
}
public static final DeferActionBuilder from(String toString, Func0 supplier) {
return new DeferActionBuilder<>(toString, supplier);
}
public DeferActionBuilder(FuncUnit0 runnable) {
this(null, runnable);
}
public DeferActionBuilder(String toString, FuncUnit0 runnable) {
this(toString, runnable.thenReturnNull());
}
public DeferActionBuilder(Func0 supplier) {
this(null, supplier);
}
public DeferActionBuilder(String toString, Func0 supplier) {
this.toString = (toString != null) ? toString : "Task#" + supplier.toString();
this.supplier = requireNonNull(supplier);
}
// TODO - Find the way to make this work without resorting to "Object"
@SafeVarargs
@SuppressWarnings({ "unchecked", "rawtypes" })
public final DeferActionBuilder config(Function , DeferActionBuilder