MCPcopy Create free account
hub / github.com/grpc/grpc-java / getNumberAsLong

Method getNumberAsLong

core/src/main/java/io/grpc/internal/JsonUtil.java:187–211  ·  view source on GitHub ↗

Gets a number from an object for the given key, casted to an long. If the key is not present, this returns null. If the value does not represent a long integer, throws an exception.

(Map<String, ?> obj, String key)

Source from the content-addressed store, hash-verified

185 * exception.
186 */
187 public static Long getNumberAsLong(Map<String, ?> obj, String key) {
188 assert key != null;
189 if (!obj.containsKey(key)) {
190 return null;
191 }
192 Object value = obj.get(key);
193 if (value instanceof Double) {
194 Double d = (Double) value;
195 long l = d.longValue();
196 if (l != d) {
197 throw new ClassCastException("Number expected to be long: " + d);
198 }
199 return l;
200 }
201 if (value instanceof String) {
202 try {
203 return Long.parseLong((String) value);
204 } catch (NumberFormatException e) {
205 throw new IllegalArgumentException(
206 String.format("value '%s' for key '%s' is not a long integer", value, key));
207 }
208 }
209 throw new IllegalArgumentException(
210 String.format("value '%s' for key '%s' is not a long integer", value, key));
211 }
212
213 /**
214 * Gets a string from an object for the given key. If the key is not present, this returns null.

Calls 2

containsKeyMethod · 0.80
getMethod · 0.65

Tested by 3

getNumberMethod · 0.76
leastRequest_legacyMethod · 0.76