• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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;
18 
19 /**
20  * Definition of a method exposed by a {@link Server}.
21  *
22  * @see ServerServiceDefinition
23  */
24 public final class ServerMethodDefinition<ReqT, RespT> {
25   private final MethodDescriptor<ReqT, RespT> method;
26   private final ServerCallHandler<ReqT, RespT> handler;
27 
ServerMethodDefinition(MethodDescriptor<ReqT, RespT> method, ServerCallHandler<ReqT, RespT> handler)28   private ServerMethodDefinition(MethodDescriptor<ReqT, RespT> method,
29       ServerCallHandler<ReqT, RespT> handler) {
30     this.method = method;
31     this.handler = handler;
32   }
33 
34   /**
35    * Create a new instance.
36    *
37    * @param method the {@link MethodDescriptor} for this method.
38    * @param handler to dispatch calls to.
39    * @return a new instance.
40    */
create( MethodDescriptor<ReqT, RespT> method, ServerCallHandler<ReqT, RespT> handler)41   public static <ReqT, RespT> ServerMethodDefinition<ReqT, RespT> create(
42       MethodDescriptor<ReqT, RespT> method,
43       ServerCallHandler<ReqT, RespT> handler) {
44     return new ServerMethodDefinition<ReqT, RespT>(method, handler);
45   }
46 
47   /** The {@code MethodDescriptor} for this method. */
getMethodDescriptor()48   public MethodDescriptor<ReqT, RespT> getMethodDescriptor() {
49     return method;
50   }
51 
52   /** Handler for incoming calls. */
getServerCallHandler()53   public ServerCallHandler<ReqT, RespT> getServerCallHandler() {
54     return handler;
55   }
56 
57   /**
58    * Create a new method definition with a different call handler.
59    *
60    * @param handler to bind to a cloned instance of this.
61    * @return a cloned instance of this with the new handler bound.
62    */
withServerCallHandler( ServerCallHandler<ReqT, RespT> handler)63   public ServerMethodDefinition<ReqT, RespT> withServerCallHandler(
64       ServerCallHandler<ReqT, RespT> handler) {
65     return new ServerMethodDefinition<ReqT, RespT>(method, handler);
66   }
67 }
68