LF工具
io.terminus.trantorframework.sdk.flow
Class LF
由于 Trantor 对逻辑流的编写具有非常严格的语法限制,导致开发人员无法像编写普通 Java code 一样在 Java 框架内随心所欲地编写业务代码,比如循环、并发、数据转换等操作在现在的逻辑流语法下都是不支持的,但是代码开发对这些不支持的操作是具有强需求的,因此 Trantor 提供了一个 LF 工具类帮助大家处理完成上述操作,开发过程按需调用 API 即可。
Since:
0.16
Field Summary
| Modifier and Type | Field and Description |
|---|---|
| private static LFConcurrentExecutor | lfConcurrentExecutor 并发任务执行器,主要提供多线程并发执行方式处理所有任务,并同步等待所有任务全部执行完成后返回。 |
| public static final long | CONCURRENT_DEFAULT_TIMEOUT 并发执行器执行任务的默认超时时间。 |
Method Summary
| Modifier and Type | Method and Description |
|---|---|
| public static void | concurrent(Runnable… branches) |
| public static void | concurrent(long timeout, Runnable… branches) Concurrent 节点执行流 可以创建多个分支, 并行的执行多个分支的任务. 所有任务执行完成之后, 才会执行后续节点 |
| public static | foreach(Collection Map 节点执行流 循环 ModelArray 或 ModelPaging, 无返回值 |
| public static | foreach(Paging |
| public static <T, R> List | map(Collection Map 节点执行流 循环 Model 对象集合, 函数会将返回内容聚合形成新的数组, 放入临时上下文 |
| public static <T, R> Paging | map(Paging |
| public static <T, R> List | mapNotNull(Collection Map 节点执行流 循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文 对处理结果先进行 Not Null 过滤后再返回 |
| public static <T, R> Paging | mapNotNull(Paging |
| private static <T, R> List | map(Collection Map 节点执行流 循环 Model 对象集合, 函数会将返回内容聚合形成新的数组, 放入临时上下文 |
| private static <T, R> Paging | map(Paging |
| public static <T, R> List | parallel(Collection Parallel 节点执行流 并行执行 ModelArray 或 ModelPaging 内的对象, 其他与 Map 类似, 不过会在所有并行任务执行完才会执行合并 |
| public static <T, R> Paging | parallel(Paging |
| public static <T, R> List | parallelNotNull(Collection Parallel 节点执行流 并行执行 ModelArray 或 ModelPaging 内的对象, 其他与 Map 类似, 不过会在所有并行任务执行完才会执行合并 对处理结果先进行 Not Null 过滤后再返回 |
| public static <T, R> Paging | parallelNotNull(Paging |
| public static <T, R> List | flatMap(Collection Map 节点执行流 循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文 |
| public static <T, R> Paging | flatMap(Paging |
| public static <T, R> List | flatMapNotNull(Collection Map 节点执行流 循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文 对处理结果先进行 Not Null 过滤后再返回 |
| public static <T, R> Paging | flatMapNotNull(Paging |
| private static <T, R> List | flatMap(Collection flatMap 节点执行流 循环 Model 对象集合, 函数会将返回内容聚合形成新的数组, 放入临时上下文 |
| private static <T, R> Paging | flatMap(Paging |
| public static <T, R> List | flatMapCollection(Collection Map 节点执行流 循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文 |
| public static <T, R> Paging | flatMapCollection(Paging |
| public static <T, R> List | flatMapCollectionNotNull(Collection Map 节点执行流 循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文 对处理结果先进行 Not Null 过滤后再返回 |
| public static <T, R> Paging | flatMapCollectionNotNull(Paging |
| private static <T, R> Paging | flatMapCollection(Paging Map 节点执行流 循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文 |
| private static <T, R> List | flatMapCollection(Collection |
Method Detail
concurrent
public static void concurrent(Runnable... branches);public static void concurrent(long timeout, Runnable... branches);Concurrent 节点执行流。 可以创建多个分支, 并行的执行多个分支的任务. 所有任务执行完成之后, 才会执行后续节点。
参数:
-
timeout - 任务执行超时时间,超出这个时间方法将直接抛出运行时异常
单位:毫秒(ms)
-
branches - 提交的可运行任务列表
代码示例:
@FlowImplpublic class LFConcurrentFlowImpl implements LFConcurrentFlow {
private final FuncA funcA; private final FuncB funcB; private final FuncC funcC;
public LFTestFlowImpl(FuncA funcA, FuncB funcB, FuncC funcC) { this.funcA = funcA; this.funcB = funcB; this.funcC = funcC; }
@Override public void execute() { LF.concurrent(60 * 1000, () -> funcA.execute(), () -> funcB.execute(), () -> funcC.execute()); }}foreach
public static <T> void foreach(Collection<T> data, Consumer<T> action);public static <T> void foreach(Paging<T> paging, Consumer<T> action);Map 节点执行流。
循环 ModelArray 或 ModelPaging, 无返回值。
参数:
-
data - 需要处理的数据集合
-
action - 执行的处理, 无返回对象
-
- 入参的模型类型
代码示例:
@FlowImplpublic class LFForeachFlowImpl implements LFForeachFlow {
private final ComputeAgeByBirthdayFunc computeAgeByBirthdayFunc;
public LFForeachFlowImpl(ComputeAgeByBirthdayFunc computeAgeByBirthdayFunc) { this.computeAgeByBirthdayFunc = computeAgeByBirthdayFunc; }
@Override public void execute() { // 查询所有员工信息 List<Staff> staffList = DS.findAll(Staff.class, "*", null); // computeAgeByBirthdayFunc:根据出生日期计算年龄 LF.foreach(staffList, computeAgeByBirthdayFunc::execute); }}map
public static <T, R> List<R> map(Collection<T> data, Function<T, R> action);public static <T, R> Paging<R> map(Paging<T> paging, Function<T, R> action);private static <T, R> List<R> map(Collection<T> data, Function<T, R> action, boolean filterNull, boolean parallel);private static <T, R> Paging<R> map(Paging<T> paging, Function<T, R> action, boolean filterNull, boolean parallel);Map 节点执行流。
循环 Model 对象集合, 函数会将返回内容聚合形成新的数组, 放入临时上下文。
参数:
-
data - 需要处理的数据集合
-
action - 执行的处理, 需要返回一个返回的对象
-
filterNull - 是否过滤掉结果集合中的 IS NULL 元素
-
parallel - 是否并行
-
- 入参的模型类型 -
- 出参的模型类型
返回:
使用函数 action 处理之后返回的集合数据
代码示例:
@FlowImplpublic class LFMapFlowImpl implements LFMapFlow {
private final ComputeAgeByBirthdayFunc computeAgeByBirthdayFunc;
public LFForeachFlowImpl(ComputeAgeByBirthdayFunc computeAgeByBirthdayFunc) { this.computeAgeByBirthdayFunc = computeAgeByBirthdayFunc; }
@Override public void execute() { // 查询所有员工信息 List<Staff> staffList = DS.findAll(Staff.class, "*", null); List<Staff> newStaffList = LF.map(staffList, staff -> { // computeAgeByBirthdayFunc:根据出生日期计算年龄 Staff newStaff = computeAgeByBirthdayFunc.execute(staff); return newStaff; }); // 省略后续步骤... }}mapNotNull
public static <T, R> List<R> mapNotNull(Collection<T> data, Function<T, R> action);public static <T, R> Paging<R> mapNotNull(Paging<T> paging, Function<T, R> action);Map 节点执行流。
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文。
对处理结果先进行 Not Null 过滤后再返回。
等价于 map 方法
filterNull = true的情况。map(data, action, true, false);map(paging, action, true, false);
参数:
-
data - 需要处理的数据集合
-
action - 执行的处理, 需要返回一个返回的对象
-
- 入参的模型类型 -
- 出参的模型类型
返回:
使用函数 action 处理之后返回的集合数据
代码示例:
@FlowImplpublic class LFMapNotNullFlowImpl implements LFMapNotNullFlow {
private final ComputeAgeByBirthdayFunc computeAgeByBirthdayFunc;
public LFForeachFlowImpl(ComputeAgeByBirthdayFunc computeAgeByBirthdayFunc) { this.computeAgeByBirthdayFunc = computeAgeByBirthdayFunc; }
@Override public void execute() { // 查询所有员工信息 List<Staff> staffList = DS.findAll(Staff.class, "*", null); // 获取年龄小于30岁的年轻员工列表 List<Staff> youngStaffList = LF.mapNotNull(staffList, staff -> { // computeAgeByBirthdayFunc:根据出生日期计算年龄 Staff newStaff = computeAgeByBirthdayFunc.execute(staff); int age = newStaff.getAge(); return age < 30 ? staff : null; }); // 省略后续步骤... }}parallel
public static <T, R> List<R> parallel(Collection<T> data, Function<T, R> action);public static <T, R> Paging<R> parallel(Paging<T> paging, Function<T, R> action);Parallel 节点执行流。
并行执行 ModelArray 或 ModelPaging 内的对象, 其他与 Map 类似, 不过会在所有并行任务执行完才会执行合并。
等价于 map 方法
parallel = true的情况。map(data, action, false, true);map(paging, action, false, true);
参数:
-
data - 需要处理的数据集合
-
action - 执行的处理, 需要返回一个返回的对象
-
- 入参的模型类型 -
- 出参的模型类型
返回:
使用函数 action 处理之后返回的集合数据
代码示例:
@FlowImplpublic class LFParallelFlowImpl implements LFParallelFlow {
private final ComputeAgeByBirthdayFunc computeAgeByBirthdayFunc;
public LFForeachFlowImpl(ComputeAgeByBirthdayFunc computeAgeByBirthdayFunc) { this.computeAgeByBirthdayFunc = computeAgeByBirthdayFunc; }
@Override public void execute() { // 查询所有员工信息 List<Staff> staffList = DS.findAll(Staff.class, "*", null); List<Staff> newStaffList = LF.parallel(staffList, staff -> { // computeAgeByBirthdayFunc:根据出生日期计算年龄 Staff newStaff = computeAgeByBirthdayFunc.execute(staff); return newStaff; }); // 省略后续步骤... }}parallelNotNull
public static <T, R> List<R> parallelNotNull(Collection<T> data, Function<T, R> action);public static <T, R> Paging<R> parallelNotNull(Paging<T> paging, Function<T, R> action);Parallel 节点执行流。
并行执行 ModelArray 或 ModelPaging 内的对象, 其他与 Map 类似, 不过会在所有并行任务执行完才会执行合并。
对处理结果先进行 Not Null 过滤后再返回。
等价于 map 方法
filterNull = true && parallel = true的情况。map(data, action, true, true);map(paging, action, true, true);
参数:
-
data - 需要处理的数据集合
-
action - 执行的处理, 需要返回一个返回的对象
-
- 入参的模型类型 -
- 出参的模型类型
返回:
使用函数 action 处理之后返回的集合数据
代码示例:
package io.terminus.trantor.example.flow;
import io.terminus.trantor.example.func.ComputeAgeByBirthdayFunc;import io.terminus.trantor.example.model.Staff;import io.terminus.trantorframework.api.annotation.FlowImpl;import io.terminus.trantorframework.sdk.flow.LF;import io.terminus.trantorframework.sdk.sql.DS;
import java.util.Calendar;import java.util.List;
/** * @author hzj273812(黄子敬) * @email hzj273812@alibaba-inc.com * @create 2021/7/8 下午2:49 **/@FlowImplpublic class LFParallelNotNullFlowImpl implements LFParallelNotNullFlow {
private final ComputeAgeByBirthdayFunc computeAgeByBirthdayFunc;
public LFForeachFlowImpl(ComputeAgeByBirthdayFunc computeAgeByBirthdayFunc) { this.computeAgeByBirthdayFunc = computeAgeByBirthdayFunc; }
@Override public void execute() { // 查询所有员工信息 List<Staff> staffList = DS.findAll(Staff.class, "*", null); // 获取年龄小于30岁的年轻员工列表 List<Staff> youngStaffList = LF.parallelNotNull(staffList, staff -> { // computeAgeByBirthdayFunc:根据出生日期计算年龄 Staff newStaff = computeAgeByBirthdayFunc.execute(staff); int age = newStaff.getAge(); return age < 30 ? staff : null; }); // 省略后续步骤... }}flatMap
public static <T, R> List<R> flatMap(Collection<T> data, Function<T, Stream<R>> action);public static <T, R> Paging<R> flatMap(Paging<T> paging, Function<T, Stream<R>> action);private static <T, R> List<R> flatMap(Collection<T> data, Function<T, Stream<R>> action, boolean filterNull, boolean parallel);private static <T, R> Paging<R> flatMap(Paging<T> paging, Function<T, Stream<R>> action, boolean filterNull, boolean parallel);Map 节点执行流。
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文。
参数:
-
data - 需要处理的数据集合
-
action - 执行的处理, 需要返回一个返回的对象
-
filterNull - 是否过滤掉结果集合中的 IS NULL 元素
-
parallel - 是否并行
-
- 入参的模型类型 -
- 出参的模型类型
返回:
使用函数 action 处理之后返回的集合数据
代码示例:
// TODOflatMapNotNull
public static <T, R> List<R> flatMapNotNull(Collection<T> data, Function<T, Stream<R>> action);public static <T, R> Paging<R> flatMapNotNull(Paging<T> paging, Function<T, Stream<R>> action);Map 节点执行流。
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文。
参数:
-
data - 需要处理的数据集合
-
action - 执行的处理, 需要返回一个返回的对象
-
- 入参的模型类型 -
- 出参的模型类型
返回:
使用函数 action 处理之后返回的集合数据
代码示例:
// TODOflatMapCollection
public static <T, R> List<R> flatMapCollection(Collection<T> data, Function<T, Collection<R>> action);public static <T, R> Paging<R> flatMapCollection(Paging<T> paging, Function<T, Collection<R>> action);private static <T, R> Paging<R> flatMapCollection(Paging<T> paging, Function<T, Collection<R>> action, boolean filterNull, boolean parallel);private static <T, R> List<R> flatMapCollection(Collection<T> data, Function<T, Collection<R>> action, boolean filterNull, boolean parallel);Map 节点执行流。
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文。
参数:
-
data - 需要处理的数据集合
-
action - 执行的处理, 需要返回一个返回的对象
-
filterNull - 是否过滤掉结果集合中的 IS NULL 元素
-
parallel - 是否并行
-
- 入参的模型类型 -
- 出参的模型类型
返回:
使用函数 action 处理之后返回的集合数据
代码示例:
// TODOflatMapCollectionNotNull
public static <T, R> List<R> flatMapCollectionNotNull(Collection<T> data, Function<T, Collection<R>> action);public static <T, R> Paging<R> flatMapCollectionNotNull(Paging<T> paging, Function<T, Collection<R>> action);Map 节点执行流。
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文。
参数:
-
data - 需要处理的数据集合
-
action - 执行的处理, 需要返回一个返回的对象
-
- 入参的模型类型 -
- 出参的模型类型
返回:
使用函数 action 处理之后返回的集合数据
代码示例:
// TODO