| 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.subscriptions; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.AtomicInteger; |
| 17 |
|
| 18 |
import org.reactivestreams.Subscriber; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 21 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 22 |
|
| 23 |
/** |
| 24 |
* A Subscription that holds a constant value and emits it only when requested. |
| 25 |
* @param <T> the value type |
| 26 |
*/ |
| 27 |
public final class ScalarSubscription<T> extends AtomicInteger implements QueueSubscription<T> { |
| 28 |
|
| 29 |
private static final long serialVersionUID = -3830916580126663321L; |
| 30 |
/** The single value to emit, set to null. */ |
| 31 |
final T value; |
| 32 |
/** The actual subscriber. */ |
| 33 |
final Subscriber<? super T> subscriber; |
| 34 |
|
| 35 |
/** No request has been issued yet. */ |
| 36 |
static final int NO_REQUEST = 0; |
| 37 |
/** Request has been called.*/ |
| 38 |
static final int REQUESTED = 1; |
| 39 |
/** Cancel has been called. */ |
| 40 |
static final int CANCELLED = 2; |
| 41 |
|
| 42 |
public ScalarSubscription(Subscriber<? super T> subscriber, T value) { |
| 43 |
this.subscriber = subscriber; |
| 44 |
this.value = value; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public void request(long n) { |
| 49 |
if (!SubscriptionHelper.validate(n)) { |
| 50 |
return; |
| 51 |
} |
| 52 |
if (compareAndSet(NO_REQUEST, REQUESTED)) { |
| 53 |
Subscriber<? super T> s = subscriber; |
| 54 |
|
| 55 |
s.onNext(value); |
| 56 |
if (get() != CANCELLED) { |
| 57 |
s.onComplete(); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void cancel() { |
| 65 |
lazySet(CANCELLED); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns true if this Subscription was cancelled. |
| 70 |
* @return true if this Subscription was cancelled |
| 71 |
*/ |
| 72 |
public boolean isCancelled() { |
| 73 |
return get() == CANCELLED; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public boolean offer(T e) { |
| 78 |
throw new UnsupportedOperationException("Should not be called!"); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public boolean offer(T v1, T v2) { |
| 83 |
throw new UnsupportedOperationException("Should not be called!"); |
| 84 |
} |
| 85 |
|
| 86 |
@Nullable |
| 87 |
@Override |
| 88 |
public T poll() { |
| 89 |
if (get() == NO_REQUEST) { |
| 90 |
lazySet(REQUESTED); |
| 91 |
return value; |
| 92 |
} |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public boolean isEmpty() { |
| 98 |
return get() != NO_REQUEST; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void clear() { |
| 103 |
lazySet(1); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public int requestFusion(int mode) { |
| 108 |
return mode & SYNC; |
| 109 |
} |
| 110 |
} |