TransformUtil--自定义对象转换器

41 阅读1分钟
import java.util.List;

/**
 * 通用的分页返回对象
 */
public class PageResponse<T> {

    private Integer pageNum;

    private Integer pageSize;

    private Integer total;

    private Integer totalPages;

    private List<T> list;

    private PageResponse() {
    }

    public PageResponse(Integer pageNum, Integer pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 自定义的对象转换器,主要使用BeanUtil#copyProperties进行转换,也可以传入转换方法处理
 *
 **/
public class TransformUtil {

    private TransformUtil() {
    }

    /**
     * 直接进行对象的转换,对象属性复制
     *
     * @param source   被转换对象
     * @param supplier 自定义转换后对象
     * @param <R>      返回的对象
     * @param <T>      被转换的对象
     * @return 返回转换后对象
     */
    public static <R, T> R transform(T source, Supplier<R> supplier) {
        if (null == source) {
            return null;
        }
        R target = supplier.get();
        BeanUtils.copyProperties(source, target);
        return target;
    }

    /**
     * 使用转换方法转换对象
     *
     * @param source          被转换对象
     * @param transformMethod 转换方法
     * @param <R>             返回的对象
     * @param <T>             被转换的对象
     * @return 返回转换后对象
     */
    public static <R, T> R transform(T source, Function<T, R> transformMethod) {
        if (null == source) {
            return null;
        }
        return transformMethod.apply(source);
    }

    /**
     * 转换List对象
     */
    public static <R, T> List<R> transform(List<T> sourceList, Supplier<R> supplier) {
        return transform(sourceList, (Function<T, R>) source -> transform(source, supplier));
    }

    /**
     * 使用转换方法转换List对象
     */
    public static <R, T> List<R> transform(List<T> sourceList, Function<T, R> transformMethod) {
        List<R> targetList = new ArrayList<>(sourceList.size());
        sourceList.forEach(source -> targetList.add(transform(source, transformMethod)));
        return targetList;
    }

    /**
     * 转换PageResponse对象
     */
    public static <R, T> PageResponse<R> transform(PageResponse<T> sourcePage, Supplier<R> supplier) {
        return transform(sourcePage, (Function<T, R>) source -> transform(source, supplier));
    }

    /**
     * 使用转换方法转换PageResponse对象
     */
    public static <R, T> PageResponse<R> transform(PageResponse<T> sourcePage, Function<T, R> transformMethod) {
        PageResponse<R> targetPage = new PageResponse<>(sourcePage.getPageNum(), sourcePage.getPageSize());
        targetPage.setTotalPages(sourcePage.getTotalPages());
        targetPage.setTotal(sourcePage.getTotal());
        List<R> targetList = new ArrayList<>(sourcePage.getList().size());
        sourcePage.getList().forEach(source -> targetList.add(transform(source, transformMethod)));
        targetPage.setList(targetList);
        return targetPage;
    }
    
}