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)
| 538 | * @throws IllegalArgumentException If value cannot be converted to double |
| 539 | */ |
| 540 | public static Double getDouble(Map<String, Object> map, String key) throws IllegalArgumentException { |
| 541 | Object value = map == null || key == null ? null : map.get(key); |
| 542 | if (value == null) { |
| 543 | return null; |
| 544 | } |
| 545 | |
| 546 | if (value instanceof Number) { |
| 547 | return ((Number) value).doubleValue(); |
| 548 | } |
| 549 | |
| 550 | if (value instanceof String) { |
| 551 | try { |
| 552 | return Double.parseDouble((String) value); |
| 553 | } catch (NumberFormatException e) { |
| 554 | throw new IllegalArgumentException("Cannot convert String value '" + value + "' to double: " + e.getMessage()); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to double"); |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Get a double value from a Map |