Skip to content

Commit

Permalink
Convert SnapshotArray from List<S> to List<T> (firebase#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
SUPERCILEX authored and samtstern committed Sep 14, 2017
1 parent 0bea78b commit 9744199
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @param <T> the model object class.
*/
public abstract class BaseObservableSnapshotArray<S, E, L extends BaseChangeEventListener<S, E>, T>
extends AbstractList<S> {
extends AbstractList<T> {

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

@Override
public S get(int index) {
return getSnapshots().get(index);
public T get(int index) {
return mCachingParser.parseSnapshot(getSnapshot(index));
}

@Override
public int size() {
return getSnapshots().size();
}

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

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

// Catch up new listener to existing state
for (int i = 0; i < size(); i++) {
listener.onChildChanged(ChangeEventType.ADDED, get(i), i, -1);
listener.onChildChanged(ChangeEventType.ADDED, getSnapshot(i), i, -1);
}
if (mHasDataChanged) {
listener.onDataChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return mArray.size() == INITIAL_SIZE;
}
});
}

@After
public void tearDown() throws Exception {
public void tearDown() {
mArray.removeChangeEventListener(mListener);
mRef.getRoot().removeValue();
}
Expand All @@ -78,7 +78,7 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return mArray.size() == 4;
}
});
Expand All @@ -93,8 +93,8 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(3).getNumber() == 4;
public Boolean call() {
return mArray.get(3).getNumber() == 4;
}
});
}
Expand All @@ -108,9 +108,8 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(3).getNumber() == 3
&& mArray.getObject(0).getNumber() == 4;
public Boolean call() {
return mArray.get(3).getNumber() == 3 && mArray.get(0).getNumber() == 4;
}
});
}
Expand All @@ -120,14 +119,14 @@ public void testChangePriorities() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mArray.get(2).getRef().setPriority(0.5);
mArray.getSnapshot(2).getRef().setPriority(0.5);
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(0).getNumber() == 3
&& mArray.getObject(1).getNumber() == 1
&& mArray.getObject(2).getNumber() == 2;
public Boolean call() {
return mArray.get(0).getNumber() == 3
&& mArray.get(1).getNumber() == 1
&& mArray.get(2).getNumber() == 2;
//return isValuesEqual(mArray, new int[]{3, 1, 2});
}
});
Expand All @@ -145,8 +144,8 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(3).getNumber() == 5;
public Boolean call() {
return mArray.get(3).getNumber() == 5;
}
});

Expand All @@ -158,8 +157,8 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(3).getNumber() == 6;
public Boolean call() {
return mArray.get(3).getNumber() == 6;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return mArray.size() == INITIAL_SIZE;
}
});
}

@After
public void tearDown() throws Exception {
public void tearDown() {
mArray.removeChangeEventListener(mListener);
mRef.getRoot().removeValue();
}
Expand All @@ -75,7 +75,7 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return mArray.size() == 4;
}
});
Expand All @@ -90,8 +90,8 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(3).equals(4);
public Boolean call() {
return mArray.get(3).equals(4);
}
});
}
Expand All @@ -105,9 +105,8 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(3).equals(3)
&& mArray.getObject(0).equals(4);
public Boolean call() {
return mArray.get(3).equals(3) && mArray.get(0).equals(4);
}
});
}
Expand All @@ -117,11 +116,11 @@ public void testChangePriorityBackToFront() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mArray.get(2).getRef().setPriority(0.5);
mArray.getSnapshot(2).getRef().setPriority(0.5);
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return isValuesEqual(mArray, new int[]{3, 1, 2});
}
});
Expand All @@ -132,11 +131,11 @@ public void testChangePriorityFrontToBack() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mArray.get(0).getRef().setPriority(4);
mArray.getSnapshot(0).getRef().setPriority(4);
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return isValuesEqual(mArray, new int[]{2, 3, 1});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return mArray.size() == INITIAL_SIZE;
}
});
}

@After
public void tearDown() throws Exception {
public void tearDown() {
mArray.removeChangeEventListener(mListener);
mRef.getRoot().removeValue();
}
Expand All @@ -80,7 +80,7 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return mArray.size() == 4;
}
});
Expand All @@ -95,8 +95,8 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(3).getNumber() == 4;
public Boolean call() {
return mArray.get(3).getNumber() == 4;
}
});
}
Expand All @@ -110,9 +110,8 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(3).getNumber() == 3
&& mArray.getObject(0).getNumber() == 4;
public Boolean call() {
return mArray.get(3).getNumber() == 3 && mArray.get(0).getNumber() == 4;
}
});
}
Expand All @@ -122,14 +121,14 @@ public void testChangePriorities() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mKeyRef.child(mArray.get(2).getKey()).setPriority(0.5);
mKeyRef.child(mArray.getSnapshot(2).getKey()).setPriority(0.5);
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(0).getNumber() == 3
&& mArray.getObject(1).getNumber() == 1
&& mArray.getObject(2).getNumber() == 2;
public Boolean call() {
return mArray.get(0).getNumber() == 3
&& mArray.get(1).getNumber() == 1
&& mArray.get(2).getNumber() == 2;
//return isValuesEqual(mArray, new int[]{3, 1, 2});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return mArray.size() == INITIAL_SIZE;
}
});
}

@After
public void tearDown() throws Exception {
public void tearDown() {
mArray.removeChangeEventListener(mListener);
mRef.getRoot().removeValue();
}
Expand All @@ -81,7 +81,7 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return mArray.size() == 4;
}
});
Expand All @@ -96,8 +96,8 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(3).equals(4);
public Boolean call() {
return mArray.get(3).equals(4);
}
});
}
Expand All @@ -111,9 +111,8 @@ public void run() {
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getObject(3).equals(3)
&& mArray.getObject(0).equals(4);
public Boolean call() {
return mArray.get(3).equals(3) && mArray.get(0).equals(4);
}
});
}
Expand All @@ -123,11 +122,11 @@ public void testChangePriorities() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mKeyRef.child(mArray.get(2).getKey()).setPriority(0.5);
mKeyRef.child(mArray.getSnapshot(2).getKey()).setPriority(0.5);
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return isValuesEqual(mArray, new int[]{3, 1, 2});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public void onError(DatabaseError error) {

public static boolean isValuesEqual(ObservableSnapshotArray<Integer> array, int[] expected) {
if (array.size() != expected.length) return false;
for (int i = 0; i < array.size(); i++) {
if (!array.getObject(i).equals(expected[i])) {
for (Integer i : array) {
if (!i.equals(expected[i])) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private int returnOrFindIndexForKey(int index, String key) {
int keyIndex = 0;

while (dataIndex < dataCount && keyIndex < mKeySnapshots.size()) {
String superKey = mKeySnapshots.getObject(keyIndex);
String superKey = mKeySnapshots.get(keyIndex);
if (key.equals(superKey)) {
break;
} else if (mDataSnapshots.get(dataIndex).getKey().equals(superKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public ObservableSnapshotArray<T> getSnapshots() {

@Override
public T getItem(int position) {
return mSnapshots.getObject(position);
return mSnapshots.get(position);
}

@Override
public DatabaseReference getRef(int position) {
return mSnapshots.get(position).getRef();
return mSnapshots.getSnapshot(position).getRef();
}

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

@Override
Expand Down
Loading

0 comments on commit 9744199

Please sign in to comment.