Skip to content

Commit 4495f14

Browse files
Merge branch 'time-interval' of git://github.com/zsxwing/RxJava into merge-prs
Conflicts: rxjava-core/src/main/java/rx/Observable.java
2 parents c7ff93b + 56fde61 commit 4495f14

File tree

4 files changed

+64
-88
lines changed

4 files changed

+64
-88
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import rx.operators.OperationTakeUntil;
6161
import rx.operators.OperationTakeWhile;
6262
import rx.operators.OperationThrottleFirst;
63-
import rx.operators.OperationTimeInterval;
6463
import rx.operators.OperationWindow;
6564
import rx.operators.OperatorAll;
6665
import rx.operators.OperatorAmb;
@@ -119,6 +118,7 @@
119118
import rx.operators.OperatorSkipWhile;
120119
import rx.operators.OperatorSubscribeOn;
121120
import rx.operators.OperatorTake;
121+
import rx.operators.OperatorTimeInterval;
122122
import rx.operators.OperatorTimeout;
123123
import rx.operators.OperatorTimeoutWithSelector;
124124
import rx.operators.OperatorTimerOnce;
@@ -6872,7 +6872,7 @@ public final Observable<T> throttleWithTimeout(long timeout, TimeUnit unit, Sche
68726872
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212107.aspx">MSDN: Observable.TimeInterval</a>
68736873
*/
68746874
public final Observable<TimeInterval<T>> timeInterval() {
6875-
return create(OperationTimeInterval.timeInterval(this));
6875+
return lift(new OperatorTimeInterval(Schedulers.immediate()));
68766876
}
68776877

68786878
/**
@@ -6888,7 +6888,7 @@ public final Observable<TimeInterval<T>> timeInterval() {
68886888
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212107.aspx">MSDN: Observable.TimeInterval</a>
68896889
*/
68906890
public final Observable<TimeInterval<T>> timeInterval(Scheduler scheduler) {
6891-
return create(OperationTimeInterval.timeInterval(this, scheduler));
6891+
return lift(new OperatorTimeInterval(scheduler));
68926892
}
68936893

68946894
/**

rxjava-core/src/main/java/rx/operators/OperationTimeInterval.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.operators;
17+
18+
import rx.Observable.Operator;
19+
import rx.Scheduler;
20+
import rx.Subscriber;
21+
import rx.schedulers.TimeInterval;
22+
23+
/**
24+
* Records the time interval between consecutive elements in an observable sequence.
25+
*/
26+
public final class OperatorTimeInterval<T> implements Operator<TimeInterval<T>, T> {
27+
28+
private final Scheduler scheduler;
29+
30+
public OperatorTimeInterval(Scheduler scheduler) {
31+
this.scheduler = scheduler;
32+
}
33+
34+
@Override
35+
public Subscriber<? super T> call(final Subscriber<? super TimeInterval<T>> subscriber) {
36+
return new Subscriber<T>(subscriber) {
37+
38+
// The beginning time is the time when the observer subscribes.
39+
private long lastTimestamp = scheduler.now();
40+
41+
42+
@Override
43+
public void onNext(T args) {
44+
long nowTimestamp = scheduler.now();
45+
subscriber.onNext(new TimeInterval<T>(nowTimestamp - lastTimestamp, args));
46+
lastTimestamp = nowTimestamp;
47+
}
48+
49+
@Override
50+
public void onCompleted() {
51+
subscriber.onCompleted();
52+
}
53+
54+
@Override
55+
public void onError(Throwable e) {
56+
subscriber.onError(e);
57+
}
58+
};
59+
}
60+
}

rxjava-core/src/test/java/rx/operators/OperationTimeIntervalTest.java renamed to rxjava-core/src/test/java/rx/operators/OperatorTimeIntervalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import rx.schedulers.TimeInterval;
3333
import rx.subjects.PublishSubject;
3434

35-
public class OperationTimeIntervalTest {
35+
public class OperatorTimeIntervalTest {
3636

3737
private static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;
3838

0 commit comments

Comments
 (0)