All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.org.atool.fluent.mybatis.utility.PoJoHelper Maven / Gradle / Ivy

There is a newer version: 1.9.9
Show newest version
package cn.org.atool.fluent.mybatis.utility;

import cn.org.atool.fluent.mybatis.base.IEntity;
import cn.org.atool.fluent.mybatis.base.crud.IQuery;
import cn.org.atool.fluent.mybatis.base.intf.IToMap;
import cn.org.atool.fluent.mybatis.exception.FluentMybatisException;
import cn.org.atool.fluent.mybatis.functions.MapFunction;
import cn.org.atool.fluent.mybatis.metadata.SetterMeta;
import cn.org.atool.fluent.mybatis.segment.model.PagedOffset;
import lombok.NonNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static cn.org.atool.fluent.mybatis.typehandler.ConvertorKit.convertValueToType;
import static cn.org.atool.fluent.mybatis.utility.MybatisUtil.assertNotNull;
import static java.util.stream.Collectors.toList;

/**
 * PoJo转换工具类
 *
 * @author darui.wu
 */
@SuppressWarnings({"unchecked", "rawtypes", "unused"})
public class PoJoHelper {
    /**
     * 将Map转换为指定的PoJo对象
     *
     * @param list      Map或Entity对象列表
     * @param converter 转换函数
     * @param     PoJo类型
     * @return 转换后的对象列表
     */
    public static  List toPoJoList(List> list, MapFunction converter) {
        return list == null ? null : (List) list.stream().map(map -> toPoJo(map, converter)).collect(toList());
    }

    /**
     * 将Map转换为指定的PoJo对象
     *
     * @param map       map或entity对象
     * @param converter 转换函数
     * @param     PoJo类型
     * @return 转换后的对象
     */
    public static  POJO toPoJo(Map map, @NonNull MapFunction converter) {
        return map == null ? null : (POJO) converter.apply(map);
    }

    /**
     * 将Map转换为指定的PoJo对象
     *
     * @param clazz  POJO类型
     * @param list   map对象列表
     * @param  POJO类型
     * @return POJO实例列表
     */
    public static  List toPoJoList(Class clazz, List> list) {
        return list == null ? null : list.stream().map(map -> toPoJo(clazz, map)).collect(toList());
    }

    /**
     * 将Map转换为指定的PoJo对象
     *
     * @param clazz  POJO类型
     * @param list   map对象列表
     * @param  POJO类型
     * @return POJO实例列表
     */
    public static  List toPoJoListIgnoreNotFound(Class clazz, List> list) {
        return list == null ? null : list.stream().map(map -> toPoJoIgnoreNotFound(clazz, map)).collect(toList());
    }

    /**
     * 将Map转换为指定的PoJo对象
     *
     * @param clazz  POJO类型
     * @param map    map对象
     * @param  PoJo类型
     * @return 根据Map值设置后的对象
     */
    public static  POJO toPoJo(@NonNull Class clazz, @NonNull Map map) {
        return toPoJo(clazz, map, false);
    }

    /**
     * 将Map转换为指定的PoJo对象
     *
     * @param clazz  POJO类型
     * @param map    map对象
     * @param  PoJo类型
     * @return 根据Map值设置后的对象
     */
    public static  POJO toPoJoIgnoreNotFound(@NonNull Class clazz, @NonNull Map map) {
        return toPoJo(clazz, map, true);
    }

    private static  POJO toPoJo(@NonNull Class klass, @NonNull Map map, boolean ignoreNotFound) {
        POJO target = newInstance(klass);
        for (Map.Entry entry : map.entrySet()) {
            String name = MybatisUtil.underlineToCamel(entry.getKey(), false);
            SetterMeta meta = SetterMeta.get(klass, name);
            if (meta == null) {
                if (ignoreNotFound) {
                    continue;
                }
                throw new RuntimeException("property[" + name + "] of class[" + klass.getName() + "] not found.");
            }
            try {
                Object value = entry.getValue();
                if (value != null) {
                    Object _value = convertValueToType(value, meta.fType);
                    meta.setValue(target, _value);
                }
            } catch (Exception e) {
                String err = String.format("convert map to object[class=%s, property=%s, type=%s] error: %s",
                    klass.getName(), name, meta.fType.toString(), e.getMessage());
                throw new RuntimeException(err, e);
            }
        }
        return target;
    }

    private static  POJO newInstance(Class clazz) {
        try {
            return clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException("convert map to object[type=" + clazz.getName() + "] error: " + e.getMessage(), e);
        }
    }

    /**
     * 将object对象转换为map
     *
     * @param object 转换对象
     * @return Map
     */
    public static Map toMap(Object object) {
        assertNotNull("object", object);
        if (object instanceof IEntity) {
            return ((IEntity) object).toEntityMap();
        } else if (object instanceof Map) {
            return new HashMap((Map) object);
        } else if (object instanceof IToMap) {
            return ((IToMap) object).toMap();
        } else {
            return IToMap.toMap(object);
        }
    }

    /**
     * 校验marker方式分页的分页参数合法性
     *
     * @param query 查询条件
     * @return 最大查询数
     */
    public static int validateTagPaged(IQuery query) {
        PagedOffset paged = query.data().paged();
        if (paged == null) {
            throw new FluentMybatisException("Paged parameter not set");
        }
        if (paged.getOffset() != 0) {
            throw new FluentMybatisException("The offset of TagList should from zero, please use method: limit(size) or limit(0, size) .");
        }
        return paged.getLimit();
    }

    /**
     * 返回list对象属性值数组
     *
     * @param list    对象列表
     * @param getFunc 获取list元素对应属性方法
     * @param      转换对象类型
     * @return 属性值数组
     */
    public static  Object[] getFields(List list, Function getFunc) {
        return list.stream().map(getFunc).toArray(Object[]::new);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy