集合封装类

// An highlighted block
package com.softinfor.common.core.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cxl
 * @date 2024/3/17
 */
public class ListUtils {


    /**
     * 分页函数
     * @author pochettino
     * @param currentPage   当前页数
     * @param pageSize  每一页的数据条数
     * @param list  要进行分页的数据列表
     * @return  当前页要展示的数据
     */
    public static Page getPages(Integer currentPage, Integer pageSize, List list) {
        Page page = new Page();
        if(list==null){
            return  null;
        }
        int size = list.size();

        if(pageSize > size) {
            pageSize = size;
        }
        if (pageSize!=0){
            // 求出最大页数,防止currentPage越界
            int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;

            if(currentPage > maxPage) {
                currentPage = maxPage;
            }
        }
        // 当前页第一条数据的下标
        int curIdx = currentPage > 1 ? (currentPage - 1) * pageSize : 0;

        List pageList = new ArrayList();

        // 将当前页的数据放进pageList
        for(int i = 0; i < pageSize && curIdx + i < size; i++) {
            pageList.add(list.get(curIdx + i));
        }

        page.setCurrent(currentPage).setSize(pageSize).setTotal(list.size()).setRecords(pageList);
        return page;
    }
}

BeanUtils

package com.softinfor.common.core.utils;

import org.springframework.cglib.beans.BeanCopier;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Date
 * @Author cxl
 */
public class BeanUtil {
    private static final Map<String, BeanCopier> BEAN_COPIER_MAP = new HashMap<>();

    /**
     * 深拷贝
     *
     * @param str
     * @param target
     */
    public static void copy(Object str, Object target) {
        if (str == null) {
            return;
        }

        Objects.requireNonNull(target);
        String key = getKey(str, target);
        BeanCopier beanCopier;
        if (!BEAN_COPIER_MAP.containsKey(key)) {
            beanCopier = BeanCopier.create(str.getClass(), target.getClass(), false);
            BEAN_COPIER_MAP.put(key, beanCopier);
        } else {
            beanCopier = BEAN_COPIER_MAP.get(key);
        }
        beanCopier.copy(str, target, null);
    }

    /**
     * 深拷贝
     *
     * @param source
     * @param target
     * @param <T>
     * @return
     */
    public static <T> T copy(Object source, Class<T> target) {
        if (source == null) {
            return null;
        }
        Objects.requireNonNull(target);
        T dest = null;
        try {
            dest = target.getDeclaredConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        copy(source, dest);
        return dest;
    }

    /**
     * List深拷贝
     *
     * @param strList
     * @param tClass
     * @param <T>
     * @param <S>
     * @return
     */
    public static <T, S> List<T> copyList(List<S> strList, Class<T> tClass) {
        Objects.requireNonNull(tClass);
        return strList.stream().map(src -> copy(src, tClass)).collect(Collectors.toList());

    }

    /**
     * 获取Map Key
     *
     * @param str
     * @param target
     * @return
     */
    private static String getKey(Object str, Object target) {
        return str.getClass().getName() + target.getClass().getName();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值