zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 1 | package com.google.net.stubby; |
| 2 | |
| 3 | import com.google.common.base.Preconditions; |
| 4 | import com.google.common.util.concurrent.MoreExecutors; |
| 5 | import com.google.common.util.concurrent.Service; |
| 6 | import com.google.net.stubby.newtransport.ClientTransportFactory; |
| 7 | |
| 8 | import java.util.concurrent.ExecutorService; |
| 9 | |
| 10 | import javax.annotation.Nullable; |
| 11 | |
| 12 | /** |
| 13 | * The base class for channel builders. |
nathanmittler | 0304b3d | 2014-10-24 13:39:13 -0700 | [diff] [blame^] | 14 | * |
| 15 | * @param <BuilderT> The concrete type of this builder. |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 16 | */ |
nathanmittler | 0304b3d | 2014-10-24 13:39:13 -0700 | [diff] [blame^] | 17 | public abstract class AbstractChannelBuilder<BuilderT extends AbstractChannelBuilder<?>> |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 18 | 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 | } |