Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableSkipLast.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.flowable;
15
16 import java.util.ArrayDeque;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
22
23 public final class FlowableSkipLast<T> extends AbstractFlowableWithUpstream<T, T> {
24 final int skip;
25
26 public FlowableSkipLast(Flowable<T> source, int skip) {
27 super(source);
28 this.skip = skip;
29 }
30
31 @Override
32 protected void subscribeActual(Subscriber<? super T> s) {
33 source.subscribe(new SkipLastSubscriber<>(s, skip));
34 }
35
36 static final class SkipLastSubscriber<T> extends ArrayDeque<T> implements FlowableSubscriber<T>, Subscription {
37
38 private static final long serialVersionUID = -3807491841935125653L;
39 final Subscriber<? super T> downstream;
40 final int skip;
41
42 Subscription upstream;
43
44 SkipLastSubscriber(Subscriber<? super T> actual, int skip) {
45 super(skip);
46 this.downstream = actual;
47 this.skip = skip;
48 }
49
50 @Override
51 public void onSubscribe(Subscription s) {
52 if (SubscriptionHelper.validate(this.upstream, s)) {
53 this.upstream = s;
54 downstream.onSubscribe(this);
55 }
56 }
57
58 @Override
59 public void onNext(T t) {
60 if (skip == size()) {
61 downstream.onNext(poll());
62 } else {
63 upstream.request(1);
64 }
65 offer(t);
66 }
67
68 @Override
69 public void onError(Throwable t) {
70 downstream.onError(t);
71 }
72
73 @Override
74 public void onComplete() {
75 downstream.onComplete();
76 }
77
78 @Override
79 public void request(long n) {
80 upstream.request(n);
81 }
82
83 @Override
84 public void cancel() {
85 upstream.cancel();
86 }
87 }
88 }