Stay organized with collections Save and categorize content based on your preferences.

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 Data

An empty Data object with no elements.

static final int

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

Public constructors

Data(@NonNull Data other)

Public methods

boolean
static @NonNull Data

Converts a byte array to Data.

boolean
getBoolean(@NonNull String key, boolean defaultValue)

Gets the boolean value for the given key.

@Nullable boolean[]

Gets the boolean array value for the given key.

byte
getByte(@NonNull String key, byte defaultValue)

Gets the byte value for the given key.

@Nullable byte[]

Gets the byte array value for the given key.

double
getDouble(@NonNull String key, double defaultValue)

Gets the double value for the given key.

@Nullable double[]

Gets the double array value for the given key.

float
getFloat(@NonNull String key, float defaultValue)

Gets the float value for the given key.

@Nullable float[]

Gets the float array value for the given key.

int
getInt(@NonNull String key, int defaultValue)

Gets the integer value for the given key.

@Nullable int[]

Gets the integer array value for the given key.

@NonNull Map<StringObject>

Gets all the values in this Data object.

long
getLong(@NonNull String key, long defaultValue)

Gets the long value for the given key.

@Nullable long[]

Gets the long array value for the given key.

@Nullable String

Gets the String value for the given key.

@Nullable String[]

Gets the String array value for the given key.

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.

int
@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

EMPTY

public static final Data EMPTY

An empty Data object with no elements.

MAX_DATA_BYTES

public static final int MAX_DATA_BYTES = 10240

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 constructors

Data

public Data(@NonNull Data other)

Public methods

equals

public boolean equals(Object o)

fromByteArray

@TypeConverter
public static @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
java.lang.IllegalStateException java.lang.IllegalStateException

if bytes is bigger than MAX_DATA_BYTES

getBoolean

public 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 @Nullable boolean[] getBooleanArray(@NonNull String key)

Gets the boolean array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
@Nullable boolean[]

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

getByte

public 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 @Nullable byte[] getByteArray(@NonNull String key)

Gets the byte array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
@Nullable byte[]

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

getDouble

public 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 @Nullable double[] getDoubleArray(@NonNull String key)

Gets the double array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
@Nullable double[]

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

getFloat

public 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 @Nullable float[] getFloatArray(@NonNull String key)

Gets the float array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
@Nullable float[]

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

getInt

public 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 @Nullable int[] getIntArray(@NonNull String key)

Gets the integer array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
@Nullable int[]

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

getKeyValueMap

public @NonNull Map<StringObjectgetKeyValueMap()

Gets all the values in this Data object.

Returns
@NonNull Map<StringObject>

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

getLong

public 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 @Nullable long[] getLongArray(@NonNull String key)

Gets the long array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
@Nullable long[]

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

getString

public @Nullable String getString(@NonNull String key)

Gets the String value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
@Nullable String

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

getStringArray

public @Nullable String[] getStringArray(@NonNull String key)

Gets the String array value for the given key.

Parameters
@NonNull String key

The key for the argument

Returns
@Nullable String[]

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

hasKeyWithValueOfType

public boolean <T> hasKeyWithValueOfType(@NonNull String key, @NonNull 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
<T>

The expected type

@NonNull String key

The String key

@NonNull Class<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

public @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
java.lang.IllegalStateException java.lang.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.