Repository navigation
Expand file tree
/
Copy pathDataSnapshot.java
More file actions
326 lines (303 loc) · 10.4 KB
/
Copy pathDataSnapshot.java
File metadata and controls
326 lines (303 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.database;
import com.google.firebase.database.annotations.Nullable;
import com.google.firebase.database.core.Path;
import com.google.firebase.database.snapshot.IndexedNode;
import com.google.firebase.database.snapshot.NamedNode;
import com.google.firebase.database.snapshot.Node;
import com.google.firebase.database.utilities.Validation;
import com.google.firebase.database.utilities.encoding.CustomClassMapper;
import java.util.Iterator;
/**
* A DataSnapshot instance contains data from a Firebase Database location. Any time you read
* Database data, you receive the data as a DataSnapshot. <br>
* <br>
* DataSnapshots are passed to the methods in listeners that you attach with {@link
* DatabaseReference#addValueEventListener(ValueEventListener)}, {@link
* DatabaseReference#addChildEventListener(ChildEventListener)}, or {@link
* DatabaseReference#addListenerForSingleValueEvent(ValueEventListener)}. <br>
* <br>
* They are efficiently-generated immutable copies of the data at a Firebase Database location. They
* can't be modified and will never change. To modify data at a location, use a {@link
* DatabaseReference DatabaseReference} reference (e.g. with {@link
* DatabaseReference#setValueAsync(Object)}).
*/
public class DataSnapshot {
private final IndexedNode node;
private final DatabaseReference query;
/**
* @param ref A DatabaseReference
* @param node The indexed node
*/
DataSnapshot(DatabaseReference ref, IndexedNode node) {
this.node = node;
this.query = ref;
}
/**
* Get a DataSnapshot for the location at the specified relative path. The relative path can
* either be a simple child key (e.g. 'fred') or a deeper slash-separated path (e.g.
* 'fred/name/first'). If the child location has no data, an empty DataSnapshot is returned.
*
* @param path A relative path to the location of child data
* @return The DataSnapshot for the child location
*/
public DataSnapshot child(String path) {
DatabaseReference childRef = query.child(path);
Node childNode = this.node.getNode().getChild(new Path(path));
return new DataSnapshot(childRef, IndexedNode.from(childNode));
}
/**
* Can be used to determine if this DataSnapshot has data at a particular location
*
* @param path A relative path to the location of child data
* @return Whether or not the specified child location has data
*/
public boolean hasChild(String path) {
if (query.getParent() == null) {
Validation.validateRootPathString(path);
} else {
Validation.validatePathString(path);
}
return !node.getNode().getChild(new Path(path)).isEmpty();
}
/**
* Indicates whether this snapshot has any children
*
* @return True if the snapshot has any children, otherwise false
*/
public boolean hasChildren() {
return node.getNode().getChildCount() > 0;
}
/**
* Returns true if the snapshot contains a non-null value.
*
* @return True if the snapshot contains a non-null value, otherwise false
*/
public boolean exists() {
return !node.getNode().isEmpty();
}
/**
* getValue() returns the data contained in this snapshot as native types. The possible types
* returned are:
*
* <ul>
* <li>Boolean
* <li>String
* <li>Long
* <li>Double
* <li>Map<String, Object>
* <li>List<Object>
* </ul>
*
* <p>This list is recursive; the possible types for <code>Object</code> in the above list
* is given by the same list. These types correspond to the types available in JSON.
*
* @return The data contained in this snapshot as native types or null if there is no data at this
* location.
*/
@Nullable
public Object getValue() {
return node.getNode().getValue();
}
/**
* getValue() returns the data contained in this snapshot as native types. The possible types
* returned are:
*
* <ul>
* <li>Boolean
* <li>String
* <li>Long
* <li>Double
* <li>Map<String, Object>
* <li>List<Object>
* </ul>
*
* <p>This list is recursive; the possible types for <code>Object</code> in the above list is
* given by the same list. These types correspond to the types available in JSON.
*
* <p>If useExportFormat is set to true, priority information will be included in the output.
* Priority information shows up as a .priority key in a map. For data that would not otherwise be
* a map, the map will also include a .value key with the data.
*
* @param useExportFormat Whether or not to include priority information
* @return The data, along with its priority, in native types or null if there is no data at this
* location.
*/
@Nullable
public Object getValue(boolean useExportFormat) {
return node.getNode().getValue(useExportFormat);
}
/**
* This method is used to marshall the data contained in this snapshot into a class of your
* choosing. The class must fit 2 simple constraints:
*
* <ol>
* <li>The class must have a default constructor that takes no arguments
* <li>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
* </ol>
*
* <p>An example class might look like:
*
* <pre><code>
* 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 = snapshot.getValue(Message.class);
* </code></pre>
*
* @param valueType The class into which this snapshot should be marshalled
* @param <T> The type to return. Implicitly defined from the class passed in
* @return An instance of the class passed in, populated with the data from this snapshot, or null
* if there is no data at this location.
*/
@Nullable
public <T> T getValue(Class<T> valueType) {
Object value = node.getNode().getValue();
return CustomClassMapper.convertToCustomClass(value, valueType);
}
/**
* 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 <code>List</code> of Message
* instances, you will need to do something like the following:
*
* <pre><code>
* GenericTypeIndicator<List<Message>> t = new GenericTypeIndicator<List<
* Message>>()
* {};
* List<Message> messages = snapshot.getValue(t);
* </code></pre>
*
* <p>It is important to use a subclass of {@link GenericTypeIndicator}. See {@link
* GenericTypeIndicator} for more details
*
* @param t A subclass of {@link GenericTypeIndicator} indicating the type of generic collection
* to be returned.
* @param <T> The type to return. Implicitly defined from the {@link GenericTypeIndicator} passed
* in
* @return A properly typed collection, populated with the data from this snapshot, or null if
* there is no data at this location.
*/
@Nullable
public <T> T getValue(GenericTypeIndicator<T> t) {
Object value = node.getNode().getValue();
return CustomClassMapper.convertToCustomClass(value, t);
}
/**
* @return The number of immediate children in the this snapshot
*/
public long getChildrenCount() {
return node.getNode().getChildCount();
}
/**
* Used to obtain a reference to the source location for this snapshot.
*
* @return A DatabaseReference corresponding to the location that this snapshot came from
*/
public DatabaseReference getRef() {
return query;
}
/**
* @return the key name for the source location of this snapshot
*/
public String getKey() {
return query.getKey();
}
/**
* Gives access to all of the immediate children of this snapshot. Can be used in native for
* loops: <code>
* <br> for (DataSnapshot child : parent.getChildren()) {
* <br> ...
* <br> }
* </code>
*
* @return The immediate children of this snapshot
*/
public Iterable<DataSnapshot> getChildren() {
final Iterator<NamedNode> iter = node.iterator();
return new Iterable<DataSnapshot>() {
@Override
public Iterator<DataSnapshot> iterator() {
return new Iterator<DataSnapshot>() {
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public DataSnapshot next() {
NamedNode namedNode = iter.next();
return new DataSnapshot(
query.child(namedNode.getName().asString()), IndexedNode.from(namedNode.getNode()));
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove called on immutable collection");
}
};
}
};
}
/**
* Returns the priority of the data contained in this snapshot as a native type. Possible return
* types:
*
* <ul>
* <li>Double
* <li>String
* </ul>
*
* <p>Note that null is also allowed.
*
* @return the priority of the data contained in this snapshot as a native type
*/
public Object getPriority() {
Object priority = node.getNode().getPriority().getValue();
if (priority instanceof Long) {
return Double.valueOf((Long) priority);
} else {
return priority;
}
}
@Override
public String toString() {
return "DataSnapshot { key = "
+ this.query.getKey()
+ ", value = "
+ this.node.getNode().getValue(true)
+ " }";
}
}