Debian Codegraph @

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