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

com.github.thinkerou.karate.grpc.ComponentObserver Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
package com.github.thinkerou.karate.grpc;

import java.util.logging.Logger;

import com.google.common.collect.ImmutableList;

import io.grpc.stub.StreamObserver;

/**
 * ComponentObserver
 *
 * A StreamObserver which groups multiple observers and executes them all.
 *
 * @author thinkerou
 */
public final class ComponentObserver implements StreamObserver {

    private static final Logger logger = Logger.getLogger(ComponentObserver.class.getName());

    private final ImmutableList> observers;

    @SafeVarargs
    public static  ComponentObserver of(StreamObserver... observers) {
        return new ComponentObserver<>(ImmutableList.copyOf(observers));
    }

    private ComponentObserver(ImmutableList> observers) {
        this.observers = observers;
    }

    @Override
    public void onCompleted() {
        observers.forEach(tStreamObserver -> {
            try {
                tStreamObserver.onCompleted();
            } catch (Throwable t) {
                logger.warning(t.getMessage());
            }
        });
    }

    @Override
    public void onError(Throwable t) {
        observers.forEach(tStreamObserver -> {
            try {
                tStreamObserver.onError(t);
            } catch (Throwable e) {
                logger.warning(e.getMessage());
            }
        });
    }

    @Override
    public void onNext(T value) {
        observers.forEach(tStreamObserver -> {
            try {
                tStreamObserver.onNext(value);
            } catch (Throwable t) {
                logger.warning(t.getMessage());
            }
        });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy