Improve key presence checking in SafeData.

This isn't a perfect check either, but it should be safer and more
consistent than using static "invalid values".
pull/1/head
Greyson Parrelli 7 years ago
parent d25ebdc818
commit 444e01deae

@ -11,9 +11,6 @@ import androidx.work.Data;
*/ */
public class SafeData { public class SafeData {
private final static int INVALID_INT = Integer.MIN_VALUE;
private final static long INVALID_LONG = Long.MIN_VALUE;
private final Data data; private final Data data;
public SafeData(@NonNull Data data) { public SafeData(@NonNull Data data) {
@ -21,60 +18,38 @@ public class SafeData {
} }
public int getInt(@NonNull String key) { public int getInt(@NonNull String key) {
int value = data.getInt(key, INVALID_INT); assertKeyPresence(key);
return data.getInt(key, -1);
if (value == INVALID_INT) {
throw new IllegalStateException("Missing key: " + key);
}
return value;
} }
public long getLong(@NonNull String key) { public long getLong(@NonNull String key) {
long value = data.getLong(key, INVALID_LONG); assertKeyPresence(key);
return data.getLong(key, -1);
if (value == INVALID_LONG) {
throw new IllegalStateException("Missing key: " + key);
}
return value;
} }
public @NonNull String getString(@NonNull String key) { public String getString(@NonNull String key) {
String value = data.getString(key); assertKeyPresence(key);
if (value == null) {
throw new IllegalStateException("Missing key: " + key);
}
return value;
}
public @Nullable String getNullableString(@NonNull String key) {
return data.getString(key); return data.getString(key);
} }
public @NonNull String[] getStringArray(@NonNull String key) { public String[] getStringArray(@NonNull String key) {
String[] value = data.getStringArray(key); assertKeyPresence(key);
return data.getStringArray(key);
if (value == null) { }
throw new IllegalStateException("Missing key: " + key);
}
return value; public long[] getLongArray(@NonNull String key) {
assertKeyPresence(key);
return data.getLongArray(key);
} }
public @NonNull long[] getLongArray(@NonNull String key) { public boolean getBoolean(@NonNull String key) {
long[] value = data.getLongArray(key); assertKeyPresence(key);
return data.getBoolean(key, false);
}
if (value == null) { private void assertKeyPresence(@NonNull String key) {
if (!data.getKeyValueMap().containsKey(key)) {
throw new IllegalStateException("Missing key: " + key); throw new IllegalStateException("Missing key: " + key);
} }
return value;
}
public boolean getBoolean(@NonNull String key, boolean defaultValue) {
return data.getBoolean(key, defaultValue);
} }
} }

@ -78,7 +78,7 @@ public class AttachmentDownloadJob extends MasterSecretJob implements Injectable
messageId = data.getLong(KEY_MESSAGE_ID); messageId = data.getLong(KEY_MESSAGE_ID);
partRowId = data.getLong(KEY_PART_ROW_ID); partRowId = data.getLong(KEY_PART_ROW_ID);
partUniqueId = data.getLong(KEY_PAR_UNIQUE_ID); partUniqueId = data.getLong(KEY_PAR_UNIQUE_ID);
manual = data.getBoolean(KEY_MANUAL, false); manual = data.getBoolean(KEY_MANUAL);
} }
@Override @Override

@ -50,11 +50,11 @@ public class DirectoryRefreshJob extends ContextJob {
@Override @Override
protected void initialize(@NonNull SafeData data) { protected void initialize(@NonNull SafeData data) {
String serializedAddress = data.getNullableString(KEY_ADDRESS); String serializedAddress = data.getString(KEY_ADDRESS);
Address address = serializedAddress != null ? Address.fromSerialized(serializedAddress) : null; Address address = serializedAddress != null ? Address.fromSerialized(serializedAddress) : null;
recipient = address != null ? Recipient.from(context, address, true) : null; recipient = address != null ? Recipient.from(context, address, true) : null;
notifyOfNewUsers = data.getBoolean(KEY_NOTIFY_OF_NEW_USERS, false); notifyOfNewUsers = data.getBoolean(KEY_NOTIFY_OF_NEW_USERS);
} }
@Override @Override

@ -83,7 +83,7 @@ public class MmsDownloadJob extends MasterSecretJob {
protected void initialize(@NonNull SafeData data) { protected void initialize(@NonNull SafeData data) {
messageId = data.getLong(KEY_MESSAGE_ID); messageId = data.getLong(KEY_MESSAGE_ID);
threadId = data.getLong(KEY_THREAD_ID); threadId = data.getLong(KEY_THREAD_ID);
automatic = data.getBoolean(KEY_AUTOMATIC, false); automatic = data.getBoolean(KEY_AUTOMATIC);
} }
@Override @Override

@ -99,8 +99,8 @@ public class MultiDeviceContactUpdateJob extends MasterSecretJob implements Inje
@Override @Override
protected void initialize(@NonNull SafeData data) { protected void initialize(@NonNull SafeData data) {
address = data.getNullableString(KEY_ADDRESS); address = data.getString(KEY_ADDRESS);
forceSync = data.getBoolean(KEY_FORCE_SYNC, false); forceSync = data.getBoolean(KEY_FORCE_SYNC);
} }
@Override @Override

@ -48,7 +48,7 @@ public class MultiDeviceReadReceiptUpdateJob extends ContextJob implements Injec
@Override @Override
protected void initialize(@NonNull SafeData data) { protected void initialize(@NonNull SafeData data) {
enabled = data.getBoolean(KEY_ENABLED, false); enabled = data.getBoolean(KEY_ENABLED);
} }
@Override @Override

@ -40,7 +40,7 @@ public class PushContentReceiveJob extends PushReceivedJob {
@Override @Override
protected void initialize(@NonNull SafeData data) { protected void initialize(@NonNull SafeData data) {
this.data = data.getNullableString(KEY_DATA); this.data = data.getString(KEY_DATA);
} }
@Override @Override

@ -84,7 +84,7 @@ public class PushGroupSendJob extends PushSendJob implements InjectableType {
@Override @Override
protected void initialize(@NonNull SafeData data) { protected void initialize(@NonNull SafeData data) {
messageId = data.getLong(KEY_MESSAGE_ID); messageId = data.getLong(KEY_MESSAGE_ID);
filterAddress = data.getNullableString(KEY_FILTER_ADDRESS); filterAddress = data.getString(KEY_FILTER_ADDRESS);
} }
@Override @Override

@ -59,7 +59,7 @@ public class RetrieveProfileAvatarJob extends ContextJob implements InjectableTy
@Override @Override
protected void initialize(@NonNull SafeData data) { protected void initialize(@NonNull SafeData data) {
profileAvatar = data.getNullableString(KEY_PROFILE_AVATAR); profileAvatar = data.getString(KEY_PROFILE_AVATAR);
recipient = Recipient.from(context, Address.fromSerialized(data.getString(KEY_ADDRESS)), true); recipient = Recipient.from(context, Address.fromSerialized(data.getString(KEY_ADDRESS)), true);
} }

Loading…
Cancel
Save