-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathDocumentSnapshotMapper.java
More file actions
100 lines (74 loc) · 3.43 KB
/
DocumentSnapshotMapper.java
File metadata and controls
100 lines (74 loc) · 3.43 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
package durdinapps.rxfirebase2;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
public abstract class DocumentSnapshotMapper<T, U> implements Function<T, U> {
private DocumentSnapshotMapper() {
}
public static <U> DocumentSnapshotMapper<DocumentSnapshot, U> of(Class<U> clazz) {
return new TypedDocumentSnapshotMapper<U>(clazz);
}
public static <U> DocumentSnapshotMapper<QuerySnapshot, List<U>> listOf(Class<U> clazz) {
return new TypedListQuerySnapshotMapper<>(clazz);
}
public static <U> DocumentSnapshotMapper<QuerySnapshot, List<U>> listOf(Class<U> clazz, Function<DocumentSnapshot, U> mapper) {
return new TypedListQuerySnapshotMapper<>(clazz, mapper);
}
public static <U> TypedMapQuerySnapshotMapper<U> mapOf(Class<U> clazz) {
return new TypedMapQuerySnapshotMapper<>(clazz);
}
private static <U> U getDataSnapshotTypedValue(DocumentSnapshot documentSnapshot, Class<U> clazz) {
return documentSnapshot.toObject(clazz);
}
private static class TypedDocumentSnapshotMapper<U> extends DocumentSnapshotMapper<DocumentSnapshot, U> {
private final Class<U> clazz;
public TypedDocumentSnapshotMapper(final Class<U> clazz) {
this.clazz = clazz;
}
@Override
public U apply(final DocumentSnapshot documentSnapshot) {
return getDataSnapshotTypedValue(documentSnapshot, clazz);
}
}
private static class TypedListQuerySnapshotMapper<U> extends DocumentSnapshotMapper<QuerySnapshot, List<U>> {
private final Class<U> clazz;
private final Function<DocumentSnapshot, U> mapper;
TypedListQuerySnapshotMapper(final Class<U> clazz) {
this(clazz, null);
}
TypedListQuerySnapshotMapper(final Class<U> clazz, Function<DocumentSnapshot, U> mapper) {
this.clazz = clazz;
this.mapper = mapper;
}
@Override
public List<U> apply(final QuerySnapshot querySnapshot) throws Exception {
List<U> items = new ArrayList<>();
for (DocumentSnapshot documentSnapshot : querySnapshot) {
items.add(mapper != null
? mapper.apply(documentSnapshot)
: getDataSnapshotTypedValue(documentSnapshot, clazz));
}
return items;
}
}
private static class TypedMapQuerySnapshotMapper<U> extends DocumentSnapshotMapper<QuerySnapshot, LinkedHashMap<String, U>> {
private final Class<U> clazz;
TypedMapQuerySnapshotMapper(final Class<U> clazz) {
this.clazz = clazz;
}
@Override
public LinkedHashMap<String, U> apply(final QuerySnapshot querySnapshot) {
LinkedHashMap<String, U> items = new LinkedHashMap<>();
for (DocumentSnapshot documentSnapshot : querySnapshot) {
items.put(documentSnapshot.getId(), getDataSnapshotTypedValue(documentSnapshot, clazz));
}
return items;
}
}
static final Predicate<QuerySnapshot> QUERY_EXISTENCE_PREDICATE = querySnapshot -> !querySnapshot.isEmpty();
static final Predicate<DocumentSnapshot> DOCUMENT_EXISTENCE_PREDICATE = DocumentSnapshot::exists;
}