跳转到内容

LF工具

io.terminus.trantorframework.sdk.flow

Class LF

由于 Trantor 对逻辑流的编写具有非常严格的语法限制,导致开发人员无法像编写普通 Java code 一样在 Java 框架内随心所欲地编写业务代码,比如循环、并发、数据转换等操作在现在的逻辑流语法下都是不支持的,但是代码开发对这些不支持的操作是具有强需求的,因此 Trantor 提供了一个 LF 工具类帮助大家处理完成上述操作,开发过程按需调用 API 即可。

Since:

​ 0.16

Field Summary

Modifier and TypeField and Description
private static LFConcurrentExecutorlfConcurrentExecutor
并发任务执行器,主要提供多线程并发执行方式处理所有任务,并同步等待所有任务全部执行完成后返回。
public static final longCONCURRENT_DEFAULT_TIMEOUT
并发执行器执行任务的默认超时时间。

Method Summary

Modifier and TypeMethod and Description
public static voidconcurrent(Runnable… branches)
public static voidconcurrent(long timeout, Runnable… branches)
Concurrent 节点执行流
可以创建多个分支, 并行的执行多个分支的任务. 所有任务执行完成之后, 才会执行后续节点
public static voidforeach(Collection data, Consumer action)
Map 节点执行流
循环 ModelArray 或 ModelPaging, 无返回值
public static voidforeach(Paging paging, Consumer action)
public static <T, R> Listmap(Collection data, Function<T, R> action)
Map 节点执行流
循环 Model 对象集合, 函数会将返回内容聚合形成新的数组, 放入临时上下文
public static <T, R> Pagingmap(Paging paging, Function<T, R> action)
public static <T, R> ListmapNotNull(Collection data, Function<T, R> action)
Map 节点执行流
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文
对处理结果先进行 Not Null 过滤后再返回
public static <T, R> PagingmapNotNull(Paging paging, Function<T, R> action)
private static <T, R> Listmap(Collection data, Function<T, R> action, boolean filterNull, boolean parallel)
Map 节点执行流
循环 Model 对象集合, 函数会将返回内容聚合形成新的数组, 放入临时上下文
private static <T, R> Pagingmap(Paging paging, Function<T, R> action, boolean filterNull, boolean parallel)
public static <T, R> Listparallel(Collection data, Function<T, R> action)
Parallel 节点执行流
并行执行 ModelArray 或 ModelPaging 内的对象, 其他与 Map 类似, 不过会在所有并行任务执行完才会执行合并
public static <T, R> Pagingparallel(Paging paging, Function<T, R> action)
public static <T, R> ListparallelNotNull(Collection data, Function<T, R> action)
Parallel 节点执行流
并行执行 ModelArray 或 ModelPaging 内的对象, 其他与 Map 类似, 不过会在所有并行任务执行完才会执行合并
对处理结果先进行 Not Null 过滤后再返回
public static <T, R> PagingparallelNotNull(Paging paging, Function<T, R> action)
public static <T, R> ListflatMap(Collection data, Function<T, Stream> action)
Map 节点执行流
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文
public static <T, R> PagingflatMap(Paging paging, Function<T, Stream> action)
public static <T, R> ListflatMapNotNull(Collection data, Function<T, Stream> action)
Map 节点执行流
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文
对处理结果先进行 Not Null 过滤后再返回
public static <T, R> PagingflatMapNotNull(Paging paging, Function<T, Stream> action)
private static <T, R> ListflatMap(Collection data, Function<T, Stream> action, boolean filterNull, boolean parallel)
flatMap 节点执行流
循环 Model 对象集合, 函数会将返回内容聚合形成新的数组, 放入临时上下文
private static <T, R> PagingflatMap(Paging paging, Function<T, Stream> action, boolean filterNull, boolean parallel)
public static <T, R> ListflatMapCollection(Collection data, Function<T, Collection> action)
Map 节点执行流
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文
public static <T, R> PagingflatMapCollection(Paging paging, Function<T, Collection> action)
public static <T, R> ListflatMapCollectionNotNull(Collection data, Function<T, Collection> action)
Map 节点执行流
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文
对处理结果先进行 Not Null 过滤后再返回
public static <T, R> PagingflatMapCollectionNotNull(Paging paging, Function<T, Collection> action)
private static <T, R> PagingflatMapCollection(Paging paging, Function<T, Collection> action, boolean filterNull, boolean parallel)
Map 节点执行流
循环 ModelArray 或 ModelPaging, 将数组内的模型放入临时上下文
private static <T, R> ListflatMapCollection(Collection data, Function<T, Collection> action, boolean filterNull, boolean parallel)

Method Detail

concurrent

public static void concurrent(Runnable... branches);
public static void concurrent(long timeout, Runnable... branches);

Concurrent 节点执行流。 可以创建多个分支, 并行的执行多个分支的任务. 所有任务执行完成之后, 才会执行后续节点。

参数:

  • timeout - 任务执行超时时间,超出这个时间方法将直接抛出运行时异常

    ​ 单位:毫秒(ms)

  • branches - 提交的可运行任务列表

代码示例:

@FlowImpl
public 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 - 执行的处理, 无返回对象

  • - 入参的模型类型

代码示例:

@FlowImpl
public 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 处理之后返回的集合数据

代码示例:

@FlowImpl
public 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 处理之后返回的集合数据

代码示例:

@FlowImpl
public 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 处理之后返回的集合数据

代码示例:

@FlowImpl
public 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
**/
@FlowImpl
public 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 处理之后返回的集合数据

代码示例:

// TODO

flatMapNotNull

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 处理之后返回的集合数据

代码示例:

// TODO

flatMapCollection

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 处理之后返回的集合数据

代码示例:

// TODO

flatMapCollectionNotNull

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