13
13
14
14
package io .reactivex .internal .observers ;
15
15
16
+ import java .util .concurrent .atomic .AtomicReference ;
17
+
16
18
import io .reactivex .*;
17
19
import io .reactivex .disposables .Disposable ;
18
20
import io .reactivex .exceptions .*;
19
21
import io .reactivex .functions .Consumer ;
20
22
import io .reactivex .internal .disposables .DisposableHelper ;
21
23
import io .reactivex .plugins .RxJavaPlugins ;
22
24
23
- public final class ToNotificationObserver <T > implements Observer <T > {
24
- final Consumer <? super Notification <Object >> consumer ;
25
+ public final class ToNotificationObserver <T >
26
+ extends AtomicReference <Disposable >
27
+ implements Observer <T >, Disposable {
28
+ private static final long serialVersionUID = -7420197867343208289L ;
25
29
26
- Disposable s ;
30
+ final Consumer <? super Notification < Object >> consumer ;
27
31
28
32
public ToNotificationObserver (Consumer <? super Notification <Object >> consumer ) {
29
33
this .consumer = consumer ;
30
34
}
31
35
32
36
@ Override
33
37
public void onSubscribe (Disposable s ) {
34
- if (DisposableHelper .validate (this .s , s )) {
35
- this .s = s ;
36
- }
38
+ DisposableHelper .setOnce (this , s );
37
39
}
38
40
39
41
@ Override
40
42
public void onNext (T t ) {
41
43
if (t == null ) {
42
- s .dispose ();
44
+ get () .dispose ();
43
45
onError (new NullPointerException ("onNext called with null. Null values are generally not allowed in 2.x operators and sources." ));
44
46
} else {
45
47
try {
46
48
consumer .accept (Notification .<Object >createOnNext (t ));
47
49
} catch (Throwable ex ) {
48
50
Exceptions .throwIfFatal (ex );
49
- s .dispose ();
51
+ get () .dispose ();
50
52
onError (ex );
51
53
}
52
54
}
@@ -71,4 +73,14 @@ public void onComplete() {
71
73
RxJavaPlugins .onError (ex );
72
74
}
73
75
}
76
+
77
+ @ Override
78
+ public void dispose () {
79
+ DisposableHelper .dispose (this );
80
+ }
81
+
82
+ @ Override
83
+ public boolean isDisposed () {
84
+ return DisposableHelper .isDisposed (get ());
85
+ }
74
86
}
0 commit comments