@@ -23,28 +23,26 @@ public class FirebaseArrayOfObjects<E> extends ImmutableList<E> {
23
23
* @param snapshots a list of {@link DataSnapshot}s to be converted to a model type
24
24
* @param modelClass the model representation of a {@link DataSnapshot}
25
25
*/
26
- protected FirebaseArrayOfObjects (List <DataSnapshot > snapshots , Class <E > modelClass ) {
26
+ protected FirebaseArrayOfObjects (List <DataSnapshot > snapshots ,
27
+ Class <E > modelClass ,
28
+ SnapshotParser <E > parser ) {
27
29
mSnapshots = snapshots ;
28
30
mEClass = modelClass ;
29
- mParser = new SnapshotParser <E >() {
30
- @ Override
31
- public E parseSnapshot (DataSnapshot snapshot ) {
32
- return snapshot .getValue (mEClass );
33
- }
34
- };
31
+ mParser = parser ;
35
32
}
36
33
37
34
/**
38
35
* @param snapshots a list of {@link DataSnapshot}s to be converted to a model type
39
36
* @param modelClass the model representation of a {@link DataSnapshot}
40
37
*/
41
38
public static <T > FirebaseArrayOfObjects <T > newInstance (List <DataSnapshot > snapshots ,
42
- Class <T > modelClass ) {
43
- if (snapshots instanceof FirebaseArray ) {
44
- return new FirebaseArrayOfObjectsOptimized <>((FirebaseArray ) snapshots , modelClass );
45
- } else {
46
- return new FirebaseArrayOfObjects <>(snapshots , modelClass );
47
- }
39
+ final Class <T > modelClass ) {
40
+ return getArray (snapshots , modelClass , new SnapshotParser <T >() {
41
+ @ Override
42
+ public T parseSnapshot (DataSnapshot snapshot ) {
43
+ return snapshot .getValue (modelClass );
44
+ }
45
+ });
48
46
}
49
47
50
48
/**
@@ -55,9 +53,18 @@ public static <T> FirebaseArrayOfObjects<T> newInstance(List<DataSnapshot> snaps
55
53
public static <T > FirebaseArrayOfObjects <T > newInstance (List <DataSnapshot > snapshots ,
56
54
Class <T > modelClass ,
57
55
SnapshotParser <T > parser ) {
58
- FirebaseArrayOfObjects <T > array = newInstance (snapshots , modelClass );
59
- array .mParser = parser ;
60
- return array ;
56
+ return getArray (snapshots , modelClass , parser );
57
+ }
58
+
59
+ private static <T > FirebaseArrayOfObjects <T > getArray (List <DataSnapshot > snapshots ,
60
+ Class <T > modelClass ,
61
+ SnapshotParser <T > parser ) {
62
+ if (snapshots instanceof FirebaseArray ) {
63
+ return new FirebaseArrayOfObjectsOptimized <>(
64
+ (FirebaseArray ) snapshots , modelClass , parser );
65
+ } else {
66
+ return new FirebaseArrayOfObjects <>(snapshots , modelClass , parser );
67
+ }
61
68
}
62
69
63
70
public List <DataSnapshot > getSnapshots () {
@@ -173,8 +180,10 @@ protected static class FirebaseArrayOfObjectsOptimized<E> extends FirebaseArrayO
173
180
implements ChangeEventListener {
174
181
protected List <E > mObjects = new ArrayList <>();
175
182
176
- public FirebaseArrayOfObjectsOptimized (FirebaseArray snapshots , Class <E > modelClass ) {
177
- super (snapshots , modelClass );
183
+ public FirebaseArrayOfObjectsOptimized (FirebaseArray snapshots ,
184
+ Class <E > modelClass ,
185
+ SnapshotParser <E > parser ) {
186
+ super (snapshots , modelClass , parser );
178
187
snapshots .addChangeEventListener (this );
179
188
}
180
189
0 commit comments