Data

class Data
kotlin.Any
   ↳ 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

A builder for Data objects.

Constants

static Int

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

Public constructors

<init>(@NonNull other: Data)

Public methods

Boolean
equals(other: Any?)

static Data
fromByteArray(@NonNull bytes: ByteArray)

Converts a byte array to Data.

Boolean
getBoolean(@NonNull key: String, defaultValue: Boolean)

Gets the boolean value for the given key.

BooleanArray?
getBooleanArray(@NonNull key: String)

Gets the boolean array value for the given key.

Byte
getByte(@NonNull key: String, defaultValue: Byte)

Gets the byte value for the given key.

ByteArray?
getByteArray(@NonNull key: String)

Gets the byte array value for the given key.

Double
getDouble(@NonNull key: String, defaultValue: Double)

Gets the double value for the given key.

DoubleArray?
getDoubleArray(@NonNull key: String)

Gets the double array value for the given key.

Float
getFloat(@NonNull key: String, defaultValue: Float)

Gets the float value for the given key.

FloatArray?
getFloatArray(@NonNull key: String)

Gets the float array value for the given key.

Int
getInt(@NonNull key: String, defaultValue: Int)

Gets the integer value for the given key.

IntArray?
getIntArray(@NonNull key: String)

Gets the integer array value for the given key.

MutableMap<String!, Any!>

Gets all the values in this Data object.

Long
getLong(@NonNull key: String, defaultValue: Long)

Gets the long value for the given key.

LongArray?
getLongArray(@NonNull key: String)

Gets the long array value for the given key.

String?
getString(@NonNull key: String)

Gets the String value for the given key.

Array<String!>?
getStringArray(@NonNull key: String)

Gets the String array value for the given key.

Boolean
hasKeyWithValueOfType(@NonNull key: String, @NonNull klass: Class<T>)

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

ByteArray

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

String

Extension functions

From androidx.work
Boolean

Returns true if the instance of Data has a value corresponding to the given key with an expected type T.

Properties

static Data!

An empty Data object with no elements.

Constants

MAX_DATA_BYTES

static val MAX_DATA_BYTES: Int

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.

Value: 10 * 1024

Public constructors

<init>

Data(@NonNull other: Data)

Public methods

equals

fun equals(other: Any?): Boolean

fromByteArray

@NonNull static fun fromByteArray(@NonNull bytes: ByteArray): Data

Converts a byte array to Data.

Parameters
bytes ByteArray: The byte array representation to convert
Return
Data: An Data object built from the input
Exceptions
IllegalStateException if bytes is bigger than MAX_DATA_BYTES

getBoolean

fun getBoolean(@NonNull key: String, defaultValue: Boolean): Boolean

Gets the boolean value for the given key.

Parameters
key String: The key for the argument
defaultValue String: The default value to return if the key is not found
Return
Boolean: The value specified by the key if it exists; the default value otherwise

getBooleanArray

@Nullable fun getBooleanArray(@NonNull key: String): BooleanArray?

Gets the boolean array value for the given key.

Parameters
key String: The key for the argument
Return
BooleanArray?: The value specified by the key if it exists; null otherwise

getByte

fun getByte(@NonNull key: String, defaultValue: Byte): Byte

Gets the byte value for the given key.

Parameters
key String: The key for the argument
defaultValue String: The default value to return if the key is not found
Return
Byte: The value specified by the key if it exists; the default value otherwise

getByteArray

@Nullable fun getByteArray(@NonNull key: String): ByteArray?

Gets the byte array value for the given key.

Parameters
key String: The key for the argument
Return
ByteArray?: The value specified by the key if it exists; null otherwise

getDouble

fun getDouble(@NonNull key: String, defaultValue: Double): Double

Gets the double value for the given key.

Parameters
key String: The key for the argument
defaultValue String: The default value to return if the key is not found
Return
Double: The value specified by the key if it exists; the default value otherwise

getDoubleArray

@Nullable fun getDoubleArray(@NonNull key: String): DoubleArray?

Gets the double array value for the given key.

Parameters
key String: The key for the argument
Return
DoubleArray?: The value specified by the key if it exists; null otherwise

getFloat

fun getFloat(@NonNull key: String, defaultValue: Float): Float

Gets the float value for the given key.

Parameters
key String: The key for the argument
defaultValue String: The default value to return if the key is not found
Return
Float: The value specified by the key if it exists; the default value otherwise

getFloatArray

@Nullable fun getFloatArray(@NonNull key: String): FloatArray?

Gets the float array value for the given key.

Parameters
key String: The key for the argument
Return
FloatArray?: The value specified by the key if it exists; null otherwise

getInt

fun getInt(@NonNull key: String, defaultValue: Int): Int

Gets the integer value for the given key.

Parameters
key String: The key for the argument
defaultValue String: The default value to return if the key is not found
Return
Int: The value specified by the key if it exists; the default value otherwise

getIntArray

@Nullable fun getIntArray(@NonNull key: String): IntArray?

Gets the integer array value for the given key.

Parameters
key String: The key for the argument
Return
IntArray?: The value specified by the key if it exists; null otherwise

getKeyValueMap

@NonNull fun getKeyValueMap(): MutableMap<String!, Any!>

Gets all the values in this Data object.

Return
MutableMap<String!, Any!>: A Map of key-value pairs for this object; this Map is unmodifiable and should be used for reads only.

getLong

fun getLong(@NonNull key: String, defaultValue: Long): Long

Gets the long value for the given key.

Parameters
key String: The key for the argument
defaultValue String: The default value to return if the key is not found
Return
Long: The value specified by the key if it exists; the default value otherwise

getLongArray

@Nullable fun getLongArray(@NonNull key: String): LongArray?

Gets the long array value for the given key.

Parameters
key String: The key for the argument
Return
LongArray?: The value specified by the key if it exists; null otherwise

getString

@Nullable fun getString(@NonNull key: String): String?

Gets the String value for the given key.

Parameters
key String: The key for the argument
Return
String?: The value specified by the key if it exists; the default value otherwise

getStringArray

@Nullable fun getStringArray(@NonNull key: String): Array<String!>?

Gets the String array value for the given key.

Parameters
key String: The key for the argument
Return
Array<String!>?: The value specified by the key if it exists; null otherwise

hasKeyWithValueOfType

fun <T : Any!> hasKeyWithValueOfType(@NonNull key: String, @NonNull klass: Class<T>): Boolean

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 String: The Class container for the expected type
<T> String: The expected type
Return
Boolean: true If the instance of Data has a value for the given String key with the expected type.

hashCode

fun hashCode(): Int

toByteArray

@NonNull fun toByteArray(): ByteArray

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.

Return
ByteArray: The byte array representation of the input
Exceptions
IllegalStateException if the serialized payload is bigger than MAX_DATA_BYTES

toString

@NonNull fun toString(): String

Properties

EMPTY

static val EMPTY: Data!

An empty Data object with no elements.