Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableLastSingle.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 java.util.NoSuchElementException;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
23
24 /**
25 * Consumes the source Publisher and emits its last item or the defaultItem
26 * if empty.
27 *
28 * @param <T> the value type
29 */
30 public final class FlowableLastSingle<T> extends Single<T> {
31
32 final Publisher<T> source;
33
34 final T defaultItem;
35
36 public FlowableLastSingle(Publisher<T> source, T defaultItem) {
37 this.source = source;
38 this.defaultItem = defaultItem;
39 }
40
41 // TODO fuse back to Flowable
42
43 @Override
44 protected void subscribeActual(SingleObserver<? super T> observer) {
45 source.subscribe(new LastSubscriber<>(observer, defaultItem));
46 }
47
48 static final class LastSubscriber<T> implements FlowableSubscriber<T>, Disposable {
49
50 final SingleObserver<? super T> downstream;
51
52 final T defaultItem;
53
54 Subscription upstream;
55
56 T item;
57
58 LastSubscriber(SingleObserver<? super T> actual, T defaultItem) {
59 this.downstream = actual;
60 this.defaultItem = defaultItem;
61 }
62
63 @Override
64 public void dispose() {
65 upstream.cancel();
66 upstream = SubscriptionHelper.CANCELLED;
67 }
68
69 @Override
70 public boolean isDisposed() {
71 return upstream == SubscriptionHelper.CANCELLED;
72 }
73
74 @Override
75 public void onSubscribe(Subscription s) {
76 if (SubscriptionHelper.validate(this.upstream, s)) {
77 this.upstream = s;
78
79 downstream.onSubscribe(this);
80
81 s.request(Long.MAX_VALUE);
82 }
83 }
84
85 @Override
86 public void onNext(T t) {
87 item = t;
88 }
89
90 @Override
91 public void onError(Throwable t) {
92 upstream = SubscriptionHelper.CANCELLED;
93 item = null;
94 downstream.onError(t);
95 }
96
97 @Override
98 public void onComplete() {
99 upstream = SubscriptionHelper.CANCELLED;
100 T v = item;
101 if (v != null) {
102 item = null;
103 downstream.onSuccess(v);
104 } else {
105 v = defaultItem;
106
107 if (v != null) {
108 downstream.onSuccess(v);
109 } else {
110 downstream.onError(new NoSuchElementException());
111 }
112 }
113 }
114 }
115 }