io.rsocket.internal.SwitchTransform Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rsocket-core Show documentation
Show all versions of rsocket-core Show documentation
Core functionality for the RSocket library
/*
* Copyright 2015-2018 the original author or authors.
*
* 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 io.rsocket.internal;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.function.BiFunction;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Operators;
public final class SwitchTransform extends Flux {
final Publisher extends T> source;
final BiFunction, Publisher extends R>> transformer;
public SwitchTransform(
Publisher extends T> source, BiFunction, Publisher extends R>> transformer) {
this.source = Objects.requireNonNull(source, "source");
this.transformer = Objects.requireNonNull(transformer, "transformer");
}
@Override
public void subscribe(CoreSubscriber super R> actual) {
Flux.from(source).subscribe(new SwitchTransformSubscriber<>(actual, transformer));
}
static final class SwitchTransformSubscriber implements CoreSubscriber {
@SuppressWarnings("rawtypes")
static final AtomicIntegerFieldUpdater ONCE =
AtomicIntegerFieldUpdater.newUpdater(SwitchTransformSubscriber.class, "once");
final CoreSubscriber super R> actual;
final BiFunction, Publisher extends R>> transformer;
final UnboundedProcessor processor = new UnboundedProcessor<>();
Subscription s;
volatile int once;
SwitchTransformSubscriber(
CoreSubscriber super R> actual,
BiFunction, Publisher extends R>> transformer) {
this.actual = actual;
this.transformer = transformer;
}
@Override
public void onSubscribe(Subscription s) {
if (Operators.validate(this.s, s)) {
this.s = s;
processor.onSubscribe(s);
}
}
@Override
public void onNext(T t) {
if (once == 0 && ONCE.compareAndSet(this, 0, 1)) {
try {
Publisher extends R> result =
Objects.requireNonNull(
transformer.apply(t, processor), "The transformer returned a null value");
Flux.from(result).subscribe(actual);
} catch (Throwable e) {
onError(Operators.onOperatorError(s, e, t, actual.currentContext()));
return;
}
}
processor.onNext(t);
}
@Override
public void onError(Throwable t) {
processor.onError(t);
}
@Override
public void onComplete() {
processor.onComplete();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy