| 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.io.Serializable; |
| 17 |
|
| 18 |
import java.util.Objects; |
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.Observer; |
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
|
| 24 |
/** |
| 25 |
* Lightweight notification handling utility class. |
| 26 |
*/ |
| 27 |
public enum NotificationLite { |
| 28 |
COMPLETE |
| 29 |
; |
| 30 |
|
| 31 |
/** |
| 32 |
* Wraps a Throwable. |
| 33 |
*/ |
| 34 |
static final class ErrorNotification implements Serializable { |
| 35 |
|
| 36 |
private static final long serialVersionUID = -8759979445933046293L; |
| 37 |
final Throwable e; |
| 38 |
ErrorNotification(Throwable e) { |
| 39 |
this.e = e; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public String toString() { |
| 44 |
return "NotificationLite.Error[" + e + "]"; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public int hashCode() { |
| 49 |
return e.hashCode(); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public boolean equals(Object obj) { |
| 54 |
if (obj instanceof ErrorNotification) { |
| 55 |
ErrorNotification n = (ErrorNotification) obj; |
| 56 |
return Objects.equals(e, n.e); |
| 57 |
} |
| 58 |
return false; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Wraps a Subscription. |
| 64 |
*/ |
| 65 |
static final class SubscriptionNotification implements Serializable { |
| 66 |
|
| 67 |
private static final long serialVersionUID = -1322257508628817540L; |
| 68 |
final Subscription upstream; |
| 69 |
SubscriptionNotification(Subscription s) { |
| 70 |
this.upstream = s; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public String toString() { |
| 75 |
return "NotificationLite.Subscription[" + upstream + "]"; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Wraps a Disposable. |
| 81 |
*/ |
| 82 |
static final class DisposableNotification implements Serializable { |
| 83 |
|
| 84 |
private static final long serialVersionUID = -7482590109178395495L; |
| 85 |
final Disposable upstream; |
| 86 |
|
| 87 |
DisposableNotification(Disposable d) { |
| 88 |
this.upstream = d; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public String toString() { |
| 93 |
return "NotificationLite.Disposable[" + upstream + "]"; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Converts a value into a notification value. |
| 99 |
* @param <T> the actual value type |
| 100 |
* @param value the value to convert |
| 101 |
* @return the notification representing the value |
| 102 |
*/ |
| 103 |
public static <T> Object next(T value) { |
| 104 |
return value; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns a complete notification. |
| 109 |
* @return a complete notification |
| 110 |
*/ |
| 111 |
public static Object complete() { |
| 112 |
return COMPLETE; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Converts a Throwable into a notification value. |
| 117 |
* @param e the Throwable to convert |
| 118 |
* @return the notification representing the Throwable |
| 119 |
*/ |
| 120 |
public static Object error(Throwable e) { |
| 121 |
return new ErrorNotification(e); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Converts a Subscription into a notification value. |
| 126 |
* @param s the Subscription to convert |
| 127 |
* @return the notification representing the Subscription |
| 128 |
*/ |
| 129 |
public static Object subscription(Subscription s) { |
| 130 |
return new SubscriptionNotification(s); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Converts a Disposable into a notification value. |
| 135 |
* @param d the disposable to convert |
| 136 |
* @return the notification representing the Disposable |
| 137 |
*/ |
| 138 |
public static Object disposable(Disposable d) { |
| 139 |
return new DisposableNotification(d); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Checks if the given object represents a complete notification. |
| 144 |
* @param o the object to check |
| 145 |
* @return true if the object represents a complete notification |
| 146 |
*/ |
| 147 |
public static boolean isComplete(Object o) { |
| 148 |
return o == COMPLETE; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Checks if the given object represents a error notification. |
| 153 |
* @param o the object to check |
| 154 |
* @return true if the object represents a error notification |
| 155 |
*/ |
| 156 |
public static boolean isError(Object o) { |
| 157 |
return o instanceof ErrorNotification; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Checks if the given object represents a subscription notification. |
| 162 |
* @param o the object to check |
| 163 |
* @return true if the object represents a subscription notification |
| 164 |
*/ |
| 165 |
public static boolean isSubscription(Object o) { |
| 166 |
return o instanceof SubscriptionNotification; |
| 167 |
} |
| 168 |
|
| 169 |
public static boolean isDisposable(Object o) { |
| 170 |
return o instanceof DisposableNotification; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Extracts the value from the notification object. |
| 175 |
* @param <T> the expected value type when unwrapped |
| 176 |
* @param o the notification object |
| 177 |
* @return the extracted value |
| 178 |
*/ |
| 179 |
@SuppressWarnings("unchecked") |
| 180 |
public static <T> T getValue(Object o) { |
| 181 |
return (T)o; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Extracts the Throwable from the notification object. |
| 186 |
* @param o the notification object |
| 187 |
* @return the extracted Throwable |
| 188 |
*/ |
| 189 |
public static Throwable getError(Object o) { |
| 190 |
return ((ErrorNotification)o).e; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Extracts the Subscription from the notification object. |
| 195 |
* @param o the notification object |
| 196 |
* @return the extracted Subscription |
| 197 |
*/ |
| 198 |
public static Subscription getSubscription(Object o) { |
| 199 |
return ((SubscriptionNotification)o).upstream; |
| 200 |
} |
| 201 |
|
| 202 |
public static Disposable getDisposable(Object o) { |
| 203 |
return ((DisposableNotification)o).upstream; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Calls the appropriate Subscriber method based on the type of the notification. |
| 208 |
* <p>Does not check for a subscription notification, see {@link #acceptFull(Object, Subscriber)}. |
| 209 |
* @param <T> the expected value type when unwrapped |
| 210 |
* @param o the notification object |
| 211 |
* @param s the subscriber to call methods on |
| 212 |
* @return true if the notification was a terminal event (i.e., complete or error) |
| 213 |
* @see #acceptFull(Object, Subscriber) |
| 214 |
*/ |
| 215 |
@SuppressWarnings("unchecked") |
| 216 |
public static <T> boolean accept(Object o, Subscriber<? super T> s) { |
| 217 |
if (o == COMPLETE) { |
| 218 |
s.onComplete(); |
| 219 |
return true; |
| 220 |
} else |
| 221 |
if (o instanceof ErrorNotification) { |
| 222 |
s.onError(((ErrorNotification)o).e); |
| 223 |
return true; |
| 224 |
} |
| 225 |
s.onNext((T)o); |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Calls the appropriate Observer method based on the type of the notification. |
| 231 |
* <p>Does not check for a subscription notification. |
| 232 |
* @param <T> the expected value type when unwrapped |
| 233 |
* @param o the notification object |
| 234 |
* @param observer the Observer to call methods on |
| 235 |
* @return true if the notification was a terminal event (i.e., complete or error) |
| 236 |
*/ |
| 237 |
@SuppressWarnings("unchecked") |
| 238 |
public static <T> boolean accept(Object o, Observer<? super T> observer) { |
| 239 |
if (o == COMPLETE) { |
| 240 |
observer.onComplete(); |
| 241 |
return true; |
| 242 |
} else |
| 243 |
if (o instanceof ErrorNotification) { |
| 244 |
observer.onError(((ErrorNotification)o).e); |
| 245 |
return true; |
| 246 |
} |
| 247 |
observer.onNext((T)o); |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Calls the appropriate Subscriber method based on the type of the notification. |
| 253 |
* @param <T> the expected value type when unwrapped |
| 254 |
* @param o the notification object |
| 255 |
* @param s the subscriber to call methods on |
| 256 |
* @return true if the notification was a terminal event (i.e., complete or error) |
| 257 |
* @see #accept(Object, Subscriber) |
| 258 |
*/ |
| 259 |
@SuppressWarnings("unchecked") |
| 260 |
public static <T> boolean acceptFull(Object o, Subscriber<? super T> s) { |
| 261 |
if (o == COMPLETE) { |
| 262 |
s.onComplete(); |
| 263 |
return true; |
| 264 |
} else |
| 265 |
if (o instanceof ErrorNotification) { |
| 266 |
s.onError(((ErrorNotification)o).e); |
| 267 |
return true; |
| 268 |
} else |
| 269 |
if (o instanceof SubscriptionNotification) { |
| 270 |
s.onSubscribe(((SubscriptionNotification)o).upstream); |
| 271 |
return false; |
| 272 |
} |
| 273 |
s.onNext((T)o); |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Calls the appropriate Observer method based on the type of the notification. |
| 279 |
* @param <T> the expected value type when unwrapped |
| 280 |
* @param o the notification object |
| 281 |
* @param observer the subscriber to call methods on |
| 282 |
* @return true if the notification was a terminal event (i.e., complete or error) |
| 283 |
* @see #accept(Object, Observer) |
| 284 |
*/ |
| 285 |
@SuppressWarnings("unchecked") |
| 286 |
public static <T> boolean acceptFull(Object o, Observer<? super T> observer) { |
| 287 |
if (o == COMPLETE) { |
| 288 |
observer.onComplete(); |
| 289 |
return true; |
| 290 |
} else |
| 291 |
if (o instanceof ErrorNotification) { |
| 292 |
observer.onError(((ErrorNotification)o).e); |
| 293 |
return true; |
| 294 |
} else |
| 295 |
if (o instanceof DisposableNotification) { |
| 296 |
observer.onSubscribe(((DisposableNotification)o).upstream); |
| 297 |
return false; |
| 298 |
} |
| 299 |
observer.onNext((T)o); |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
public String toString() { |
| 305 |
return "NotificationLite.Complete"; |
| 306 |
} |
| 307 |
} |