Data
public final class Data
A persistable set of key/value pairs which are used as inputs and outputs for ListenableWorkers. Keys are Strings, and values can be Strings, primitive types, or their array variants.
This is a lightweight container, and should not be considered your data store. As such, there is an enforced .MAX_DATA_BYTES limit on the serialized (byte array) size of the payloads. This class will throw IllegalStateExceptions if you try to serialize or deserialize past this limit.
Summary
Nested types |
|---|
public final class Data.BuilderA builder for |
Constants |
|
|---|---|
static final int |
The maximum number of bytes for Data when it is serialized (converted to a byte array). |
Public methods |
|
|---|---|
boolean |
|
static final @NonNull Data |
@TypeConverterConverts a byte array to |
final boolean |
getBoolean(@NonNull String key, boolean defaultValue)Gets the boolean value for the given key. |
final boolean[] |
getBooleanArray(@NonNull String key)Gets the boolean array value for the given key. |
final byte |
Gets the byte value for the given key. |
final byte[] |
getByteArray(@NonNull String key)Gets the byte array value for the given key. |
final double |
Gets the double value for the given key. |
final double[] |
getDoubleArray(@NonNull String key)Gets the double array value for the given key. |
final float |
Gets the float value for the given key. |
final float[] |
getFloatArray(@NonNull String key)Gets the float array value for the given key. |
final int |
Gets the integer value for the given key. |
final int[] |
getIntArray(@NonNull String key)Gets the integer array value for the given key. |
final @NonNull Map<@NonNull String, Object> |
Gets all the values in this Data object. |
final long |
Gets the long value for the given key. |
final long[] |
getLongArray(@NonNull String key)Gets the long array value for the given key. |
final String |
Gets the String value for the given key. |
final String[] |
getStringArray(@NonNull String key)Gets the String array value for the given key. |
final boolean |
Returns |
int |
hashCode() |
final @NonNull byte[] |
Converts this Data to a byte array suitable for sending to other processes in your application. |
@NonNull String |
toString() |
Extension functions |
|
|---|---|
final boolean |
Returns true if the instance of |
Constants
MAX_DATA_BYTES
public static final int MAX_DATA_BYTES
The maximum number of bytes for Data when it is serialized (converted to a byte array). Please see the class-level Javadoc for more information.
Public methods
fromByteArray
@TypeConverter
public static final @NonNull Data fromByteArray(@NonNull byte[] bytes)
Converts a byte array to Data.
| Parameters | |
|---|---|
@NonNull byte[] bytes |
The byte array representation to convert |
| Throws | |
|---|---|
kotlin.IllegalStateException |
if bytes is bigger than .MAX_DATA_BYTES |
getBoolean
public final boolean getBoolean(@NonNull String key, boolean defaultValue)
Gets the boolean value for the given key.
| Parameters | |
|---|---|
@NonNull String key |
The key for the argument |
boolean defaultValue |
The default value to return if the key is not found |
| Returns | |
|---|---|
boolean |
The value specified by the key if it exists; the default value otherwise |
getBooleanArray
public final boolean[] getBooleanArray(@NonNull String key)
Gets the boolean array value for the given key.
| Returns | |
|---|---|
boolean[] |
The value specified by the key if it exists; |
getByte
public final byte getByte(@NonNull String key, byte defaultValue)
Gets the byte value for the given key.
| Parameters | |
|---|---|
@NonNull String key |
The key for the argument |
byte defaultValue |
The default value to return if the key is not found |
| Returns | |
|---|---|
byte |
The value specified by the key if it exists; the default value otherwise |
getByteArray
public final byte[] getByteArray(@NonNull String key)
Gets the byte array value for the given key.
| Returns | |
|---|---|
byte[] |
The value specified by the key if it exists; |
getDouble
public final double getDouble(@NonNull String key, double defaultValue)
Gets the double value for the given key.
| Parameters | |
|---|---|
@NonNull String key |
The key for the argument |
double defaultValue |
The default value to return if the key is not found |
| Returns | |
|---|---|
double |
The value specified by the key if it exists; the default value otherwise |
getDoubleArray
public final double[] getDoubleArray(@NonNull String key)
Gets the double array value for the given key.
| Returns | |
|---|---|
double[] |
The value specified by the key if it exists; |
getFloat
public final float getFloat(@NonNull String key, float defaultValue)
Gets the float value for the given key.
| Parameters | |
|---|---|
@NonNull String key |
The key for the argument |
float defaultValue |
The default value to return if the key is not found |
| Returns | |
|---|---|
float |
The value specified by the key if it exists; the default value otherwise |
getFloatArray
public final float[] getFloatArray(@NonNull String key)
Gets the float array value for the given key.
| Returns | |
|---|---|
float[] |
The value specified by the key if it exists; |
getInt
public final int getInt(@NonNull String key, int defaultValue)
Gets the integer value for the given key.
| Parameters | |
|---|---|
@NonNull String key |
The key for the argument |
int defaultValue |
The default value to return if the key is not found |
| Returns | |
|---|---|
int |
The value specified by the key if it exists; the default value otherwise |
getIntArray
public final int[] getIntArray(@NonNull String key)
Gets the integer array value for the given key.
| Returns | |
|---|---|
int[] |
The value specified by the key if it exists; |
getKeyValueMap
public final @NonNull Map<@NonNull String, Object> getKeyValueMap()
Gets all the values in this Data object.
getLong
public final long getLong(@NonNull String key, long defaultValue)
Gets the long value for the given key.
| Parameters | |
|---|---|
@NonNull String key |
The key for the argument |
long defaultValue |
The default value to return if the key is not found |
| Returns | |
|---|---|
long |
The value specified by the key if it exists; the default value otherwise |
getLongArray
public final long[] getLongArray(@NonNull String key)
Gets the long array value for the given key.
| Returns | |
|---|---|
long[] |
The value specified by the key if it exists; |
getString
public final String getString(@NonNull String key)
Gets the String value for the given key.
| Returns | |
|---|---|
String |
The value specified by the key if it exists; |
getStringArray
public final String[] getStringArray(@NonNull String key)
Gets the String array value for the given key.
| Returns | |
|---|---|
String[] |
The value specified by the key if it exists; |
hasKeyWithValueOfType
public final boolean <T extends Object> hasKeyWithValueOfType(
@NonNull String key,
@NonNull Class<@NonNull T> klass
)
Returns true if the instance of Data has a non-null value corresponding to the given String key with the expected type of T.
toByteArray
public final @NonNull byte[] toByteArray()
Converts this Data to a byte array suitable for sending to other processes in your application. There are no versioning guarantees with this byte array, so you should not use this for IPCs between applications or persistence.
| Returns | |
|---|---|
@NonNull byte[] |
The byte array representation of the input |
| Throws | |
|---|---|
kotlin.IllegalStateException |
if the serialized payload is bigger than .MAX_DATA_BYTES |