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

com.github.shootercheng.export.common.ExportCommon Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
package com.github.shootercheng.export.common;

import com.github.shootercheng.export.exception.ParamBuildException;
import com.github.shootercheng.export.utils.ReflectUtil;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author James
 */
public class ExportCommon {
    /**
     * calculate page start index
     * @param sum total number of data
     * @param pageNum page size
     * @return index collection
     */
    public static List calIndexList(int sum, int pageNum) {
        List list = new ArrayList<>(sum / pageNum);
        Integer startIndex = 0;
        if (sum <= pageNum) {
            list.add(startIndex);
            return list;
        }
        while (startIndex + pageNum < sum) {
            list.add(startIndex);
            startIndex = startIndex + pageNum;
        }
        // the last page
        list.add(startIndex);
        return list;
    }

    public static char[] UPPER_CHAR = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S',
            'T','U','V','W','X','Y','Z'};

    /**
     * 从 0 开始, AAA-702 不考虑
     * @param num the char num
     * @return excel column str
     */
    public static String calExcelNumChar(int num) {
        if (num < 0 || num >= 702) {
            throw new IllegalArgumentException("column num input error");
        }
        if (num < 26) {
            return String.valueOf(UPPER_CHAR[num]);
        }
        int firstIndex = num / 26 - 1;
        int secondIndex = num % 26;
        return String.valueOf(UPPER_CHAR[firstIndex]) + UPPER_CHAR[secondIndex];
    }

    public static Map EXCEL_COLUMN = new HashMap<>();

    public static Map COLUMN_NUM = new HashMap<>();

    static {
        for (int i = 0; i < 702; i++) {
            String columnStr = calExcelNumChar(i);
            EXCEL_COLUMN.put(columnStr, i);
            COLUMN_NUM.put(i, columnStr);
        }
    }

    public static List buildParamGetter(Class clazz, Map fieldColumnMap) {
        Set keySet = fieldColumnMap.keySet();
        List keyList = new ArrayList<>(keySet);
        keyList = keyList.stream().sorted().collect(Collectors.toList());
        Map allBeanGetterMap = ReflectUtil.getBeanGetterMap(clazz);
        List resultGetter = new ArrayList<>(keyList.size());
        for (String key : keyList) {
            String fieldName = fieldColumnMap.get(key);
            Method getterMethod = allBeanGetterMap.get(fieldName.toLowerCase());
            if (getterMethod == null) {
                throw new ParamBuildException("Bean " + clazz + " not contain field getter " + fieldName +
                        " ,please check config column map");
            }
            resultGetter.add(getterMethod);
        }
        return resultGetter;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy