| 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.AtomicReference; |
| 17 |
|
| 18 |
import org.reactivestreams.Subscriber; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
/** |
| 24 |
* Atomic container for Throwables including combining and having a |
| 25 |
* terminal state via ExceptionHelper. |
| 26 |
* <p> |
| 27 |
* Watch out for the leaked AtomicReference methods! |
| 28 |
*/ |
| 29 |
public final class AtomicThrowable extends AtomicReference<Throwable> { |
| 30 |
|
| 31 |
private static final long serialVersionUID = 3949248817947090603L; |
| 32 |
|
| 33 |
/** |
| 34 |
* Atomically adds a Throwable to this container (combining with a previous Throwable is necessary). |
| 35 |
* @param t the throwable to add |
| 36 |
* @return true if successful, false if the container has been terminated |
| 37 |
*/ |
| 38 |
public boolean tryAddThrowable(Throwable t) { |
| 39 |
return ExceptionHelper.addThrowable(this, t); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Atomically adds a Throwable to this container (combining with a previous Throwable is necessary) |
| 44 |
* or reports the error the global error handler and no changes are made. |
| 45 |
* @param t the throwable to add |
| 46 |
* @return true if successful, false if the container has been terminated |
| 47 |
*/ |
| 48 |
public boolean tryAddThrowableOrReport(Throwable t) { |
| 49 |
if (tryAddThrowable(t)) { |
| 50 |
return true; |
| 51 |
} |
| 52 |
RxJavaPlugins.onError(t); |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Atomically terminate the container and return the contents of the last |
| 58 |
* non-terminal Throwable of it. |
| 59 |
* @return the last Throwable |
| 60 |
*/ |
| 61 |
public Throwable terminate() { |
| 62 |
return ExceptionHelper.terminate(this); |
| 63 |
} |
| 64 |
|
| 65 |
public boolean isTerminated() { |
| 66 |
return get() == ExceptionHelper.TERMINATED; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Tries to terminate this atomic throwable (by swapping in the TERMINATED indicator) |
| 71 |
* and calls {@link RxJavaPlugins#onError(Throwable)} if there was a non-null, non-indicator |
| 72 |
* exception contained within before. |
| 73 |
* @since 3.0.0 |
| 74 |
*/ |
| 75 |
public void tryTerminateAndReport() { |
| 76 |
Throwable ex = terminate(); |
| 77 |
if (ex != null && ex != ExceptionHelper.TERMINATED) { |
| 78 |
RxJavaPlugins.onError(ex); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Tries to terminate this atomic throwable (by swapping in the TERMINATED indicator) |
| 84 |
* and notifies the consumer if there was no error (onComplete) or there was a |
| 85 |
* non-null, non-indicator exception contained before (onError). |
| 86 |
* If there was a terminated indicator, the consumer is not signaled. |
| 87 |
* @param consumer the consumer to notify |
| 88 |
*/ |
| 89 |
public void tryTerminateConsumer(Subscriber<?> consumer) { |
| 90 |
Throwable ex = terminate(); |
| 91 |
if (ex == null) { |
| 92 |
consumer.onComplete(); |
| 93 |
} else if (ex != ExceptionHelper.TERMINATED) { |
| 94 |
consumer.onError(ex); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Tries to terminate this atomic throwable (by swapping in the TERMINATED indicator) |
| 100 |
* and notifies the consumer if there was no error (onComplete) or there was a |
| 101 |
* non-null, non-indicator exception contained before (onError). |
| 102 |
* If there was a terminated indicator, the consumer is not signaled. |
| 103 |
* @param consumer the consumer to notify |
| 104 |
*/ |
| 105 |
public void tryTerminateConsumer(Observer<?> consumer) { |
| 106 |
Throwable ex = terminate(); |
| 107 |
if (ex == null) { |
| 108 |
consumer.onComplete(); |
| 109 |
} else if (ex != ExceptionHelper.TERMINATED) { |
| 110 |
consumer.onError(ex); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Tries to terminate this atomic throwable (by swapping in the TERMINATED indicator) |
| 116 |
* and notifies the consumer if there was no error (onComplete) or there was a |
| 117 |
* non-null, non-indicator exception contained before (onError). |
| 118 |
* If there was a terminated indicator, the consumer is not signaled. |
| 119 |
* @param consumer the consumer to notify |
| 120 |
*/ |
| 121 |
public void tryTerminateConsumer(MaybeObserver<?> consumer) { |
| 122 |
Throwable ex = terminate(); |
| 123 |
if (ex == null) { |
| 124 |
consumer.onComplete(); |
| 125 |
} else if (ex != ExceptionHelper.TERMINATED) { |
| 126 |
consumer.onError(ex); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Tries to terminate this atomic throwable (by swapping in the TERMINATED indicator) |
| 132 |
* and notifies the consumer if there was no error (onComplete) or there was a |
| 133 |
* non-null, non-indicator exception contained before (onError). |
| 134 |
* If there was a terminated indicator, the consumer is not signaled. |
| 135 |
* @param consumer the consumer to notify |
| 136 |
*/ |
| 137 |
public void tryTerminateConsumer(SingleObserver<?> consumer) { |
| 138 |
Throwable ex = terminate(); |
| 139 |
if (ex != null && ex != ExceptionHelper.TERMINATED) { |
| 140 |
consumer.onError(ex); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Tries to terminate this atomic throwable (by swapping in the TERMINATED indicator) |
| 146 |
* and notifies the consumer if there was no error (onComplete) or there was a |
| 147 |
* non-null, non-indicator exception contained before (onError). |
| 148 |
* If there was a terminated indicator, the consumer is not signaled. |
| 149 |
* @param consumer the consumer to notify |
| 150 |
*/ |
| 151 |
public void tryTerminateConsumer(CompletableObserver consumer) { |
| 152 |
Throwable ex = terminate(); |
| 153 |
if (ex == null) { |
| 154 |
consumer.onComplete(); |
| 155 |
} else if (ex != ExceptionHelper.TERMINATED) { |
| 156 |
consumer.onError(ex); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Tries to terminate this atomic throwable (by swapping in the TERMINATED indicator) |
| 162 |
* and notifies the consumer if there was no error (onComplete) or there was a |
| 163 |
* non-null, non-indicator exception contained before (onError). |
| 164 |
* If there was a terminated indicator, the consumer is not signaled. |
| 165 |
* @param consumer the consumer to notify |
| 166 |
*/ |
| 167 |
public void tryTerminateConsumer(Emitter<?> consumer) { |
| 168 |
Throwable ex = terminate(); |
| 169 |
if (ex == null) { |
| 170 |
consumer.onComplete(); |
| 171 |
} else if (ex != ExceptionHelper.TERMINATED) { |
| 172 |
consumer.onError(ex); |
| 173 |
} |
| 174 |
} |
| 175 |
} |