forked from wujun728/jun_java_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultMap.java
More file actions
260 lines (220 loc) · 4.82 KB
/
Copy pathResultMap.java
File metadata and controls
260 lines (220 loc) · 4.82 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package org.myframework.util;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 使用“装饰器”模式来使用 MAP
*/
public class ResultMap<K, V> implements Map<K, V> {
public static final int DEFAULT_NUMBER = 0;
public static final String EMPTY_STRING = "";
private static final Log log = LogFactory.getLog(ResultMap.class);
private Map<K, V> map;
/**
* 默认 clone map
*
* @param map
*/
public ResultMap(Map<K, V> map) {
this.map = map;
this.initIdx();
}
/**
* 将数据转化为对应的索引数据类型
*/
private void initIdx() {
if (this.containsKey("latnId")) {
this.valueToNumber((K) "latnId");
}
if (this.containsKey("billingCycleId")) {
this.valueToNumber((K) "billingCycleId");
}
}
public ResultMap() {
super();
}
public void setMap(Map<K, V> map) {
this.map = map;
}
/**
* 删除Key
*
* @param keys
* @return
*/
public Object[] removes(Object[] keys) {
List<Object> removedKeys = new ArrayList<Object>();
for (int i = 0; i < keys.length; i++) {
Object key = keys[i];
if (this.map.containsKey(key)) {
removedKeys.add(this.map.remove(key));
}
}
return removedKeys.toArray();
}
/**
* 转换为HashMap
*
* @return
*/
public HashMap<K, V> toHashMap() {
return new HashMap<K, V>(this.map);
}
/**
* 获取字符串
*
* @param key
* @return
*/
public String getString(Object key) {
return hasKey(key) ? map.get(key).toString().trim() : EMPTY_STRING;
}
/**
* @param key
* @return
*/
public boolean hasKey(Object key) {
return map.containsKey(key);
}
/**
* 转化STRING 为 NUMBER(ORACLE索引优化)
*
* @param key
* @return
*/
public void valueToNumber(K key) {
this.put(key, (V) Long.valueOf(this.getLong(key)));
}
/**
* 转化为STRING (ORACLE索引优化)
*
* @param key
* @return
*/
public void valueToString(K key) {
this.put(key, (V) this.getString(key));
}
/**
* 获取整型数
*
* @param key
* @return
*/
public int getInt(K key) {
Object value = get(key);
if (value instanceof String) {
String stringValue = getString(key);
return Integer.parseInt(stringValue);
} else if (value instanceof Integer) {
return ((Integer) value).intValue();
} else {
return DEFAULT_NUMBER;
}
}
public long getLong(K key) {
Object value = get(key);
if (value instanceof String) {
String stringValue = getString(key);
return Long.parseLong(stringValue);
} else if (value instanceof Long) {
return ((Long) value).longValue();
} else if (value instanceof BigDecimal) {
return ((BigDecimal) value).longValue();
} else {
return DEFAULT_NUMBER;
}
}
public double getDouble(K key) {
Object value = get(key);
if (value instanceof String) {
String stringValue = getString(key);
return Double.parseDouble(stringValue);
} else if (value instanceof Double) {
return ((Double) value).doubleValue();
} else {
return DEFAULT_NUMBER;
}
}
public BigDecimal getBigDecimal(K key) {
Object value = get(key);
if (value instanceof String) {
String stringValue = getString(key);
return new BigDecimal(stringValue);
} else if (value instanceof BigDecimal) {
return (BigDecimal) value;
} else {
return null;
}
}
public boolean getBoolean(K key) {
return getString(key).equals(Boolean.TRUE.toString());
}
/**
* 获取日期字符串 以pattern格式化
*
* @param key
* @return
*/
public String getDateString(K key, String pattern) {
DateFormat dateFormat = new SimpleDateFormat(pattern);
Object value = this.map.get(key);
return dateFormat.format((Date) value);
}
public void clear() {
this.map.clear();
}
public boolean containsKey(Object key) {
return this.map.containsKey(key);
}
public boolean containsValue(Object value) {
return this.map.containsKey(value);
}
public Set<Map.Entry<K, V>> entrySet() {
return this.map.entrySet();
}
@Override
public boolean equals(Object obj) {
return this.map.equals(obj);
}
public V get(Object key) {
return this.map.get(key);
}
@Override
public int hashCode() {
return this.map.hashCode();
}
public boolean isEmpty() {
return this.map.isEmpty();
}
public Set<K> keySet() {
return this.map.keySet();
}
public V put(K key, V value) {
return this.map.put(key, value);
}
public void putAll(Map<? extends K, ? extends V> t) {
this.map.putAll(t);
}
public V remove(Object key) {
return this.map.remove(key);
}
public int size() {
return this.map.size();
}
@Override
public String toString() {
return this.map.toString();
}
public Collection<V> values() {
return this.map.values();
}
}