Skip to content

Commit

Permalink
Merge pull request #2880 from davidmoten/reduce-optimization
Browse files Browse the repository at this point in the history
Use singleton reduction functions in count and countLong
  • Loading branch information
zsxwing committed Apr 21, 2015
2 parents 491092d + d1e45ae commit 9fb5614
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3630,12 +3630,16 @@ public final Boolean call(T t1) {
* @see #countLong()
*/
public final Observable<Integer> count() {
return reduce(0, new Func2<Integer, T, Integer>() {
return reduce(0, CountHolder.INSTANCE);
}

private static final class CountHolder {
static final Func2<Integer, Object, Integer> INSTANCE = new Func2<Integer, Object, Integer>() {
@Override
public final Integer call(Integer t1, T t2) {
return t1 + 1;
public final Integer call(Integer count, Object o) {
return count + 1;
}
});
};
}

/**
Expand All @@ -3657,14 +3661,18 @@ public final Integer call(Integer t1, T t2) {
* @see #count()
*/
public final Observable<Long> countLong() {
return reduce(0L, new Func2<Long, T, Long>() {
return reduce(0L, CountLongHolder.INSTANCE);
}

private static final class CountLongHolder {
static final Func2<Long, Object, Long> INSTANCE = new Func2<Long, Object, Long>() {
@Override
public final Long call(Long t1, T t2) {
return t1 + 1;
public final Long call(Long count, Object o) {
return count + 1;
}
});
};
}

/**
* Returns an Observable that mirrors the source Observable, except that it drops items emitted by the
* source Observable that are followed by another item within a computed debounce duration.
Expand Down

0 comments on commit 9fb5614

Please sign in to comment.