Skip to content

Commit 9fb5614

Browse files
committed
Merge pull request ReactiveX#2880 from davidmoten/reduce-optimization
Use singleton reduction functions in count and countLong
2 parents 491092d + d1e45ae commit 9fb5614

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/main/java/rx/Observable.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3630,12 +3630,16 @@ public final Boolean call(T t1) {
36303630
* @see #countLong()
36313631
*/
36323632
public final Observable<Integer> count() {
3633-
return reduce(0, new Func2<Integer, T, Integer>() {
3633+
return reduce(0, CountHolder.INSTANCE);
3634+
}
3635+
3636+
private static final class CountHolder {
3637+
static final Func2<Integer, Object, Integer> INSTANCE = new Func2<Integer, Object, Integer>() {
36343638
@Override
3635-
public final Integer call(Integer t1, T t2) {
3636-
return t1 + 1;
3639+
public final Integer call(Integer count, Object o) {
3640+
return count + 1;
36373641
}
3638-
});
3642+
};
36393643
}
36403644

36413645
/**
@@ -3657,14 +3661,18 @@ public final Integer call(Integer t1, T t2) {
36573661
* @see #count()
36583662
*/
36593663
public final Observable<Long> countLong() {
3660-
return reduce(0L, new Func2<Long, T, Long>() {
3664+
return reduce(0L, CountLongHolder.INSTANCE);
3665+
}
3666+
3667+
private static final class CountLongHolder {
3668+
static final Func2<Long, Object, Long> INSTANCE = new Func2<Long, Object, Long>() {
36613669
@Override
3662-
public final Long call(Long t1, T t2) {
3663-
return t1 + 1;
3670+
public final Long call(Long count, Object o) {
3671+
return count + 1;
36643672
}
3665-
});
3673+
};
36663674
}
3667-
3675+
36683676
/**
36693677
* Returns an Observable that mirrors the source Observable, except that it drops items emitted by the
36703678
* source Observable that are followed by another item within a computed debounce duration.

0 commit comments

Comments
 (0)