1 /* 2 * Copyright 2019 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.stub; 18 19 import io.grpc.CallOptions; 20 import io.grpc.Channel; 21 import io.grpc.stub.ClientCalls.StubType; 22 import javax.annotation.CheckReturnValue; 23 import javax.annotation.concurrent.ThreadSafe; 24 25 /** 26 * Stub implementations for future stubs. 27 * 28 * <p>DO NOT MOCK: Customizing options doesn't work properly in mocks. Use InProcessChannelBuilder 29 * to create a real channel suitable for testing. It is also possible to mock Channel instead. 30 * 31 * @since 1.26.0 32 */ 33 @ThreadSafe 34 @CheckReturnValue 35 public abstract class AbstractFutureStub<S extends AbstractFutureStub<S>> extends AbstractStub<S> { 36 AbstractFutureStub(Channel channel, CallOptions callOptions)37 protected AbstractFutureStub(Channel channel, CallOptions callOptions) { 38 super(channel, callOptions); 39 } 40 41 /** 42 * Returns a new future stub with the given channel for the provided method configurations. 43 * 44 * @since 1.26.0 45 * @param factory the factory to create a future stub 46 * @param channel the channel that this stub will use to do communications 47 */ newStub( StubFactory<T> factory, Channel channel)48 public static <T extends AbstractStub<T>> T newStub( 49 StubFactory<T> factory, Channel channel) { 50 return newStub(factory, channel, CallOptions.DEFAULT); 51 } 52 53 /** 54 * Returns a new future stub with the given channel for the provided method configurations. 55 * 56 * @since 1.26.0 57 * @param factory the factory to create a future stub 58 * @param channel the channel that this stub will use to do communications 59 * @param callOptions the runtime call options to be applied to every call on this stub 60 * @return a future stub 61 */ newStub( StubFactory<T> factory, Channel channel, CallOptions callOptions)62 public static <T extends AbstractStub<T>> T newStub( 63 StubFactory<T> factory, Channel channel, CallOptions callOptions) { 64 T stub = factory.newStub( 65 channel, callOptions.withOption(ClientCalls.STUB_TYPE_OPTION, StubType.FUTURE)); 66 assert stub instanceof AbstractFutureStub 67 : String.format("Expected AbstractFutureStub, but got %s.", stub.getClass()); 68 return stub; 69 } 70 } 71