Skip to content

Commit 7601ded

Browse files
committed
Remove null parser possibility and fix broken FirebaseIndexAdapters
Signed-off-by: Alex Saveau <[email protected]>
1 parent ce0302d commit 7601ded

8 files changed

+45
-61
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@ public abstract class CachingObservableSnapshotArray<T> extends ObservableSnapsh
1616
private Map<String, T> mObjectCache = new HashMap<>();
1717

1818
/**
19-
* See {@link ObservableSnapshotArray#ObservableSnapshotArray()}.
20-
*/
21-
public CachingObservableSnapshotArray() {
22-
super();
23-
}
24-
25-
/**
26-
* See {@link ObservableSnapshotArray#ObservableSnapshotArray(Class)}.
19+
* @see ObservableSnapshotArray#ObservableSnapshotArray(Class)
2720
*/
2821
public CachingObservableSnapshotArray(@NonNull Class<T> tClass) {
2922
super(tClass);
3023
}
3124

3225
/**
33-
* See {@link ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser)}.
26+
* @see ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser)
3427
*/
3528
public CachingObservableSnapshotArray(@NonNull SnapshotParser<T> parser) {
3629
super(parser);

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,12 @@ public class FirebaseArray<T> extends CachingObservableSnapshotArray<T> implemen
3333
private List<DataSnapshot> mSnapshots = new ArrayList<>();
3434

3535
/**
36-
* Create a new FirebaseArray with no {@link SnapshotParser}. Calls to {@link #getObject(int)}
37-
* will fail.
36+
* Create a new FirebaseArray that parses snapshots as members of a given class.
3837
*
3938
* @param query The Firebase location to watch for data changes. Can also be a slice of a
4039
* location, using some combination of {@code limit()}, {@code startAt()}, and
4140
* {@code endAt()}.
42-
*/
43-
public FirebaseArray(Query query) {
44-
super();
45-
init(query);
46-
}
47-
48-
/**
49-
* Create a new FirebaseArray that parses snapshots as members of a given class.
50-
*
5141
* @see ObservableSnapshotArray#ObservableSnapshotArray(Class)
52-
* @see FirebaseArray#FirebaseArray(Query)
5342
*/
5443
public FirebaseArray(Query query, Class<T> tClass) {
5544
super(tClass);
@@ -60,7 +49,7 @@ public FirebaseArray(Query query, Class<T> tClass) {
6049
* Create a new FirebaseArray with a custom {@link SnapshotParser}.
6150
*
6251
* @see ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser)
63-
* @see FirebaseArray#FirebaseArray(Query)
52+
* @see FirebaseArray#FirebaseArray(Query, Class)
6453
*/
6554
public FirebaseArray(Query query, SnapshotParser<T> parser) {
6655
super(parser);

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,14 @@ public class FirebaseIndexArray<T> extends CachingObservableSnapshotArray<T> imp
3939
private List<DataSnapshot> mDataSnapshots = new ArrayList<>();
4040

4141
/**
42-
* Create a new FirebaseIndexArray without a {@link SnapshotParser}.
43-
* Calls to {@link #getObject(int)} will fail.
42+
* Create a new FirebaseIndexArray that parses snapshots as members of a given class.
4443
*
4544
* @param keyQuery The Firebase location containing the list of keys to be found in {@code
4645
* dataRef}. Can also be a slice of a location, using some combination of {@code
4746
* limit()}, {@code startAt()}, and {@code endAt()}.
4847
* @param dataRef The Firebase location to watch for data changes. Each key key found at {@code
4948
* keyQuery}'s location represents a list item in the {@link RecyclerView}.
50-
*/
51-
public FirebaseIndexArray(Query keyQuery, DatabaseReference dataRef) {
52-
super();
53-
init(keyQuery, dataRef);
54-
}
55-
56-
/**
57-
* Create a new FirebaseIndexArray that parses snapshots as members of a given class.
58-
*
5949
* @see ObservableSnapshotArray#ObservableSnapshotArray(Class)
60-
* @see FirebaseIndexArray#FirebaseIndexArray(Query, DatabaseReference)
6150
*/
6251
public FirebaseIndexArray(Query keyQuery, DatabaseReference dataRef, Class<T> tClass) {
6352
super(tClass);
@@ -68,7 +57,7 @@ public FirebaseIndexArray(Query keyQuery, DatabaseReference dataRef, Class<T> tC
6857
* Create a new FirebaseIndexArray with a custom {@link SnapshotParser}.
6958
*
7059
* @see ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser)
71-
* @see FirebaseIndexArray#FirebaseIndexArray(Query, DatabaseReference)
60+
* @see FirebaseIndexArray#FirebaseIndexArray(Query, DatabaseReference, Class)
7261
*/
7362
public FirebaseIndexArray(Query keyQuery, DatabaseReference dataRef, SnapshotParser<T> parser) {
7463
super(parser);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public abstract class FirebaseIndexListAdapter<T> extends FirebaseListAdapter<T>
1313
* limit()}, {@code startAt()}, and {@code endAt()}.
1414
* @param dataRef The Firebase location to watch for data changes. Each key key found in {@code
1515
* keyQuery}'s location represents a list item in the {@code ListView}.
16-
* @see FirebaseListAdapter#FirebaseListAdapter(Activity, FirebaseArray, Class, int)
16+
* @see FirebaseListAdapter#FirebaseListAdapter(Activity, ObservableSnapshotArray, Class, int)
1717
*/
1818
public FirebaseIndexListAdapter(Activity activity,
1919
Class<T> modelClass,
2020
@LayoutRes int modelLayout,
2121
Query keyQuery,
2222
DatabaseReference dataRef) {
23-
super(activity, new FirebaseIndexArray(keyQuery, dataRef), modelClass, modelLayout);
23+
init(activity, new FirebaseIndexArray<>(keyQuery, dataRef, this), modelClass, modelLayout);
2424
}
2525
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ public abstract class FirebaseIndexRecyclerAdapter<T, VH extends RecyclerView.Vi
1414
* limit()}, {@code startAt()}, and {@code endAt()}.
1515
* @param dataRef The Firebase location to watch for data changes. Each key key found at {@code
1616
* keyQuery}'s location represents a list item in the {@link RecyclerView}.
17-
* @see FirebaseRecyclerAdapter#FirebaseRecyclerAdapter(FirebaseArray, Class, int, Class)
17+
* @see FirebaseRecyclerAdapter#FirebaseRecyclerAdapter(ObservableSnapshotArray, Class, int,
18+
* Class)
1819
*/
1920
public FirebaseIndexRecyclerAdapter(Class<T> modelClass,
2021
@LayoutRes int modelLayout,
2122
Class<VH> viewHolderClass,
2223
Query keyQuery,
2324
DatabaseReference dataRef) {
24-
super(new FirebaseIndexArray(keyQuery, dataRef), modelClass, modelLayout, viewHolderClass);
25+
init(new FirebaseIndexArray<>(keyQuery, dataRef, this),
26+
modelClass,
27+
modelLayout,
28+
viewHolderClass);
2529
}
2630
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public abstract class FirebaseListAdapter<T> extends BaseAdapter implements Fire
3232
protected Class<T> mModelClass;
3333
protected int mLayout;
3434

35+
/**
36+
* Internal constructor that does nothing. Can be used as a workaround to pass `this` into a
37+
* constructor argument.
38+
*
39+
* @see #init(Activity, ObservableSnapshotArray, Class, int)
40+
*/
41+
protected FirebaseListAdapter() {
42+
}
43+
3544
/**
3645
* @param activity The {@link Activity} containing the {@link ListView}
3746
* @param modelClass Firebase will marshall the data at a location into an instance of a class
@@ -61,10 +70,10 @@ public FirebaseListAdapter(Activity activity,
6170
init(activity, new FirebaseArray<>(query, this), modelClass, modelLayout);
6271
}
6372

64-
private void init(Activity activity,
65-
ObservableSnapshotArray<T> snapshots,
66-
Class<T> modelClass,
67-
@LayoutRes int modelLayout) {
73+
protected void init(Activity activity,
74+
ObservableSnapshotArray<T> snapshots,
75+
Class<T> modelClass,
76+
@LayoutRes int modelLayout) {
6877
mActivity = activity;
6978
mSnapshots = snapshots;
7079
mModelClass = modelClass;

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public abstract class FirebaseRecyclerAdapter<T, VH extends RecyclerView.ViewHol
3636
protected Class<VH> mViewHolderClass;
3737
protected int mModelLayout;
3838

39+
/**
40+
* Internal constructor that does nothing. Can be used as a workaround to pass `this` into a
41+
* constructor argument.
42+
*
43+
* @see #init(ObservableSnapshotArray, Class, int, Class)
44+
*/
45+
protected FirebaseRecyclerAdapter() {
46+
}
47+
3948
/**
4049
* @param snapshots The data used to populate the adapter
4150
* @param modelClass Firebase will marshall the data at a location into an instance of a
@@ -66,10 +75,10 @@ public FirebaseRecyclerAdapter(Class<T> modelClass,
6675
init(new FirebaseArray<>(query, this), modelClass, modelLayout, viewHolderClass);
6776
}
6877

69-
private void init(ObservableSnapshotArray<T> snapshots,
70-
Class<T> modelClass,
71-
@LayoutRes int modelLayout,
72-
Class<VH> viewHolderClass) {
78+
protected void init(ObservableSnapshotArray<T> snapshots,
79+
Class<T> modelClass,
80+
@LayoutRes int modelLayout,
81+
Class<VH> viewHolderClass) {
7382
mSnapshots = snapshots;
7483
mModelClass = modelClass;
7584
mViewHolderClass = viewHolderClass;

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,24 @@ public abstract class ObservableSnapshotArray<E> extends ImmutableList<DataSnaps
2323
protected final List<ChangeEventListener> mListeners = new CopyOnWriteArrayList<>();
2424
protected final SnapshotParser<E> mParser;
2525

26-
/**
27-
* Create an ObservableSnapshotArray without a {@link SnapshotParser}. Calls to
28-
* {@link #getObject(int)} will fail.
29-
*/
30-
public ObservableSnapshotArray() {
31-
mParser = null;
32-
}
33-
3426
/**
3527
* Create an ObservableSnapshotArray where snapshots are parsed as objects of a particular
3628
* class.
3729
*
38-
* @param tClass the class as which DataSnapshots should be parsed.
30+
* @param clazz the class as which DataSnapshots should be parsed.
3931
* @see ClassSnapshotParser
4032
*/
41-
public ObservableSnapshotArray(@NonNull Class<E> tClass) {
42-
this(new ClassSnapshotParser<>(tClass));
33+
public ObservableSnapshotArray(@NonNull Class<E> clazz) {
34+
this(new ClassSnapshotParser<>(clazz));
4335
}
4436

4537
/**
4638
* Create an ObservableSnapshotArray with a custom {@link SnapshotParser}.
4739
*
48-
* @param parser the {@link SnapshotParser to use}.
40+
* @param parser the {@link SnapshotParser} to use
4941
*/
5042
public ObservableSnapshotArray(@NonNull SnapshotParser<E> parser) {
51-
mParser = parser;
43+
mParser = Preconditions.checkNotNull(parser);
5244
}
5345

5446
/**
@@ -138,7 +130,6 @@ public final boolean isListening(ChangeEventListener listener) {
138130
* initialized this will throw an unchecked exception.
139131
*/
140132
public E getObject(int index) {
141-
Preconditions.checkNotNull(mParser);
142133
return mParser.parseSnapshot(get(index));
143134
}
144135

0 commit comments

Comments
 (0)