Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/util/HalfSerializer.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.util;
15
16 import java.util.concurrent.atomic.AtomicInteger;
17
18 import org.reactivestreams.Subscriber;
19
20 import io.reactivex.rxjava3.core.Observer;
21
22 /**
23 * Utility methods to perform half-serialization: a form of serialization
24 * where onNext is guaranteed to be called from a single thread but
25 * onError or onComplete may be called from any threads.
26 */
27 public final class HalfSerializer {
28 /** Utility class. */
29 private HalfSerializer() {
30 throw new IllegalStateException("No instances!");
31 }
32
33 /**
34 * Emits the given value if possible and terminates if there was an onComplete or onError
35 * while emitting, drops the value otherwise.
36 * @param <T> the value type
37 * @param subscriber the target Subscriber to emit to
38 * @param value the value to emit
39 * @param wip the serialization work-in-progress counter/indicator
40 * @param errors the holder of Throwables
41 * @return true if the operation succeeded, false if there sequence completed
42 */
43 public static <T> boolean onNext(Subscriber<? super T> subscriber, T value,
44 AtomicInteger wip, AtomicThrowable errors) {
45 if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
46 subscriber.onNext(value);
47 if (wip.decrementAndGet() == 0) {
48 return true;
49 }
50 errors.tryTerminateConsumer(subscriber);
51 }
52 return false;
53 }
54
55 /**
56 * Emits the given exception if possible or adds it to the given error container to
57 * be emitted by a concurrent onNext if one is running.
58 * Undeliverable exceptions are sent to the RxJavaPlugins.onError.
59 * @param subscriber the target Subscriber to emit to
60 * @param ex the Throwable to emit
61 * @param wip the serialization work-in-progress counter/indicator
62 * @param errors the holder of Throwables
63 */
64 public static void onError(Subscriber<?> subscriber, Throwable ex,
65 AtomicInteger wip, AtomicThrowable errors) {
66 if (errors.tryAddThrowableOrReport(ex)) {
67 if (wip.getAndIncrement() == 0) {
68 errors.tryTerminateConsumer(subscriber);
69 }
70 }
71 }
72
73 /**
74 * Emits an onComplete signal or an onError signal with the given error or indicates
75 * the concurrently running onNext should do that.
76 * @param subscriber the target Subscriber to emit to
77 * @param wip the serialization work-in-progress counter/indicator
78 * @param errors the holder of Throwables
79 */
80 public static void onComplete(Subscriber<?> subscriber, AtomicInteger wip, AtomicThrowable errors) {
81 if (wip.getAndIncrement() == 0) {
82 errors.tryTerminateConsumer(subscriber);
83 }
84 }
85
86 /**
87 * Emits the given value if possible and terminates if there was an onComplete or onError
88 * while emitting, drops the value otherwise.
89 * @param <T> the value type
90 * @param observer the target Observer to emit to
91 * @param value the value to emit
92 * @param wip the serialization work-in-progress counter/indicator
93 * @param errors the holder of Throwables
94 */
95 public static <T> void onNext(Observer<? super T> observer, T value,
96 AtomicInteger wip, AtomicThrowable errors) {
97 if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
98 observer.onNext(value);
99 if (wip.decrementAndGet() != 0) {
100 errors.tryTerminateConsumer(observer);
101 }
102 }
103 }
104
105 /**
106 * Emits the given exception if possible or adds it to the given error container to
107 * be emitted by a concurrent onNext if one is running.
108 * Undeliverable exceptions are sent to the RxJavaPlugins.onError.
109 * @param observer the target Subscriber to emit to
110 * @param ex the Throwable to emit
111 * @param wip the serialization work-in-progress counter/indicator
112 * @param errors the holder of Throwables
113 */
114 public static void onError(Observer<?> observer, Throwable ex,
115 AtomicInteger wip, AtomicThrowable errors) {
116 if (errors.tryAddThrowableOrReport(ex)) {
117 if (wip.getAndIncrement() == 0) {
118 errors.tryTerminateConsumer(observer);
119 }
120 }
121 }
122
123 /**
124 * Emits an onComplete signal or an onError signal with the given error or indicates
125 * the concurrently running onNext should do that.
126 * @param observer the target Subscriber to emit to
127 * @param wip the serialization work-in-progress counter/indicator
128 * @param errors the holder of Throwables
129 */
130 public static void onComplete(Observer<?> observer, AtomicInteger wip, AtomicThrowable errors) {
131 if (wip.getAndIncrement() == 0) {
132 errors.tryTerminateConsumer(observer);
133 }
134 }
135
136 }