2018-08-09 14:15:43 +00:00
|
|
|
package org.thoughtcrime.securesms.jobmanager;
|
|
|
|
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
|
|
|
|
import androidx.work.Data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A wrapper around {@link Data} that does its best to throw an exception whenever a key isn't
|
|
|
|
* present in the {@link Data} object.
|
|
|
|
*/
|
|
|
|
public class SafeData {
|
|
|
|
|
|
|
|
private final Data data;
|
|
|
|
|
|
|
|
public SafeData(@NonNull Data data) {
|
|
|
|
this.data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getInt(@NonNull String key) {
|
2018-10-02 19:31:12 +00:00
|
|
|
assertKeyPresence(key);
|
|
|
|
return data.getInt(key, -1);
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public long getLong(@NonNull String key) {
|
2018-10-02 19:31:12 +00:00
|
|
|
assertKeyPresence(key);
|
|
|
|
return data.getLong(key, -1);
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 19:31:12 +00:00
|
|
|
public String getString(@NonNull String key) {
|
|
|
|
assertKeyPresence(key);
|
2018-08-09 14:15:43 +00:00
|
|
|
return data.getString(key);
|
|
|
|
}
|
|
|
|
|
2018-10-02 19:31:12 +00:00
|
|
|
public String[] getStringArray(@NonNull String key) {
|
|
|
|
assertKeyPresence(key);
|
|
|
|
return data.getStringArray(key);
|
|
|
|
}
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2018-10-02 19:31:12 +00:00
|
|
|
public long[] getLongArray(@NonNull String key) {
|
|
|
|
assertKeyPresence(key);
|
|
|
|
return data.getLongArray(key);
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 19:31:12 +00:00
|
|
|
public boolean getBoolean(@NonNull String key) {
|
|
|
|
assertKeyPresence(key);
|
|
|
|
return data.getBoolean(key, false);
|
|
|
|
}
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2018-10-02 19:31:12 +00:00
|
|
|
private void assertKeyPresence(@NonNull String key) {
|
|
|
|
if (!data.getKeyValueMap().containsKey(key)) {
|
2018-08-09 14:15:43 +00:00
|
|
|
throw new IllegalStateException("Missing key: " + key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|