Get a long value from a Map @param map Source map @param key The key @return The long value @throws IllegalArgumentException If value cannot be converted to long
(Map<String, Object> map, String key)
| 453 | * @throws IllegalArgumentException If value cannot be converted to long |
| 454 | */ |
| 455 | public static long getLongValue(Map<String, Object> map, String key) throws IllegalArgumentException { |
| 456 | Object value = map == null || key == null ? null : map.get(key); |
| 457 | if (value == null) { |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | if (value instanceof Number) { |
| 462 | return ((Number) value).longValue(); |
| 463 | } |
| 464 | |
| 465 | if (value instanceof String) { |
| 466 | try { |
| 467 | return Long.parseLong((String) value); |
| 468 | } catch (NumberFormatException e) { |
| 469 | throw new IllegalArgumentException("Cannot convert String value '" + value + "' to long: " + e.getMessage()); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to long"); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Get a double value from a Map |