!19 enhance Converter支持Consumer函数

Merge pull request !19 from 秋辞未寒/main
This commit is contained in:
easii 2025-07-16 06:24:53 +00:00 committed by Gitee
commit 793da1b8d1
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 279 additions and 53 deletions

View File

@ -1,10 +1,13 @@
package io.github.linpeilie;
import io.github.linpeilie.utils.CollectionUtils;
import io.github.linpeilie.utils.ObjectUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.function.Consumer;
public class Converter {
@ -20,77 +23,134 @@ public class Converter {
@SuppressWarnings("unchecked")
public <S, T> T convert(S source, Class<T> targetType) {
if (source == null) {
if (ObjectUtils.isAnyNull(source, targetType)) {
return null;
}
BaseMapper<S, T> mapper = (BaseMapper<S, T>) converterFactory.getMapper(source.getClass(), targetType);
if (mapper != null) {
return mapper.convert(source);
Class<?> sourceType = source.getClass();
BaseMapper<S, T> mapper = (BaseMapper<S, T>) converterFactory.getMapper(sourceType, targetType);
if (ObjectUtils.isNull(mapper)) {
throw new ConvertException("cannot find converter from " + sourceType.getSimpleName() + " to " + targetType.getSimpleName());
}
throw new ConvertException(
"cannot find converter from " + source.getClass().getSimpleName() + " to " + targetType.getSimpleName());
return mapper.convert(source);
}
public <S, T> T convert(S source, Class<T> targetType, Consumer<T> beanConsumer) {
T bean = convert(source, targetType);
return ObjectUtils.handleIfNull(bean, beanConsumer);
}
@SuppressWarnings("unchecked")
public <S, T> T convert(S source, T target) {
if (source == null) {
if (ObjectUtils.isAnyNull(source, target)) {
return null;
}
if (target == null) {
return null;
Class<?> sourceType = source.getClass();
Class<?> targetType = target.getClass();
BaseMapper<S, T> mapper = (BaseMapper<S, T>) converterFactory.getMapper(sourceType, targetType);
if (ObjectUtils.isNull(mapper)) {
throw new ConvertException("cannot find converter from " + sourceType.getSimpleName() + " to " + targetType.getSimpleName());
}
BaseMapper<S, T> mapper = (BaseMapper<S, T>) converterFactory.getMapper(source.getClass(), target.getClass());
if (mapper != null) {
return mapper.convert(source, target);
}
throw new ConvertException("cannot find converter from " + source.getClass().getSimpleName() + " to " +
target.getClass().getSimpleName());
return mapper.convert(source, target);
}
public <S, T> List<T> convert(List<S> source, Class<T> targetType) {
if (source == null || source.isEmpty()) {
return new ArrayList<>();
}
return source.stream().map(item -> convert(item, targetType)).collect(Collectors.toList());
public <S, T> T convert(S source, T target, Consumer<T> beanConsumer) {
T bean = convert(source, target);
return ObjectUtils.handleIfNull(bean, beanConsumer);
}
public <S, T> List<T> convert(List<S> sourceList, Class<T> targetType) {
return convert(sourceList, targetType, (Consumer<T>) null);
}
@SuppressWarnings("unchecked")
public <S, T> T convert(S source, Class<T> target, CycleAvoidingMappingContext context) {
if (source == null) {
return null;
}
BaseCycleAvoidingMapper<S, T> mapper =
(BaseCycleAvoidingMapper<S, T>) converterFactory.getCycleAvoidingMapper(source.getClass(), target);
if (mapper != null) {
return mapper.convert(source, context);
}
throw new ConvertException("cannot find converter from " + source.getClass().getSimpleName() + " to " +
target.getSimpleName());
}
public <S, T> List<T> convert(List<S> source, Class<T> targetType, CycleAvoidingMappingContext context) {
if (source == null || source.isEmpty()) {
public <S, T> List<T> convert(List<S> sourceList, Class<T> targetType, Consumer<T> beanConsumer) {
if (CollectionUtils.isEmpty(sourceList)) {
return new ArrayList<>();
}
return source.stream().map(item -> convert(item, targetType, context)).collect(Collectors.toList());
if (ObjectUtils.isNull(targetType)) {
throw new ConvertException("targetType cannot be null");
}
Class<?> sourceType = sourceList.getFirst().getClass();
BaseMapper<S, T> mapper = (BaseMapper<S, T>) converterFactory.getMapper(sourceType, targetType);
if (ObjectUtils.isNull(mapper)) {
throw new ConvertException("cannot find converter from " + sourceType.getSimpleName() + " to " + targetType.getSimpleName());
}
// 如果 beanConsumer 本来就为 null则不再调用带 Consumer 参数的 convert 方法避免在循环中进行不必要的非空判断
if (ObjectUtils.isNotNull(beanConsumer)) {
return CollectionUtils.toList(sourceList, source -> {
T bean = mapper.convert(source);
beanConsumer.accept(bean);
return bean;
});
}
return mapper.convert(sourceList);
}
public <T> T convert(Map<String, Object> map, Class<T> target) {
if (map == null || map.isEmpty()) {
@SuppressWarnings("unchecked")
public <S, T> T convert(S source, Class<T> targetType, CycleAvoidingMappingContext context) {
if (ObjectUtils.isAnyNull(source, targetType)) {
return null;
}
if (map.values().stream().allMatch(Objects::isNull)) {
Class<?> sourceType = source.getClass();
BaseCycleAvoidingMapper<S, T> mapper = (BaseCycleAvoidingMapper<S, T>) converterFactory.getCycleAvoidingMapper(sourceType, targetType);
if (ObjectUtils.isNull(mapper)) {
throw new ConvertException("cannot find converter from " + sourceType.getSimpleName() + " to " + targetType.getSimpleName());
}
return mapper.convert(source, context);
}
public <S, T> T convert(S source, Class<T> targetType, CycleAvoidingMappingContext context, Consumer<T> beanConsumer) {
T bean = convert(source, targetType, context);
return ObjectUtils.handleIfNull(bean, beanConsumer);
}
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 (CollectionUtils.isEmpty(sourceList)) {
return new ArrayList<>();
}
if (ObjectUtils.isNull(targetType)) {
throw new ConvertException("targetType cannot be null");
}
Class<?> sourceType = sourceList.getFirst().getClass();
BaseCycleAvoidingMapper<S, T> mapper = (BaseCycleAvoidingMapper<S, T>) converterFactory.getCycleAvoidingMapper(sourceType, targetType);
if (ObjectUtils.isNull(mapper)) {
throw new ConvertException("cannot find converter from " + sourceType.getSimpleName() + " to " + targetType.getSimpleName());
}
// 如果 beanConsumer 本来就为 null则不再调用带 Consumer 参数的 convert 方法避免在循环中进行不必要的非空判断
if (ObjectUtils.isNotNull(beanConsumer)) {
return CollectionUtils.toList(sourceList, source -> {
T bean = mapper.convert(source, context);
beanConsumer.accept(bean);
return bean;
});
}
return mapper.convert(sourceList, context);
}
public <T> T convert(Map<String, Object> map, Class<T> targetType) {
if (CollectionUtils.isEmpty(map) || map.values().stream().allMatch(Objects::isNull)) {
return null;
}
final BaseMapMapper<T> mapper = converterFactory.getMapMapper(target);
if (mapper != null) {
return mapper.convert(map);
if (ObjectUtils.isNull(targetType)) {
throw new ConvertException("targetType cannot be null");
}
throw new ConvertException("cannot find converter from " + map.getClass().getName() + " to " +
target.getSimpleName());
final BaseMapMapper<T> mapper = converterFactory.getMapMapper(targetType);
if (ObjectUtils.isNull(mapper)) {
throw new ConvertException("cannot find converter from " + map.getClass().getName() + " to " + targetType.getSimpleName());
}
return mapper.convert(map);
}
public <T> T convert(Map<String, Object> map, Class<T> targetType, Consumer<T> beanConsumer) {
T bean = convert(map, targetType);
return ObjectUtils.handleIfNull(bean, beanConsumer);
}
}

View File

@ -1,23 +1,52 @@
package io.github.linpeilie.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CollectionUtils {
/**
* 判断集合是否为空
*
* @param collection 集合对象
* @return 是否为空
*/
public static boolean isEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}
/**
* 判断集合是否不为空
*
* @param collection 集合对象
* @return 是否不为空
*/
public static boolean isNotEmpty(Collection<?> collection) {
return !isEmpty(collection);
}
/**
* 判断Map是否为空
*
* @param map Map对象
* @return 是否为空
*/
public static boolean isEmpty(Map<?, ?> map) {
return null == map || map.isEmpty();
}
/**
* 判断Map是否不为空
*
* @param map Map对象
* @return 是否不为空
*/
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
@SafeVarargs
public static <T> List<T> newArrayList(T... values) {
if (values == null || values.length == 0) {
return new ArrayList<>();
@ -34,4 +63,26 @@ public class CollectionUtils {
return list.stream().map(str -> prefix + str + suffix).collect(Collectors.joining(delimiter));
}
/**
* 将collection转化为List集合但是两者的泛型不同<br>
* <B>{@code Collection<E> ------> List<T> } </B>
*
* @param collection 需要转化的集合
* @param function collection中的泛型转化为list泛型的lambda表达式
* @param <E> collection中的泛型
* @param <T> List中的泛型
* @return 转化后的list
*/
public static <E, T> List<T> toList(Collection<E> collection, Function<E, T> function) {
if (CollectionUtils.isEmpty(collection)) {
return CollectionUtils.newArrayList();
}
return collection
.stream()
.map(function)
.filter(Objects::nonNull)
// 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,115 @@
package io.github.linpeilie.utils;
import java.util.function.Consumer;
import java.util.function.Function;
public class ObjectUtils {
/**
* 检查对象是否为null<br>
* 判断标准为
*
* <pre>
* 1. == null
* 2. equals(null)
* </pre>
*
* @param obj 对象
* @return 是否为null
*/
public static boolean isNull(Object obj) {
//noinspection ConstantConditions
return null == obj || obj.equals(null);
}
/**
* 检查对象是否不为null
* <pre>
* 1. != null
* 2. not equals(null)
* </pre>
*
* @param obj 对象
* @return 是否为非null
*/
public static boolean isNotNull(Object obj) {
return !isNull(obj);
}
/**
* 是否包含{@code null}元素
*
* @param <T> 数组元素类型
* @param array 被检查的数组
* @return 是否包含{@code null}元素
*/
@SuppressWarnings("unchecked")
public static <T> boolean isAnyNull(T... array) {
if (ArrayUtil.isNotEmpty(array)) {
for (T element : array) {
if (isNull(element)) {
return true;
}
}
}
return array == null;
}
/**
* 是否全都不为{@code null}元素
*
* @param <T> 数组元素类型
* @param array 被检查的数组
* @return 是否全都不为{@code null}元素
*/
@SuppressWarnings("unchecked")
public static <T> boolean isAllNotNull(T... array) {
return !isAnyNull(array);
}
/**
* 如果给定对象不为null 则以给定对象作为参数执行handle方法
*
* @param <T> 被检查对象
* @param source Object 类型对象
* @param handle 非空时自定义的处理方法
* @return 处理后的对象
*/
public static <T> T handleIfNull(T source, Consumer<T> handle) {
if (isAllNotNull(source, handle)) {
handle.accept(source);
}
return source;
}
/**
* 如果给定对象为{@code null} 返回默认值, 如果不为null 返回自定义handle处理后的返回值
*
* @param <T> 被检查对象为{@code null}返回默认值否则返回自定义handle处理后的返回值
* @param <R> 被检查的对象类型
* @param source Object 类型对象
* @param handle 非空时自定义的处理方法
* @param defaultValue 默认为空的返回值
* @return 处理后的返回值
*/
public static <T, R> T defaultIfNull(R source, Function<R, ? extends T> handle, final T defaultValue) {
if (isNotNull(source)) {
return handle.apply(source);
}
return defaultValue;
}
/**
* 如果给定对象为{@code null} 返回默认值, 如果不为null 返回自定义handle处理后的返回值
*
* @param <T> 被检查对象为{@code null}返回默认值否则返回自定义handle处理后的返回值
* @param <R> 被检查的对象类型
* @param source Object 类型对象
* @param handle 非空时自定义的处理方法
* @return 处理后的返回值
*/
public static <T, R> T defaultIfNull(R source, Function<R, ? extends T> handle) {
return defaultIfNull(source, handle, null);
}
}