package reactor.core.publisher;
import java.io.Serializable;
import java.util.Objects;
import org.reactivestreams.Subscription;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;
import reactor.util.context.ContextView;
final class ImmutableSignal<T> implements Signal<T>, Serializable {
private static final long serialVersionUID = -2004454746525418508L;
private final transient ContextView contextView;
private final SignalType type;
private final Throwable throwable;
private final T value;
private transient final Subscription subscription;
ImmutableSignal(ContextView contextView, SignalType type, @Nullable T value, @Nullable Throwable e, @Nullable Subscription subscription) {
this.contextView = contextView;
this.value = value;
this.subscription = subscription;
this.throwable = e;
this.type = type;
}
@Override
@Nullable
public Throwable getThrowable() {
return throwable;
}
@Override
@Nullable
public Subscription getSubscription() {
return subscription;
}
@Override
@Nullable
public T get() {
return value;
}
@Override
public SignalType getType() {
return type;
}
@Override
public ContextView getContextView() {
return contextView;
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || !(o instanceof Signal)) {
return false;
}
Signal<?> signal = (Signal<?>) o;
if (getType() != signal.getType()) {
return false;
}
if (isOnComplete()) {
return true;
}
if (isOnSubscribe()) {
return Objects.equals(this.getSubscription(), signal.getSubscription());
}
if (isOnError()) {
return Objects.equals(this.getThrowable(), signal.getThrowable());
}
if (isOnNext()) {
return Objects.equals(this.get(), signal.get());
}
return false;
}
@Override
public int hashCode() {
int result = getType().hashCode();
if (isOnError()) {
return 31 * result + (getThrowable() != null ? getThrowable().hashCode() :
0);
}
if (isOnNext()) {
return 31 * result + (get() != null ? get().hashCode() : 0);
}
if (isOnSubscribe()) {
return 31 * result + (getSubscription() != null ?
getSubscription().hashCode() : 0);
}
return result;
}
@Override
public String toString() {
switch (this.getType()) {
case ON_SUBSCRIBE:
return String.format("onSubscribe(%s)", this.getSubscription());
case ON_NEXT:
return String.format("onNext(%s)", this.get());
case ON_ERROR:
return String.format("onError(%s)", this.getThrowable());
case ON_COMPLETE:
return "onComplete()";
default:
return String.format("Signal type=%s", this.getType());
}
}
private static final Signal<?> ON_COMPLETE =
new ImmutableSignal<>(Context.empty(), SignalType.ON_COMPLETE, null, null, null);
@SuppressWarnings("unchecked")
static <U> Signal<U> onComplete() {
return (Signal<U>) ON_COMPLETE;
}
}