Skip to content

Commit 988b2f6

Browse files
committed
Fixed issue #595 about null in toList operator
1 parent a252dca commit 988b2f6

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

rxjava-core/src/main/java/rx/operators/OperationToObservableList.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ public ToObservableList(Observable<? extends T> that) {
5555
public Subscription onSubscribe(final Observer<? super List<T>> observer) {
5656

5757
return that.subscribe(new Observer<T>() {
58-
final ConcurrentLinkedQueue<T> list = new ConcurrentLinkedQueue<T>();
58+
final List<T> list = new ArrayList<T>();
5959

6060
public void onNext(T value) {
61-
// onNext can be concurrently executed so list must be thread-safe
6261
list.add(value);
6362
}
6463

@@ -68,16 +67,10 @@ public void onError(Throwable ex) {
6867

6968
public void onCompleted() {
7069
try {
71-
// copy from LinkedQueue to List since ConcurrentLinkedQueue does not implement the List interface
72-
ArrayList<T> l = new ArrayList<T>(list.size());
73-
for (T t : list) {
74-
l.add(t);
75-
}
76-
7770
// benjchristensen => I want to make this list immutable but some clients are sorting this
7871
// instead of using toSortedList() and this change breaks them until we migrate their code.
7972
// observer.onNext(Collections.unmodifiableList(l));
80-
observer.onNext(l);
73+
observer.onNext(new ArrayList<T>(list));
8174
observer.onCompleted();
8275
} catch (Throwable e) {
8376
onError(e);

rxjava-core/src/test/java/rx/operators/OperationToObservableListTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,17 @@ public void testListMultipleObservers() {
6666
verify(o2, Mockito.never()).onError(any(Throwable.class));
6767
verify(o2, times(1)).onCompleted();
6868
}
69+
70+
@Test
71+
public void testListWithNullValue() {
72+
Observable<String> w = Observable.from("one", null, "three");
73+
Observable<List<String>> observable = Observable.create(toObservableList(w));
74+
75+
@SuppressWarnings("unchecked")
76+
Observer<List<String>> aObserver = mock(Observer.class);
77+
observable.subscribe(aObserver);
78+
verify(aObserver, times(1)).onNext(Arrays.asList("one", null, "three"));
79+
verify(aObserver, Mockito.never()).onError(any(Throwable.class));
80+
verify(aObserver, times(1)).onCompleted();
81+
}
6982
}

0 commit comments

Comments
 (0)