Skip to content

Commit

Permalink
fix:默认值方法调用禁止返回null问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ronghao committed May 23, 2019
1 parent 9344a94 commit 0d53866
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void onClick(View v) {
//key20未存储的数据,返回默认值
Test key21Value = CacheUtil.get(CacheUtil.translateKey("null1"), Test.class, true);
//key21未存储的数据,且无默认构造方法,返回null
Integer key22Value = CacheUtil.get("key22", Integer.class, 100,true);
int key22Value = CacheUtil.get("key22", int.class, 100,true);
//key21已存储的数据,返回默认值

String value = "测试:\n"
Expand Down Expand Up @@ -225,7 +225,7 @@ public void onClick(View v) {
+ (key21Value != null ? key21Value.toString() : "null")
+ "\n"
+ "保存实体对象[Test类](有默认返回对象):"
+ (key22Value != null ? key22Value.toString() : "null")
+ (String.valueOf(key22Value))
+ "\n";
mTextView.setText(value);
}
Expand Down
8 changes: 3 additions & 5 deletions library/src/main/java/com/haohaohu/cachemanage/CacheUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ public static <T> T get(String key, Class<T> classOfT, boolean isEncrypt) {
* @param t 错误情况下返回数据
* @return 实体对象
*/
@Nullable
public static <T> T get(String key, Class<T> classOfT, T t) {
return get(key, classOfT, t, getConfig().isEncrypt());
}
Expand All @@ -379,10 +378,9 @@ public static <T> T get(String key, Class<T> classOfT, T t) {
* @param isEncrypt 是否加密
* @return 实体对象
*/
@Nullable
public static <T> T get(String key, Class<T> classOfT, T t, boolean isEncrypt) {
public static <T> T get(String key, Class<T> classOfT, @NonNull T t, boolean isEncrypt) {
if (TextUtils.isEmpty(key) || classOfT == null) {
return null;
return t;
}
try {
LockUtil.getInstance().readLock().lock();
Expand Down Expand Up @@ -413,7 +411,7 @@ public static <T> T get(String key, Class<T> classOfT, T t, boolean isEncrypt) {
}
return t;
} catch (Exception e) {
return null;
return t;
} finally {
LockUtil.getInstance().readLock().unlock();
}
Expand Down

0 comments on commit 0d53866

Please sign in to comment.