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

io.micronaut.rxjava3.instrument.RxInstrumentedWrappers Maven / Gradle / Ivy

There is a newer version: 3.6.0
Show newest version
/*
 * Copyright 2017-2020 original 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
 *
 * https://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.micronaut.rxjava3.instrument;

import io.micronaut.core.annotation.Internal;
import io.micronaut.scheduling.instrument.InvocationInstrumenter;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.CompletableObserver;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.FlowableSubscriber;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.MaybeObserver;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleObserver;
import io.reactivex.rxjava3.flowables.ConnectableFlowable;
import io.reactivex.rxjava3.observables.ConnectableObservable;
import io.reactivex.rxjava3.parallel.ParallelFlowable;
import org.reactivestreams.Subscriber;

/**
 * Inspired by code in Brave. Provides general instrumentation abstraction for RxJava2.
 * See https://github.com/openzipkin/brave/tree/master/context/rxjava2/src/main/java/brave/context/rxjava2/internal.
 *
 * @author graemerocher
 * @since 1.1
 */
@Internal
final class RxInstrumentedWrappers {

    /**
     * Wrap a subscriber.
     *
     * @param downstream          The downstream subscriber
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */
    static  Subscriber wrap(Subscriber downstream, RxInstrumenterFactory instrumenterFactory) {
        if (downstream instanceof RxInstrumentedComponent) {
            return downstream;
        }

        final InvocationInstrumenter instumenter = instrumenterFactory.create();
        if (instumenter != null) {
            if (downstream instanceof FlowableSubscriber) {
                return new RxInstrumentedFlowableSubscriber<>(downstream, instrumenterFactory);
            } else {
                return new RxInstrumentedSubscriber<>(downstream, instrumenterFactory);
            }
        } else {
            return downstream;
        }
    }

    /**
     * Wrap a completable source.
     *
     * @param source              The source
     * @param instrumenterFactory The instrumenterFactory
     * @return The wrapped subscriber
     */
    static Completable wrap(Completable source, RxInstrumenterFactory instrumenterFactory) {
        if (source instanceof RxInstrumentedComponent) {
            return source;
        }
        final InvocationInstrumenter instumenter = instrumenterFactory.create();
        if (instumenter == null) {
            return source;
        }
        return new RxInstrumentedCompletable(source, instumenter);
    }

    /**
     * Wrap a maybe.
     *
     * @param source              The source
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */
    static  Maybe wrap(Maybe source, RxInstrumenterFactory instrumenterFactory) {
        if (source instanceof RxInstrumentedComponent) {
            return source;
        }
        final InvocationInstrumenter instumenter = instrumenterFactory.create();
        if (instumenter == null) {
            return source;
        }
        return new RxInstrumentedMaybe<>(source, instumenter);
    }

    /**
     * Wrap a single.
     *
     * @param source              The source
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */
    static  Single wrap(Single source, RxInstrumenterFactory instrumenterFactory) {
        if (source instanceof RxInstrumentedComponent) {
            return source;
        }
        final InvocationInstrumenter instumenter = instrumenterFactory.create();
        if (instumenter == null) {
            return source;
        }
        return new RxInstrumentedSingle<>(source, instumenter);
    }

    /**
     * Wrap a observable.
     *
     * @param source              The source
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */
    static  Observable wrap(Observable source, RxInstrumenterFactory instrumenterFactory) {
        if (source instanceof RxInstrumentedComponent) {
            return source;
        }
        final InvocationInstrumenter instumenter = instrumenterFactory.create();
        if (instumenter == null) {
            return source;
        }
        return new RxInstrumentedObservable<>(source, instumenter);
    }

    /**
     * Wrap a connectable observable.
     *
     * @param source              The source
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */
    static  ConnectableObservable wrap(ConnectableObservable source, RxInstrumenterFactory instrumenterFactory) {
        if (source instanceof RxInstrumentedComponent) {
            return source;
        }
        final InvocationInstrumenter instumenter = instrumenterFactory.create();
        if (instumenter == null) {
            return source;
        }
        return new RxInstrumentedConnectableObservable<>(source, instumenter);
    }

    /**
     * Wrap a flowable.
     *
     * @param source              The source
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */
    static  Flowable wrap(Flowable source, RxInstrumenterFactory instrumenterFactory) {
        if (source instanceof RxInstrumentedComponent) {
            return source;
        }
        final InvocationInstrumenter instumenter = instrumenterFactory.create();
        if (instumenter == null) {
            return source;
        }
        return new RxInstrumentedFlowable<>(source, instumenter);
    }

    /**
     * Wrap a flowable.
     *
     * @param source              The source
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */

    static  ConnectableFlowable wrap(ConnectableFlowable source, RxInstrumenterFactory instrumenterFactory) {
        if (source instanceof RxInstrumentedComponent) {
            return source;
        }
        final InvocationInstrumenter instumenter = instrumenterFactory.create();
        if (instumenter == null) {
            return source;
        }
        return new RxInstrumentedConnectableFlowable<>(source, instumenter);
    }

    /**
     * Wrap a parallel flowable.
     *
     * @param source              The source
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */

    static  ParallelFlowable wrap(ParallelFlowable source, RxInstrumenterFactory instrumenterFactory) {
        if (source instanceof RxInstrumentedComponent) {
            return source;
        }
        return new RxInstrumentedParallelFlowable<>(source, instrumenterFactory);
    }

    /**
     * Wrap a observer.
     *
     * @param downstream          The downstream observer
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */
    static  Observer wrap(Observer downstream, RxInstrumenterFactory instrumenterFactory) {
        if (downstream instanceof RxInstrumentedComponent) {
            return downstream;
        }
        final InvocationInstrumenter instrumenter = instrumenterFactory.create();
        if (instrumenter != null) {
            return new RxInstrumentedObserver<>(downstream, instrumenterFactory);
        }
        return downstream;
    }

    /**
     * Wrap a observer.
     *
     * @param downstream          The downstream observer
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */
    static  SingleObserver wrap(SingleObserver downstream, RxInstrumenterFactory instrumenterFactory) {
        if (downstream instanceof RxInstrumentedComponent) {
            return downstream;
        }
        final InvocationInstrumenter instrumenter = instrumenterFactory.create();
        if (instrumenter != null) {
            return new RxInstrumentedSingleObserver<>(downstream, instrumenterFactory);
        }
        return downstream;
    }

    /**
     * Wrap a observer.
     *
     * @param downstream          The downstream observer
     * @param instrumenterFactory The instrumenterFactory
     * @param                  The type
     * @return The wrapped subscriber
     */
    static  MaybeObserver wrap(MaybeObserver downstream, RxInstrumenterFactory instrumenterFactory) {
        if (downstream instanceof RxInstrumentedComponent) {
            return downstream;
        }
        final InvocationInstrumenter instrumenter = instrumenterFactory.create();
        if (instrumenter != null) {
            return new RxInstrumentedMaybeObserver<>(downstream, instrumenterFactory);
        }
        return downstream;
    }

    /**
     * Wrap a observer.
     *
     * @param downstream          The downstream observer
     * @param instrumenterFactory The instrumenterFactory
     * @return The wrapped subscriber
     */
    static CompletableObserver wrap(CompletableObserver downstream, RxInstrumenterFactory instrumenterFactory) {
        if (downstream instanceof RxInstrumentedComponent) {
            return downstream;
        }
        final InvocationInstrumenter instrumenter = instrumenterFactory.create();
        if (instrumenter != null) {
            return new RxInstrumentedCompletableObserver(downstream, instrumenterFactory);
        } else {
            return downstream;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy