16
16
17
17
public abstract class DataSnapshotMapper <T , U > implements Function <T , U > {
18
18
19
- private DataSnapshotMapper () {
20
- }
19
+ private DataSnapshotMapper () {
20
+ }
21
21
22
- public static <U > DataSnapshotMapper <DataSnapshot , U > of (Class <U > clazz ) {
23
- return new TypedDataSnapshotMapper <U >(clazz );
24
- }
22
+ public static <U > DataSnapshotMapper <DataSnapshot , U > of (Class <U > clazz ) {
23
+ return new TypedDataSnapshotMapper <U >(clazz );
24
+ }
25
25
26
- public static <U > DataSnapshotMapper <DataSnapshot , List <U >> listOf (Class <U > clazz ) {
27
- return new TypedListDataSnapshotMapper <>(clazz );
28
- }
26
+ public static <U > DataSnapshotMapper <DataSnapshot , List <U >> listOf (Class <U > clazz ) {
27
+ return new TypedListDataSnapshotMapper <>(clazz );
28
+ }
29
29
30
- public static <U > DataSnapshotMapper <DataSnapshot , List <U >> listOf (Class <U > clazz , Function <DataSnapshot , U > mapper ) {
31
- return new TypedListDataSnapshotMapper <>(clazz , mapper );
32
- }
30
+ public static <U > DataSnapshotMapper <DataSnapshot , List <U >> listOf (Class <U > clazz , Function <DataSnapshot , U > mapper ) {
31
+ return new TypedListDataSnapshotMapper <>(clazz , mapper );
32
+ }
33
33
34
- public static <U > DataSnapshotMapper <DataSnapshot , LinkedHashMap <String , U >> mapOf (Class <U > clazz ) {
35
- return new TypedMapDataSnapshotMapper <>(clazz );
36
- }
34
+ public static <U > DataSnapshotMapper <DataSnapshot , LinkedHashMap <String , U >> mapOf (Class <U > clazz ) {
35
+ return new TypedMapDataSnapshotMapper <>(clazz );
36
+ }
37
37
38
- public static <U > DataSnapshotMapper <DataSnapshot , U > of (GenericTypeIndicator <U > genericTypeIndicator ) {
39
- return new GenericTypedDataSnapshotMapper <U >(genericTypeIndicator );
40
- }
38
+ public static <U > DataSnapshotMapper <DataSnapshot , U > of (GenericTypeIndicator <U > genericTypeIndicator ) {
39
+ return new GenericTypedDataSnapshotMapper <U >(genericTypeIndicator );
40
+ }
41
41
42
- public static <U > DataSnapshotMapper <RxFirebaseChildEvent <DataSnapshot >, RxFirebaseChildEvent <U >> ofChildEvent (Class <U > clazz ) {
43
- return new ChildEventDataSnapshotMapper <U >(clazz );
44
- }
42
+ public static <U > DataSnapshotMapper <RxFirebaseChildEvent <DataSnapshot >, RxFirebaseChildEvent <U >> ofChildEvent (Class <U > clazz ) {
43
+ return new ChildEventDataSnapshotMapper <U >(clazz );
44
+ }
45
45
46
- private static <U > U getDataSnapshotTypedValue (DataSnapshot dataSnapshot , Class <U > clazz ) {
47
- U value = dataSnapshot .getValue (clazz );
48
- if (value == null ) {
49
- throw Exceptions .propagate (new RxFirebaseDataCastException (
50
- "unable to cast firebase data response to " + clazz .getSimpleName ()));
51
- }
52
- return value ;
53
- }
46
+ private static <U > U getDataSnapshotTypedValue (DataSnapshot dataSnapshot , Class <U > clazz ) {
47
+ U value = dataSnapshot .getValue (clazz );
48
+ if (value == null ) {
49
+ throw Exceptions .propagate (new RxFirebaseDataCastException (
50
+ "unable to cast firebase data response to " + clazz .getSimpleName ()));
51
+ }
52
+ return value ;
53
+ }
54
54
55
- private static class TypedDataSnapshotMapper <U > extends DataSnapshotMapper <DataSnapshot , U > {
55
+ private static class TypedDataSnapshotMapper <U > extends DataSnapshotMapper <DataSnapshot , U > {
56
56
57
- private final Class <U > clazz ;
57
+ private final Class <U > clazz ;
58
58
59
- public TypedDataSnapshotMapper (final Class <U > clazz ) {
60
- this .clazz = clazz ;
61
- }
59
+ public TypedDataSnapshotMapper (final Class <U > clazz ) {
60
+ this .clazz = clazz ;
61
+ }
62
62
63
- @ Override
64
- public U apply (final DataSnapshot dataSnapshot ) {
63
+ @ Override
64
+ public U apply (final DataSnapshot dataSnapshot ) {
65
65
return getDataSnapshotTypedValue (dataSnapshot , clazz );
66
- }
67
- }
68
-
69
- private static class TypedListDataSnapshotMapper <U > extends DataSnapshotMapper <DataSnapshot , List <U >> {
70
-
71
- private final Class <U > clazz ;
72
- private final Function <DataSnapshot , U > mapper ;
73
-
74
- TypedListDataSnapshotMapper (final Class <U > clazz ) {
75
- this (clazz , null );
76
- }
77
-
78
- TypedListDataSnapshotMapper (final Class <U > clazz , Function <DataSnapshot , U > mapper ) {
79
- this .clazz = clazz ;
80
- this .mapper = mapper ;
81
- }
82
-
83
- @ Override
84
- public List <U > apply (final DataSnapshot dataSnapshot ) throws Exception {
85
- List <U > items = new ArrayList <>();
86
- for (DataSnapshot childSnapshot : dataSnapshot .getChildren ()) {
87
- items .add (mapper != null
88
- ? mapper .apply (childSnapshot )
89
- : getDataSnapshotTypedValue (childSnapshot , clazz ));
90
- }
91
- return items ;
92
- }
93
- }
94
-
95
- private static class TypedMapDataSnapshotMapper <U > extends DataSnapshotMapper <DataSnapshot , LinkedHashMap <String , U >> {
96
-
97
- private final Class <U > clazz ;
98
-
99
- TypedMapDataSnapshotMapper (final Class <U > clazz ) {
100
- this .clazz = clazz ;
101
- }
102
-
103
- @ Override
104
- public LinkedHashMap <String , U > apply (final DataSnapshot dataSnapshot ) {
105
- LinkedHashMap <String , U > items = new LinkedHashMap <>();
106
- for (DataSnapshot childSnapshot : dataSnapshot .getChildren ()) {
107
- items .put (childSnapshot .getKey (), getDataSnapshotTypedValue (childSnapshot , clazz ));
108
- }
109
- return items ;
110
- }
111
- }
112
-
113
- private static class GenericTypedDataSnapshotMapper <U > extends DataSnapshotMapper <DataSnapshot , U > {
114
-
115
- private final GenericTypeIndicator <U > genericTypeIndicator ;
116
-
117
- public GenericTypedDataSnapshotMapper (GenericTypeIndicator <U > genericTypeIndicator ) {
118
- this .genericTypeIndicator = genericTypeIndicator ;
119
- }
120
-
121
- @ Override
122
- public U apply (DataSnapshot dataSnapshot ) {
66
+ }
67
+ }
68
+
69
+ private static class TypedListDataSnapshotMapper <U > extends DataSnapshotMapper <DataSnapshot , List <U >> {
70
+
71
+ private final Class <U > clazz ;
72
+ private final Function <DataSnapshot , U > mapper ;
73
+
74
+ TypedListDataSnapshotMapper (final Class <U > clazz ) {
75
+ this (clazz , null );
76
+ }
77
+
78
+ TypedListDataSnapshotMapper (final Class <U > clazz , Function <DataSnapshot , U > mapper ) {
79
+ this .clazz = clazz ;
80
+ this .mapper = mapper ;
81
+ }
82
+
83
+ @ Override
84
+ public List <U > apply (final DataSnapshot dataSnapshot ) throws Exception {
85
+ List <U > items = new ArrayList <>();
86
+ for (DataSnapshot childSnapshot : dataSnapshot .getChildren ()) {
87
+ items .add (mapper != null
88
+ ? mapper .apply (childSnapshot )
89
+ : getDataSnapshotTypedValue (childSnapshot , clazz ));
90
+ }
91
+ return items ;
92
+ }
93
+ }
94
+
95
+ private static class TypedMapDataSnapshotMapper <U > extends DataSnapshotMapper <DataSnapshot , LinkedHashMap <String , U >> {
96
+
97
+ private final Class <U > clazz ;
98
+
99
+ TypedMapDataSnapshotMapper (final Class <U > clazz ) {
100
+ this .clazz = clazz ;
101
+ }
102
+
103
+ @ Override
104
+ public LinkedHashMap <String , U > apply (final DataSnapshot dataSnapshot ) {
105
+ LinkedHashMap <String , U > items = new LinkedHashMap <>();
106
+ for (DataSnapshot childSnapshot : dataSnapshot .getChildren ()) {
107
+ items .put (childSnapshot .getKey (), getDataSnapshotTypedValue (childSnapshot , clazz ));
108
+ }
109
+ return items ;
110
+ }
111
+ }
112
+
113
+ private static class GenericTypedDataSnapshotMapper <U > extends DataSnapshotMapper <DataSnapshot , U > {
114
+
115
+ private final GenericTypeIndicator <U > genericTypeIndicator ;
116
+
117
+ public GenericTypedDataSnapshotMapper (GenericTypeIndicator <U > genericTypeIndicator ) {
118
+ this .genericTypeIndicator = genericTypeIndicator ;
119
+ }
120
+
121
+ @ Override
122
+ public U apply (DataSnapshot dataSnapshot ) {
123
123
U value = dataSnapshot .getValue (genericTypeIndicator );
124
124
if (value == null ) {
125
- throw Exceptions .propagate (new RxFirebaseDataCastException (
126
- "unable to cast firebase data response to generic type" ));
125
+ throw Exceptions .propagate (new RxFirebaseDataCastException (
126
+ "unable to cast firebase data response to generic type" ));
127
127
}
128
128
return value ;
129
- }
130
- }
131
-
132
- private static class ChildEventDataSnapshotMapper <U >
133
- extends DataSnapshotMapper <RxFirebaseChildEvent <DataSnapshot >, RxFirebaseChildEvent <U >> {
134
-
135
- private final Class <U > clazz ;
136
-
137
- public ChildEventDataSnapshotMapper (final Class <U > clazz ) {
138
- this .clazz = clazz ;
139
- }
140
-
141
- @ Override
142
- public RxFirebaseChildEvent <U > apply (final RxFirebaseChildEvent <DataSnapshot > rxFirebaseChildEvent ) {
143
- DataSnapshot dataSnapshot = rxFirebaseChildEvent .getValue ();
144
- if (dataSnapshot .exists ()) {
145
- return new RxFirebaseChildEvent <U >(
146
- dataSnapshot .getKey (),
147
- getDataSnapshotTypedValue (dataSnapshot , clazz ),
148
- rxFirebaseChildEvent .getPreviousChildName (),
149
- rxFirebaseChildEvent .getEventType ());
150
- } else {
151
- throw Exceptions .propagate (new RuntimeException ("child dataSnapshot doesn't exist" ));
152
- }
153
- }
154
- }
155
-
156
- static final Predicate <DataSnapshot > DATA_SNAPSHOT_EXISTENCE_PREDICATE = new Predicate <DataSnapshot >() {
157
- @ Override
158
- public boolean test (@ NonNull DataSnapshot dataSnapshot ) throws Exception {
159
- return dataSnapshot .exists ();
160
- }
161
- };
129
+ }
130
+ }
131
+
132
+ private static class ChildEventDataSnapshotMapper <U >
133
+ extends DataSnapshotMapper <RxFirebaseChildEvent <DataSnapshot >, RxFirebaseChildEvent <U >> {
134
+
135
+ private final Class <U > clazz ;
136
+
137
+ public ChildEventDataSnapshotMapper (final Class <U > clazz ) {
138
+ this .clazz = clazz ;
139
+ }
140
+
141
+ @ Override
142
+ public RxFirebaseChildEvent <U > apply (final RxFirebaseChildEvent <DataSnapshot > rxFirebaseChildEvent ) {
143
+ DataSnapshot dataSnapshot = rxFirebaseChildEvent .getValue ();
144
+ if (dataSnapshot .exists ()) {
145
+ return new RxFirebaseChildEvent <U >(
146
+ dataSnapshot .getKey (),
147
+ getDataSnapshotTypedValue (dataSnapshot , clazz ),
148
+ rxFirebaseChildEvent .getPreviousChildName (),
149
+ rxFirebaseChildEvent .getEventType ());
150
+ } else {
151
+ throw Exceptions .propagate (new RuntimeException ("child dataSnapshot doesn't exist" ));
152
+ }
153
+ }
154
+ }
155
+
156
+ static final Predicate <DataSnapshot > DATA_SNAPSHOT_EXISTENCE_PREDICATE = new Predicate <DataSnapshot >() {
157
+ @ Override
158
+ public boolean test (@ NonNull DataSnapshot dataSnapshot ) throws Exception {
159
+ return dataSnapshot .exists ();
160
+ }
161
+ };
162
162
}
0 commit comments