-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathDevIntent.java
334 lines (298 loc) · 8.05 KB
/
DevIntent.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package dev.base;
import android.content.Intent;
import android.os.Bundle;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import dev.utils.common.StringUtils;
/**
* detail: Intent 传参读写辅助类
* @author Ttt
* <pre>
* 统一存储为 String 需要转换其他类型则可通过
* {@link dev.utils.common.ConvertUtils} 进行转换
* 或者自行通过 JSON 映射实体类等
* <p></p>
* 可存储 key、value 为 null 数据 ( 提供方法清除 null 数据 )
* <p></p>
* 自动读取 Intent、Bundle 数据进行填充
* 仅支持 String、Integer、Long、Double、Float、Boolean 类型
* 并自动存储为 String
* <p></p>
* 通过 {@link #insert()} 可将 Map 数据插入到 Intent、Bundle 中
* </pre>
*/
public class DevIntent {
// 存储数据 Map
private final LinkedHashMap<String, String> mDataMaps = new LinkedHashMap<>();
private DevIntent() {
}
// ==========
// = 静态方法 =
// ==========
/**
* 创建 DevIntent
* @return {@link DevIntent}
*/
public static DevIntent with() {
return new DevIntent();
}
/**
* 创建 DevIntent
* @param intent {@link Intent}
* @return {@link DevIntent}
*/
public static DevIntent with(final Intent intent) {
return new DevIntent().reader(intent);
}
/**
* 创建 DevIntent
* @param bundle {@link Bundle}
* @return {@link DevIntent}
*/
public static DevIntent with(final Bundle bundle) {
return new DevIntent().reader(bundle);
}
// =============
// = 对外公开方法 =
// =============
/**
* 插入数据
* @param intent {@link Intent}
* @return {@link Intent}
*/
public Intent insert(final Intent intent) {
if (intent != null) {
for (Map.Entry<String, String> entry : mDataMaps.entrySet()) {
intent.putExtra(entry.getKey(), entry.getValue());
}
}
return intent;
}
/**
* 插入数据
* @return {@link Bundle}
*/
public Bundle insert() {
return insert(new Bundle());
}
/**
* 插入数据
* @param bundle {@link Bundle}
* @return {@link Bundle}
*/
public Bundle insert(final Bundle bundle) {
if (bundle != null) {
for (Map.Entry<String, String> entry : mDataMaps.entrySet()) {
bundle.putString(entry.getKey(), entry.getValue());
}
}
return bundle;
}
// =
/**
* 读取数据并存储
* @param intent {@link Intent}
* @return {@link DevIntent}
*/
public DevIntent reader(final Intent intent) {
if (intent != null) {
return reader(intent.getExtras());
}
return this;
}
/**
* 读取数据并存储
* @param bundle {@link Bundle}
* @return {@link DevIntent}
*/
public DevIntent reader(final Bundle bundle) {
if (bundle != null) {
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
if (value == null) {
continue;
}
if (value instanceof String) {
put(key, String.valueOf(value));
} else if (value instanceof Integer) {
put(key, String.valueOf(value));
} else if (value instanceof Long) {
put(key, String.valueOf(value));
} else if (value instanceof Double) {
put(key, String.valueOf(value));
} else if (value instanceof Float) {
put(key, String.valueOf(value));
} else if (value instanceof Boolean) {
put(key, String.valueOf(value));
}
}
}
return this;
}
// =======
// = 通用 =
// =======
/**
* 获取存储数据 Map
* @return 存储数据 Map
*/
public Map<String, String> getDataMaps() {
return mDataMaps;
}
/**
* 是否存在 Key
* @param key 保存的 key
* @return {@code true} yes, {@code false} no
*/
public boolean containsKey(final String key) {
return mDataMaps.containsKey(key);
}
/**
* 是否存在 Value
* @param value 保存的 value
* @return {@code true} yes, {@code false} no
*/
public boolean containsValue(final String value) {
return mDataMaps.containsValue(value);
}
/**
* 对应 Key 保存的 Value 是否为 null
* @param key 保存的 key
* @return {@code true} yes, {@code false} no
*/
public boolean isNullValue(final String key) {
return get(key) == null;
}
/**
* 保存数据
* @param key 保存的 key
* @param value 保存的 value
* @return {@link DevIntent}
*/
public DevIntent put(
final String key,
final String value
) {
mDataMaps.put(key, value);
return this;
}
/**
* 保存集合数据
* @param map {@link Map}
* @return {@link DevIntent}
*/
public DevIntent putAll(final Map<String, String> map) {
if (map != null) mDataMaps.putAll(map);
return this;
}
/**
* 移除数据
* @param key 保存的 key
* @return {@link DevIntent}
*/
public DevIntent remove(final String key) {
mDataMaps.remove(key);
return this;
}
/**
* 移除集合数据
* @param keys 保存的 key 集合
* @return {@link DevIntent}
*/
public DevIntent removeAll(final List<String> keys) {
if (keys != null) {
for (String key : keys) {
mDataMaps.remove(key);
}
}
return this;
}
/**
* 获取对应 Key 保存的 Value
* @param key 保存的 key
* @return 保存的 value
*/
public String get(final String key) {
return mDataMaps.get(key);
}
/**
* 清空数据
* @return {@link DevIntent}
*/
public DevIntent clear() {
mDataMaps.clear();
return this;
}
// =
/**
* 清除 null 数据
* <pre>
* key、value 只要其中一个为 null 就清除
* </pre>
* @return {@link DevIntent}
*/
public DevIntent clearNull() {
return clearNullKey().clearNullValue();
}
/**
* 清除 null Key 数据
* @return {@link DevIntent}
*/
public DevIntent clearNullKey() {
return remove(null);
}
/**
* 清除 null Value 数据
* <pre>
* value 只要为 null 就清除
* </pre>
* @return {@link DevIntent}
*/
public DevIntent clearNullValue() {
Iterator<Map.Entry<String, String>> iterator = mDataMaps.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
if (entry.getValue() == null) {
iterator.remove();
}
}
return this;
}
// =
/**
* 清除 empty 数据
* <pre>
* key、value 只要其中一个为 empty ( null、"" ) 就清除
* </pre>
* @return {@link DevIntent}
*/
public DevIntent clearEmpty() {
return clearEmptyKey().clearEmptyValue();
}
/**
* 清除 empty Key 数据
* @return {@link DevIntent}
*/
public DevIntent clearEmptyKey() {
return remove(null).remove("");
}
/**
* 清除 empty Value 数据
* <pre>
* value 只要为 empty ( null、"" ) 就清除
* </pre>
* @return {@link DevIntent}
*/
public DevIntent clearEmptyValue() {
Iterator<Map.Entry<String, String>> iterator = mDataMaps.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
if (StringUtils.isEmpty(entry.getValue())) {
iterator.remove();
}
}
return this;
}
}