69 lines
1.7 KiB
Java
69 lines
1.7 KiB
Java
package androidx.lifecycle;
|
|
|
|
import androidx.annotation.MainThread;
|
|
import androidx.annotation.Nullable;
|
|
import java.io.Closeable;
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
public abstract class ViewModel {
|
|
@Nullable
|
|
private final Map<String, Object> mBagOfTags = new HashMap();
|
|
private volatile boolean mCleared = false;
|
|
|
|
private static void closeWithRuntimeException(Object obj) {
|
|
if (obj instanceof Closeable) {
|
|
try {
|
|
((Closeable) obj).close();
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
@MainThread
|
|
public final void clear() {
|
|
this.mCleared = true;
|
|
Map<String, Object> map = this.mBagOfTags;
|
|
if (map != null) {
|
|
synchronized (map) {
|
|
for (Object obj : this.mBagOfTags.values()) {
|
|
closeWithRuntimeException(obj);
|
|
}
|
|
}
|
|
}
|
|
onCleared();
|
|
}
|
|
|
|
public <T> T getTag(String str) {
|
|
T t;
|
|
Map<String, Object> map = this.mBagOfTags;
|
|
if (map == null) {
|
|
return null;
|
|
}
|
|
synchronized (map) {
|
|
t = (T) this.mBagOfTags.get(str);
|
|
}
|
|
return t;
|
|
}
|
|
|
|
public void onCleared() {
|
|
}
|
|
|
|
public <T> T setTagIfAbsent(String str, T t) {
|
|
Object obj;
|
|
synchronized (this.mBagOfTags) {
|
|
obj = this.mBagOfTags.get(str);
|
|
if (obj == null) {
|
|
this.mBagOfTags.put(str, t);
|
|
}
|
|
}
|
|
if (obj != null) {
|
|
t = obj;
|
|
}
|
|
if (this.mCleared) {
|
|
closeWithRuntimeException(t);
|
|
}
|
|
return t;
|
|
}
|
|
}
|