| 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.observers; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 17 |
import io.reactivex.rxjava3.core.Observer; |
| 18 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 19 |
|
| 20 |
/** |
| 21 |
* Represents a fuseable container for a single value. |
| 22 |
* |
| 23 |
* @param <T> the value type received and emitted |
| 24 |
*/ |
| 25 |
public class DeferredScalarDisposable<T> extends BasicIntQueueDisposable<T> { |
| 26 |
|
| 27 |
private static final long serialVersionUID = -5502432239815349361L; |
| 28 |
|
| 29 |
/** The target of the events. */ |
| 30 |
protected final Observer<? super T> downstream; |
| 31 |
|
| 32 |
/** The value stored temporarily when in fusion mode. */ |
| 33 |
protected T value; |
| 34 |
|
| 35 |
/** Indicates there was a call to complete(T). */ |
| 36 |
static final int TERMINATED = 2; |
| 37 |
|
| 38 |
/** Indicates the Disposable has been disposed. */ |
| 39 |
static final int DISPOSED = 4; |
| 40 |
|
| 41 |
/** Indicates this Disposable is in fusion mode and is currently empty. */ |
| 42 |
static final int FUSED_EMPTY = 8; |
| 43 |
/** Indicates this Disposable is in fusion mode and has a value. */ |
| 44 |
static final int FUSED_READY = 16; |
| 45 |
/** Indicates this Disposable is in fusion mode and its value has been consumed. */ |
| 46 |
static final int FUSED_CONSUMED = 32; |
| 47 |
|
| 48 |
/** |
| 49 |
* Constructs a DeferredScalarDisposable by wrapping the Observer. |
| 50 |
* @param downstream the Observer to wrap, not null (not verified) |
| 51 |
*/ |
| 52 |
public DeferredScalarDisposable(Observer<? super T> downstream) { |
| 53 |
this.downstream = downstream; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public final int requestFusion(int mode) { |
| 58 |
if ((mode & ASYNC) != 0) { |
| 59 |
lazySet(FUSED_EMPTY); |
| 60 |
return ASYNC; |
| 61 |
} |
| 62 |
return NONE; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Complete the target with a single value or indicate there is a value available in |
| 67 |
* fusion mode. |
| 68 |
* @param value the value to signal, not null (not verified) |
| 69 |
*/ |
| 70 |
public final void complete(T value) { |
| 71 |
int state = get(); |
| 72 |
if ((state & (FUSED_READY | FUSED_CONSUMED | TERMINATED | DISPOSED)) != 0) { |
| 73 |
return; |
| 74 |
} |
| 75 |
Observer<? super T> a = downstream; |
| 76 |
if (state == FUSED_EMPTY) { |
| 77 |
this.value = value; |
| 78 |
lazySet(FUSED_READY); |
| 79 |
a.onNext(null); |
| 80 |
} else { |
| 81 |
lazySet(TERMINATED); |
| 82 |
a.onNext(value); |
| 83 |
} |
| 84 |
if (get() != DISPOSED) { |
| 85 |
a.onComplete(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Complete the target with an error signal. |
| 91 |
* @param t the Throwable to signal, not null (not verified) |
| 92 |
*/ |
| 93 |
public final void error(Throwable t) { |
| 94 |
int state = get(); |
| 95 |
if ((state & (FUSED_READY | FUSED_CONSUMED | TERMINATED | DISPOSED)) != 0) { |
| 96 |
RxJavaPlugins.onError(t); |
| 97 |
return; |
| 98 |
} |
| 99 |
lazySet(TERMINATED); |
| 100 |
downstream.onError(t); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Complete the target without any value. |
| 105 |
*/ |
| 106 |
public final void complete() { |
| 107 |
int state = get(); |
| 108 |
if ((state & (FUSED_READY | FUSED_CONSUMED | TERMINATED | DISPOSED)) != 0) { |
| 109 |
return; |
| 110 |
} |
| 111 |
lazySet(TERMINATED); |
| 112 |
downstream.onComplete(); |
| 113 |
} |
| 114 |
|
| 115 |
@Nullable |
| 116 |
@Override |
| 117 |
public final T poll() { |
| 118 |
if (get() == FUSED_READY) { |
| 119 |
T v = value; |
| 120 |
value = null; |
| 121 |
lazySet(FUSED_CONSUMED); |
| 122 |
return v; |
| 123 |
} |
| 124 |
return null; |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public final boolean isEmpty() { |
| 129 |
return get() != FUSED_READY; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public final void clear() { |
| 134 |
lazySet(FUSED_CONSUMED); |
| 135 |
value = null; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void dispose() { |
| 140 |
set(DISPOSED); |
| 141 |
value = null; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Try disposing this Disposable and return true if the current thread succeeded. |
| 146 |
* @return true if the current thread succeeded |
| 147 |
*/ |
| 148 |
public final boolean tryDispose() { |
| 149 |
return getAndSet(DISPOSED) != DISPOSED; |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public final boolean isDisposed() { |
| 154 |
return get() == DISPOSED; |
| 155 |
} |
| 156 |
|
| 157 |
} |