UserTools.java
2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.objecteye.utils;
import com.alibaba.fastjson.JSONArray;
import com.objecteye.entity.PageResult;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class UserTools {
public static JSONArray getJSONArrayByList(List list) {
JSONArray jsonArray = new JSONArray();
if (list == null || list.isEmpty()) {
return jsonArray;
}
jsonArray.addAll(list);
return jsonArray;
}
public static String nowToDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
/**
* 统一手动分页
*
* @param baseList 原始数据集合
* @param currentPage 页码
* @param pageVolume 页面容量
* @return 结果集
*/
public static PageResult makePageResultByBaseList(List baseList, int currentPage, int pageVolume) {
return new PageResult<>(getPageTotalByBaseList(baseList, pageVolume),
getPagedResultByBaseList(baseList, currentPage, pageVolume));
}
/**
* 手动分页获取页码数量
*
* @param baseList 原始数据集合
* @param pageVolume 页面容量
* @return 页数
*/
public static int getPageTotalByBaseList(List baseList, int pageVolume) {
return getPageTotalByBaseList(baseList.size(), pageVolume);
}
/**
* 手动分页获取页码数量
*
* @param size 集合数量
* @param pageVolume 页面容量
* @return 页数
*/
public static int getPageTotalByBaseList(int size, int pageVolume) {
return (size / pageVolume) + ((size % pageVolume > 0) ? 1 : 0);
}
/**
* 手动获取分页对应页面的数据
*
* @param baseList 原始数据集合
* @param currentPage 页码
* @param pageVolume 页面容量
* @return 数据集合
*/
public static List getPagedResultByBaseList(List baseList, int currentPage, int pageVolume) {
int pageTotal = getPageTotalByBaseList(baseList, pageVolume);
int fromIndex = (currentPage - 1) * pageVolume;
int toIndex;
if (currentPage == pageTotal) {
toIndex = baseList.size();
} else if (pageTotal == 0) {
return new ArrayList<>();
} else {
toIndex = currentPage * pageVolume;
if (toIndex > baseList.size()) {
toIndex = baseList.size();
}
if (fromIndex >= toIndex) {
return new ArrayList<>();
}
}
return baseList.subList(fromIndex, toIndex);
}
}