Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: Emit an IllegalArgumentException instead of ArithmeticException if the observable is empty #480

Merged
merged 1 commit into from
Nov 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3593,14 +3593,16 @@ public static Observable<Double> sumDoubles(Observable<Double> source) {

/**
* Returns an Observable that computes the average of all elements in the source Observable.
* For an empty source, it causes an ArithmeticException.
* For an empty source, it causes an IllegalArgumentException.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/average.png">
*
* @param source
* Source observable to compute the average of.
* @return an Observable emitting the averageof all the elements of the source Observable
* as its single item.
* @throws IllegalArgumentException
* if Observable sequence is empty.
* @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.average%28v=vs.103%29.aspx">MSDN: Observable.Average</a>
*/
public static Observable<Integer> average(Observable<Integer> source) {
Expand Down
14 changes: 10 additions & 4 deletions rxjava-core/src/main/java/rx/operators/OperationAverage.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public Tuple2<Integer> call(Tuple2<Integer> accu, Integer next) {
}).map(new Func1<Tuple2<Integer>, Integer>() {
@Override
public Integer call(Tuple2<Integer> result) {
return result.current / result.count; // may throw DivisionByZero, this should be correct...
if (result.count == 0) {
throw new IllegalArgumentException("Sequence contains no elements");
}
return result.current / result.count;
}
});
}
Expand All @@ -58,7 +61,10 @@ public Tuple2<Long> call(Tuple2<Long> accu, Long next) {
}).map(new Func1<Tuple2<Long>, Long>() {
@Override
public Long call(Tuple2<Long> result) {
return result.current / result.count; // may throw DivisionByZero, this should be correct...
if (result.count == 0) {
throw new IllegalArgumentException("Sequence contains no elements");
}
return result.current / result.count;
}
});
}
Expand All @@ -73,7 +79,7 @@ public Tuple2<Float> call(Tuple2<Float> accu, Float next) {
@Override
public Float call(Tuple2<Float> result) {
if (result.count == 0) {
throw new ArithmeticException("divide by zero");
throw new IllegalArgumentException("Sequence contains no elements");
}
return result.current / result.count;
}
Expand All @@ -90,7 +96,7 @@ public Tuple2<Double> call(Tuple2<Double> accu, Double next) {
@Override
public Double call(Tuple2<Double> result) {
if (result.count == 0) {
throw new ArithmeticException("divide by zero");
throw new IllegalArgumentException("Sequence contains no elements");
}
return result.current / result.count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void testEmptyAverage() throws Throwable {
average(src).subscribe(w);

verify(w, never()).onNext(anyInt());
verify(w, times(1)).onError(any(ArithmeticException.class));
verify(w, times(1)).onError(isA(IllegalArgumentException.class));
verify(w, never()).onCompleted();
}

Expand All @@ -73,7 +73,7 @@ public void testEmptyAverageLongs() throws Throwable {
averageLongs(src).subscribe(wl);

verify(wl, never()).onNext(anyLong());
verify(wl, times(1)).onError(any(ArithmeticException.class));
verify(wl, times(1)).onError(isA(IllegalArgumentException.class));
verify(wl, never()).onCompleted();
}

Expand All @@ -94,7 +94,7 @@ public void testEmptyAverageFloats() throws Throwable {
averageFloats(src).subscribe(wf);

verify(wf, never()).onNext(anyFloat());
verify(wf, times(1)).onError(any(ArithmeticException.class));
verify(wf, times(1)).onError(isA(IllegalArgumentException.class));
verify(wf, never()).onCompleted();
}

Expand All @@ -115,7 +115,7 @@ public void testEmptyAverageDoubles() throws Throwable {
averageDoubles(src).subscribe(wd);

verify(wd, never()).onNext(anyDouble());
verify(wd, times(1)).onError(any(ArithmeticException.class));
verify(wd, times(1)).onError(isA(IllegalArgumentException.class));
verify(wd, never()).onCompleted();
}
}