4
4
import android .support .annotation .NonNull ;
5
5
6
6
import com .google .firebase .firestore .DocumentSnapshot ;
7
+ import com .google .firebase .firestore .QuerySnapshot ;
8
+
9
+ import java .util .ArrayList ;
10
+ import java .util .LinkedHashMap ;
11
+ import java .util .List ;
7
12
8
13
import io .reactivex .functions .Function ;
9
14
import io .reactivex .functions .Predicate ;
@@ -14,18 +19,30 @@ private DocumentSnapshotMapper() {
14
19
}
15
20
16
21
public static <U > DocumentSnapshotMapper <DocumentSnapshot , U > of (Class <U > clazz ) {
17
- return new DocumentSnapshotMapper .TypedDataSnapshotMapper <U >(clazz );
22
+ return new TypedDocumentSnapshotMapper <U >(clazz );
23
+ }
24
+
25
+ public static <U > DocumentSnapshotMapper <QuerySnapshot , List <U >> listOf (Class <U > clazz ) {
26
+ return new DocumentSnapshotMapper .TypedListDocumentSnapshotMapper <>(clazz );
27
+ }
28
+
29
+ public static <U > DocumentSnapshotMapper <QuerySnapshot , List <U >> listOf (Class <U > clazz , Function <DocumentSnapshot , U > mapper ) {
30
+ return new DocumentSnapshotMapper .TypedListDocumentSnapshotMapper <>(clazz , mapper );
31
+ }
32
+
33
+ public static <U > TypedMapDocumentSnapshotMapper <U > mapOf (Class <U > clazz ) {
34
+ return new DocumentSnapshotMapper .TypedMapDocumentSnapshotMapper <>(clazz );
18
35
}
19
36
20
37
private static <U > U getDataSnapshotTypedValue (DocumentSnapshot documentSnapshot , Class <U > clazz ) {
21
38
return documentSnapshot .toObject (clazz );
22
39
}
23
40
24
- private static class TypedDataSnapshotMapper <U > extends DocumentSnapshotMapper <DocumentSnapshot , U > {
41
+ private static class TypedDocumentSnapshotMapper <U > extends DocumentSnapshotMapper <DocumentSnapshot , U > {
25
42
26
43
private final Class <U > clazz ;
27
44
28
- public TypedDataSnapshotMapper (final Class <U > clazz ) {
45
+ public TypedDocumentSnapshotMapper (final Class <U > clazz ) {
29
46
this .clazz = clazz ;
30
47
}
31
48
@@ -35,7 +52,59 @@ public U apply(final DocumentSnapshot documentSnapshot) {
35
52
}
36
53
}
37
54
38
- static final Predicate <DocumentSnapshot > DOCUMENT_EXISTENCE_PREDICATE = new Predicate <DocumentSnapshot >() {
55
+ private static class TypedListDocumentSnapshotMapper <U > extends DocumentSnapshotMapper <QuerySnapshot , List <U >> {
56
+
57
+ private final Class <U > clazz ;
58
+ private final Function <DocumentSnapshot , U > mapper ;
59
+
60
+ TypedListDocumentSnapshotMapper (final Class <U > clazz ) {
61
+ this (clazz , null );
62
+ }
63
+
64
+ TypedListDocumentSnapshotMapper (final Class <U > clazz , Function <DocumentSnapshot , U > mapper ) {
65
+ this .clazz = clazz ;
66
+ this .mapper = mapper ;
67
+ }
68
+
69
+ @ Override
70
+ public List <U > apply (final QuerySnapshot querySnapshot ) throws Exception {
71
+ List <U > items = new ArrayList <>();
72
+ for (DocumentSnapshot documentSnapshot : querySnapshot ) {
73
+ items .add (mapper != null
74
+ ? mapper .apply (documentSnapshot )
75
+ : getDataSnapshotTypedValue (documentSnapshot , clazz ));
76
+ }
77
+ return items ;
78
+ }
79
+ }
80
+
81
+
82
+ private static class TypedMapDocumentSnapshotMapper <U > extends DocumentSnapshotMapper <QuerySnapshot , LinkedHashMap <String , U >> {
83
+
84
+ private final Class <U > clazz ;
85
+
86
+ TypedMapDocumentSnapshotMapper (final Class <U > clazz ) {
87
+ this .clazz = clazz ;
88
+ }
89
+
90
+ @ Override
91
+ public LinkedHashMap <String , U > apply (final QuerySnapshot querySnapshot ) {
92
+ LinkedHashMap <String , U > items = new LinkedHashMap <>();
93
+ for (DocumentSnapshot documentSnapshot : querySnapshot ) {
94
+ items .put (documentSnapshot .getId (), getDataSnapshotTypedValue (documentSnapshot , clazz ));
95
+ }
96
+ return items ;
97
+ }
98
+ }
99
+
100
+ static final Predicate <QuerySnapshot > QUERY_EXISTENCE_PREDICATE = new Predicate <QuerySnapshot >() {
101
+ @ Override
102
+ public boolean test (@ NonNull QuerySnapshot querySnapshot ) throws Exception {
103
+ return querySnapshot .isEmpty ();
104
+ }
105
+ };
106
+
107
+ static final Predicate <DocumentSnapshot > DOCUMENT_EXISTENCE_PREDICATE = new Predicate <DocumentSnapshot >() {
39
108
@ Override
40
109
public boolean test (@ NonNull DocumentSnapshot documentSnapshot ) throws Exception {
41
110
return documentSnapshot .exists ();
0 commit comments