Get an int value from a Map @param map Source map @param key The key @return The int value @throws IllegalArgumentException If value cannot be converted to int
(Map<String, Object> map, String key)
| 397 | * @throws IllegalArgumentException If value cannot be converted to int |
| 398 | */ |
| 399 | public static int getIntValue(Map<String, Object> map, String key) throws IllegalArgumentException { |
| 400 | Object value = map == null || key == null ? null : map.get(key); |
| 401 | if (value == null) { |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | if (value instanceof Number) { |
| 406 | return ((Number) value).intValue(); |
| 407 | } |
| 408 | |
| 409 | if (value instanceof String) { |
| 410 | try { |
| 411 | return Integer.parseInt((String) value); |
| 412 | } catch (NumberFormatException e) { |
| 413 | throw new IllegalArgumentException("Cannot convert String value '" + value + "' to int: " + e.getMessage()); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to int"); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Get an int value from a Map |