org.kinotic.continuum.grind.api.Tasks Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of continuum-grind Show documentation
Show all versions of continuum-grind Show documentation
A Microservice and IOT framework for everyone!
/*
*
* Copyright 2008-2021 Kinotic and the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kinotic.continuum.grind.api;
import org.kinotic.continuum.grind.internal.api.InstanceTask;
import org.kinotic.continuum.grind.internal.api.NoopTask;
import org.kinotic.continuum.grind.internal.api.ValueTask;
import org.springframework.context.support.GenericApplicationContext;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.function.Supplier;
/**
*
* Created by Navid Mitchell on 3/24/20
*/
public class Tasks {
public static Task fromCallable(Callable instance) {
return fromCallable(null, instance);
}
public static Task fromCallable(String description,
Callable instance) {
return new InstanceTask<>(description,
instance,
callable -> {
try {
return callable.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
public static Task fromSupplier(Supplier instance) {
return fromSupplier(null, instance);
}
public static Task fromSupplier(String description,
Supplier instance) {
return new InstanceTask<>(description,
instance,
Supplier::get);
}
public static Task fromValue(T value) {
return fromValue(null, value);
}
public static Task fromValue(String description,
T value) {
return new ValueTask<>(description, value);
}
public static Task fromRunnable(Runnable instance) {
return fromRunnable(null, instance);
}
public static Task fromRunnable(String description,
Runnable instance) {
return new InstanceTask<>(description,
instance,
runnable -> {
runnable.run();
return null;
});
}
/**
* Special type of task that allows a step to be skipped if needed.
* This is useful if a {@link Supplier} needs to only supply a task under certain conditions
* @param description of why the task is a noop task.
* @return the noop task
*/
public static Task noop(String description){
return new NoopTask<>(description);
}
/**
* Special type of task that allows a step to be skipped if needed.
* This is useful if a {@link Supplier} needs to only supply a task under certain conditions
* @return the noop task
*/
public static Task noop(){
return new NoopTask<>();
}
public static Task transformResult(Task from, Function transformer){
return new Task<>() {
@Override
public String getDescription() {
return from.getDescription();
}
@Override
public R execute(GenericApplicationContext applicationContext) throws Exception {
return transformer.apply(from.execute(applicationContext));
}
};
}
}