This commit is contained in:
hyb1996
2017-12-30 17:29:57 +08:00
parent fba9f0b4e9
commit 05877c57b7

View File

@@ -6,6 +6,7 @@ import android.util.SparseArray;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Created by Stardust on 2017/7/11. * Created by Stardust on 2017/7/11.
@@ -13,7 +14,7 @@ import java.util.Map;
public class IntentExtras implements Serializable { public class IntentExtras implements Serializable {
private static int mMaxId = -1; private static AtomicInteger mMaxId = new AtomicInteger(-1);
private static final String EXTRA_ID = "com.stardust.util.IntentExtras.id"; private static final String EXTRA_ID = "com.stardust.util.IntentExtras.id";
private static SparseArray<Map<String, Object>> extraStore = new SparseArray<>(); private static SparseArray<Map<String, Object>> extraStore = new SparseArray<>();
@@ -25,26 +26,26 @@ public class IntentExtras implements Serializable {
public static IntentExtras fromIntent(Intent intent) { public static IntentExtras fromIntent(Intent intent) {
int id = intent.getIntExtra(EXTRA_ID, -1); int id = intent.getIntExtra(EXTRA_ID, -1);
if (id < 0) { if (id < 0) {
throw new IllegalArgumentException(""); throw new IllegalArgumentException();
} }
return new IntentExtras(id); return new IntentExtras(id);
} }
private Map<String, Object> mMap; private Map<String, Object> mMap;
private int mId;
private IntentExtras() { private IntentExtras() {
mMap = new HashMap<>(); mMap = new HashMap<>();
mMaxId++; int id = mMaxId.incrementAndGet();
mId = mMaxId; extraStore.put(id, mMap);
extraStore.put(mId, mMap);
} }
private IntentExtras(int id) { private IntentExtras(int id) {
mMap = extraStore.get(id); mMap = extraStore.get(id);
if (mMap == null) {
mMap = new HashMap<>();
}
extraStore.remove(id); extraStore.remove(id);
mMaxId = id;
} }
@@ -53,13 +54,6 @@ public class IntentExtras implements Serializable {
return (T) mMap.get(key); return (T) mMap.get(key);
} }
@SuppressWarnings("unchecked")
public <T> T getAndClear(String key) {
T value = (T) mMap.get(key);
recycle();
return value;
}
public IntentExtras put(String key, Object value) { public IntentExtras put(String key, Object value) {
mMap.put(key, value); mMap.put(key, value);
return this; return this;
@@ -69,9 +63,4 @@ public class IntentExtras implements Serializable {
intent.putExtra(EXTRA_ID, mMaxId); intent.putExtra(EXTRA_ID, mMaxId);
return intent; return intent;
} }
public void recycle() {
extraStore.remove(mId);
mMap = null;
}
} }