hu.akarnokd.rxjava2.subjects.AsyncSubject Maven / Gradle / Ivy
Show all versions of rxjava2-backport Show documentation
/**
* Copyright 2015 David Karnok and Netflix, Inc.
*
* 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 hu.akarnokd.rxjava2.subjects;
import java.util.Arrays;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import hu.akarnokd.rxjava2.functions.IntFunction;
import hu.akarnokd.rxjava2.internal.util.*;
import hu.akarnokd.rxjava2.plugins.RxJavaPlugins;
/**
* A Subject that emits the very last value followed by a completion event or the received error to Subscribers.
*
* The implementation of onXXX methods are technically thread-safe but non-serialized calls
* to them may lead to undefined state in the currently subscribed Subscribers.
*
*
Due to the nature Observables are constructed, the AsyncSubject can't be instantiated through
* {@code new} but must be created via the {@link #create()} method.
*
* @param the value type
*/
public final class AsyncSubject extends Subject {
/**
* Constructs an empty AsyncSubject.
* @param the observed and observable value type
* @return the new AsyncSubject instance.
*/
public static AsyncSubject create() {
State state = new State();
return new AsyncSubject(state);
}
/** The state holding onto the latest value or error and the array of subscribers. */
final State state;
/**
* Indicates the subject has been terminated. It is checked in the onXXX methods in
* a relaxed matter: concurrent calls may not properly see it (which shouldn't happen if
* the reactive-streams contract is held).
*/
boolean done;
protected AsyncSubject(State state) {
super(state);
this.state = state;
}
@Override
public void onSubscribe(Subscription s) {
if (done) {
s.cancel();
return;
}
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(T t) {
if (done) {
return;
}
if (t == null) {
onError(new NullPointerException());
return;
}
state.lazySet(t);
}
@Override
public void onError(Throwable t) {
if (done) {
RxJavaPlugins.onError(t);
return;
}
if (t == null) {
t = new NullPointerException();
}
done = true;
state.lazySet(NotificationLite.error(t));
for (AsyncSubscription as : state.terminate()) {
as.setError(t);
}
}
@Override
public void onComplete() {
if (done) {
return;
}
done = true;
@SuppressWarnings("unchecked")
T value = (T)state.get();
for (AsyncSubscription as : state.terminate()) {
as.setValue(value);
}
}
@Override
public boolean hasSubscribers() {
return state.subscribers().length != 0;
}
@Override
public boolean hasValue() {
Object o = state.get();
return o != null && !NotificationLite.isError(o);
}
@Override
public boolean hasComplete() {
Object o = state.get();
return state.subscribers() == State.TERMINATED && !NotificationLite.isError(o);
}
@Override
public boolean hasThrowable() {
return NotificationLite.isError(state.get());
}
@Override
public Throwable getThrowable() {
Object o = state.get();
if (NotificationLite.isError(o)) {
return NotificationLite.getError(o);
}
return null;
}
@Override
@SuppressWarnings("unchecked")
public T getValue() {
Object o = state.get();
if (o != null && !NotificationLite.isError(o)) {
return (T)o;
}
return null;
}
@Override
@SuppressWarnings("unchecked")
public T[] getValues(T[] array) {
Object o = state.get();
if (o != null && !NotificationLite.isError(o) && !NotificationLite.isComplete(o)) {
int n = array.length;
if (n == 0) {
array = Arrays.copyOf(array, 1);
}
array[0] = (T)o;
if (array.length > 1) {
array[1] = null;
}
} else {
if (array.length != 0) {
array[0] = null;
}
}
return array;
}
/**
* The state of the AsyncSubject.
*
* @param the value type
*/
@SuppressWarnings("rawtypes")
static final class State extends AtomicReference