Skip to content

Commit 200b673

Browse files
Merge branch 'OperatorDefaultIfEmpty' of github.com:akarnokd/RxJava into merge-prs
Conflicts: rxjava-core/src/main/java/rx/Observable.java
2 parents a3a7e8a + e404661 commit 200b673

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
@@ -49,7 +49,6 @@
4949
import rx.operators.OnSubscribeFromIterable;
5050
import rx.operators.OnSubscribeRange;
5151
import rx.operators.OperationDebounce;
52-
import rx.operators.OperationDefaultIfEmpty;
5352
import rx.operators.OperationDefer;
5453
import rx.operators.OperationDelay;
5554
import rx.operators.OperationDematerialize;
@@ -98,6 +97,7 @@
9897
import rx.operators.OperatorCast;
9998
import rx.operators.OperatorCombineLatest;
10099
import rx.operators.OperatorConcat;
100+
import rx.operators.OperatorDefaultIfEmpty;
101101
import rx.operators.OperatorDoOnEach;
102102
import rx.operators.OperatorElementAt;
103103
import rx.operators.OperatorFilter;
@@ -3469,7 +3469,7 @@ public final Observable<T> debounce(long timeout, TimeUnit unit, Scheduler sched
34693469
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229624.aspx">MSDN: Observable.DefaultIfEmpty</a>
34703470
*/
34713471
public final Observable<T> defaultIfEmpty(T defaultValue) {
3472-
return create(OperationDefaultIfEmpty.defaultIfEmpty(this, defaultValue));
3472+
return lift(new OperatorDefaultIfEmpty<T>(defaultValue));
34733473
}
34743474

34753475
/**

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)