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

cn.nextop.gadget.etcd.impl.ClientStub Maven / Gradle / Ivy

The newest version!
package cn.nextop.gadget.etcd.impl;

import static cn.nextop.gadget.etcd.support.util.Objects.cast;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import cn.nextop.gadget.etcd.support.util.Strings;
import com.google.protobuf.AbstractMessage;

import cn.nextop.gadget.etcd.Client;
import cn.nextop.gadget.etcd.support.observer.PluralStreamObserver;
import cn.nextop.gadget.etcd.support.observer.SingleStreamObserver;
import io.grpc.ManagedChannel;
import io.grpc.stub.StreamObserver;

/**
 * @author Baoyi Chen
 */
public abstract class ClientStub implements Client.Stub {
	//
	protected final String name;
	protected final ClientImpl client;
	
	/**
	 * 
	 */
	public ClientStub(String name, ClientImpl client) {
		this.name = name; this.client = client;
	}
	
	/**
	 * 
	 */
	@Override
	public String getName() {
		return this.name;
	}
	
	@Override
	public ClientImpl getClient() {
		return this.client;
	}
	
	@Override
	public String toString() {
		return Strings.build(this)
		.append("name", name).toString();
	}
	
	/**
	 * 
	 */
	protected  T create(Function factory) {
		final T r = factory.apply(client.getChannel()); return r;
	}
	
	/**
	 * 
	 */
	protected final  SingleStreamObserver single(AbstractMessage request) {
		StreamObserver r = new SingleStreamObserver(); return cast(r);
	}
	
	protected final  PluralStreamObserver plural(AbstractMessage request) {
		StreamObserver r = new PluralStreamObserver(); return cast(r);
	}
	
	/**
	 * 
	 */
	protected  CompletableFuture invoke (final Supplier> src, Consumer> dst) {
		final SingleStreamObserver observer = src.get(); dst.accept(observer); return observer.future();
	}
	
	protected  CompletableFuture> invokes(Supplier> src, Consumer> dst) {
		final PluralStreamObserver observer = src.get(); dst.accept(observer); return observer.future();
	}
}