Skip to content

Commit ce0302d

Browse files
committed
Cleanup, fix documentation typos, make preconditions return passed in object, change type name to E in ObservableSnapshotArray to unhide type T in toArray method, remove getSnapshots(int) method from FirebaseArray since there's already get(int), use index instead of newIndex in onKeyMove in FirebaseIndexArray
Signed-off-by: Alex Saveau <[email protected]>
1 parent a4d7ad1 commit ce0302d

12 files changed

+91
-97
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ public void run() {
109109
}, new Callable<Boolean>() {
110110
@Override
111111
public Boolean call() throws Exception {
112-
return mArray.getObject(3).getNumber() == 3 &&
113-
mArray.getObject(0).getNumber() == 4;
112+
return mArray.getObject(3).getNumber() == 3
113+
&& mArray.getObject(0).getNumber() == 4;
114114
}
115115
});
116116
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public void run() {
111111
}, new Callable<Boolean>() {
112112
@Override
113113
public Boolean call() throws Exception {
114-
return mArray.getObject(3).getNumber() == 3 &&
115-
mArray.getObject(0).getNumber() == 4;
114+
return mArray.getObject(3).getNumber() == 3
115+
&& mArray.getObject(0).getNumber() == 4;
116116
}
117117
});
118118
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99

1010
/**
1111
* An extension of {@link ObservableSnapshotArray} that caches the result of {@link #getObject(int)}
12-
* so that repeated calls for the same key are not expensive (unless the underlying snapshot
13-
* has changed).
12+
* so that repeated calls for the same key are not expensive (unless the underlying snapshot has
13+
* changed).
1414
*/
1515
public abstract class CachingObservableSnapshotArray<T> extends ObservableSnapshotArray<T> {
16-
1716
private Map<String, T> mObjectCache = new HashMap<>();
1817

1918
/**
@@ -69,6 +68,4 @@ protected void updateData(int index, DataSnapshot snapshot) {
6968
getSnapshots().set(index, snapshot);
7069
mObjectCache.remove(snapshot.getKey());
7170
}
72-
73-
7471
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.google.firebase.database.DatabaseError;
66

77
public interface ChangeEventListener {
8-
98
/**
109
* The type of event received when a child has been updated.
1110
*/

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66

77
/**
88
* A convenience implementation of {@link SnapshotParser} that converts a {@link DataSnapshot} to
9-
* the parameterized class via {@link DataSnapshot#getValue(Class)}.
9+
* the parametrized class via {@link DataSnapshot#getValue(Class)}.
10+
*
1011
* @param <T> the POJO class to create from snapshots.
1112
*/
1213
public class ClassSnapshotParser<T> implements SnapshotParser<T> {
13-
1414
private Class<T> mClass;
1515

1616
public ClassSnapshotParser(@NonNull Class<T> clazz) {
17-
Preconditions.checkNotNull(clazz);
18-
mClass = clazz;
17+
mClass = Preconditions.checkNotNull(clazz);
1918
}
2019

2120
@Override

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
88
interface FirebaseAdapter<T> extends ChangeEventListener, SnapshotParser<T> {
9-
109
/**
1110
* If you need to do some setup before the adapter starts listening for change events in the
1211
* database, do so it here and then call {@code super.startListening()}.

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929
* This class implements a collection on top of a Firebase location.
3030
*/
3131
public class FirebaseArray<T> extends CachingObservableSnapshotArray<T> implements ChildEventListener, ValueEventListener {
32-
3332
private Query mQuery;
3433
private List<DataSnapshot> mSnapshots = new ArrayList<>();
3534

3635
/**
37-
* Create a new FirebaseArray with no {@link SnapshotParser}. Calls to
38-
* {@link #getObject(int)} will fail.
36+
* Create a new FirebaseArray with no {@link SnapshotParser}. Calls to {@link #getObject(int)}
37+
* will fail.
3938
*
4039
* @param query The Firebase location to watch for data changes. Can also be a slice of a
4140
* location, using some combination of {@code limit()}, {@code startAt()}, and
@@ -49,8 +48,8 @@ public FirebaseArray(Query query) {
4948
/**
5049
* Create a new FirebaseArray that parses snapshots as members of a given class.
5150
*
52-
* See {@link ObservableSnapshotArray#ObservableSnapshotArray(Class)}.
53-
* See {@link #FirebaseArray(Query)}.
51+
* @see ObservableSnapshotArray#ObservableSnapshotArray(Class)
52+
* @see FirebaseArray#FirebaseArray(Query)
5453
*/
5554
public FirebaseArray(Query query, Class<T> tClass) {
5655
super(tClass);
@@ -60,8 +59,8 @@ public FirebaseArray(Query query, Class<T> tClass) {
6059
/**
6160
* Create a new FirebaseArray with a custom {@link SnapshotParser}.
6261
*
63-
* See {@link ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser)}.
64-
* See {@link #FirebaseArray(Query)}.
62+
* @see ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser)
63+
* @see FirebaseArray#FirebaseArray(Query)
6564
*/
6665
public FirebaseArray(Query query, SnapshotParser<T> parser) {
6766
super(parser);
@@ -140,8 +139,10 @@ public void onChildMoved(DataSnapshot snapshot, String previousChildKey) {
140139
int newIndex = previousChildKey == null ? 0 : (getIndexForKey(previousChildKey) + 1);
141140
mSnapshots.add(newIndex, snapshot);
142141

143-
notifyChangeEventListeners(ChangeEventListener.EventType.MOVED, snapshot,
144-
newIndex, oldIndex);
142+
notifyChangeEventListeners(ChangeEventListener.EventType.MOVED,
143+
snapshot,
144+
newIndex,
145+
oldIndex);
145146
}
146147

147148
@Override
@@ -166,10 +167,6 @@ private int getIndexForKey(String key) {
166167
throw new IllegalArgumentException("Key not found");
167168
}
168169

169-
public DataSnapshot getSnapshot(int index) {
170-
return mSnapshots.get(index);
171-
}
172-
173170
@Override
174171
public boolean equals(Object obj) {
175172
if (this == obj) return true;

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

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public FirebaseIndexArray(Query keyQuery, DatabaseReference dataRef) {
5656
/**
5757
* Create a new FirebaseIndexArray that parses snapshots as members of a given class.
5858
*
59-
* See {@link ObservableSnapshotArray#ObservableSnapshotArray(Class)}.
60-
* See {@link #FirebaseIndexArray(Query, DatabaseReference)}.
59+
* @see ObservableSnapshotArray#ObservableSnapshotArray(Class)
60+
* @see FirebaseIndexArray#FirebaseIndexArray(Query, DatabaseReference)
6161
*/
6262
public FirebaseIndexArray(Query keyQuery, DatabaseReference dataRef, Class<T> tClass) {
6363
super(tClass);
@@ -67,24 +67,23 @@ public FirebaseIndexArray(Query keyQuery, DatabaseReference dataRef, Class<T> tC
6767
/**
6868
* Create a new FirebaseIndexArray with a custom {@link SnapshotParser}.
6969
*
70-
* See {@link ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser)}.
71-
* See {@link #FirebaseIndexArray(Query, DatabaseReference)}.
70+
* @see ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser)
71+
* @see FirebaseIndexArray#FirebaseIndexArray(Query, DatabaseReference)
7272
*/
7373
public FirebaseIndexArray(Query keyQuery, DatabaseReference dataRef, SnapshotParser<T> parser) {
7474
super(parser);
7575
init(keyQuery, dataRef);
7676
}
7777

7878
private void init(Query keyQuery, DatabaseReference dataRef) {
79+
mDataRef = dataRef;
7980
mKeySnapshots = new FirebaseArray<>(keyQuery, new SnapshotParser<String>() {
8081
@Override
8182
public String parseSnapshot(DataSnapshot snapshot) {
8283
return snapshot.getKey();
8384
}
8485
});
8586

86-
mDataRef = dataRef;
87-
8887
mKeySnapshots.addChangeEventListener(this);
8988
}
9089

@@ -162,8 +161,7 @@ private boolean isKeyAtIndex(String key, int index) {
162161
}
163162

164163
protected void onKeyAdded(DataSnapshot data) {
165-
String key = data.getKey();
166-
Query ref = mDataRef.child(key);
164+
Query ref = mDataRef.child(data.getKey());
167165

168166
// Start listening
169167
mRefs.put(ref, ref.addValueEventListener(new DataRefListener()));
@@ -174,9 +172,11 @@ protected void onKeyMoved(DataSnapshot data, int index, int oldIndex) {
174172

175173
if (isKeyAtIndex(key, oldIndex)) {
176174
DataSnapshot snapshot = removeData(oldIndex);
177-
int newIndex = getIndexForKey(key);
178-
mDataSnapshots.add(newIndex, snapshot);
179-
notifyChangeEventListeners(ChangeEventListener.EventType.MOVED, snapshot, newIndex, oldIndex);
175+
mDataSnapshots.add(index, snapshot);
176+
notifyChangeEventListeners(ChangeEventListener.EventType.MOVED,
177+
snapshot,
178+
index,
179+
oldIndex);
180180
}
181181
}
182182

@@ -190,6 +190,34 @@ protected void onKeyRemoved(DataSnapshot data, int index) {
190190
}
191191
}
192192

193+
@Override
194+
public boolean equals(Object obj) {
195+
if (this == obj) return true;
196+
if (obj == null || getClass() != obj.getClass()) return false;
197+
if (!super.equals(obj)) return false;
198+
199+
FirebaseIndexArray array = (FirebaseIndexArray) obj;
200+
201+
return mDataRef.equals(array.mDataRef) && mDataSnapshots.equals(array.mDataSnapshots);
202+
}
203+
204+
@Override
205+
public int hashCode() {
206+
int result = super.hashCode();
207+
result = 31 * result + mDataRef.hashCode();
208+
result = 31 * result + mDataSnapshots.hashCode();
209+
return result;
210+
}
211+
212+
@Override
213+
public String toString() {
214+
if (isListening()) {
215+
return "FirebaseIndexArray is listening at " + mDataRef + ":\n" + mDataSnapshots;
216+
} else {
217+
return "FirebaseIndexArray is inactive";
218+
}
219+
}
220+
193221
/**
194222
* A ValueEventListener attached to the joined child data.
195223
*/
@@ -226,33 +254,4 @@ public void onCancelled(DatabaseError error) {
226254
notifyListenersOnCancelled(error);
227255
}
228256
}
229-
230-
@Override
231-
public boolean equals(Object obj) {
232-
if (this == obj) return true;
233-
if (obj == null || getClass() != obj.getClass()) return false;
234-
if (!super.equals(obj)) return false;
235-
236-
FirebaseIndexArray array = (FirebaseIndexArray) obj;
237-
238-
return mDataRef.equals(array.mDataRef) && mDataSnapshots.equals(array.mDataSnapshots);
239-
}
240-
241-
@Override
242-
public int hashCode() {
243-
int result = super.hashCode();
244-
result = 31 * result + mDataRef.hashCode();
245-
result = 31 * result + mDataSnapshots.hashCode();
246-
return result;
247-
}
248-
249-
@Override
250-
public String toString() {
251-
if (isListening()) {
252-
return "FirebaseIndexArray is listening at " + mDataRef + ":\n" + mDataSnapshots;
253-
} else {
254-
return "FirebaseIndexArray is inactive";
255-
}
256-
}
257-
258257
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public FirebaseListAdapter(Activity activity,
4545
ObservableSnapshotArray<T> snapshots,
4646
Class<T> modelClass,
4747
@LayoutRes int modelLayout) {
48-
4948
init(activity, snapshots, modelClass, modelLayout);
5049
}
5150

@@ -59,12 +58,13 @@ public FirebaseListAdapter(Activity activity,
5958
Class<T> modelClass,
6059
@LayoutRes int modelLayout,
6160
Query query) {
62-
63-
init(activity, new FirebaseArray<T>(query, this), modelClass, modelLayout);
61+
init(activity, new FirebaseArray<>(query, this), modelClass, modelLayout);
6462
}
6563

66-
private void init(Activity activity, ObservableSnapshotArray<T> snapshots,
67-
Class<T> modelClass, @LayoutRes int modelLayout) {
64+
private void init(Activity activity,
65+
ObservableSnapshotArray<T> snapshots,
66+
Class<T> modelClass,
67+
@LayoutRes int modelLayout) {
6868
mActivity = activity;
6969
mSnapshots = snapshots;
7070
mModelClass = modelClass;
@@ -86,8 +86,10 @@ public void cleanup() {
8686
}
8787

8888
@Override
89-
public void onChildChanged(ChangeEventListener.EventType type, DataSnapshot snapshot,
90-
int index, int oldIndex) {
89+
public void onChildChanged(ChangeEventListener.EventType type,
90+
DataSnapshot snapshot,
91+
int index,
92+
int oldIndex) {
9193
notifyDataSetChanged();
9294
}
9395

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030
public abstract class FirebaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder>
3131
extends RecyclerView.Adapter<VH> implements FirebaseAdapter<T> {
32-
3332
private static final String TAG = "FirebaseRecyclerAdapter";
3433

3534
protected ObservableSnapshotArray<T> mSnapshots;
@@ -51,7 +50,6 @@ public FirebaseRecyclerAdapter(ObservableSnapshotArray<T> snapshots,
5150
Class<T> modelClass,
5251
@LayoutRes int modelLayout,
5352
Class<VH> viewHolderClass) {
54-
5553
init(snapshots, modelClass, modelLayout, viewHolderClass);
5654
}
5755

@@ -65,12 +63,13 @@ public FirebaseRecyclerAdapter(Class<T> modelClass,
6563
@LayoutRes int modelLayout,
6664
Class<VH> viewHolderClass,
6765
Query query) {
68-
6966
init(new FirebaseArray<>(query, this), modelClass, modelLayout, viewHolderClass);
7067
}
7168

72-
private void init(ObservableSnapshotArray<T> snapshots, Class<T> modelClass,
73-
@LayoutRes int modelLayout, Class<VH> viewHolderClass) {
69+
private void init(ObservableSnapshotArray<T> snapshots,
70+
Class<T> modelClass,
71+
@LayoutRes int modelLayout,
72+
Class<VH> viewHolderClass) {
7473
mSnapshots = snapshots;
7574
mModelClass = modelClass;
7675
mViewHolderClass = viewHolderClass;
@@ -92,8 +91,10 @@ public void cleanup() {
9291
}
9392

9493
@Override
95-
public void onChildChanged(ChangeEventListener.EventType type, DataSnapshot snapshot,
96-
int index, int oldIndex) {
94+
public void onChildChanged(ChangeEventListener.EventType type,
95+
DataSnapshot snapshot,
96+
int index,
97+
int oldIndex) {
9798
switch (type) {
9899
case ADDED:
99100
notifyItemInserted(index);

0 commit comments

Comments
 (0)