MutableData
public class MutableData
Instances of this class encapsulate the data and priority at a location. It is used in transactions, and it is intended to be inspected and then updated to the desired data at that location. Note that changes made to a child MutableData instance will be visible to the parent and vice versa.
Summary
Public methods |
|
|---|---|
@NonNull MutableData |
Used to obtain a MutableData instance that encapsulates the data and priority at the given relative path. |
boolean |
|
@NonNull Iterable<MutableData> |
Used to iterate over the immediate children at this location |
long |
|
@Nullable String |
getKey() |
@Nullable Object |
Gets the current priority at this location. |
@Nullable Object |
getValue()getValue() returns the data contained in this instance as native types. |
@Nullable T |
<T> getValue(@NonNull GenericTypeIndicator<T> t)Due to the way that Java implements generics, it takes an extra step to get back a properly-typed Collection. |
@Nullable T |
This method is used to marshall the data contained in this instance into a class of your choosing. |
boolean |
|
boolean |
|
void |
setPriority(@Nullable Object priority)Sets the priority at this location |
void |
Set the data at this location to the given value. |
String |
toString() |
Extension functions |
|
|---|---|
final T |
<T extends Object> DatabaseKt.getValue(@NonNull MutableData receiver)Returns the content of the MutableData converted to a POJO. |
Public methods
child
public @NonNull MutableData child(@NonNull String path)
Used to obtain a MutableData instance that encapsulates the data and priority at the given relative path.
| Returns | |
|---|---|
@NonNull MutableData |
An instance encapsulating the data and priority at the given path |
getChildren
public @NonNull Iterable<MutableData> getChildren()
Used to iterate over the immediate children at this location
for (MutableData child : parent.getChildren()) {
...
}| Returns | |
|---|---|
@NonNull Iterable<MutableData> |
The immediate children at this location |
getChildrenCount
public long getChildrenCount()
| Returns | |
|---|---|
long |
The number of immediate children at this location |
getPriority
public @Nullable Object getPriority()
Gets the current priority at this location. The possible return types are:
DoubleString
getValue
public @Nullable Object getValue()
getValue() returns the data contained in this instance as native types. The possible types returned are:
BooleanLongDoubleStringMap<String, Object>List<Object>
java.lang.Object in the above list is given by the same list. These types correspond to the types available in JSON.
getValue
public @Nullable T <T> getValue(@NonNull GenericTypeIndicator<T> t)
Due to the way that Java implements generics, it takes an extra step to get back a properly-typed Collection. So, in the case where you want a java.util.List of Message instances, you will need to do something like the following:
GenericTypeIndicator<List<Message>> t =
new GenericTypeIndicator<List<Message>>() {};
List<Message> messages = mutableData.getValue(t);GenericTypeIndicator. See GenericTypeIndicator for more details
| Parameters | |
|---|---|
<T> |
The type to return. Implicitly defined from the |
@NonNull GenericTypeIndicator<T> t |
A subclass of |
| Returns | |
|---|---|
@Nullable T |
A properly typed collection, populated with the data from this instance, or null if there is no data at this location. |
getValue
public @Nullable T <T> getValue(@NonNull Class<T> valueType)
This method is used to marshall the data contained in this instance into a class of your choosing. The class must fit 2 simple constraints:
- The class must have a default constructor that takes no arguments
- The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
class Message { private String author; private String text; private Message() {} public Message(String author, String text) { this.author = author; this.text = text; } public String getAuthor() { return author; } public String getText() { return text; } } // Later Message m = mutableData.getValue(Message.class);
| Parameters | |
|---|---|
<T> |
The type to return. Implicitly defined from the class passed in |
@NonNull Class<T> valueType |
The class into which this data in this instance should be marshalled |
| Returns | |
|---|---|
@Nullable T |
An instance of the class passed in, populated with the data from this instance, or null if there is no data at this location. |
hasChild
public boolean hasChild(@NonNull String path)
| Returns | |
|---|---|
boolean |
True if data exists at the given path, otherwise false |
hasChildren
public boolean hasChildren()
| Returns | |
|---|---|
boolean |
True if the data at this location has children, false otherwise |
setValue
public void setValue(@Nullable Object value)
Set the data at this location to the given value. The native types accepted by this method for the value correspond to the JSON types:
BooleanLongDoubleMap<String, Object>List<Object>
- The class must have a default constructor that takes no arguments
- The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
Map<String, MyPOJO>, as well as null values.
Note that this overrides the priority, which must be set separately.
| Parameters | |
|---|---|
@Nullable Object value |
The value to set at this location or null to delete the existing data |
| Throws | |
|---|---|
com.google.firebase.database.DatabaseException com.google.firebase.database.DatabaseException |
Extension functions
DatabaseKt.getValue
public final T <T extends Object> DatabaseKt.getValue(@NonNull MutableData receiver)
Returns the content of the MutableData converted to a POJO.
Supports generics like List<> or Map<>. Use @JvmSuppressWildcards to force the compiler to use the type T, and not ? extends T.