Skip to content

Commit

Permalink
OperatorTimeInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
zsxwing committed Apr 27, 2014
1 parent 95e0636 commit 56fde61
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 88 deletions.
6 changes: 3 additions & 3 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
import rx.operators.OperationTakeUntil;
import rx.operators.OperationTakeWhile;
import rx.operators.OperationThrottleFirst;
import rx.operators.OperationTimeInterval;
import rx.operators.OperatorTimeInterval;
import rx.operators.OperationTimer;
import rx.operators.OperationToMap;
import rx.operators.OperationToMultimap;
Expand Down Expand Up @@ -6880,7 +6880,7 @@ public final Observable<T> throttleWithTimeout(long timeout, TimeUnit unit, Sche
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212107.aspx">MSDN: Observable.TimeInterval</a>
*/
public final Observable<TimeInterval<T>> timeInterval() {
return create(OperationTimeInterval.timeInterval(this));
return lift(new OperatorTimeInterval(Schedulers.immediate()));
}

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

/**
Expand Down
84 changes: 0 additions & 84 deletions rxjava-core/src/main/java/rx/operators/OperationTimeInterval.java

This file was deleted.

60 changes: 60 additions & 0 deletions rxjava-core/src/main/java/rx/operators/OperatorTimeInterval.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;

import rx.Observable.Operator;
import rx.Scheduler;
import rx.Subscriber;
import rx.schedulers.TimeInterval;

/**
* Records the time interval between consecutive elements in an observable sequence.
*/
public final class OperatorTimeInterval<T> implements Operator<TimeInterval<T>, T> {

private final Scheduler scheduler;

public OperatorTimeInterval(Scheduler scheduler) {
this.scheduler = scheduler;
}

@Override
public Subscriber<? super T> call(final Subscriber<? super TimeInterval<T>> subscriber) {
return new Subscriber<T>(subscriber) {

// The beginning time is the time when the observer subscribes.
private long lastTimestamp = scheduler.now();


@Override
public void onNext(T args) {
long nowTimestamp = scheduler.now();
subscriber.onNext(new TimeInterval<T>(nowTimestamp - lastTimestamp, args));
lastTimestamp = nowTimestamp;
}

@Override
public void onCompleted() {
subscriber.onCompleted();
}

@Override
public void onError(Throwable e) {
subscriber.onError(e);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import rx.schedulers.TimeInterval;
import rx.subjects.PublishSubject;

public class OperationTimeIntervalTest {
public class OperatorTimeIntervalTest {

private static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;

Expand Down

0 comments on commit 56fde61

Please sign in to comment.