Skip to content

Commit

Permalink
Merge pull request ReactiveX#480 from zsxwing/issue-423-average
Browse files Browse the repository at this point in the history
BugFix: Emit an IllegalArgumentException instead of ArithmeticException if the observable is empty
  • Loading branch information
benjchristensen committed Nov 12, 2013
2 parents 3e50245 + 0a31cc9 commit 7dc0d4c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
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 @@ -3596,14 +3596,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();
}
}

0 comments on commit 7dc0d4c

Please sign in to comment.