Get a double value from a Map @param map Source map @param key The key @return The double value @throws IllegalArgumentException If value cannot be converted to double
(Map<String, Object> map, String key)
| 566 | * @throws IllegalArgumentException If value cannot be converted to double |
| 567 | */ |
| 568 | public static double getDoubleValue(Map<String, Object> map, String key) throws IllegalArgumentException { |
| 569 | Object value = map == null || key == null ? null : map.get(key); |
| 570 | if (value == null) { |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | if (value instanceof Number) { |
| 575 | return ((Number) value).doubleValue(); |
| 576 | } |
| 577 | |
| 578 | if (value instanceof String) { |
| 579 | try { |
| 580 | return Double.parseDouble((String) value); |
| 581 | } catch (NumberFormatException e) { |
| 582 | throw new IllegalArgumentException("Cannot convert String value '" + value + "' to double: " + e.getMessage()); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to double"); |
| 587 | } |
| 588 | |
| 589 | |
| 590 | /** |