RpcUtil.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
38 public final class RpcUtil {
39  private RpcUtil() {}
40 
45  @SuppressWarnings("unchecked")
46  public static <Type extends Message> RpcCallback<Type> specializeCallback(
47  final RpcCallback<Message> originalCallback) {
48  return (RpcCallback<Type>) originalCallback;
49  // The above cast works, but only due to technical details of the Java
50  // implementation. A more theoretically correct -- but less efficient --
51  // implementation would be as follows:
52  // return new RpcCallback<Type>() {
53  // public void run(Type parameter) {
54  // originalCallback.run(parameter);
55  // }
56  // };
57  }
58 
66  public static <Type extends Message> RpcCallback<Message> generalizeCallback(
67  final RpcCallback<Type> originalCallback,
68  final Class<Type> originalClass,
69  final Type defaultInstance) {
70  return new RpcCallback<Message>() {
71  @Override
72  public void run(final Message parameter) {
73  Type typedParameter;
74  try {
75  typedParameter = originalClass.cast(parameter);
76  } catch (ClassCastException ignored) {
77  typedParameter = copyAsType(defaultInstance, parameter);
78  }
79  originalCallback.run(typedParameter);
80  }
81  };
82  }
83 
88  @SuppressWarnings("unchecked")
89  private static <Type extends Message> Type copyAsType(
90  final Type typeDefaultInstance, final Message source) {
91  return (Type) typeDefaultInstance.newBuilderForType().mergeFrom(source).build();
92  }
93 
99  public static <ParameterType> RpcCallback<ParameterType> newOneTimeCallback(
100  final RpcCallback<ParameterType> originalCallback) {
101  return new RpcCallback<ParameterType>() {
102  private boolean alreadyCalled = false;
103 
104  @Override
105  public void run(final ParameterType parameter) {
106  synchronized (this) {
107  if (alreadyCalled) {
108  throw new AlreadyCalledException();
109  }
110  alreadyCalled = true;
111  }
112 
113  originalCallback.run(parameter);
114  }
115  };
116  }
117 
119  public static final class AlreadyCalledException extends RuntimeException {
120  private static final long serialVersionUID = 5469741279507848266L;
121 
123  super("This RpcCallback was already called and cannot be called multiple times.");
124  }
125  }
126 }
com.google.protobuf.RpcCallback.run
void run(ParameterType parameter)
com.google.protobuf.RpcUtil
Definition: RpcUtil.java:38
com.google.protobuf.RpcUtil.newOneTimeCallback
static< ParameterType > RpcCallback< ParameterType > newOneTimeCallback(final RpcCallback< ParameterType > originalCallback)
Definition: RpcUtil.java:99
com.google.protobuf.Descriptors.FieldDescriptor.Type
Definition: Descriptors.java:1215
com.google.protobuf.RpcUtil.RpcUtil
RpcUtil()
Definition: RpcUtil.java:39
com.google.protobuf.RpcUtil.AlreadyCalledException.AlreadyCalledException
AlreadyCalledException()
Definition: RpcUtil.java:122
com.google.protobuf.RpcUtil.specializeCallback
static< Type extends Message > RpcCallback< Type > specializeCallback(final RpcCallback< Message > originalCallback)
Definition: RpcUtil.java:46
com.google.protobuf.RpcCallback
Definition: RpcCallback.java:44
com.google.protobuf.RpcUtil.generalizeCallback
static< Type extends Message > RpcCallback< Message > generalizeCallback(final RpcCallback< Type > originalCallback, final Class< Type > originalClass, final Type defaultInstance)
Definition: RpcUtil.java:66
source
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:3072
com.google.protobuf.RpcUtil.AlreadyCalledException.serialVersionUID
static final long serialVersionUID
Definition: RpcUtil.java:120
com.google.protobuf.RpcUtil.copyAsType
static< Type extends Message > Type copyAsType(final Type typeDefaultInstance, final Message source)
Definition: RpcUtil.java:89
com.google.protobuf.RpcUtil.AlreadyCalledException
Definition: RpcUtil.java:119
com.google.protobuf.Message
Definition: Message.java:50


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58