-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathInternalUtils.java
140 lines (123 loc) · 4.74 KB
/
InternalUtils.java
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
package com.kvn.poi.exp.function;
import org.joda.time.DateTime;
import java.util.Date;
import java.util.List;
/**
* <pre>
* 内部函数库,使用EL表达式在excel模板中调用。
* 示例:
* #{ T(com.kvn.poi.function.InternalUtils).fmtDate(beginTime,'yyyy-MM-dd') }
* </pre>
*
* @author wzy
* @date 2017年7月6日 下午5:47:03
*/
public class InternalUtils {
public static void main(String[] args) {
Integer i1 = new Integer(3);
Integer i2 = new Integer(2);
System.out.println(((Comparable) i1).compareTo(i2));
}
/**
* 日期格式化
*
* @param date
* @param pattern
* @return
*/
public static String fmtDate(Date date, String pattern) {
DateTime dt = new DateTime(date.getTime());
return dt.toString(pattern);
}
/**
* 匹配替换函数,处理target非数组的情况
*
* @param source 源数据
* @param target 待匹配的目标数据
* @param matchedPlaceholder 匹配成功后返回的占位符
* @param missedPlaceholder 匹配失败后返回的占位符
* @return
*/
public static <T> String matchReplaceBy(T source, T target, String matchedPlaceholder, String missedPlaceholder) {
if (source == null || target == null) {
return missedPlaceholder;
}
Class<?> targetClass = target.getClass();
if (targetClass.isArray()) {
throw new IllegalArgumentException("请使用其他函数,泛型不支持数组类型:target class is " + targetClass);
}
if (targetClass.isPrimitive()) { // 基本数据类型
return source == target ? matchedPlaceholder : missedPlaceholder;
}
if (Comparable.class.isAssignableFrom(targetClass)) { // 基本数据的包装类型,或者实现了 Comparable 的类型
return ((Comparable) source).compareTo(target) == 0 ? matchedPlaceholder : missedPlaceholder;
}
if (targetClass == String.class) {
return ((String) source).trim().equals(((String) target).trim()) ? matchedPlaceholder : missedPlaceholder;
}
throw new IllegalArgumentException("不支持的数据类型:" + targetClass);
}
/**
* 匹配替换函数,处理target为List的情况
*
* @param source 源数据
* @param targets 待匹配的目标数据数组
* @param matchedPlaceholder 匹配成功后返回的占位符
* @param missedPlaceholder 匹配失败后返回的占位符
* @return
*/
public static <T> String matchReplaceBy(T source, List<T> targets, String matchedPlaceholder, String missedPlaceholder) {
if (source == null || targets == null) {
return missedPlaceholder;
}
for (T target : targets) {
if (target.getClass().isPrimitive() && source == target) { // 基本数据类型
return matchedPlaceholder;
}
if (Comparable.class.isAssignableFrom(target.getClass()) && ((Comparable) source).compareTo(target) == 0) { // 基本数据的包装类型,或者实现了 Comparable 的类型
return matchedPlaceholder;
}
if (target.getClass() == String.class && ((String) source).trim().equals(((String) target).trim())) {
return matchedPlaceholder;
}
}
return missedPlaceholder;
}
/**
* 匹配替换函数
*
* @param source 源数据
* @param targetArray 待匹配的目标数据数组
* @param matchedPlaceholder 匹配成功后返回的占位符
* @param missedPlaceholder 匹配失败后返回的占位符
* @return
*/
public static String matchArrReplaceBy(String source, String[] targetArray, String matchedPlaceholder, String missedPlaceholder) {
if (source == null || targetArray == null) {
return missedPlaceholder;
}
for (String target : targetArray) {
if (source.trim().equals(target.trim())) {
return matchedPlaceholder;
}
}
return missedPlaceholder;
}
/**
* 匹配替换函数
*
* @param source 源数据
* @param targetArr 待匹配的目标数据
* @param matchedPlaceholder 匹配成功后返回的占位符
* @param missedPlaceholder 匹配失败后返回的占位符
* @return
*/
public static String matchArrReplaceBy(int source, int[] targetArr, String matchedPlaceholder, String missedPlaceholder) {
for (int target : targetArr) {
if (source == target) {
return matchedPlaceholder;
}
}
return missedPlaceholder;
}
}