Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

Data

public final class Data
extends Object

java.lang.Object
   ↳ androidx.work.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 classes

class Data.Builder

A builder for Data objects. 

Constants

int MAX_DATA_BYTES

The maximum number of bytes for Data when it is serialized (converted to a byte array).

Fields

public static final Data EMPTY

An empty Data object with no elements.

Public constructors

Data(Data other)

Public methods

boolean equals(Object o)
static Data fromByteArray(byte[] bytes)

Converts a byte array to Data.

boolean getBoolean(String key, boolean defaultValue)

Gets the boolean value for the given key.

boolean[] getBooleanArray(String key)

Gets the boolean array value for the given key.

byte getByte(String key, byte defaultValue)

Gets the byte value for the given key.

byte[] getByteArray(String key)

Gets the byte array value for the given key.

double getDouble(String key, double defaultValue)

Gets the double value for the given key.

double[] getDoubleArray(String key)

Gets the double array value for the given key.

float getFloat(String key, float defaultValue)

Gets the float value for the given key.

float[] getFloatArray(String key)

Gets the float array value for the given key.

int getInt(String key, int defaultValue)

Gets the integer value for the given key.

int[] getIntArray(String key)

Gets the integer array value for the given key.

Map<String, Object> getKeyValueMap()

Gets all the values in this Data object.

long getLong(String key, long defaultValue)

Gets the long value for the given key.

long[] getLongArray(String key)

Gets the long array value for the given key.

String getString(String key)

Gets the String value for the given key.

String[] getStringArray(String key)

Gets the String array value for the given key.

<T> boolean hasKeyWithValueOfType(String key, Class<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.

int hashCode()
byte[] toByteArray()

Converts this Data to a byte array suitable for sending to other processes in your application.

String toString()

Inherited methods

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.

Constant Value: 10240 (0x00002800)

Fields

EMPTY

public static final Data EMPTY

An empty Data object with no elements.

Public constructors

Data

public Data (Data other)

Parameters
other Data

Public methods

equals

public boolean equals (Object o)

Parameters
o Object

Returns
boolean

fromByteArray

public static Data fromByteArray (byte[] bytes)

Converts a byte array to Data.

Parameters
bytes byte: The byte array representation to convert

Returns
Data An Data object built from the input

Throws
IllegalStateException if bytes is bigger than MAX_DATA_BYTES

getBoolean

public boolean getBoolean (String key, 
                boolean defaultValue)

Gets the boolean value for the given key.

Parameters
key String: The key for the argument

defaultValue boolean: 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 boolean[] getBooleanArray (String key)

Gets the boolean array value for the given key.

Parameters
key String: The key for the argument

Returns
boolean[] The value specified by the key if it exists; null otherwise

getByte

public byte getByte (String key, 
                byte defaultValue)

Gets the byte value for the given key.

Parameters
key String: The key for the argument

defaultValue byte: 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 byte[] getByteArray (String key)

Gets the byte array value for the given key.

Parameters
key String: The key for the argument

Returns
byte[] The value specified by the key if it exists; null otherwise

getDouble

public double getDouble (String key, 
                double defaultValue)

Gets the double value for the given key.

Parameters
key String: The key for the argument

defaultValue double: 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 double[] getDoubleArray (String key)

Gets the double array value for the given key.

Parameters
key String: The key for the argument

Returns
double[] The value specified by the key if it exists; null otherwise

getFloat

public float getFloat (String key, 
                float defaultValue)

Gets the float value for the given key.

Parameters
key String: The key for the argument

defaultValue float: 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 float[] getFloatArray (String key)

Gets the float array value for the given key.

Parameters
key String: The key for the argument

Returns
float[] The value specified by the key if it exists; null otherwise

getInt

public int getInt (String key, 
                int defaultValue)

Gets the integer value for the given key.

Parameters
key String: The key for the argument

defaultValue int: 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 int[] getIntArray (String key)

Gets the integer array value for the given key.

Parameters
key String: The key for the argument

Returns
int[] The value specified by the key if it exists; null otherwise

getKeyValueMap

public Map<String, Object> getKeyValueMap ()

Gets all the values in this Data object.

Returns
Map<String, Object> A Map of key-value pairs for this object; this Map is unmodifiable and should be used for reads only.

getLong

public long getLong (String key, 
                long defaultValue)

Gets the long value for the given key.

Parameters
key String: The key for the argument

defaultValue long: 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 long[] getLongArray (String key)

Gets the long array value for the given key.

Parameters
key String: The key for the argument

Returns
long[] The value specified by the key if it exists; null otherwise

getString

public String getString (String key)

Gets the String value for the given key.

Parameters
key String: The key for the argument

Returns
String The value specified by the key if it exists; the default value otherwise

getStringArray

public String[] getStringArray (String key)

Gets the String array value for the given key.

Parameters
key String: The key for the argument

Returns
String[] The value specified by the key if it exists; null otherwise

hasKeyWithValueOfType

public boolean hasKeyWithValueOfType (String key, 
                Class<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.

Parameters
key String: The String key

klass Class: The Class container for the expected type

Returns
boolean true If the instance of Data has a value for the given String key with the expected type.

hashCode

public int hashCode ()

Returns
int

toByteArray

public 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
byte[] The byte array representation of the input

Throws
IllegalStateException if the serialized payload is bigger than MAX_DATA_BYTES

toString

public String toString ()

Returns
String