com.mobaijun.core.util.MapstructUtil Maven / Gradle / Ivy
/*
* Copyright (C) 2022 [www.mobaijun.com]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mobaijun.core.util;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mobaijun.core.spring.SpringUtil;
import io.github.linpeilie.Converter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Description:
* Mapstruct 工具类
* 参考文档:mapstruct-plus
* Author: [mobaijun]
* Date: [2024/8/19 16:00]
* IntelliJ IDEA Version: [IntelliJ IDEA 2023.1.4]
*/
public class MapstructUtil {
private final static Converter CONVERTER = SpringUtil.getBean(Converter.class);
/**
* 将 T 类型对象,转换为 desc 类型的对象并返回
*
* @param source 数据来源实体
* @param desc 描述对象 转换后的对象
* @return desc
*/
public static V convert(T source, Class desc) {
if (Objects.isNull(source)) {
return null;
}
if (Objects.isNull(desc)) {
return null;
}
return CONVERTER.convert(source, desc);
}
/**
* 将 T 类型对象,按照配置的映射字段规则,给 desc 类型的对象赋值并返回 desc 对象
*
* @param source 数据来源实体
* @param desc 转换后的对象
* @return desc
*/
public static V convert(T source, V desc) {
if (Objects.isNull(source)) {
return null;
}
if (Objects.isNull(desc)) {
return null;
}
return CONVERTER.convert(source, desc);
}
/**
* 将 T 类型的集合,转换为 desc 类型的集合并返回
*
* @param sourceList 数据来源实体列表
* @param desc 描述对象 转换后的对象
* @return desc
*/
public static List convert(List sourceList, Class desc) {
if (sourceList.isEmpty()) {
return Collections.emptyList();
}
if (Objects.isNull(desc)) {
return null;
}
return CONVERTER.convert(sourceList, desc);
}
/**
* 将 Map 转换为 beanClass 类型的集合并返回
*
* @param map 数据来源
* @param beanClass bean类
* @return bean对象
*/
public static T convert(Map map, Class beanClass) {
if (MapUtil.isEmpty(map)) {
return null;
}
if (ObjectUtil.isNull(beanClass)) {
return null;
}
return CONVERTER.convert(map, beanClass);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy