Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableFromPublisher.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 org.reactivestreams.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
21
22 public final class ObservableFromPublisher<T> extends Observable<T> {
23
24 final Publisher<? extends T> source;
25
26 public ObservableFromPublisher(Publisher<? extends T> publisher) {
27 this.source = publisher;
28 }
29
30 @Override
31 protected void subscribeActual(final Observer<? super T> o) {
32 source.subscribe(new PublisherSubscriber<T>(o));
33 }
34
35 static final class PublisherSubscriber<T>
36 implements FlowableSubscriber<T>, Disposable {
37
38 final Observer<? super T> downstream;
39 Subscription upstream;
40
41 PublisherSubscriber(Observer<? super T> o) {
42 this.downstream = o;
43 }
44
45 @Override
46 public void onComplete() {
47 downstream.onComplete();
48 }
49
50 @Override
51 public void onError(Throwable t) {
52 downstream.onError(t);
53 }
54
55 @Override
56 public void onNext(T t) {
57 downstream.onNext(t);
58 }
59
60 @Override
61 public void onSubscribe(Subscription s) {
62 if (SubscriptionHelper.validate(this.upstream, s)) {
63 this.upstream = s;
64 downstream.onSubscribe(this);
65 s.request(Long.MAX_VALUE);
66 }
67 }
68
69 @Override
70 public void dispose() {
71 upstream.cancel();
72 upstream = SubscriptionHelper.CANCELLED;
73 }
74
75 @Override
76 public boolean isDisposed() {
77 return upstream == SubscriptionHelper.CANCELLED;
78 }
79 }
80 }