MemoryPagination 記憶體分頁
`
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field;
import java.sql.Date;
import java.text.NumberFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
-
@author yaoguanquan
-
@creote 2020-08-06-15:36
*/
public class MemoryPagination {/**
-
給list的每個屬性都指定是升序還是降序
-
list元素的屬性可以是數字(byte、short、int、long、float、double等,支援正數、負數、0)、char、String、java.util.Date
-
@param list 資料集合
-
@param pageNum 第幾頁
-
@param pageSize 每頁多少條
-
@param isSort 是否排序
-
@param sortNameArr 引數陣列
-
@param sortArr 每個屬性對應的升降序陣列, true升序,false降序
*/
public staticList sortMemoryPagination(List list, int pageNum, int pageSize,final boolean isSort , final String[] sortNameArr, final boolean[] sortArr) {
if(isSort){
if (sortNameArr.length != sortArr.length) {
throw new RuntimeException("屬性陣列元素個數和升降序陣列元素個數不相等");
}
Collections.sort(list, new Comparator() {
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();
}
return ret;
}
});}
return pagination(list,pageNum,pageSize);
}
/**
- 記憶體分頁
- @param records 待分頁的資料
- @param pageNum 當前頁碼
- @param pageSize 每頁顯示的條數
- @return 分頁之後的資料
*/
private staticList pagination(List 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());
}
}
}
/**
- 對2個物件按照指定屬性名稱進行排序
- @param sortname
-
屬性名稱
- @param isAsc
-
true升序,false降序
- @param a
- @param b
- @return
- @throws Exception
*/
private staticint 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;
}
/**
- 獲取指定物件的指定屬性值(去除private,protected的限制)
- @param obj
-
屬性名稱所在的物件
- @param fieldName
-
屬性名稱
- @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) {
// 如果是private,protected修飾的屬性,需要修改為可以訪問的
field.setAccessible(true);
object = field.get(obj);
// 還原private,protected屬性的訪問性質
field.setAccessible(accessible);
return object;
}
object = field.get(obj);
return object;
}
/**
- 給數字物件按照指定長度在左側補0.
- 使用案例: addZero2Str(11,4) 返回 "0011", addZero2Str(-18,6)返回 "-000018"
- @param numObj
-
數字物件
- @param length
-
指定的長度
- @return
*/
public static String addZero2Str(Number numObj, int length) {
NumberFormat nf = NumberFormat.getInstance();
// 設定是否使用分組
nf.setGroupingUsed(false);
// 設定最大整數位數
nf.setMaximumIntegerDigits(length);
// 設定最小整數位數
nf.setMinimumIntegerDigits(length);
return nf.format(numObj);
}
public static void main(String[] args) {
Listlist = 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}; 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() {
// return ss;
// }
//
// public void setSs(Double ss) {
// this.ss = ss;
// }
//
// public Integer getId() {
// return id;
// }
//
// public void setId(Integer id) {
// this.id = id;
// }
//
// public String getName() {
// return name;
// }
//
// public void setName(String name) {
// this.name = name;
// }
//
// public String getValue() {
// return value;
// }
//
// public void setValue(String value) {
// this.value = value;
// }
//
// public Integer getSort() {
// return sort;
// }
//
// public void setSort(Integer sort) {
// this.sort = sort;
// }
//
// @Override
// public String toString() {
// return "SysDict{" +
// "id=" + id +
// ", name='" + name + ''' +
// ", value='" + value + ''' +
// ", sort=" + sort +
// ", ss=" + ss +
// '}';
// }
//
//}
`