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

Reimplement the 'SkipLast' operator #1050

Merged
merged 4 commits into from
Apr 20, 2014
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
import rx.operators.OperationSequenceEqual;
import rx.operators.OperationSingle;
import rx.operators.OperationSkip;
import rx.operators.OperationSkipLast;
import rx.operators.OperationSkipUntil;
import rx.operators.OperationSum;
import rx.operators.OperationSwitch;
Expand Down Expand Up @@ -117,6 +116,8 @@
import rx.operators.OperatorScan;
import rx.operators.OperatorSerialize;
import rx.operators.OperatorSkip;
import rx.operators.OperatorSkipLast;
import rx.operators.OperatorSkipLastTimed;
import rx.operators.OperatorSkipWhile;
import rx.operators.OperatorSubscribeOn;
import rx.operators.OperatorSynchronize;
Expand Down Expand Up @@ -6043,7 +6044,7 @@ public final Observable<T> skip(long time, TimeUnit unit, Scheduler scheduler) {
* @see <a href="http://msdn.microsoft.com/en-us/library/hh211750.aspx">MSDN: Observable.SkipLast</a>
*/
public final Observable<T> skipLast(int count) {
return create(OperationSkipLast.skipLast(this, count));
return lift(new OperatorSkipLast<T>(count));
}

/**
Expand Down Expand Up @@ -6083,7 +6084,7 @@ public final Observable<T> skipLast(long time, TimeUnit unit) {
* @see <a href="http://msdn.microsoft.com/en-us/library/hh211750.aspx">MSDN: Observable.SkipLast</a>
*/
public final Observable<T> skipLast(long time, TimeUnit unit, Scheduler scheduler) {
return create(new OperationSkipLast.SkipLastTimed<T>(this, time, unit, scheduler));
return lift(new OperatorSkipLastTimed<T>(time, unit, scheduler));
}

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

This file was deleted.

77 changes: 77 additions & 0 deletions rxjava-core/src/main/java/rx/operators/OperatorSkipLast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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 java.util.ArrayDeque;
import java.util.Deque;

import rx.Observable.Operator;
import rx.Subscriber;

/**
* Bypasses a specified number of elements at the end of an observable sequence.
*/
public class OperatorSkipLast<T> implements Operator<T, T> {

private final int count;

public OperatorSkipLast(int count) {
if (count < 0) {
throw new IndexOutOfBoundsException("count could not be negative");
}
this.count = count;
}

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

private final NotificationLite<T> on = NotificationLite.instance();

/**
* Store the last count elements until now.
*/
private final Deque<Object> deque = new ArrayDeque<Object>();

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

@Override
public void onError(Throwable e) {
subscriber.onError(e);
}

@Override
public void onNext(T value) {
if (count == 0) {
// If count == 0, we do not need to put value into deque
// and remove it at once. We can emit the value
// directly.
subscriber.onNext(value);
return;
}
if (deque.size() == count) {
subscriber.onNext(on.getValue(deque.removeFirst()));
}
deque.offerLast(on.next(value));
}

};
}

}
Loading