All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.rsocket.internal.SwitchTransform Maven / Gradle / Ivy

There is a newer version: 1.1.4
Show newest version
/*
 * 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 source;
  final BiFunction, Publisher> transformer;

  public SwitchTransform(
      Publisher source, BiFunction, Publisher> transformer) {
    this.source = Objects.requireNonNull(source, "source");
    this.transformer = Objects.requireNonNull(transformer, "transformer");
  }

  @Override
  public void subscribe(CoreSubscriber 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 actual;
    final BiFunction, Publisher> transformer;
    final UnboundedProcessor processor = new UnboundedProcessor<>();
    Subscription s;
    volatile int once;

    SwitchTransformSubscriber(
        CoreSubscriber actual,
        BiFunction, Publisher> 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 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