Skip to content

Commit e404661

Browse files
committed
OperatorDefaultIfEmpty
1 parent 4e77f8a commit e404661

File tree

5 files changed

+149
-159
lines changed

5 files changed

+149
-159
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import rx.operators.OperationCombineLatest;
5353
import rx.operators.OperationConcat;
5454
import rx.operators.OperationDebounce;
55-
import rx.operators.OperationDefaultIfEmpty;
5655
import rx.operators.OperationDefer;
5756
import rx.operators.OperationDelay;
5857
import rx.operators.OperationDematerialize;
@@ -95,6 +94,7 @@
9594
import rx.operators.OperatorAsObservable;
9695
import rx.operators.OperatorCache;
9796
import rx.operators.OperatorCast;
97+
import rx.operators.OperatorDefaultIfEmpty;
9898
import rx.operators.OperatorDoOnEach;
9999
import rx.operators.OperatorElementAt;
100100
import rx.operators.OperatorFilter;
@@ -3450,7 +3450,7 @@ public final Observable<T> debounce(long timeout, TimeUnit unit, Scheduler sched
34503450
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229624.aspx">MSDN: Observable.DefaultIfEmpty</a>
34513451
*/
34523452
public final Observable<T> defaultIfEmpty(T defaultValue) {
3453-
return create(OperationDefaultIfEmpty.defaultIfEmpty(this, defaultValue));
3453+
return lift(new OperatorDefaultIfEmpty<T>(defaultValue));
34543454
}
34553455

34563456
/**

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

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package rx.operators;
17+
18+
import rx.Observable.Operator;
19+
import rx.Subscriber;
20+
21+
/**
22+
* Returns the elements of the specified sequence or the specified default value
23+
* in a singleton sequence if the sequence is empty.
24+
* @param <T> the value type
25+
*/
26+
public class OperatorDefaultIfEmpty<T> implements Operator<T, T> {
27+
final T defaultValue;
28+
29+
public OperatorDefaultIfEmpty(T defaultValue) {
30+
this.defaultValue = defaultValue;
31+
}
32+
33+
@Override
34+
public Subscriber<? super T> call(final Subscriber<? super T> child) {
35+
return new Subscriber<T>(child) {
36+
boolean hasValue;
37+
@Override
38+
public void onNext(T t) {
39+
hasValue = true;
40+
child.onNext(t);
41+
}
42+
43+
@Override
44+
public void onError(Throwable e) {
45+
child.onError(e);
46+
}
47+
48+
@Override
49+
public void onCompleted() {
50+
if (!hasValue) {
51+
try {
52+
child.onNext(defaultValue);
53+
} catch (Throwable e) {
54+
child.onError(e);
55+
return;
56+
}
57+
}
58+
child.onCompleted();
59+
}
60+
61+
};
62+
}
63+
64+
}

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

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.operators;
17+
18+
import static org.mockito.Mockito.*;
19+
20+
import org.junit.Test;
21+
22+
import rx.Observable;
23+
import rx.Observer;
24+
import rx.Subscriber;
25+
26+
public class OperatorDefaultIfEmptyTest {
27+
28+
@Test
29+
public void testDefaultIfEmpty() {
30+
Observable<Integer> source = Observable.from(1, 2, 3);
31+
Observable<Integer> observable = source.defaultIfEmpty(10);
32+
33+
@SuppressWarnings("unchecked")
34+
Observer<Integer> observer = mock(Observer.class);
35+
observable.subscribe(observer);
36+
verify(observer, never()).onNext(10);
37+
verify(observer).onNext(1);
38+
verify(observer).onNext(2);
39+
verify(observer).onNext(3);
40+
verify(observer).onCompleted();
41+
verify(observer, never()).onError(any(Throwable.class));
42+
}
43+
44+
@Test
45+
public void testDefaultIfEmptyWithEmpty() {
46+
Observable<Integer> source = Observable.empty();
47+
Observable<Integer> observable = source.defaultIfEmpty(10);
48+
49+
@SuppressWarnings("unchecked")
50+
Observer<Integer> observer = mock(Observer.class);
51+
observable.subscribe(observer);
52+
53+
verify(observer).onNext(10);
54+
verify(observer).onCompleted();
55+
verify(observer, never()).onError(any(Throwable.class));
56+
}
57+
58+
@Test
59+
public void testEmptyButClientThrows() {
60+
final Observer<Integer> o = mock(Observer.class);
61+
62+
Observable.<Integer>empty().defaultIfEmpty(1).subscribe(new Subscriber<Integer>() {
63+
@Override
64+
public void onNext(Integer t) {
65+
throw new OperationReduceTest.CustomException();
66+
}
67+
68+
@Override
69+
public void onError(Throwable e) {
70+
o.onError(e);
71+
}
72+
73+
@Override
74+
public void onCompleted() {
75+
o.onCompleted();
76+
}
77+
});
78+
79+
verify(o).onError(any(OperationReduceTest.CustomException.class));
80+
verify(o, never()).onNext(any(Integer.class));
81+
verify(o, never()).onCompleted();
82+
}
83+
}

0 commit comments

Comments
 (0)