Gets a string from an object for the given key. If the key is not present, this returns null. If the value is not a String, throws an exception.
(Map<String, ?> obj, String key)
| 215 | * If the value is not a String, throws an exception. |
| 216 | */ |
| 217 | @Nullable |
| 218 | public static String getString(Map<String, ?> obj, String key) { |
| 219 | assert key != null; |
| 220 | if (!obj.containsKey(key)) { |
| 221 | return null; |
| 222 | } |
| 223 | Object value = obj.get(key); |
| 224 | if (!(value instanceof String)) { |
| 225 | throw new ClassCastException( |
| 226 | String.format("value '%s' for key '%s' in '%s' is not String", value, key, obj)); |
| 227 | } |
| 228 | return (String) value; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Gets a string from an object for the given key, parsed as a duration (defined by protobuf). If |