forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRxJavaPlugins.java
More file actions
427 lines (362 loc) · 13.9 KB
/
Copy pathRxJavaPlugins.java
File metadata and controls
427 lines (362 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.plugins;
import org.reactivestreams.*;
import io.reactivex.*;
import io.reactivex.functions.*;
/**
* Utility class to inject handlers to certain standard RxJava operations.
*/
public final class RxJavaPlugins {
static volatile Consumer<Throwable> errorHandler;
static volatile Function<Subscriber<Object>, Subscriber<Object>> onSubscribeHandler;
static volatile Function<Observer<Object>, Observer<Object>> onNbpSubscribeHandler;
static volatile Function<Publisher<Object>, Publisher<Object>> onCreateHandler;
static volatile Function<Runnable, Runnable> onScheduleHandler;
static volatile Function<Scheduler, Scheduler> onInitComputationHandler;
static volatile Function<Scheduler, Scheduler> onInitSingleHandler;
static volatile Function<Scheduler, Scheduler> onInitIOHandler;
static volatile Function<Scheduler, Scheduler> onInitNewThreadHandler;
static volatile Function<Scheduler, Scheduler> onComputationHandler;
static volatile Function<Scheduler, Scheduler> onSingleHandler;
static volatile Function<Scheduler, Scheduler> onIOHandler;
static volatile Function<Scheduler, Scheduler> onNewThreadHandler;
/** Prevents changing the plugins. */
private static volatile boolean lockdown;
/**
* Prevents changing the plugins from then on.
* <p>This allows container-like environments to prevent clients
* messing with plugins.
*/
public static void lockdown() {
lockdown = true;
}
/**
* Returns true if the plugins were locked down.
* @return true if the plugins were locked down
*/
public static boolean isLockdown() {
return lockdown;
}
public static Function<Scheduler, Scheduler> getComputationSchedulerHandler() {
return onComputationHandler;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Function<Publisher<T>, Publisher<T>> getCreateHandler() {
return (Function)onCreateHandler;
}
public static Consumer<Throwable> getErrorHandler() {
return errorHandler;
}
public static Function<Scheduler, Scheduler> getInitComputationSchedulerHandler() {
return onInitComputationHandler;
}
public static Function<Scheduler, Scheduler> getInitIOSchedulerHandler() {
return onInitIOHandler;
}
public static Function<Scheduler, Scheduler> getInitNewThreadSchedulerHandler() {
return onInitNewThreadHandler;
}
public static Function<Scheduler, Scheduler> getInitSingleSchedulerHandler() {
return onInitSingleHandler;
}
public static Function<Scheduler, Scheduler> getIOSchedulerHandler() {
return onIOHandler;
}
public static Function<Scheduler, Scheduler> getNewThreadSchedulerHandler() {
return onNewThreadHandler;
}
public static Function<Runnable, Runnable> getScheduleHandler() {
return onScheduleHandler;
}
public static Function<Scheduler, Scheduler> getSingleSchedulerHandler() {
return onSingleHandler;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Function<Subscriber<T>, Subscriber<T>> getSubscribeHandler() {
return (Function)onSubscribeHandler;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Function<Observer<T>, Observer<T>> getNbpSubscribeHandler() {
return (Function)onNbpSubscribeHandler;
}
public static Scheduler initComputationScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onInitComputationHandler;
if (f == null) {
return defaultScheduler;
}
return f.apply(defaultScheduler); // JIT will skip this
}
public static Scheduler initIOScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onInitIOHandler;
if (f == null) {
return defaultScheduler;
}
return f.apply(defaultScheduler);
}
public static Scheduler initNewThreadScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onInitNewThreadHandler;
if (f == null) {
return defaultScheduler;
}
return f.apply(defaultScheduler);
}
public static Scheduler initSingleScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onInitSingleHandler;
if (f == null) {
return defaultScheduler;
}
return f.apply(defaultScheduler);
}
public static Scheduler onComputationScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onComputationHandler;
if (f == null) {
return defaultScheduler;
}
return f.apply(defaultScheduler);
}
/**
* Called when an Observable is created.
* @param <T> the value type
* @param publisher the original publisher
* @return the replacement publisher
*/
@SuppressWarnings({ "unchecked", "rawtypes"})
public static <T> Publisher<T> onCreate(Publisher<T> publisher) {
Function<Publisher<Object>, Publisher<Object>> f = onCreateHandler;
if (f == null) {
return publisher;
}
return (Publisher)((Function)f).apply(publisher);
}
/**
* Called when an undeliverable error occurs.
* @param error the error to report
*/
public static void onError(Throwable error) {
Consumer<Throwable> f = errorHandler;
if (f != null) {
try {
f.accept(error);
return;
} catch (Throwable e) {
if (error == null) {
error = new NullPointerException();
}
e.printStackTrace();
}
} else {
if (error == null) {
error = new NullPointerException();
}
}
error.printStackTrace();
}
public static Scheduler onIOScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onIOHandler;
if (f == null) {
return defaultScheduler;
}
return f.apply(defaultScheduler);
}
public static Scheduler onNewThreadScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onNewThreadHandler;
if (f == null) {
return defaultScheduler;
}
return f.apply(defaultScheduler);
}
/**
* Called when a task is scheduled.
* @param run the runnable instance
* @return the replacement runnable
*/
public static Runnable onSchedule(Runnable run) {
Function<Runnable, Runnable> f = onScheduleHandler;
if (f == null) {
return run;
}
return f.apply(run);
}
public static Scheduler onSingleScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onSingleHandler;
if (f == null) {
return defaultScheduler;
}
return f.apply(defaultScheduler);
}
/**
* Called when a subscriber subscribes to an observable.
* @param <T> the value type
* @param subscriber the original subscriber
* @return the subscriber replacement
*/
@SuppressWarnings({ "unchecked", "rawtypes"})
public static <T> Subscriber<T> onSubscribe(Subscriber<T> subscriber) {
Function<Subscriber<Object>, Subscriber<Object>> f = onSubscribeHandler;
if (f == null) {
return subscriber;
}
return (Subscriber)((Function)f).apply(subscriber);
}
/**
* Called when a subscriber subscribes to an observable.
* @param <T> the value type
* @param subscriber the original NbpSubscriber
* @return the replacement NbpSubscriber
*/
@SuppressWarnings({ "unchecked", "rawtypes"})
public static <T> Observer<T> onNbpSubscribe(Observer<T> subscriber) {
Function<Observer<Object>, Observer<Object>> f = onNbpSubscribeHandler;
if (f == null) {
return subscriber;
}
return (Observer)((Function)f).apply(subscriber);
}
/**
* Called when a subscriber subscribes to an observable.
* @param <T> the value type
* @param subscriber the original subscriber
* @return the replacement subscriber
*/
@SuppressWarnings({ "unchecked", "rawtypes"})
public static <T> Observer<T> onSubscribe(Observer<T> subscriber) {
Function<Observer<Object>, Observer<Object>> f = onNbpSubscribeHandler;
if (f == null) {
return subscriber;
}
return (Observer)((Function)f).apply(subscriber);
}
/**
* Removes all handlers and resets the default behavior.
*/
public static void reset() {
setCreateHandler(null);
setErrorHandler(null);
setScheduleHandler(null);
setSubscribeHandler(null);
setComputationSchedulerHandler(null);
setInitComputationSchedulerHandler(null);
setIOSchedulerHandler(null);
setInitIOSchedulerHandler(null);
setSingleSchedulerHandler(null);
setInitSingleSchedulerHandler(null);
setNewThreadSchedulerHandler(null);
setInitNewThreadSchedulerHandler(null);
}
public static void setComputationSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onComputationHandler = handler;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> void setCreateHandler(Function<Publisher<T>, Publisher<T>> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onCreateHandler = (Function)handler;
}
public static void setErrorHandler(Consumer<Throwable> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
errorHandler = handler;
}
public static void setInitComputationSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onInitComputationHandler = handler;
}
public static void setInitIOSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onInitIOHandler = handler;
}
public static void setInitNewThreadSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onInitNewThreadHandler = handler;
}
public static void setInitSingleSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onInitSingleHandler = handler;
}
public static void setIOSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onIOHandler = handler;
}
public static void setNewThreadSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onNewThreadHandler = handler;
}
public static void setScheduleHandler(Function<Runnable, Runnable> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onScheduleHandler = handler;
}
public static void setSingleSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onSingleHandler = handler;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> void setSubscribeHandler(Function<Subscriber<T>, Subscriber<T>> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onSubscribeHandler = (Function)handler;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> void setNbpSubscribeHandler(Function<Observer<T>, Observer<T>> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onNbpSubscribeHandler = (Function)handler;
}
/**
* Rewokes the lockdown, only for testing purposes.
*/
/* test. */static void unlock() {
lockdown = false;
}
/** Singleton consumer that calls RxJavaPlugins.onError. */
static final Consumer<Throwable> CONSUME_BY_RXJAVA_PLUGIN = new Consumer<Throwable>() {
@Override
public void accept(Throwable e) {
RxJavaPlugins.onError(e);
}
};
/**
* Returns a consumer which relays the received Throwable to RxJavaPlugins.onError().
* @return the consumer
*/
public static final Consumer<Throwable> errorConsumer() {
return CONSUME_BY_RXJAVA_PLUGIN;
}
/** Helper class, no instances. */
private RxJavaPlugins() {
throw new IllegalStateException("No instances!");
}
}