blob: 0c5348b5a5bd5020a927cdf08814c20561956d48 [file] [log] [blame]
zhangkun8d6d12e2014-10-15 13:04:19 -07001package com.google.net.stubby;
2
3import com.google.common.base.Preconditions;
4import com.google.common.util.concurrent.MoreExecutors;
5import com.google.common.util.concurrent.Service;
6import com.google.net.stubby.newtransport.ClientTransportFactory;
7
8import java.util.concurrent.ExecutorService;
9
10import javax.annotation.Nullable;
11
12/**
13 * The base class for channel builders.
nathanmittler0304b3d2014-10-24 13:39:13 -070014 *
15 * @param <BuilderT> The concrete type of this builder.
zhangkun8d6d12e2014-10-15 13:04:19 -070016 */
nathanmittler0304b3d2014-10-24 13:39:13 -070017public abstract class AbstractChannelBuilder<BuilderT extends AbstractChannelBuilder<?>>
zhangkun8d6d12e2014-10-15 13:04:19 -070018 extends AbstractServiceBuilder<ChannelImpl, BuilderT> {
19
20 @Override
21 protected final ChannelImpl buildImpl(ExecutorService executor) {
22 ChannelEssentials essentials = buildEssentials();
23 ChannelImpl channel = new ChannelImpl(essentials.transportFactory, executor);
24 if (essentials.listener != null) {
25 channel.addListener(essentials.listener, MoreExecutors.directExecutor());
26 }
27 return channel;
28 }
29
30 /**
31 * The essentials required for creating a channel.
32 */
33 protected static class ChannelEssentials {
34 final ClientTransportFactory transportFactory;
35 @Nullable final Service.Listener listener;
36
37 /**
38 * @param transportFactory the created channel uses this factory to create transports
39 * @param listener will be called at the channel's life-cycle events
40 */
41 public ChannelEssentials(ClientTransportFactory transportFactory,
42 @Nullable Service.Listener listener) {
43 this.transportFactory = Preconditions.checkNotNull(transportFactory);
44 this.listener = listener;
45 }
46 }
47
48 protected abstract ChannelEssentials buildEssentials();
49}