update MapstructPlus增强,bean转换过程支持Consumer消费函数
Signed-off-by: 秋辞未寒 <545073804@qq.com>
This commit is contained in:
parent
08e40b611b
commit
b9f861685a
@ -0,0 +1,66 @@
|
||||
package io.github.linpeilie;
|
||||
|
||||
import org.mapstruct.Context;
|
||||
import org.mapstruct.MappingTarget;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public interface BaseCycleAvoidingMapper<S, T> extends BaseMapper<S, T> {
|
||||
|
||||
T convert(S source, @Context CycleAvoidingMappingContext context);
|
||||
|
||||
default T convert(S source, @Context CycleAvoidingMappingContext context, Consumer<T> beanConsumer) {
|
||||
T bean = convert(source,context);
|
||||
if (Objects.nonNull(bean) && Objects.nonNull(beanConsumer)) {
|
||||
beanConsumer.accept(bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
T convert(S source, @MappingTarget T target, @Context CycleAvoidingMappingContext context);
|
||||
|
||||
default T convert(S source, @MappingTarget T target, @Context CycleAvoidingMappingContext context, Consumer<T> beanConsumer) {
|
||||
T bean = convert(source,target,context);
|
||||
if (Objects.nonNull(bean) && Objects.nonNull(beanConsumer)) {
|
||||
beanConsumer.accept(bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
default List<T> convert(List<S> sourceList, @Context CycleAvoidingMappingContext context) {
|
||||
return sourceList.stream()
|
||||
.map(item -> convert(item, context))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
default List<T> convert(List<S> sourceList, @Context CycleAvoidingMappingContext context, Consumer<T> beanConsumer) {
|
||||
// 如果 beanConsumer 本来就为 null,则不再调用带 Consumer 参数的 convert 方法,避免在循环中进行不必要的非空判断
|
||||
if (Objects.nonNull(beanConsumer)) {
|
||||
return sourceList.stream()
|
||||
.map(source -> convert(source, beanConsumer))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return sourceList.stream()
|
||||
.map(item -> convert(item, context))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
default T convert(S source) {
|
||||
return convert(source, new CycleAvoidingMappingContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
default T convert(S source, @MappingTarget T target) {
|
||||
return convert(source, new CycleAvoidingMappingContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
default List<T> convert(List<S> sourceList) {
|
||||
return convert(sourceList, new CycleAvoidingMappingContext());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package io.github.linpeilie;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface BaseMapMapper<T> {
|
||||
|
||||
T convert(Map<String, Object> map);
|
||||
|
||||
default T convert(Map<String, Object> map, Consumer<T> beanConsumer) {
|
||||
T bean = convert(map);
|
||||
if (Objects.nonNull(bean) && Objects.nonNull(beanConsumer)) {
|
||||
beanConsumer.accept(bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package io.github.linpeilie;
|
||||
|
||||
import io.github.linpeilie.utils.CollectionUtils;
|
||||
import org.mapstruct.MappingTarget;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public interface BaseMapper<S, T> {
|
||||
|
||||
T convert(S source);
|
||||
|
||||
default T convert(S source, Consumer<T> beanConsumer) {
|
||||
T bean = convert(source);
|
||||
if (Objects.nonNull(bean) && Objects.nonNull(beanConsumer)) {
|
||||
beanConsumer.accept(bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
T convert(S source, @MappingTarget T target);
|
||||
|
||||
default T convert(S source, @MappingTarget T target, Consumer<T> beanConsumer) {
|
||||
T bean = convert(source,target);
|
||||
if (Objects.nonNull(bean) && Objects.nonNull(beanConsumer)) {
|
||||
beanConsumer.accept(bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
default List<T> convert(List<S> sourceList) {
|
||||
return convert(sourceList, null);
|
||||
}
|
||||
|
||||
default List<T> convert(List<S> sourceList, Consumer<T> beanConsumer) {
|
||||
if (CollectionUtils.isEmpty(sourceList)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
// 如果 beanConsumer 本来就为 null,则不再调用带 Consumer 参数的 convert 方法,避免在循环中进行不必要的非空判断
|
||||
if (Objects.nonNull(beanConsumer)) {
|
||||
return sourceList.stream()
|
||||
.map(source -> convert(source, beanConsumer))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return sourceList.stream()
|
||||
.map(this::convert)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
package io.github.linpeilie;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Converter {
|
||||
|
||||
private final ConverterFactory converterFactory;
|
||||
|
||||
public Converter() {
|
||||
this.converterFactory = new DefaultConverterFactory();
|
||||
}
|
||||
|
||||
public Converter(final ConverterFactory converterFactory) {
|
||||
this.converterFactory = converterFactory;
|
||||
}
|
||||
|
||||
public <S, T> T convert(S source, Class<T> targetType) {
|
||||
return convert(source, targetType, (Consumer<T>) null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S, T> T convert(S source, Class<T> targetType, Consumer<T> beanConsumer) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
BaseMapper<S, T> mapper = (BaseMapper<S, T>) converterFactory.getMapper(source.getClass(), targetType);
|
||||
if (mapper != null) {
|
||||
return mapper.convert(source, beanConsumer);
|
||||
}
|
||||
throw new ConvertException(
|
||||
"cannot find converter from " + source.getClass().getSimpleName() + " to " + targetType.getSimpleName());
|
||||
}
|
||||
|
||||
public <S, T> T convert(S source, T target) {
|
||||
return convert(source, target, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S, T> T convert(S source, T target, Consumer<T> beanConsumer) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
if (target == null) {
|
||||
return null;
|
||||
}
|
||||
Class<?> sourceClass = source.getClass();
|
||||
BaseMapper<S, T> mapper = (BaseMapper<S, T>) converterFactory.getMapper(sourceClass, target.getClass());
|
||||
if (mapper != null) {
|
||||
return mapper.convert(source, target, beanConsumer);
|
||||
}
|
||||
throw new ConvertException("cannot find converter from " + sourceClass.getSimpleName() + " to " +
|
||||
target.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
public <S, T> List<T> convert(List<S> sourceList, Class<T> targetType) {
|
||||
return convert(sourceList, targetType, (Consumer<T>) null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S, T> List<T> convert(List<S> sourceList, Class<T> targetType, Consumer<T> beanConsumer) {
|
||||
if (sourceList == null || sourceList.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Class<?> sourceType = sourceList.getFirst().getClass();
|
||||
BaseMapper<S, T> mapper = (BaseMapper<S, T>) converterFactory.getMapper(sourceType, targetType);
|
||||
if (mapper != null) {
|
||||
return mapper.convert(sourceList, beanConsumer);
|
||||
}
|
||||
throw new ConvertException("cannot find converter from " + sourceType.getSimpleName() + " to " +
|
||||
targetType.getSimpleName());
|
||||
}
|
||||
|
||||
public <S, T> T convert(S source, Class<T> target, CycleAvoidingMappingContext context) {
|
||||
return convert(source, target, context, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S, T> T convert(S source, Class<T> targetType, CycleAvoidingMappingContext context, Consumer<T> beanConsumer) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
BaseCycleAvoidingMapper<S, T> mapper = (BaseCycleAvoidingMapper<S, T>) converterFactory.getCycleAvoidingMapper(source.getClass(), targetType);
|
||||
if (mapper != null) {
|
||||
return mapper.convert(source, context, beanConsumer);
|
||||
}
|
||||
throw new ConvertException("cannot find converter from " + source.getClass().getSimpleName() + " to " +
|
||||
targetType.getSimpleName());
|
||||
}
|
||||
|
||||
public <S, T> List<T> convert(List<S> sourceList, Class<T> targetType, CycleAvoidingMappingContext context) {
|
||||
return convert(sourceList, targetType, context, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S, T> List<T> convert(List<S> sourceList, Class<T> targetType, CycleAvoidingMappingContext context, Consumer<T> beanConsumer) {
|
||||
if (sourceList == null || sourceList.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Class<?> sourceType = sourceList.getFirst().getClass();
|
||||
BaseCycleAvoidingMapper<S, T> mapper = (BaseCycleAvoidingMapper<S, T>) converterFactory.getCycleAvoidingMapper(sourceType, targetType);
|
||||
if (mapper != null) {
|
||||
return mapper.convert(sourceList, context, beanConsumer);
|
||||
}
|
||||
throw new ConvertException("cannot find converter from " + sourceType.getSimpleName() + " to " + targetType.getSimpleName());
|
||||
}
|
||||
|
||||
public <T> T convert(Map<String, Object> map, Class<T> targetType) {
|
||||
return convert(map, targetType, (Consumer<T>) null);
|
||||
}
|
||||
|
||||
public <T> T convert(Map<String, Object> map, Class<T> targetType, Consumer<T> beanConsumer) {
|
||||
if (map == null || map.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
if (map.values().stream().allMatch(Objects::isNull)) {
|
||||
return null;
|
||||
}
|
||||
final BaseMapMapper<T> mapper = converterFactory.getMapMapper(targetType);
|
||||
if (mapper != null) {
|
||||
return mapper.convert(map, beanConsumer);
|
||||
}
|
||||
throw new ConvertException("cannot find converter from " + map.getClass().getName() + " to " +
|
||||
targetType.getSimpleName());
|
||||
}
|
||||
|
||||
}
|
@ -9,12 +9,12 @@ import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Mapstruct 工具类
|
||||
* <p>参考文档:<a href="https://mapstruct.plus/introduction/quick-start.html">mapstruct-plus</a></p>
|
||||
*
|
||||
*
|
||||
* @author Michelle.Chung
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@ -23,54 +23,90 @@ public class MapstructUtils {
|
||||
private final static Converter CONVERTER = SpringUtils.getBean(Converter.class);
|
||||
|
||||
/**
|
||||
* 将 T 类型对象,转换为 desc 类型的对象并返回
|
||||
* 将 T 类型对象,转换为 target 类型的对象并返回
|
||||
*
|
||||
* @param source 数据来源实体
|
||||
* @param desc 描述对象 转换后的对象
|
||||
* @return desc
|
||||
* @param source 数据来源实体
|
||||
* @param targetType 目标类型 转换后的对象类型
|
||||
* @return target
|
||||
*/
|
||||
public static <T, V> V convert(T source, Class<V> desc) {
|
||||
if (ObjectUtil.isNull(source)) {
|
||||
public static <T, V> V convert(T source, Class<V> targetType) {
|
||||
if (ObjectUtil.hasNull(source, targetType)) {
|
||||
return null;
|
||||
}
|
||||
if (ObjectUtil.isNull(desc)) {
|
||||
return null;
|
||||
}
|
||||
return CONVERTER.convert(source, desc);
|
||||
return CONVERTER.convert(source, targetType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 T 类型对象,按照配置的映射字段规则,给 desc 类型的对象赋值并返回 desc 对象
|
||||
* 将 T 类型对象,转换为 target 类型的对象并返回
|
||||
*
|
||||
* @param source 数据来源实体
|
||||
* @param desc 转换后的对象
|
||||
* @return desc
|
||||
* @param source 数据来源实体
|
||||
* @param targetType 目标类型 转换后的对象类型
|
||||
* @param beanConsumer bean消费者 对象转换后进行一些属性设置
|
||||
* @return target
|
||||
*/
|
||||
public static <T, V> V convert(T source, V desc) {
|
||||
if (ObjectUtil.isNull(source)) {
|
||||
public static <T, V> V convert(T source, Class<V> targetType, Consumer<V> beanConsumer) {
|
||||
if (ObjectUtil.hasNull(source, targetType)) {
|
||||
return null;
|
||||
}
|
||||
if (ObjectUtil.isNull(desc)) {
|
||||
return null;
|
||||
}
|
||||
return CONVERTER.convert(source, desc);
|
||||
return CONVERTER.convert(source, targetType, beanConsumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 T 类型的集合,转换为 desc 类型的集合并返回
|
||||
* 将 T 类型对象,按照配置的映射字段规则,给 target 类型的对象赋值并返回 target 对象
|
||||
*
|
||||
* @param source 数据来源实体
|
||||
* @param target 目标对象 转换后的对象
|
||||
* @return target
|
||||
*/
|
||||
public static <T, V> V convert(T source, V target) {
|
||||
if (ObjectUtil.hasNull(source)) {
|
||||
return null;
|
||||
}
|
||||
return CONVERTER.convert(source, target);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 T 类型对象,转换为 target 类型的对象并返回
|
||||
*
|
||||
* @param source 数据来源实体
|
||||
* @param target 目标对象 转换后的对象
|
||||
* @param beanConsumer bean消费者 对象转换后进行一些属性设置
|
||||
* @return target
|
||||
*/
|
||||
public static <T, V> V convert(T source, V target, Consumer<V> beanConsumer) {
|
||||
if (ObjectUtil.hasNull(source)) {
|
||||
return null;
|
||||
}
|
||||
return CONVERTER.convert(source, target, beanConsumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 T 类型的集合,转换为 target 类型的集合并返回
|
||||
*
|
||||
* @param sourceList 数据来源实体列表
|
||||
* @param desc 描述对象 转换后的对象
|
||||
* @return desc
|
||||
* @param targetType 目标类型 转换后的对象类型
|
||||
* @return targetType
|
||||
*/
|
||||
public static <T, V> List<V> convert(List<T> sourceList, Class<V> desc) {
|
||||
if (ObjectUtil.isNull(sourceList)) {
|
||||
return null;
|
||||
}
|
||||
public static <T, V> List<V> convert(List<T> sourceList, Class<V> targetType) {
|
||||
if (CollUtil.isEmpty(sourceList)) {
|
||||
return CollUtil.newArrayList();
|
||||
}
|
||||
return CONVERTER.convert(sourceList, desc);
|
||||
return CONVERTER.convert(sourceList, targetType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 T 类型的集合,转换为 target 类型的集合并返回
|
||||
*
|
||||
* @param sourceList 数据来源实体列表
|
||||
* @param targetType 目标类型 转换后的对象类型
|
||||
* @param beanConsumer bean消费者 对象转换后进行一些属性设置
|
||||
* @return targetType
|
||||
*/
|
||||
public static <T, V> List<V> convert(List<T> sourceList, Class<V> targetType, Consumer<V> beanConsumer) {
|
||||
if (CollUtil.isEmpty(sourceList)) {
|
||||
return CollUtil.newArrayList();
|
||||
}
|
||||
return CONVERTER.convert(sourceList, targetType, beanConsumer);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -81,13 +117,25 @@ public class MapstructUtils {
|
||||
* @return bean对象
|
||||
*/
|
||||
public static <T> T convert(Map<String, Object> map, Class<T> beanClass) {
|
||||
if (MapUtil.isEmpty(map)) {
|
||||
return null;
|
||||
}
|
||||
if (ObjectUtil.isNull(beanClass)) {
|
||||
if (MapUtil.isEmpty(map) || ObjectUtil.isNull(beanClass)) {
|
||||
return null;
|
||||
}
|
||||
return CONVERTER.convert(map, beanClass);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 将 Map 转换为 beanClass 类型的集合并返回
|
||||
*
|
||||
* @param map 数据来源
|
||||
* @param beanClass bean类
|
||||
* @param beanConsumer bean消费者 对象转换后进行一些属性设置
|
||||
* @return bean对象
|
||||
*/
|
||||
public static <T> T convert(Map<String, Object> map, Class<T> beanClass, Consumer<T> beanConsumer) {
|
||||
if (MapUtil.isEmpty(map) || ObjectUtil.isNull(beanClass)) {
|
||||
return null;
|
||||
}
|
||||
return CONVERTER.convert(map, beanClass, beanConsumer);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user