在日常开发过程中,经常发现同样的方法结构出现多次,差异较小,可以借助泛型等手段抽象出公用方法。
示例
原逻辑
// 变更出入参
this.mergeOrDelInputParam(inputJson);
this.mergeOrDelQaOutputParam(outputJson);
//入参
private void mergeOrDelInputParam(String inputJson) {
if (StringUtils.isEmpty(inputJson)) {
log.info("入参为空");
return;
}
JSONArray inputJsonArr = JSONArray.parseArray(inputJson);
List<QaInputParam> qaInputParams = inputJsonArr.toJavaList(QaInputParam.class);
if (CollUtil.isEmpty(qaInputParams)) {
log.info("入参解析为空");
return;
}
// 增
List<QaInputParam> inputParams4Add = this.getQaParamsByTypes(qaInputParams, QaParamTypeEnum.ADD);
if (CollectionUtils.isNotEmpty(inputParams4Add)) {
log.info("入参新增");
this.qaInputParamService.insertBatch(inputParams4Add);
}
// 改
List<QaInputParam> inputParams4Edit = this.getQaParamsByTypes(qaInputParams, QaParamTypeEnum.EDIT);
if (CollectionUtils.isNotEmpty(inputParams4Edit)) {
log.info("入参新增");
this.qaInputParamService.updateBatchById(inputParams4Edit);
}
// 删除
List<QaInputParam> inputParams4Del = this.getQaParamsByTypes(qaInputParams, QaParamTypeEnum.DELETE);
if (CollectionUtils.isNotEmpty(inputParams4Del)) {
log.info("入参删除");
this.qaInputParamService.deleteBatchIds(inputParams4Del);
}
}
//出参
private void mergeOrDelQaOutputParam(String outputJson) {
if (StringUtils.isEmpty(outputJson)) {
log.info("出参为空");
return;
}
JSONArray outputJsonArr = JSONArray.parseArray(outputJson);
List<QaOutputParam> qaOutputParams = outputJsonArr.toJavaList(QaOutputParam.class);
if (CollUtil.isEmpty(qaOutputParams)) {
log.info("出参解析为空");
return;
}
// 增
List<QaOutputParam> outputParams4Add = this.getQaParamsByTypes(qaOutputParams, QaParamTypeEnum.ADD);
if (CollectionUtils.isNotEmpty(outputParams4Add)) {
log.info("出参新增");
this.qaOutputParamService.insertBatch(outputParams4Add);
}
// 改
List<QaOutputParam> outputParams4Edit = this.getQaParamsByTypes(qaOutputParams, QaParamTypeEnum.EDIT);
if (CollectionUtils.isNotEmpty(outputParams4Edit)) {
log.info("出参修改");
this.qaOutputParamService.updateBatchById(outputParams4Edit);
}
// 删除
List<QaOutputParam> outputParams4Del =
this.getQaParamsByTypes(qaOutputParams, QaParamTypeEnum.DELETE);
if (CollectionUtils.isNotEmpty(outputParams4Del)) {
log.info("出参删除");
this.qaOutputParamService.deleteBatchIds(outputParams4Del);
}
}
//获取要修改的数据
private <T extends AbstractCommonParam> List<T> getQaParamsByTypes(List<T> qaParams,
QaParamTypeEnum... types) {
return qaParams.stream().filter(qaOutputParam -> StringUtils.isNotBlank(qaOutputParam.getType())
&& ArrayUtils.contains(types, QaParamTypeEnum.getByCode(qaOutputParam.getType())))
.collect(Collectors.toList());
}
重构探索1
// 调用
this.persistenceParam(inputJson, QaInputParam.class, this.qaInputParamService);
this.persistenceParam(outputJson, QaOutputParam.class, this.qaOutputParamService);
//封账
private <T extends AbstractCommonParam> void persistenceParam(String qaParamJson, Class<T> clazz,
IService<T> iService) {
if (StringUtils.isEmpty(qaParamJson)) {
log.info("参数为空");
return;
}
List<T> qaParams = JSONArray.parseArray(qaParamJson).toJavaList(clazz);
if (CollUtil.isEmpty(qaParams)) {
log.info("参数解析为空");
return;
}
// 增
List<T> outputParams4Add = this.getQaParamsByTypes(qaParams, QaParamTypeEnum.ADD);
if (CollectionUtils.isNotEmpty(outputParams4Add)) {
log.info("出参新增");
iService.insertBatch(outputParams4Add);
}
// 改
List<T> outputParams4Edit = this.getQaParamsByTypes(qaParams, QaParamTypeEnum.EDIT);
if (CollectionUtils.isNotEmpty(outputParams4Edit)) {
log.info("出参修改");
iService.updateBatchById(outputParams4Edit);
}
// 删除
List<T> outputParams4Del =
this.getQaParamsByTypes(qaParams, QaParamTypeEnum.DELETE);
if (CollectionUtils.isNotEmpty(outputParams4Del)) {
log.info("出参删除");
iService.deleteBatchIds(outputParams4Del);
}
}
//获取要修改的数据
private <T extends AbstractCommonParam> List<T> getQaParamsByTypes(List<T> qaParams,
QaParamTypeEnum... types) {
return qaParams.stream().filter(qaOutputParam -> StringUtils.isNotBlank(qaOutputParam.getType())
&& ArrayUtils.contains(types, QaParamTypeEnum.getByCode(qaOutputParam.getType())))
.collect(Collectors.toList());
}
重构探索2
//调用
this.persistenceParam(inputJson, QaInputParam.class, this.qaInputParamService);
this.persistenceParam(outputJson, QaOutputParam.class, this.qaOutputParamService);
//调用
private <T extends AbstractCommonParam> void persistenceParam(String qaParamJson, Class<T> clazz,
IService<T> iService) {
if (StringUtils.isEmpty(qaParamJson)) {
log.info("参数为空");
return;
}
List<T> qaParams = JSONArray.parseArray(qaParamJson).toJavaList(clazz);
if (CollUtil.isEmpty(qaParams)) {
log.info("参数解析为空");
return;
}
// 变更
this.doPersistenceParam(qaParams, QaParamTypeEnum.ADD, iService::insertBatch);
this.doPersistenceParam(qaParams, QaParamTypeEnum.EDIT, iService::updateBatchById);
this.doPersistenceParam(qaParams, QaParamTypeEnum.DELETE,
(List<T> list) -> iService.deleteBatchIds(list.stream().map(T::getId).collect(Collectors.toList())));
}
//持久化
private <T extends AbstractCommonParam> void doPersistenceParam(List<T> qaParams, QaParamTypeEnum qaParamTypeEnum,
Function<List<T>, Boolean> function) {
List<T> qaParams4Handle = this.getQaParamsByTypes(qaParams, qaParamTypeEnum);
if (CollectionUtils.isNotEmpty(qaParams4Handle)) {
log.info("参数持久化,类型={}", qaParamTypeEnum);
function.apply(qaParams4Handle);
}
}
//获取要修改的数据
private <T extends AbstractCommonParam> List<T> getQaParamsByTypes(List<T> qaParams,
QaParamTypeEnum... types) {
return qaParams.stream().filter(qaOutputParam -> StringUtils.isNotBlank(qaOutputParam.getType())
&& ArrayUtils.contains(types, QaParamTypeEnum.getByCode(qaOutputParam.getType())))
.collect(Collectors.toList());
}