Skip to content

Commit 558d7ba

Browse files
author
吕发强
committed
添加数字工具类,调整 SpUtil 相关方法。
1 parent c049234 commit 558d7ba

File tree

2 files changed

+70
-25
lines changed

2 files changed

+70
-25
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.lvfq.library.utils;
2+
3+
import java.text.DecimalFormat;
4+
5+
/**
6+
* LvNumberUtil
7+
*
8+
* @author lvfq
9+
* @date 2017/6/29 下午4:27
10+
* @mainFunction : 数字工具类
11+
*/
12+
13+
public class LvNumberUtil {
14+
15+
/**
16+
* 最多保留小数点后两位
17+
*
18+
* @param number
19+
* @return
20+
*/
21+
public static String maxKeepTwoDecimalPlaces(double number) {
22+
DecimalFormat df = new DecimalFormat("##.##");
23+
return df.format(number);
24+
}
25+
26+
/**
27+
* 始终保留小数点后两位
28+
*
29+
* @param number
30+
* @return
31+
*/
32+
public static String keepTwoDecimalPlaces(double number) {
33+
DecimalFormat df = new DecimalFormat("#.00");
34+
return df.format(number);
35+
}
36+
37+
}

library/src/main/java/com/lvfq/library/utils/LvSpUtil.java

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import android.util.Base64;
88

99
import com.google.gson.Gson;
10+
import com.google.gson.JsonArray;
11+
import com.google.gson.JsonElement;
12+
import com.google.gson.JsonParser;
1013
import com.google.gson.reflect.TypeToken;
1114

1215
import java.io.ByteArrayInputStream;
@@ -15,7 +18,6 @@
1518
import java.io.ObjectInputStream;
1619
import java.io.ObjectOutputStream;
1720
import java.io.StreamCorruptedException;
18-
import java.lang.reflect.Type;
1921
import java.util.ArrayList;
2022
import java.util.List;
2123

@@ -360,64 +362,70 @@ public static void clear() {
360362
editor.commit();
361363
}
362364

365+
363366
/**
364-
* 保存对象(任意对象类型)可传 List, Map等。
367+
* 保存对象(所有类型)
365368
*
366369
* @param key
367370
* @param t
368371
* @param <T>
369372
*/
370373
public static <T> void setT(String key, T t) {
371-
Gson go = new Gson();
372-
String str = go.toJson(t);
373-
setString(key, str);
374+
setString(key, new Gson().toJson(t));
374375
}
375376

376-
public static Object getObject(String key, Class clazz) {
377-
Object o = null;
377+
378+
/**
379+
* 获取保存的对象
380+
*
381+
* @param key
382+
* @param tClass
383+
* @param <T>
384+
* @return
385+
*/
386+
public static <T> T getT(String key, Class<T> tClass) {
387+
T t = null;
378388
String str = getString(key, "");
379389
if (!TextUtils.isEmpty(str)) {
380390
Gson go = new Gson();
381-
o = go.fromJson(str, clazz);
391+
t = go.fromJson(str, tClass);
382392
}
383-
return o;
393+
return t;
384394
}
385395

386396
/**
387-
* recommend to use getT
397+
* 获取保存的对象,支持所有类型对象。
398+
* (主要获取集合的时候使用)
388399
*
389400
* @param key
390401
* @param type
391402
* @return
392403
*/
393-
@Deprecated
394-
public static Object getObject(String key, Type type) {
395-
Object o = null;
404+
public static <T> T getT(String key, TypeToken<T> type) {
405+
T o = null;
396406
String str = getString(key, "");
397407
if (!TextUtils.isEmpty(str)) {
398408
Gson gson = new Gson();
399-
o = gson.fromJson(str, type);
409+
o = gson.fromJson(str, type.getType());
400410
}
401411
return o;
402412
}
403413

404414
/**
405-
* 获取保存的对象
415+
* 获取保存的对象(支持非集合类Class)
406416
*
407-
* @param key key
408-
* @param t 对象类型
417+
* @param key k
409418
* @param <T>
410419
* @return t
411420
*/
412-
public static <T> T getT(String key, T t) {
413-
421+
public static <T> List<T> getList(String key, Class<T> cls) {
422+
List<T> list = new ArrayList<>();
414423
String str = getString(key, "");
415-
if (!TextUtils.isEmpty(str)) {
416-
Gson gson = new Gson();
417-
t = gson.fromJson(str, new TypeToken<T>() {
418-
}.getType());
424+
JsonArray array = new JsonParser().parse(str).getAsJsonArray();
425+
for (final JsonElement elem : array) {
426+
list.add(new Gson().fromJson(elem, cls));
419427
}
420-
// return o;
421-
return t;
428+
return list;
422429
}
430+
423431
}

0 commit comments

Comments
 (0)