/ * * *@Description: memory paging, field sort *@Author: YanMei.Li
 * @Date: Create in 10:59 2021/11/10
 * @Modified By:
 */
public class MemoryPagination {

    /** * List attributes can be numbers (byte, short, int, long, float, double, etc.), char, String, java.util.Date **@paramList data set *@paramPageNum Number of pages *@paramPageSize Number of lines * per page@paramIsSort whether to sort *@paramSortNameArr parameter array *@paramSortArr The descending ordinal group for each attribute, true ascending, false descending */
    public static <E> List<E> sortMemoryPagination(List<E> list, int pageNum, int pageSize, final boolean isSort , final String[] sortNameArr, final boolean[] sortArr) {
        if(isSort){
            if(sortNameArr.length ! = sortArr.length) {throw new RuntimeException("The number of elements in the attribute array is not equal to the number of elements in the ascending ordinal array.");
            }
            Collections.sort(list, new Comparator<E>() {
                @Override
                public int compare(E a, E b) {
                    int ret = 0;
                    try {
                        for (int i = 0; i < sortNameArr.length; i++) {
                            ret = MemoryPagination.compareObject(sortNameArr[i], sortArr[i], a, b);
                            if (0! = ret) {break; }}}catch (Exception e) {
                        e.printStackTrace();
                    }
                    returnret; }}); }return pagination(list,pageNum,pageSize);
    }

    /** ** Memory paging **@paramRecords Data to be paged *@paramPageNum Current page number *@paramPageSize Number of items to be displayed on a page *@returnData after paging */
    private static <T> List<T> pagination(List<T> records, int pageNum, int pageSize) {
        if (CollectionUtils.isEmpty(records)) {
            return Collections.emptyList();
        }
        int totalCount = records.size();
        int remainder = totalCount % pageSize;
        int pageCount = (remainder > 0)? totalCount/pageSize +1 : totalCount/pageSize;
        if (remainder == 0) {
            return records.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        } else {
            if (pageNum == pageCount) {
                return records.stream().skip((pageNum - 1) * pageSize).limit(totalCount).collect(Collectors.toList());
            } else {
                return records.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList()); }}}/** * Sorts two objects by the specified attribute name **@paramSortname * Attribute name *@paramIsAsc * true ascending, false descending *@param a
     * @param b
     * @return
     * @throws Exception
     */
    private static <E> int compareObject(final String sortname, final boolean isAsc, E a, E b) throws Exception {
        int ret;
        Object value1 = MemoryPagination.forceGetFieldValue(a, sortname);
        Object value2 = MemoryPagination.forceGetFieldValue(b, sortname);
        String str1 = value1.toString();
        String str2 = value2.toString();
        if (value1 instanceof Number && value2 instanceof Number) {
            int maxlen = Math.max(str1.length(), str2.length());
            str1 = MemoryPagination.addZero2Str((Number) value1, maxlen);
            str2 = MemoryPagination.addZero2Str((Number) value2, maxlen);
        } else if (value1 instanceof java.sql.Date && value2 instanceof java.sql.Date) {
            long time1 = ((java.sql.Date) value1).getTime();
            long time2 = ((Date) value2).getTime();
            int maxlen = Long.toString(Math.max(time1, time2)).length();
            str1 = MemoryPagination.addZero2Str(time1, maxlen);
            str2 = MemoryPagination.addZero2Str(time2, maxlen);
        }
        if (isAsc) {
            ret = str1.compareTo(str2);
        } else {
            ret = str2.compareTo(str1);
        }
        return ret;
    }

    /** * Gets the specified property value of the specified object **@paramObj * The object of the property name *@paramFieldName * Attribute name *@return
     * @throws Exception
     */
    public static Object forceGetFieldValue(Object obj, String fieldName) throws Exception {
        Field field = obj.getClass().getDeclaredField(fieldName);
        Object object = null;
        boolean accessible = field.isAccessible();
        if(! accessible) {// If the property is private,protected, you need to make it accessible
            field.setAccessible(true);
            object = field.get(obj);
            // Restore the access properties of the private,protected properties
            field.setAccessible(accessible);
            return object;
        }
        object = field.get(obj);
        return object;
    }

    ** use case: addZero2Str(11,4) returns "0011"; addZero2Str(-18,6) returns "-000018" **@paramNumObj * Digital objects *@paramLength * Specifies the length *@return* /
    public static String addZero2Str(Number numObj, int length) {
        NumberFormat nf = NumberFormat.getInstance();
        // Set whether to use grouping
        nf.setGroupingUsed(false);
        // Set the maximum number of digits
        nf.setMaximumIntegerDigits(length);
        // Set the minimum number of integers
        nf.setMinimumIntegerDigits(length);
        return nf.format(numObj);
    }

    public static void main(String[] args) {
        List<SysDict> list = new ArrayList<>();
        list.add(new SysDict(1."test1"."1".1.3.32));
        list.add(new SysDict(3."test3"."3".3.4.13));
        list.add(new SysDict(2."test2"."11".2.4.11));
        list.add(new SysDict(4."test4"."4".4.23.23));
        list.add(new SysDict(5."test5"."5".5.0.23));
        list.add(new SysDict(6."test6"."22".6.0.23));
        list.add(new SysDict(7."test7"."3".7.0.23));
        list.add(new SysDict(8."test8"."0.2".8.0.23));


// String[] sortnameArr = {"ss","sort"};
// boolean[] typeArr = {true,false};
        String[] sortnameArr = {"name"};
        boolean[] typeArr = {false};
        List<SysDict> ss = sortMemoryPagination(list, 1.2.true, sortnameArr,typeArr); System.out.println(ss.toString()); }}class SysDict {
    private Integer id;
    private String name;
    private String value;
    private Integer sort;
    private Double ss;

    public SysDict(Integer id, String name, String value, Integer sort,Double ss) {
        this.id = id;
        this.name = name;
        this.value = value;
        this.sort = sort;
        this.ss = ss;
    }

    public Double getSs(a) {
        return ss;
    }

    public void setSs(Double ss) {
        this.ss = ss;
    }

    public Integer getId(a) {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue(a) {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Integer getSort(a) {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    @Override
    public String toString(a) {
        return "SysDict{" +
                "id=" + id +
                ", name='" + name + ' '' + ", value='" + value + ''' + ", sort=" + sort + ", ss=" + ss + '}'; }}Copy the code