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)
| 369 | * @throws IllegalArgumentException If value cannot be converted to int |
| 370 | */ |
| 371 | public static Integer getInteger(Map<String, Object> map, String key) throws IllegalArgumentException { |
| 372 | Object value = map == null || key == null ? null : map.get(key); |
| 373 | if (value == null) { |
| 374 | return null; |
| 375 | } |
| 376 | |
| 377 | if (value instanceof Number) { |
| 378 | return ((Number) value).intValue(); |
| 379 | } |
| 380 | |
| 381 | if (value instanceof String) { |
| 382 | try { |
| 383 | return Integer.parseInt((String) value); |
| 384 | } catch (NumberFormatException e) { |
| 385 | throw new IllegalArgumentException("Cannot convert String value '" + value + "' to int: " + e.getMessage()); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to int"); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Get an int value from a Map |
no test coverage detected