| 1 | package spiffy.core.util; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Set; |
| 5 | |
| 6 | /** |
| 7 | * A two-dimensional hashmap, is a HashMap that enables you to refer to values via two keys rather than one. The |
| 8 | * underlying implementation is simply a HashMap containing HashMap, each of which maps to values. |
| 9 | * <P> |
| 10 | * This is quite useful e.g. when wanting to cache values such as "currency ration" based keys "day of year" and "year". |
| 11 | * |
| 12 | * @see java.util.HashMap |
| 13 | * @author Kasper B. Graversen |
| 14 | */ |
| 15 | public class TwoDHashMap<K1, K2, V> { |
| 16 | private HashMap<K1, HashMap<K2, V>> map = new HashMap<K1, HashMap<K2, V>>(); |
| 17 | |
| 18 | public TwoDHashMap() { |
| 19 | |
| 20 | } |
| 21 | |
| 22 | public TwoDHashMap(final HashMap<K1, HashMap<K2, V>> map) { |
| 23 | this.map = map; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Existence check of a value (or <tt>null</tt>) mapped to the keys. |
| 28 | * |
| 29 | * @param firstKey |
| 30 | * first key |
| 31 | * @param secondKey |
| 32 | * second key |
| 33 | * @return true when an element (or <tt>null</tt>) has been stored with the keys |
| 34 | */ |
| 35 | public boolean containsKey(final K1 firstKey, final K2 secondKey) { |
| 36 | // existence check on inner map |
| 37 | final HashMap<K2, V> innerMap = map.get(firstKey); |
| 38 | if( innerMap == null ) { return false; } |
| 39 | return innerMap.containsKey(secondKey); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Fetch a value from the Hashmap . |
| 44 | * |
| 45 | * @param firstKey |
| 46 | * first key |
| 47 | * @param secondKey |
| 48 | * second key |
| 49 | * @return the element or null. |
| 50 | */ |
| 51 | public V get(final K1 firstKey, final K2 secondKey) { |
| 52 | // existence check on inner map |
| 53 | final HashMap<K2, V> innerMap = map.get(firstKey); |
| 54 | if( innerMap == null ) { return null; } |
| 55 | return innerMap.get(secondKey); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Insert a value |
| 60 | * |
| 61 | * @param firstKey |
| 62 | * first key |
| 63 | * @param secondKey |
| 64 | * second key |
| 65 | * @param value |
| 66 | * the value to be inserted. <tt>null</tt> may be inserted as well. |
| 67 | * @return null or the value the insert is replacing. |
| 68 | */ |
| 69 | public Object set(final K1 firstKey, final K2 secondKey, final V value) { |
| 70 | // existence check on inner map |
| 71 | HashMap<K2, V> innerMap = map.get(firstKey); |
| 72 | |
| 73 | if( innerMap == null ) { |
| 74 | // no inner map, create it |
| 75 | innerMap = new HashMap<K2, V>(); |
| 76 | map.put(firstKey, innerMap); |
| 77 | } |
| 78 | |
| 79 | return innerMap.put(secondKey, value); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the number of key-value mappings in this map for the first key. |
| 84 | * |
| 85 | * @return Returns the number of key-value mappings in this map for the first key. |
| 86 | */ |
| 87 | public int size() { |
| 88 | return map.size(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the number of key-value mappings in this map for the second key. |
| 93 | * |
| 94 | * @return Returns the number of key-value mappings in this map for the second key. |
| 95 | */ |
| 96 | public int size(final K1 firstKey) { |
| 97 | // existence check on inner map |
| 98 | final HashMap<K2, V> innerMap = map.get(firstKey); |
| 99 | if( innerMap == null ) { return 0; } |
| 100 | return innerMap.size(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns a set of the keys of the outermost map. |
| 105 | */ |
| 106 | public Set<K1> keySet() { |
| 107 | return map.keySet(); |
| 108 | } |
| 109 | |
| 110 | } |