Util.java
5.8 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.lecuntao.ordering.util;
import com.alibaba.citrus.util.StringUtil;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Created by Auy_J on 2015/12/25.
*/
public class Util {
// 按任意属性进行排序
static class AnyProperComparator implements Comparator<Object> {
private String properName;// 根据此关键字属性排序
private Enum enumType;
private boolean flag;// 为true的时候是正序,为false的时候是倒序
public AnyProperComparator(String properName, boolean flag) {
super();
this.properName = properName;
this.flag = flag;
}
public void setProperName(String properName) {
this.properName = properName;
}
public String getProperName() {
return properName;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public Enum getEnumType() {
return enumType;
}
public void setEnumType(Enum enumType) {
this.enumType = enumType;
}
/**
* 实现Comparator的对比方法
*
* @param r1
* @param r2
*/
@SuppressWarnings("unchecked")
public int compare(Object r1, Object r2){
Class c = r1.getClass();
double result = 0;
try {
Field field = c.getDeclaredField(properName);
String classType = field.getType().getSimpleName();
Method method = null;
// 这里仅根据方法的返回值类型的名称来判定,比较方便
if ("String".equals(classType)) {
method = c.getMethod("get" + properName.substring(0, 1).toUpperCase() + properName.substring(1), new Class[] {});
if (flag) {
result = ((String) method.invoke(r1)).compareTo((String) method.invoke(r2));
} else {
result = ((String) method.invoke(r2)).compareTo((String) method.invoke(r1));
}
} else if ("Integer".equals(classType) || "int".equals(classType)) {
method = c.getMethod("get" + properName.substring(0, 1).toUpperCase() + properName.substring(1), new Class[] {});
if (flag) {
result = ((Integer) method.invoke(r1)) - ((Integer) method.invoke(r2));
} else {
result = ((Integer) method.invoke(r2)) - ((Integer) method.invoke(r1));
}
} else if ("Double".equals(classType) || "double".equals(classType)) {
method = c.getMethod("get" + properName.substring(0, 1).toUpperCase() + properName.substring(1), new Class[] {});
if (flag) {
result = ((Double) method.invoke(r1)) - ((Double) method.invoke(r2));
} else {
result = ((Double) method.invoke(r2)) - ((Double) method.invoke(r1));
}
} else if ("Float".equals(classType) || "float".equals(classType)) {
method = c.getMethod("get" + properName.substring(0, 1).toUpperCase() + properName.substring(1), new Class[] {});
if (flag) {
result = ((Float) method.invoke(r1)) - ((Float) method.invoke(r2));
} else {
result = ((Float) method.invoke(r2)) - ((Float) method.invoke(r1));
}
} else if ("Long".equals(classType) || "long".equals(classType)) {
method = c.getMethod("get" + properName.substring(0, 1).toUpperCase() + properName.substring(1), new Class[] {});
if (flag) {
result = ((Long) method.invoke(r1)) - ((Long) method.invoke(r2));
} else {
result = ((Long) method.invoke(r2)) - ((Long) method.invoke(r1));
}
} else if (StringUtil.contains(classType, "Enum") || StringUtil.contains(classType, "enum")){
method = c.getMethod("get" + properName.substring(0, 1).toUpperCase() + properName.substring(1), new Class[] {});
if (flag) {
result = ((Enum) method.invoke(r1)).compareTo((Enum) method.invoke(r2));
} else {
result = ((Enum) method.invoke(r2)).compareTo((Enum) method.invoke(r1));
}
}
else {
System.out.println("属性排序只支持数据类型和String,Enum类型,其它类型暂不支持。");
result = -100;
}
} catch (IllegalArgumentException | IllegalAccessException | SecurityException | NoSuchFieldException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
// 确定返回值
if (result > 0) {
return 1;
} else if (result < 0) {
return -1;
}
return 0;
}
}
/**
* 按任意给定的字段进行排序,升序或降序由flag决定
*
* @param list
* @param properName
* @param flag
* @return
*/
@SuppressWarnings("unchecked")
public static <T>List<T> anyProperSort(List<T> list, String properName, boolean flag) {
AnyProperComparator comparator = new AnyProperComparator(properName, flag);
Collections.sort(list, comparator);
return list;
}
}