Debian Codegraph @

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