Data
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 |
|---|
class Data.BuilderA builder for |
Constants |
|
|---|---|
const Int |
The maximum number of bytes for Data when it is serialized (converted to a byte array). |
Public companion functions |
|
|---|---|
Data |
@TypeConverterConverts a byte array to |
Public functions |
|
|---|---|
open operator Boolean |
|
Boolean |
getBoolean(key: String, defaultValue: Boolean)Gets the boolean value for the given key. |
BooleanArray? |
getBooleanArray(key: String)Gets the boolean array value for the given key. |
Byte |
Gets the byte value for the given key. |
ByteArray? |
getByteArray(key: String)Gets the byte array value for the given key. |
Double |
Gets the double value for the given key. |
DoubleArray? |
getDoubleArray(key: String)Gets the double array value for the given key. |
Float |
Gets the float value for the given key. |
FloatArray? |
getFloatArray(key: String)Gets the float array value for the given key. |
Int |
Gets the integer value for the given key. |
IntArray? |
getIntArray(key: String)Gets the integer array value for the given key. |
Long |
Gets the long value for the given key. |
LongArray? |
getLongArray(key: String)Gets the long array value for the given key. |
Array<String?>? |
getNullableStringArray(key: String)Gets the string array value for the given key. |
String? |
Gets the String value for the given key. |
Array<String>? |
This function is deprecated. Use getNullableStringArray(key) instead. |
Boolean |
<T : Any?> hasKeyWithValueOfType(key: String, klass: Class<T>)Returns |
open Int |
hashCode() |
ByteArray |
Converts this Data to a byte array suitable for sending to other processes in your application. |
open String |
toString() |
Extension functions |
|
|---|---|
inline Boolean |
<T : Any> Data.hasKeyWithValueOfType(key: String)Returns true if the instance of |
Constants
MAX_DATA_BYTES
const 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.
Public companion functions
fromByteArray
@TypeConverter
fun fromByteArray(bytes: ByteArray): Data
Converts a byte array to Data.
| Parameters | |
|---|---|
bytes: ByteArray |
The byte array representation to convert |
| Throws | |
|---|---|
IllegalStateException |
if bytes is bigger than .MAX_DATA_BYTES |
Public functions
getBoolean
fun getBoolean(key: String, defaultValue: Boolean): Boolean
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
fun getBooleanArray(key: String): BooleanArray?
Gets the boolean array value for the given key.
| Parameters | |
|---|---|
key: String |
The key for the argument |
| Returns | |
|---|---|
BooleanArray? |
The value specified by the key if it exists; |
getByte
fun getByte(key: String, defaultValue: Byte): Byte
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
fun getByteArray(key: String): ByteArray?
Gets the byte array value for the given key.
| Parameters | |
|---|---|
key: String |
The key for the argument |
| Returns | |
|---|---|
ByteArray? |
The value specified by the key if it exists; |
getDouble
fun getDouble(key: String, defaultValue: Double): Double
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
fun getDoubleArray(key: String): DoubleArray?
Gets the double array value for the given key.
| Parameters | |
|---|---|
key: String |
The key for the argument |
| Returns | |
|---|---|
DoubleArray? |
The value specified by the key if it exists; |
getFloat
fun getFloat(key: String, defaultValue: Float): Float
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
fun getFloatArray(key: String): FloatArray?
Gets the float array value for the given key.
| Parameters | |
|---|---|
key: String |
The key for the argument |
| Returns | |
|---|---|
FloatArray? |
The value specified by the key if it exists; |
getInt
fun getInt(key: String, defaultValue: Int): Int
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
fun getIntArray(key: String): IntArray?
Gets the integer array value for the given key.
| Parameters | |
|---|---|
key: String |
The key for the argument |
| Returns | |
|---|---|
IntArray? |
The value specified by the key if it exists; |
getLong
fun getLong(key: String, defaultValue: Long): Long
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
fun getLongArray(key: String): LongArray?
Gets the long array value for the given key.
| Parameters | |
|---|---|
key: String |
The key for the argument |
| Returns | |
|---|---|
LongArray? |
The value specified by the key if it exists; |
getNullableStringArray
fun getNullableStringArray(key: String): Array<String?>?
Gets the string array value for the given key. The array may contain null elements.
| Parameters | |
|---|---|
key: String |
The key for the argument |
getString
fun getString(key: String): String?
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; |
getStringArray
fungetStringArray(key: String): Array<String>?
Gets the String array value for the given key.
| Parameters | |
|---|---|
key: String |
The key for the argument |
hasKeyWithValueOfType
fun <T : Any?> hasKeyWithValueOfType(key: String, 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.
toByteArray
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.
| Returns | |
|---|---|
ByteArray |
The byte array representation of the input |
| Throws | |
|---|---|
IllegalStateException |
if the serialized payload is bigger than .MAX_DATA_BYTES |
Public properties
Extension functions
Data.hasKeyWithValueOfType
inline fun <T : Any> Data.hasKeyWithValueOfType(key: String): Boolean
Returns true if the instance of Data has a value corresponding to the given key with an expected type T.