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.Builder

A builder for Data objects.

Constants

static final int

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

Public fields

static final @NonNull Data

An empty Data object with no elements.

Public constructors

Data(@NonNull Data other)

Copy constructor

Public methods

boolean
equals(Object other)
static final @NonNull Data

Converts a byte array to Data.

final boolean
getBoolean(@NonNull String key, boolean defaultValue)

Gets the boolean value for the given key.

final boolean[]

Gets the boolean array value for the given key.

final byte
getByte(@NonNull String key, byte defaultValue)

Gets the byte value for the given key.

final byte[]

Gets the byte array value for the given key.

final double
getDouble(@NonNull String key, double defaultValue)

Gets the double value for the given key.

final double[]

Gets the double array value for the given key.

final float
getFloat(@NonNull String key, float defaultValue)

Gets the float value for the given key.

final float[]

Gets the float array value for the given key.

final int
getInt(@NonNull String key, int defaultValue)

Gets the integer value for the given key.

final int[]

Gets the integer array value for the given key.

final @NonNull Map<@NonNull StringObject>

Gets all the values in this Data object.

final long
getLong(@NonNull String key, long defaultValue)

Gets the long value for the given key.

final long[]

Gets the long array value for the given key.

final String

Gets the String value for the given key.

final String[]

Gets the String array value for the given key.

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.

int
final @NonNull byte[]

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

@NonNull String

Extension functions

final boolean
<T extends Object> DataKt.hasKeyWithValueOfType(
    @NonNull Data receiver,
    @NonNull String key
)

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

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 fields

EMPTY

public static final @NonNull Data EMPTY

An empty Data object with no elements.

Public constructors

Data

Added in 1.0.0
public Data(@NonNull Data other)

Copy constructor

Public methods

equals

public boolean equals(Object other)

fromByteArray

Added in 2.10.0
@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

Returns
@NonNull Data

An Data object built from the input

Throws
kotlin.IllegalStateException

if bytes is bigger than .MAX_DATA_BYTES

getBoolean

Added in 1.0.0
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

Added in 1.0.0
public final boolean[] getBooleanArray(@NonNull String key)

Gets the boolean array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
boolean[]

The value specified by the key if it exists; null otherwise

getByte

Added in 2.1.0
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

Added in 2.1.0
public final byte[] getByteArray(@NonNull String key)

Gets the byte array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
byte[]

The value specified by the key if it exists; null otherwise

getDouble

Added in 1.0.0
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

Added in 1.0.0
public final double[] getDoubleArray(@NonNull String key)

Gets the double array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
double[]

The value specified by the key if it exists; null otherwise

getFloat

Added in 1.0.0
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

Added in 1.0.0
public final float[] getFloatArray(@NonNull String key)

Gets the float array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
float[]

The value specified by the key if it exists; null otherwise

getInt

Added in 1.0.0
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

Added in 1.0.0
public final int[] getIntArray(@NonNull String key)

Gets the integer array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
int[]

The value specified by the key if it exists; null otherwise

getKeyValueMap

Added in 1.0.0
public final @NonNull Map<@NonNull StringObjectgetKeyValueMap()

Gets all the values in this Data object.

Returns
@NonNull Map<@NonNull StringObject>

A Map of key-value pairs for this object; this Map is unmodifiable and should be used for reads only.

getLong

Added in 1.0.0
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

Added in 1.0.0
public final long[] getLongArray(@NonNull String key)

Gets the long array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
long[]

The value specified by the key if it exists; null otherwise

getString

Added in 1.0.0
public final String getString(@NonNull String key)

Gets the String value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
String

The value specified by the key if it exists; null otherwise

getStringArray

Added in 1.0.0
public final String[] getStringArray(@NonNull String key)

Gets the String array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
String[]

The value specified by the key if it exists; null otherwise

hasKeyWithValueOfType

Added in 2.3.0
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.

Parameters
@NonNull String key

The String key

@NonNull Class<@NonNull T> klass

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()

toByteArray

Added in 2.3.0
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

toString

public @NonNull String toString()

Extension functions

DataKt.hasKeyWithValueOfType

public final boolean <T extends Object> DataKt.hasKeyWithValueOfType(
    @NonNull Data receiver,
    @NonNull String key
)

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