@Test public void timerAccuratelyTicksTheSeconds() throws Exception { TimeWatcher timeWatcher = new TimeWatcher(); stopwatch.start().subscribe(timeWatcher::onNext); tick(); assertThat(timeWatcher.getCurrentTime(), is(1L)); tick(); assertThat(timeWatcher.getCurrentTime(), is(2L)); tick(); assertThat(timeWatcher.getCurrentTime(), is(3L)); tick(); assertThat(timeWatcher.getCurrentTime(), is(4L)); tick(); assertThat(timeWatcher.getCurrentTime(), is(5L)); }
@NonNull @Override public Observable<Long> start() { if (timerObservable != null) { LogUtil.d(TAG, "start(). Returning original Observable"); return timerObservable; } LogUtil.d(TAG, "start(). Returning new Observable"); startTime = timeProvider.now(); timerObservable = Observable.interval(1, TIME_UNIT, scheduler) .onBackpressureDrop() .takeUntil(stop) .filter(ignored -> isNotPaused()) .map(ignored -> timeProvider.now() - startTime); return timerObservable; }
StopwatchImpl extends AbstractStopwatch { @NonNull @Override public Observable<Long> start() { if (timerObservable != null) { LogUtil.d(TAG, "start(). Returning original Observable"); return timerObservable; } LogUtil.d(TAG, "start(). Returning new Observable"); startTime = timeProvider.now(); timerObservable = Observable.interval(1, TIME_UNIT, scheduler) .onBackpressureDrop() .takeUntil(stop) .filter(ignored -> isNotPaused()) .map(ignored -> timeProvider.now() - startTime); return timerObservable; } }
StopwatchImpl extends AbstractStopwatch { @NonNull @Override public Observable<Long> start() { if (timerObservable != null) { LogUtil.d(TAG, "start(). Returning original Observable"); return timerObservable; } LogUtil.d(TAG, "start(). Returning new Observable"); startTime = timeProvider.now(); timerObservable = Observable.interval(1, TIME_UNIT, scheduler) .onBackpressureDrop() .takeUntil(stop) .filter(ignored -> isNotPaused()) .map(ignored -> timeProvider.now() - startTime); return timerObservable; } @Inject  StopwatchImpl(@NonNull TimeProvider timeProvider); }
StopwatchImpl extends AbstractStopwatch { @NonNull @Override public Observable<Long> start() { if (timerObservable != null) { LogUtil.d(TAG, "start(). Returning original Observable"); return timerObservable; } LogUtil.d(TAG, "start(). Returning new Observable"); startTime = timeProvider.now(); timerObservable = Observable.interval(1, TIME_UNIT, scheduler) .onBackpressureDrop() .takeUntil(stop) .filter(ignored -> isNotPaused()) .map(ignored -> timeProvider.now() - startTime); return timerObservable; } @Inject  StopwatchImpl(@NonNull TimeProvider timeProvider); @NonNull @Override Observable<Long> start(); @Override void togglePause(); @Override boolean isPaused(); @Override void reset(); @NonNull @Override Lap lap(); }
StopwatchImpl extends AbstractStopwatch { @NonNull @Override public Observable<Long> start() { if (timerObservable != null) { LogUtil.d(TAG, "start(). Returning original Observable"); return timerObservable; } LogUtil.d(TAG, "start(). Returning new Observable"); startTime = timeProvider.now(); timerObservable = Observable.interval(1, TIME_UNIT, scheduler) .onBackpressureDrop() .takeUntil(stop) .filter(ignored -> isNotPaused()) .map(ignored -> timeProvider.now() - startTime); return timerObservable; } @Inject  StopwatchImpl(@NonNull TimeProvider timeProvider); @NonNull @Override Observable<Long> start(); @Override void togglePause(); @Override boolean isPaused(); @Override void reset(); @NonNull @Override Lap lap();  }