Skip to content

Commit 9744199

Browse files
SUPERCILEXsamtstern
authored andcommitted
Convert SnapshotArray from List<S> to List<T> (#18)
1 parent 0bea78b commit 9744199

File tree

11 files changed

+73
-87
lines changed

11 files changed

+73
-87
lines changed

common/src/main/java/com/firebase/ui/common/BaseObservableSnapshotArray.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @param <T> the model object class.
1818
*/
1919
public abstract class BaseObservableSnapshotArray<S, E, L extends BaseChangeEventListener<S, E>, T>
20-
extends AbstractList<S> {
20+
extends AbstractList<T> {
2121

2222
private final List<L> mListeners = new CopyOnWriteArrayList<>();
2323
private final BaseCachingSnapshotParser<S, T> mCachingParser;
@@ -41,21 +41,17 @@ public BaseObservableSnapshotArray(@NonNull BaseCachingSnapshotParser<S, T> pars
4141
protected abstract List<S> getSnapshots();
4242

4343
@Override
44-
public S get(int index) {
45-
return getSnapshots().get(index);
44+
public T get(int index) {
45+
return mCachingParser.parseSnapshot(getSnapshot(index));
4646
}
4747

4848
@Override
4949
public int size() {
5050
return getSnapshots().size();
5151
}
5252

53-
/**
54-
* Get the Snapshot at a given position converted to an object of the parametrized type. This
55-
* uses the {@link BaseSnapshotParser} passed to the constructor.
56-
*/
57-
public T getObject(int index) {
58-
return mCachingParser.parseSnapshot(get(index));
53+
public S getSnapshot(int index) {
54+
return getSnapshots().get(index);
5955
}
6056

6157
/**
@@ -75,7 +71,7 @@ public L addChangeEventListener(@NonNull L listener) {
7571

7672
// Catch up new listener to existing state
7773
for (int i = 0; i < size(); i++) {
78-
listener.onChildChanged(ChangeEventType.ADDED, get(i), i, -1);
74+
listener.onChildChanged(ChangeEventType.ADDED, getSnapshot(i), i, -1);
7975
}
8076
if (mHasDataChanged) {
8177
listener.onDataChanged();

database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayOfObjectsTest.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ public void run() {
5757
}
5858
}, new Callable<Boolean>() {
5959
@Override
60-
public Boolean call() throws Exception {
60+
public Boolean call() {
6161
return mArray.size() == INITIAL_SIZE;
6262
}
6363
});
6464
}
6565

6666
@After
67-
public void tearDown() throws Exception {
67+
public void tearDown() {
6868
mArray.removeChangeEventListener(mListener);
6969
mRef.getRoot().removeValue();
7070
}
@@ -78,7 +78,7 @@ public void run() {
7878
}
7979
}, new Callable<Boolean>() {
8080
@Override
81-
public Boolean call() throws Exception {
81+
public Boolean call() {
8282
return mArray.size() == 4;
8383
}
8484
});
@@ -93,8 +93,8 @@ public void run() {
9393
}
9494
}, new Callable<Boolean>() {
9595
@Override
96-
public Boolean call() throws Exception {
97-
return mArray.getObject(3).getNumber() == 4;
96+
public Boolean call() {
97+
return mArray.get(3).getNumber() == 4;
9898
}
9999
});
100100
}
@@ -108,9 +108,8 @@ public void run() {
108108
}
109109
}, new Callable<Boolean>() {
110110
@Override
111-
public Boolean call() throws Exception {
112-
return mArray.getObject(3).getNumber() == 3
113-
&& mArray.getObject(0).getNumber() == 4;
111+
public Boolean call() {
112+
return mArray.get(3).getNumber() == 3 && mArray.get(0).getNumber() == 4;
114113
}
115114
});
116115
}
@@ -120,14 +119,14 @@ public void testChangePriorities() throws Exception {
120119
runAndWaitUntil(mArray, new Runnable() {
121120
@Override
122121
public void run() {
123-
mArray.get(2).getRef().setPriority(0.5);
122+
mArray.getSnapshot(2).getRef().setPriority(0.5);
124123
}
125124
}, new Callable<Boolean>() {
126125
@Override
127-
public Boolean call() throws Exception {
128-
return mArray.getObject(0).getNumber() == 3
129-
&& mArray.getObject(1).getNumber() == 1
130-
&& mArray.getObject(2).getNumber() == 2;
126+
public Boolean call() {
127+
return mArray.get(0).getNumber() == 3
128+
&& mArray.get(1).getNumber() == 1
129+
&& mArray.get(2).getNumber() == 2;
131130
//return isValuesEqual(mArray, new int[]{3, 1, 2});
132131
}
133132
});
@@ -145,8 +144,8 @@ public void run() {
145144
}
146145
}, new Callable<Boolean>() {
147146
@Override
148-
public Boolean call() throws Exception {
149-
return mArray.getObject(3).getNumber() == 5;
147+
public Boolean call() {
148+
return mArray.get(3).getNumber() == 5;
150149
}
151150
});
152151

@@ -158,8 +157,8 @@ public void run() {
158157
}
159158
}, new Callable<Boolean>() {
160159
@Override
161-
public Boolean call() throws Exception {
162-
return mArray.getObject(3).getNumber() == 6;
160+
public Boolean call() {
161+
return mArray.get(3).getNumber() == 6;
163162
}
164163
});
165164
}

database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayTest.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public void run() {
5454
}
5555
}, new Callable<Boolean>() {
5656
@Override
57-
public Boolean call() throws Exception {
57+
public Boolean call() {
5858
return mArray.size() == INITIAL_SIZE;
5959
}
6060
});
6161
}
6262

6363
@After
64-
public void tearDown() throws Exception {
64+
public void tearDown() {
6565
mArray.removeChangeEventListener(mListener);
6666
mRef.getRoot().removeValue();
6767
}
@@ -75,7 +75,7 @@ public void run() {
7575
}
7676
}, new Callable<Boolean>() {
7777
@Override
78-
public Boolean call() throws Exception {
78+
public Boolean call() {
7979
return mArray.size() == 4;
8080
}
8181
});
@@ -90,8 +90,8 @@ public void run() {
9090
}
9191
}, new Callable<Boolean>() {
9292
@Override
93-
public Boolean call() throws Exception {
94-
return mArray.getObject(3).equals(4);
93+
public Boolean call() {
94+
return mArray.get(3).equals(4);
9595
}
9696
});
9797
}
@@ -105,9 +105,8 @@ public void run() {
105105
}
106106
}, new Callable<Boolean>() {
107107
@Override
108-
public Boolean call() throws Exception {
109-
return mArray.getObject(3).equals(3)
110-
&& mArray.getObject(0).equals(4);
108+
public Boolean call() {
109+
return mArray.get(3).equals(3) && mArray.get(0).equals(4);
111110
}
112111
});
113112
}
@@ -117,11 +116,11 @@ public void testChangePriorityBackToFront() throws Exception {
117116
runAndWaitUntil(mArray, new Runnable() {
118117
@Override
119118
public void run() {
120-
mArray.get(2).getRef().setPriority(0.5);
119+
mArray.getSnapshot(2).getRef().setPriority(0.5);
121120
}
122121
}, new Callable<Boolean>() {
123122
@Override
124-
public Boolean call() throws Exception {
123+
public Boolean call() {
125124
return isValuesEqual(mArray, new int[]{3, 1, 2});
126125
}
127126
});
@@ -132,11 +131,11 @@ public void testChangePriorityFrontToBack() throws Exception {
132131
runAndWaitUntil(mArray, new Runnable() {
133132
@Override
134133
public void run() {
135-
mArray.get(0).getRef().setPriority(4);
134+
mArray.getSnapshot(0).getRef().setPriority(4);
136135
}
137136
}, new Callable<Boolean>() {
138137
@Override
139-
public Boolean call() throws Exception {
138+
public Boolean call() {
140139
return isValuesEqual(mArray, new int[]{2, 3, 1});
141140
}
142141
});

database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayOfObjectsTest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ public void run() {
5959
}
6060
}, new Callable<Boolean>() {
6161
@Override
62-
public Boolean call() throws Exception {
62+
public Boolean call() {
6363
return mArray.size() == INITIAL_SIZE;
6464
}
6565
});
6666
}
6767

6868
@After
69-
public void tearDown() throws Exception {
69+
public void tearDown() {
7070
mArray.removeChangeEventListener(mListener);
7171
mRef.getRoot().removeValue();
7272
}
@@ -80,7 +80,7 @@ public void run() {
8080
}
8181
}, new Callable<Boolean>() {
8282
@Override
83-
public Boolean call() throws Exception {
83+
public Boolean call() {
8484
return mArray.size() == 4;
8585
}
8686
});
@@ -95,8 +95,8 @@ public void run() {
9595
}
9696
}, new Callable<Boolean>() {
9797
@Override
98-
public Boolean call() throws Exception {
99-
return mArray.getObject(3).getNumber() == 4;
98+
public Boolean call() {
99+
return mArray.get(3).getNumber() == 4;
100100
}
101101
});
102102
}
@@ -110,9 +110,8 @@ public void run() {
110110
}
111111
}, new Callable<Boolean>() {
112112
@Override
113-
public Boolean call() throws Exception {
114-
return mArray.getObject(3).getNumber() == 3
115-
&& mArray.getObject(0).getNumber() == 4;
113+
public Boolean call() {
114+
return mArray.get(3).getNumber() == 3 && mArray.get(0).getNumber() == 4;
116115
}
117116
});
118117
}
@@ -122,14 +121,14 @@ public void testChangePriorities() throws Exception {
122121
runAndWaitUntil(mArray, new Runnable() {
123122
@Override
124123
public void run() {
125-
mKeyRef.child(mArray.get(2).getKey()).setPriority(0.5);
124+
mKeyRef.child(mArray.getSnapshot(2).getKey()).setPriority(0.5);
126125
}
127126
}, new Callable<Boolean>() {
128127
@Override
129-
public Boolean call() throws Exception {
130-
return mArray.getObject(0).getNumber() == 3
131-
&& mArray.getObject(1).getNumber() == 1
132-
&& mArray.getObject(2).getNumber() == 2;
128+
public Boolean call() {
129+
return mArray.get(0).getNumber() == 3
130+
&& mArray.get(1).getNumber() == 1
131+
&& mArray.get(2).getNumber() == 2;
133132
//return isValuesEqual(mArray, new int[]{3, 1, 2});
134133
}
135134
});

database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ public void run() {
6060
}
6161
}, new Callable<Boolean>() {
6262
@Override
63-
public Boolean call() throws Exception {
63+
public Boolean call() {
6464
return mArray.size() == INITIAL_SIZE;
6565
}
6666
});
6767
}
6868

6969
@After
70-
public void tearDown() throws Exception {
70+
public void tearDown() {
7171
mArray.removeChangeEventListener(mListener);
7272
mRef.getRoot().removeValue();
7373
}
@@ -81,7 +81,7 @@ public void run() {
8181
}
8282
}, new Callable<Boolean>() {
8383
@Override
84-
public Boolean call() throws Exception {
84+
public Boolean call() {
8585
return mArray.size() == 4;
8686
}
8787
});
@@ -96,8 +96,8 @@ public void run() {
9696
}
9797
}, new Callable<Boolean>() {
9898
@Override
99-
public Boolean call() throws Exception {
100-
return mArray.getObject(3).equals(4);
99+
public Boolean call() {
100+
return mArray.get(3).equals(4);
101101
}
102102
});
103103
}
@@ -111,9 +111,8 @@ public void run() {
111111
}
112112
}, new Callable<Boolean>() {
113113
@Override
114-
public Boolean call() throws Exception {
115-
return mArray.getObject(3).equals(3)
116-
&& mArray.getObject(0).equals(4);
114+
public Boolean call() {
115+
return mArray.get(3).equals(3) && mArray.get(0).equals(4);
117116
}
118117
});
119118
}
@@ -123,11 +122,11 @@ public void testChangePriorities() throws Exception {
123122
runAndWaitUntil(mArray, new Runnable() {
124123
@Override
125124
public void run() {
126-
mKeyRef.child(mArray.get(2).getKey()).setPriority(0.5);
125+
mKeyRef.child(mArray.getSnapshot(2).getKey()).setPriority(0.5);
127126
}
128127
}, new Callable<Boolean>() {
129128
@Override
130-
public Boolean call() throws Exception {
129+
public Boolean call() {
131130
return isValuesEqual(mArray, new int[]{3, 1, 2});
132131
}
133132
});

database/src/androidTest/java/com/firebase/ui/database/TestUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public void onError(DatabaseError error) {
7878

7979
public static boolean isValuesEqual(ObservableSnapshotArray<Integer> array, int[] expected) {
8080
if (array.size() != expected.length) return false;
81-
for (int i = 0; i < array.size(); i++) {
82-
if (!array.getObject(i).equals(expected[i])) {
81+
for (Integer i : array) {
82+
if (!i.equals(expected[i])) {
8383
return false;
8484
}
8585
}

database/src/main/java/com/firebase/ui/database/FirebaseIndexArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private int returnOrFindIndexForKey(int index, String key) {
146146
int keyIndex = 0;
147147

148148
while (dataIndex < dataCount && keyIndex < mKeySnapshots.size()) {
149-
String superKey = mKeySnapshots.getObject(keyIndex);
149+
String superKey = mKeySnapshots.get(keyIndex);
150150
if (key.equals(superKey)) {
151151
break;
152152
} else if (mDataSnapshots.get(dataIndex).getKey().equals(superKey)) {

database/src/main/java/com/firebase/ui/database/FirebaseListAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ public ObservableSnapshotArray<T> getSnapshots() {
8484

8585
@Override
8686
public T getItem(int position) {
87-
return mSnapshots.getObject(position);
87+
return mSnapshots.get(position);
8888
}
8989

9090
@Override
9191
public DatabaseReference getRef(int position) {
92-
return mSnapshots.get(position).getRef();
92+
return mSnapshots.getSnapshot(position).getRef();
9393
}
9494

9595
@Override
@@ -100,7 +100,7 @@ public int getCount() {
100100
@Override
101101
public long getItemId(int i) {
102102
// http://stackoverflow.com/questions/5100071/whats-the-purpose-of-item-ids-in-android-listview-adapter
103-
return mSnapshots.get(i).getKey().hashCode();
103+
return mSnapshots.getSnapshot(i).getKey().hashCode();
104104
}
105105

106106
@Override

0 commit comments

Comments
 (0)