|
| 1 | +package com.javaquery.util.collection; |
| 2 | + |
| 3 | +import com.javaquery.util.Regex; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | + |
| 7 | +/** |
| 8 | + * @author vicky.thakor |
| 9 | + * @since 1.2.0 |
| 10 | + */ |
| 11 | +public class JHashMap<K, V> extends HashMap<K, V> { |
| 12 | + |
| 13 | + /** |
| 14 | + * @param key the key whose associated value is to be returned |
| 15 | + * @return the value to which the specified key is mapped, or 0 if this map contains no mapping for the key |
| 16 | + */ |
| 17 | + public Integer optInt(K key){ |
| 18 | + return optInt(key, 0); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @param key the key whose associated value is to be returned |
| 23 | + * @param defaultValue the default mapping of the key |
| 24 | + * @return the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key |
| 25 | + */ |
| 26 | + @SuppressWarnings("unchecked") |
| 27 | + public Integer optInt(K key, Integer defaultValue){ |
| 28 | + Object value = getOrDefault(key, (V) defaultValue); |
| 29 | + if(value instanceof Number){ |
| 30 | + return ((Number) value).intValue(); |
| 31 | + } |
| 32 | + String strValue = String.valueOf(value); |
| 33 | + return Regex.isNumber(strValue) ? Integer.parseInt(strValue) : defaultValue; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param key the key whose associated value is to be returned |
| 38 | + * @return the value to which the specified key is mapped, or 0 if this map contains no mapping for the key |
| 39 | + */ |
| 40 | + public Long optLong(K key){ |
| 41 | + return optLong(key, 0L); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param key the key whose associated value is to be returned |
| 46 | + * @param defaultValue the default mapping of the key |
| 47 | + * @return the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key |
| 48 | + */ |
| 49 | + @SuppressWarnings("unchecked") |
| 50 | + public Long optLong(K key, Long defaultValue){ |
| 51 | + Object value = getOrDefault(key, (V) defaultValue); |
| 52 | + if(value instanceof Number){ |
| 53 | + return ((Number) value).longValue(); |
| 54 | + } |
| 55 | + String strValue = String.valueOf(value); |
| 56 | + return Regex.isNumber(strValue) ? Long.parseLong(strValue) : defaultValue; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param key the key whose associated value is to be returned |
| 61 | + * @return the value to which the specified key is mapped, or 0 if this map contains no mapping for the key |
| 62 | + */ |
| 63 | + public Double optDouble(K key){ |
| 64 | + return optDouble(key, 0D); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param key the key whose associated value is to be returned |
| 69 | + * @param defaultValue the default mapping of the key |
| 70 | + * @return the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key |
| 71 | + */ |
| 72 | + @SuppressWarnings("unchecked") |
| 73 | + public Double optDouble(K key, Double defaultValue){ |
| 74 | + Object value = getOrDefault(key, (V) defaultValue); |
| 75 | + if(value instanceof Number){ |
| 76 | + return ((Number) value).doubleValue(); |
| 77 | + } |
| 78 | + String strValue = String.valueOf(value); |
| 79 | + return Regex.isNumber(strValue) ? Double.parseDouble(strValue) : defaultValue; |
| 80 | + } |
| 81 | +} |
0 commit comments