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

Use singleton reduction functions in count and countLong #2880

Merged
merged 1 commit into from
Apr 21, 2015
Merged
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
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