15
15
*/
16
16
package rx .plugins ;
17
17
18
- import java .util .concurrent .atomic .AtomicReferenceFieldUpdater ;
18
+ import java .util .concurrent .atomic .AtomicReference ;
19
19
20
20
/**
21
21
* Registry for plugin implementations that allows global override and handles the retrieval of correct implementation based on order of precedence:
29
29
public class RxJavaPlugins {
30
30
private final static RxJavaPlugins INSTANCE = new RxJavaPlugins ();
31
31
32
- volatile RxJavaErrorHandler errorHandler ;
33
- volatile RxJavaObservableExecutionHook observableExecutionHook ;
34
- volatile RxJavaDefaultSchedulers schedulerOverrides ;
32
+ private final AtomicReference < RxJavaErrorHandler > errorHandler = new AtomicReference < RxJavaErrorHandler >() ;
33
+ private final AtomicReference < RxJavaObservableExecutionHook > observableExecutionHook = new AtomicReference < RxJavaObservableExecutionHook >() ;
34
+ private final AtomicReference < RxJavaDefaultSchedulers > schedulerOverrides = new AtomicReference < RxJavaDefaultSchedulers >() ;
35
35
36
- static final AtomicReferenceFieldUpdater <RxJavaPlugins , RxJavaErrorHandler > ERROR_HANDLER_UPDATER
37
- = AtomicReferenceFieldUpdater .newUpdater (RxJavaPlugins .class , RxJavaErrorHandler .class , "errorHandler" );
38
- static final AtomicReferenceFieldUpdater <RxJavaPlugins , RxJavaObservableExecutionHook > EXECUTION_HOOK_UPDATER
39
- = AtomicReferenceFieldUpdater .newUpdater (RxJavaPlugins .class , RxJavaObservableExecutionHook .class , "observableExecutionHook" );
40
- static final AtomicReferenceFieldUpdater <RxJavaPlugins , RxJavaDefaultSchedulers > SCHEDULER_OVERRIDE_UPDATER
41
- = AtomicReferenceFieldUpdater .newUpdater (RxJavaPlugins .class , RxJavaDefaultSchedulers .class , "schedulerOverrides" );
42
-
43
36
public static RxJavaPlugins getInstance () {
44
37
return INSTANCE ;
45
38
}
@@ -48,9 +41,9 @@ public static RxJavaPlugins getInstance() {
48
41
49
42
}
50
43
51
- /* package accessible for unit tests */ void reset () {
52
- ERROR_HANDLER_UPDATER . lazySet ( this , null );
53
- EXECUTION_HOOK_UPDATER . lazySet ( this , null );
44
+ /* package accessible for ujnit tests */ void reset () {
45
+ INSTANCE . errorHandler . set ( null );
46
+ INSTANCE . observableExecutionHook . set ( null );
54
47
}
55
48
56
49
/**
@@ -62,19 +55,19 @@ public static RxJavaPlugins getInstance() {
62
55
* @return {@link RxJavaErrorHandler} implementation to use
63
56
*/
64
57
public RxJavaErrorHandler getErrorHandler () {
65
- if (errorHandler == null ) {
58
+ if (errorHandler . get () == null ) {
66
59
// check for an implementation from System.getProperty first
67
60
Object impl = getPluginImplementationViaProperty (RxJavaErrorHandler .class );
68
61
if (impl == null ) {
69
62
// nothing set via properties so initialize with default
70
- ERROR_HANDLER_UPDATER .compareAndSet (this , null , RxJavaErrorHandlerDefault .getInstance ());
63
+ errorHandler .compareAndSet (null , RxJavaErrorHandlerDefault .getInstance ());
71
64
// we don't return from here but call get() again in case of thread-race so the winner will always get returned
72
65
} else {
73
66
// we received an implementation from the system property so use it
74
- ERROR_HANDLER_UPDATER .compareAndSet (this , null , (RxJavaErrorHandler ) impl );
67
+ errorHandler .compareAndSet (null , (RxJavaErrorHandler ) impl );
75
68
}
76
69
}
77
- return errorHandler ;
70
+ return errorHandler . get () ;
78
71
}
79
72
80
73
/**
@@ -86,8 +79,8 @@ public RxJavaErrorHandler getErrorHandler() {
86
79
* if called more than once or after the default was initialized (if usage occurs before trying to register)
87
80
*/
88
81
public void registerErrorHandler (RxJavaErrorHandler impl ) {
89
- if (!ERROR_HANDLER_UPDATER .compareAndSet (this , null , impl )) {
90
- throw new IllegalStateException ("Another strategy was already registered: " + errorHandler );
82
+ if (!errorHandler .compareAndSet (null , impl )) {
83
+ throw new IllegalStateException ("Another strategy was already registered: " + errorHandler . get () );
91
84
}
92
85
}
93
86
@@ -100,19 +93,19 @@ public void registerErrorHandler(RxJavaErrorHandler impl) {
100
93
* @return {@link RxJavaObservableExecutionHook} implementation to use
101
94
*/
102
95
public RxJavaObservableExecutionHook getObservableExecutionHook () {
103
- if (observableExecutionHook == null ) {
96
+ if (observableExecutionHook . get () == null ) {
104
97
// check for an implementation from System.getProperty first
105
98
Object impl = getPluginImplementationViaProperty (RxJavaObservableExecutionHook .class );
106
99
if (impl == null ) {
107
100
// nothing set via properties so initialize with default
108
- EXECUTION_HOOK_UPDATER .compareAndSet (this , null , RxJavaObservableExecutionHookDefault .getInstance ());
101
+ observableExecutionHook .compareAndSet (null , RxJavaObservableExecutionHookDefault .getInstance ());
109
102
// we don't return from here but call get() again in case of thread-race so the winner will always get returned
110
103
} else {
111
104
// we received an implementation from the system property so use it
112
- EXECUTION_HOOK_UPDATER .compareAndSet (this , null , (RxJavaObservableExecutionHook ) impl );
105
+ observableExecutionHook .compareAndSet (null , (RxJavaObservableExecutionHook ) impl );
113
106
}
114
107
}
115
- return observableExecutionHook ;
108
+ return observableExecutionHook . get () ;
116
109
}
117
110
118
111
/**
@@ -124,8 +117,8 @@ public RxJavaObservableExecutionHook getObservableExecutionHook() {
124
117
* if called more than once or after the default was initialized (if usage occurs before trying to register)
125
118
*/
126
119
public void registerObservableExecutionHook (RxJavaObservableExecutionHook impl ) {
127
- if (!EXECUTION_HOOK_UPDATER .compareAndSet (this , null , impl )) {
128
- throw new IllegalStateException ("Another strategy was already registered: " + observableExecutionHook );
120
+ if (!observableExecutionHook .compareAndSet (null , impl )) {
121
+ throw new IllegalStateException ("Another strategy was already registered: " + observableExecutionHook . get () );
129
122
}
130
123
}
131
124
@@ -168,19 +161,19 @@ private static Object getPluginImplementationViaProperty(Class<?> pluginClass) {
168
161
* @return {@link RxJavaErrorHandler} implementation to use
169
162
*/
170
163
public RxJavaDefaultSchedulers getDefaultSchedulers () {
171
- if (schedulerOverrides == null ) {
164
+ if (schedulerOverrides . get () == null ) {
172
165
// check for an implementation from System.getProperty first
173
166
Object impl = getPluginImplementationViaProperty (RxJavaDefaultSchedulers .class );
174
167
if (impl == null ) {
175
168
// nothing set via properties so initialize with default
176
- SCHEDULER_OVERRIDE_UPDATER .compareAndSet (this , null , RxJavaDefaultSchedulersDefault .getInstance ());
169
+ schedulerOverrides .compareAndSet (null , RxJavaDefaultSchedulersDefault .getInstance ());
177
170
// we don't return from here but call get() again in case of thread-race so the winner will always get returned
178
171
} else {
179
172
// we received an implementation from the system property so use it
180
- SCHEDULER_OVERRIDE_UPDATER .compareAndSet (this , null , (RxJavaDefaultSchedulers ) impl );
173
+ schedulerOverrides .compareAndSet (null , (RxJavaDefaultSchedulers ) impl );
181
174
}
182
175
}
183
- return schedulerOverrides ;
176
+ return schedulerOverrides . get () ;
184
177
}
185
178
186
179
/**
@@ -192,8 +185,8 @@ public RxJavaDefaultSchedulers getDefaultSchedulers() {
192
185
* if called more than once or after the default was initialized (if usage occurs before trying to register)
193
186
*/
194
187
public void registerDefaultSchedulers (RxJavaDefaultSchedulers impl ) {
195
- if (!SCHEDULER_OVERRIDE_UPDATER .compareAndSet (this , null , impl )) {
196
- throw new IllegalStateException ("Another strategy was already registered: " + schedulerOverrides );
188
+ if (!schedulerOverrides .compareAndSet (null , impl )) {
189
+ throw new IllegalStateException ("Another strategy was already registered: " + schedulerOverrides . get () );
197
190
}
198
191
}
199
192
}
0 commit comments