| 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.concurrent.atomic.AtomicReference; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 19 |
|
| 20 |
/** |
| 21 |
* A Disposable container that allows updating/replacing a Disposable |
| 22 |
* atomically and with respect of disposing the container itself. |
| 23 |
* <p> |
| 24 |
* The class extends AtomicReference directly so watch out for the API leak! |
| 25 |
* @since 2.0 |
| 26 |
*/ |
| 27 |
public final class SequentialDisposable |
| 28 |
extends AtomicReference<Disposable> |
| 29 |
implements Disposable { |
| 30 |
|
| 31 |
private static final long serialVersionUID = -754898800686245608L; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructs an empty SequentialDisposable. |
| 35 |
*/ |
| 36 |
public SequentialDisposable() { |
| 37 |
// nothing to do |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Construct a SequentialDisposable with the initial Disposable provided. |
| 42 |
* @param initial the initial disposable, null allowed |
| 43 |
*/ |
| 44 |
public SequentialDisposable(Disposable initial) { |
| 45 |
lazySet(initial); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Atomically: set the next disposable on this container and dispose the previous |
| 50 |
* one (if any) or dispose next if the container has been disposed. |
| 51 |
* @param next the Disposable to set, may be null |
| 52 |
* @return true if the operation succeeded, false if the container has been disposed |
| 53 |
* @see #replace(Disposable) |
| 54 |
*/ |
| 55 |
public boolean update(Disposable next) { |
| 56 |
return DisposableHelper.set(this, next); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Atomically: set the next disposable on this container but don't dispose the previous |
| 61 |
* one (if any) or dispose next if the container has been disposed. |
| 62 |
* @param next the Disposable to set, may be null |
| 63 |
* @return true if the operation succeeded, false if the container has been disposed |
| 64 |
* @see #update(Disposable) |
| 65 |
*/ |
| 66 |
public boolean replace(Disposable next) { |
| 67 |
return DisposableHelper.replace(this, next); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void dispose() { |
| 72 |
DisposableHelper.dispose(this); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public boolean isDisposed() { |
| 77 |
return DisposableHelper.isDisposed(get()); |
| 78 |
} |
| 79 |
} |