-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathDeferredScalarDisposable.java
154 lines (133 loc) · 4.34 KB
/
DeferredScalarDisposable.java
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
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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.internal.observers;
import io.reactivex.Observer;
import io.reactivex.plugins.RxJavaPlugins;
/**
* Represents a fuseable container for a single value.
*
* @param <T> the value type received and emitted
*/
public class DeferredScalarDisposable<T> extends BasicIntQueueDisposable<T> {
private static final long serialVersionUID = -5502432239815349361L;
/** The target of the events. */
protected final Observer<? super T> actual;
/** The value stored temporarily when in fusion mode. */
protected T value;
/** Indicates there was a call to complete(T). */
static final int TERMINATED = 2;
/** Indicates the Disposable has been disposed. */
static final int DISPOSED = 4;
/** Indicates this Disposable is in fusion mode and is currently empty. */
static final int FUSED_EMPTY = 8;
/** Indicates this Disposable is in fusion mode and has a value. */
static final int FUSED_READY = 16;
/** Indicates this Disposable is in fusion mode and its value has been consumed. */
static final int FUSED_CONSUMED = 32;
/**
* Constructs a DeferredScalarDisposable by wrapping the Observer.
* @param actual the Observer to wrap, not null (not verified)
*/
public DeferredScalarDisposable(Observer<? super T> actual) {
this.actual = actual;
}
@Override
public final int requestFusion(int mode) {
if ((mode & ASYNC) != 0) {
lazySet(FUSED_EMPTY);
return ASYNC;
}
return NONE;
}
/**
* Complete the target with a single value or indicate there is a value available in
* fusion mode.
* @param value the value to signal, not null (not verified)
*/
public final void complete(T value) {
int state = get();
if ((state & (FUSED_READY | FUSED_CONSUMED | TERMINATED | DISPOSED)) != 0) {
return;
}
if (state == FUSED_EMPTY) {
this.value = value;
lazySet(FUSED_READY);
} else {
lazySet(TERMINATED);
}
Observer<? super T> a = actual;
a.onNext(value);
if (get() != DISPOSED) {
a.onComplete();
}
}
/**
* Complete the target with an error signal.
* @param t the Throwable to signal, not null (not verified)
*/
public final void error(Throwable t) {
int state = get();
if ((state & (FUSED_READY | FUSED_CONSUMED | TERMINATED | DISPOSED)) != 0) {
RxJavaPlugins.onError(t);
return;
}
lazySet(TERMINATED);
actual.onError(t);
}
/**
* Complete the target without any value.
*/
public final void complete() {
int state = get();
if ((state & (FUSED_READY | FUSED_CONSUMED | TERMINATED | DISPOSED)) != 0) {
return;
}
lazySet(TERMINATED);
actual.onComplete();
}
@Override
public final T poll() throws Exception {
if (get() == FUSED_READY) {
T v = value;
value = null;
lazySet(FUSED_CONSUMED);
return v;
}
return null;
}
@Override
public final boolean isEmpty() {
return get() != FUSED_READY;
}
@Override
public final void clear() {
lazySet(FUSED_CONSUMED);
value = null;
}
@Override
public void dispose() {
set(DISPOSED);
value = null;
}
/**
* Try disposing this Disposable and return true if the current thread succeeded.
* @return true if the current thread succeeded
*/
public final boolean tryDispose() {
return getAndSet(DISPOSED) != DISPOSED;
}
@Override
public final boolean isDisposed() {
return get() == DISPOSED;
}
}