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

com.github.trang.copiers.MapCopiers Maven / Gradle / Ivy

There is a newer version: 2.5.4
Show newest version
package com.github.trang.copiers;

import com.github.trang.copiers.cglib.BeanToMapCopier;
import com.github.trang.copiers.cglib.MapToBeanCopier;

import java.util.List;
import java.util.Map;

/**
 * MapCopiers 工具类,提供 Map 和 JavaBean 之间的相互拷贝
 *
 * @author trang
 */
public final class MapCopiers {

    private MapCopiers() {
        throw new UnsupportedOperationException();
    }

    /**
     * 单个 Bean 转换为 Map
     *
     * @param bean 源对象
     * @return map
     */
    public static  Map beanToMap(T bean) {
        return new BeanToMapCopier<>().copy(bean);
    }

    /**
     * 单个 Bean 转换为已经存在的 Map
     *
     * @param bean 源对象
     * @param map  map
     */
    public static  void beanToMap(T bean, Map map) {
        new BeanToMapCopier<>().copy(bean, map);
    }

    /**
     * Bean List 转换为 Map List
     *
     * @param list 源对象集合
     * @return list
     */
    public static  List> beansToMap(List list) {
        return new BeanToMapCopier().map(list);
    }

    /**
     * Map 转换为 Bean
     *
     * @param map       map
     * @param beanClass 目标类型
     * @return bean
     */
    public static  T mapToBean(Map map, Class beanClass) {
        return new MapToBeanCopier<>(beanClass).copy(map);
    }

    /**
     * Map 转换为已经存在的 Bean
     *
     * @param map  map
     * @param bean 目标对象
     */
    @SuppressWarnings("unchecked")
    public static  void mapToBean(Map map, T bean) {
        Class beanClass = ((Class) bean.getClass());
        new MapToBeanCopier<>(beanClass).copy(map, bean);
    }

    /**
     * Map List 转换为 Bean List
     *
     * @param list map 集合
     * @return list
     */
    public static  List mapToBeans(List> list, Class beanClass) {
        return new MapToBeanCopier<>(beanClass).map(list);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy