Debian Codegraph @

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