Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableSkip.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.observable;
15
16 import io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
19
20 public final class ObservableSkip<T> extends AbstractObservableWithUpstream<T, T> {
21 final long n;
22 public ObservableSkip(ObservableSource<T> source, long n) {
23 super(source);
24 this.n = n;
25 }
26
27 @Override
28 public void subscribeActual(Observer<? super T> observer) {
29 source.subscribe(new SkipObserver<>(observer, n));
30 }
31
32 static final class SkipObserver<T> implements Observer<T>, Disposable {
33 final Observer<? super T> downstream;
34 long remaining;
35
36 Disposable upstream;
37
38 SkipObserver(Observer<? super T> actual, long n) {
39 this.downstream = actual;
40 this.remaining = n;
41 }
42
43 @Override
44 public void onSubscribe(Disposable d) {
45 if (DisposableHelper.validate(this.upstream, d)) {
46 this.upstream = d;
47 downstream.onSubscribe(this);
48 }
49 }
50
51 @Override
52 public void onNext(T t) {
53 if (remaining != 0L) {
54 remaining--;
55 } else {
56 downstream.onNext(t);
57 }
58 }
59
60 @Override
61 public void onError(Throwable t) {
62 downstream.onError(t);
63 }
64
65 @Override
66 public void onComplete() {
67 downstream.onComplete();
68 }
69
70 @Override
71 public void dispose() {
72 upstream.dispose();
73 }
74
75 @Override
76 public boolean isDisposed() {
77 return upstream.isDisposed();
78 }
79 }
80 }