| 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.disposables; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.exceptions.ProtocolViolationException; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
/** |
| 24 |
* Utility methods for working with Disposables atomically. |
| 25 |
*/ |
| 26 |
public enum DisposableHelper implements Disposable { |
| 27 |
/** |
| 28 |
* The singleton instance representing a terminal, disposed state, don't leak it. |
| 29 |
*/ |
| 30 |
DISPOSED |
| 31 |
; |
| 32 |
|
| 33 |
/** |
| 34 |
* Checks if the given Disposable is the common {@link #DISPOSED} enum value. |
| 35 |
* @param d the disposable to check |
| 36 |
* @return true if d is {@link #DISPOSED} |
| 37 |
*/ |
| 38 |
public static boolean isDisposed(Disposable d) { |
| 39 |
return d == DISPOSED; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Atomically sets the field and disposes the old contents. |
| 44 |
* @param field the target field |
| 45 |
* @param d the new Disposable to set |
| 46 |
* @return true if successful, false if the field contains the {@link #DISPOSED} instance. |
| 47 |
*/ |
| 48 |
public static boolean set(AtomicReference<Disposable> field, Disposable d) { |
| 49 |
for (;;) { |
| 50 |
Disposable current = field.get(); |
| 51 |
if (current == DISPOSED) { |
| 52 |
if (d != null) { |
| 53 |
d.dispose(); |
| 54 |
} |
| 55 |
return false; |
| 56 |
} |
| 57 |
if (field.compareAndSet(current, d)) { |
| 58 |
if (current != null) { |
| 59 |
current.dispose(); |
| 60 |
} |
| 61 |
return true; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Atomically sets the field to the given non-null Disposable and returns true |
| 68 |
* or returns false if the field is non-null. |
| 69 |
* If the target field contains the common DISPOSED instance, the supplied disposable |
| 70 |
* is disposed. If the field contains other non-null Disposable, an IllegalStateException |
| 71 |
* is signalled to the RxJavaPlugins.onError hook. |
| 72 |
* |
| 73 |
* @param field the target field |
| 74 |
* @param d the disposable to set, not null |
| 75 |
* @return true if the operation succeeded, false |
| 76 |
*/ |
| 77 |
public static boolean setOnce(AtomicReference<Disposable> field, Disposable d) { |
| 78 |
Objects.requireNonNull(d, "d is null"); |
| 79 |
if (!field.compareAndSet(null, d)) { |
| 80 |
d.dispose(); |
| 81 |
if (field.get() != DISPOSED) { |
| 82 |
reportDisposableSet(); |
| 83 |
} |
| 84 |
return false; |
| 85 |
} |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Atomically replaces the Disposable in the field with the given new Disposable |
| 91 |
* but does not dispose the old one. |
| 92 |
* @param field the target field to change |
| 93 |
* @param d the new disposable, null allowed |
| 94 |
* @return true if the operation succeeded, false if the target field contained |
| 95 |
* the common DISPOSED instance and the given disposable (if not null) is disposed. |
| 96 |
*/ |
| 97 |
public static boolean replace(AtomicReference<Disposable> field, Disposable d) { |
| 98 |
for (;;) { |
| 99 |
Disposable current = field.get(); |
| 100 |
if (current == DISPOSED) { |
| 101 |
if (d != null) { |
| 102 |
d.dispose(); |
| 103 |
} |
| 104 |
return false; |
| 105 |
} |
| 106 |
if (field.compareAndSet(current, d)) { |
| 107 |
return true; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Atomically disposes the Disposable in the field if not already disposed. |
| 114 |
* @param field the target field |
| 115 |
* @return true if the current thread managed to dispose the Disposable |
| 116 |
*/ |
| 117 |
public static boolean dispose(AtomicReference<Disposable> field) { |
| 118 |
Disposable current = field.get(); |
| 119 |
Disposable d = DISPOSED; |
| 120 |
if (current != d) { |
| 121 |
current = field.getAndSet(d); |
| 122 |
if (current != d) { |
| 123 |
if (current != null) { |
| 124 |
current.dispose(); |
| 125 |
} |
| 126 |
return true; |
| 127 |
} |
| 128 |
} |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Verifies that current is null, next is not null, otherwise signals errors |
| 134 |
* to the RxJavaPlugins and returns false. |
| 135 |
* @param current the current Disposable, expected to be null |
| 136 |
* @param next the next Disposable, expected to be non-null |
| 137 |
* @return true if the validation succeeded |
| 138 |
*/ |
| 139 |
public static boolean validate(Disposable current, Disposable next) { |
| 140 |
if (next == null) { |
| 141 |
RxJavaPlugins.onError(new NullPointerException("next is null")); |
| 142 |
return false; |
| 143 |
} |
| 144 |
if (current != null) { |
| 145 |
next.dispose(); |
| 146 |
reportDisposableSet(); |
| 147 |
return false; |
| 148 |
} |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Reports that the disposable is already set to the RxJavaPlugins error handler. |
| 154 |
*/ |
| 155 |
public static void reportDisposableSet() { |
| 156 |
RxJavaPlugins.onError(new ProtocolViolationException("Disposable already set!")); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Atomically tries to set the given Disposable on the field if it is null or disposes it if |
| 161 |
* the field contains {@link #DISPOSED}. |
| 162 |
* @param field the target field |
| 163 |
* @param d the disposable to set |
| 164 |
* @return true if successful, false otherwise |
| 165 |
*/ |
| 166 |
public static boolean trySet(AtomicReference<Disposable> field, Disposable d) { |
| 167 |
if (!field.compareAndSet(null, d)) { |
| 168 |
if (field.get() == DISPOSED) { |
| 169 |
d.dispose(); |
| 170 |
} |
| 171 |
return false; |
| 172 |
} |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void dispose() { |
| 178 |
// deliberately no-op |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public boolean isDisposed() { |
| 183 |
return true; |
| 184 |
} |
| 185 |
} |