Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableSkip.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 org.reactivestreams.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
20
21 public final class FlowableSkip<T> extends AbstractFlowableWithUpstream<T, T> {
22 final long n;
23 public FlowableSkip(Flowable<T> source, long n) {
24 super(source);
25 this.n = n;
26 }
27
28 @Override
29 protected void subscribeActual(Subscriber<? super T> s) {
30 source.subscribe(new SkipSubscriber<>(s, n));
31 }
32
33 static final class SkipSubscriber<T> implements FlowableSubscriber<T>, Subscription {
34 final Subscriber<? super T> downstream;
35 long remaining;
36
37 Subscription upstream;
38
39 SkipSubscriber(Subscriber<? super T> actual, long n) {
40 this.downstream = actual;
41 this.remaining = n;
42 }
43
44 @Override
45 public void onSubscribe(Subscription s) {
46 if (SubscriptionHelper.validate(this.upstream, s)) {
47 long n = remaining;
48 this.upstream = s;
49 downstream.onSubscribe(this);
50 s.request(n);
51 }
52 }
53
54 @Override
55 public void onNext(T t) {
56 if (remaining != 0L) {
57 remaining--;
58 } else {
59 downstream.onNext(t);
60 }
61 }
62
63 @Override
64 public void onError(Throwable t) {
65 downstream.onError(t);
66 }
67
68 @Override
69 public void onComplete() {
70 downstream.onComplete();
71 }
72
73 @Override
74 public void request(long n) {
75 upstream.request(n);
76 }
77
78 @Override
79 public void cancel() {
80 upstream.cancel();
81 }
82 }
83 }