Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/subscribers/BlockingBaseSubscriber.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.subscribers;
15
16 import java.util.concurrent.CountDownLatch;
17
18 import org.reactivestreams.Subscription;
19
20 import io.reactivex.rxjava3.core.FlowableSubscriber;
21 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
22 import io.reactivex.rxjava3.internal.util.*;
23
24 public abstract class BlockingBaseSubscriber<T> extends CountDownLatch
25 implements FlowableSubscriber<T> {
26
27 T value;
28 Throwable error;
29
30 Subscription upstream;
31
32 volatile boolean cancelled;
33
34 public BlockingBaseSubscriber() {
35 super(1);
36 }
37
38 @Override
39 public final void onSubscribe(Subscription s) {
40 if (SubscriptionHelper.validate(this.upstream, s)) {
41 this.upstream = s;
42 if (!cancelled) {
43 s.request(Long.MAX_VALUE);
44 if (cancelled) {
45 this.upstream = SubscriptionHelper.CANCELLED;
46 s.cancel();
47 }
48 }
49 }
50 }
51
52 @Override
53 public final void onComplete() {
54 countDown();
55 }
56
57 /**
58 * Block until the first value arrives and return it, otherwise
59 * return null for an empty source and rethrow any exception.
60 * @return the first value or null if the source is empty
61 */
62 public final T blockingGet() {
63 if (getCount() != 0) {
64 try {
65 BlockingHelper.verifyNonBlocking();
66 await();
67 } catch (InterruptedException ex) {
68 Subscription s = this.upstream;
69 this.upstream = SubscriptionHelper.CANCELLED;
70 if (s != null) {
71 s.cancel();
72 }
73 throw ExceptionHelper.wrapOrThrow(ex);
74 }
75 }
76
77 Throwable e = error;
78 if (e != null) {
79 throw ExceptionHelper.wrapOrThrow(e);
80 }
81 return value;
82 }
83 }