| 1 | package spiffy.core.util; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Set; |
| 5 | |
| 6 | /** |
| 7 | * A 3-dimensional hashmap is a HashMap that enables you to refer to values via three keys rather than one. The |
| 8 | * underlying implementation is simply a HashMap containing HashMap containing a HashMap, each of which maps to values. |
| 9 | * |
| 10 | * @author Kasper B. Graversen |
| 11 | */ |
| 12 | public class ThreeDHashMap<K1, K2, K3, V> { |
| 13 | private final HashMap<K1, HashMap<K2, HashMap<K3, V>>> map = new HashMap<K1, HashMap<K2, HashMap<K3, V>>>(); |
| 14 | |
| 15 | /** |
| 16 | * Existence check of a value (or <tt>null</tt>) mapped to the keys. |
| 17 | * |
| 18 | * @param firstKey |
| 19 | * first key |
| 20 | * @param secondKey |
| 21 | * second key |
| 22 | * @return true when an element (or <tt>null</tt>) has been stored with the keys |
| 23 | */ |
| 24 | public boolean containsKey(final K1 firstKey, final K2 secondKey) { |
| 25 | // existence check on inner map |
| 26 | final HashMap<K2, HashMap<K3, V>> innerMap1 = map.get(firstKey); |
| 27 | if( innerMap1 == null ) { return false; } |
| 28 | |
| 29 | return innerMap1.containsKey(secondKey); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Existence check of a value (or <tt>null</tt>) mapped to the keys. |
| 34 | * |
| 35 | * @param firstKey |
| 36 | * first key |
| 37 | * @param secondKey |
| 38 | * second key |
| 39 | * @param thirdKey |
| 40 | * third key |
| 41 | * @return true when an element (or <tt>null</tt>) has been stored with the keys |
| 42 | */ |
| 43 | public boolean containsKey(final K1 firstKey, final K2 secondKey, final K3 thirdKey) { |
| 44 | // existence check on inner map |
| 45 | final HashMap<K2, HashMap<K3, V>> innerMap1 = map.get(firstKey); |
| 46 | if( innerMap1 == null ) { return false; } |
| 47 | |
| 48 | // existence check on inner map1 |
| 49 | final HashMap<K3, V> innerMap2 = innerMap1.get(secondKey); |
| 50 | if( innerMap2 == null ) { return false; } |
| 51 | return innerMap2.containsKey(thirdKey); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Fetch the outermost Hashmap . |
| 56 | * |
| 57 | * @param firstKey |
| 58 | * first key |
| 59 | * @return the the innermost hashmap |
| 60 | */ |
| 61 | public HashMap<K2, HashMap<K3, V>> get(final K1 firstKey) { |
| 62 | return map.get(firstKey); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Fetch the outermost Hashmap as a TwoDHashMap . |
| 67 | * |
| 68 | * @param firstKey |
| 69 | * first key |
| 70 | * @return the the innermost hashmap |
| 71 | */ |
| 72 | public TwoDHashMap<K2, K3, V> getAs2d(final K1 firstKey) { |
| 73 | return new TwoDHashMap<K2, K3, V>(map.get(firstKey)); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Fetch the innermost Hashmap . |
| 78 | * |
| 79 | * @param firstKey |
| 80 | * first key |
| 81 | * @param secondKey |
| 82 | * second key |
| 83 | * @return the the innermost hashmap |
| 84 | */ |
| 85 | public HashMap<K3, V> get(final K1 firstKey, final K2 secondKey) { |
| 86 | // existence check on inner map |
| 87 | final HashMap<K2, HashMap<K3, V>> innerMap1 = map.get(firstKey); |
| 88 | if( innerMap1 == null ) { return null; } |
| 89 | |
| 90 | return innerMap1.get(secondKey); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Fetch a value from the Hashmap . |
| 95 | * |
| 96 | * @param firstKey |
| 97 | * first key |
| 98 | * @param secondKey |
| 99 | * second key |
| 100 | * @param thirdKey |
| 101 | * third key |
| 102 | * @return the element or null. |
| 103 | */ |
| 104 | public V get(final K1 firstKey, final K2 secondKey, final K3 thirdKey) { |
| 105 | // existence check on inner map |
| 106 | final HashMap<K2, HashMap<K3, V>> innerMap1 = map.get(firstKey); |
| 107 | if( innerMap1 == null ) { return null; } |
| 108 | |
| 109 | // existence check on inner map1 |
| 110 | final HashMap<K3, V> innerMap2 = innerMap1.get(secondKey); |
| 111 | if( innerMap2 == null ) { return null; } |
| 112 | return innerMap2.get(thirdKey); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Insert a value |
| 117 | * |
| 118 | * @param firstKey |
| 119 | * first key |
| 120 | * @param secondKey |
| 121 | * second key |
| 122 | * @param thirdKey |
| 123 | * third key |
| 124 | * @param value |
| 125 | * the value to be inserted. <tt>null</tt> may be inserted as well. |
| 126 | * @return null or the value the insert is replacing. |
| 127 | */ |
| 128 | public Object set(final K1 firstKey, final K2 secondKey, final K3 thirdKey, final V value) { |
| 129 | // existence check on inner map1 |
| 130 | HashMap<K2, HashMap<K3, V>> innerMap1 = map.get(firstKey); |
| 131 | |
| 132 | if( innerMap1 == null ) { |
| 133 | // no inner map, create it |
| 134 | innerMap1 = new HashMap<K2, HashMap<K3, V>>(); |
| 135 | map.put(firstKey, innerMap1); |
| 136 | } |
| 137 | |
| 138 | // existence check on inner map1 |
| 139 | HashMap<K3, V> innerMap2 = innerMap1.get(secondKey); |
| 140 | if( innerMap2 == null ) { |
| 141 | // no inner map, create it |
| 142 | innerMap2 = new HashMap<K3, V>(); |
| 143 | innerMap1.put(secondKey, innerMap2); |
| 144 | } |
| 145 | |
| 146 | return innerMap2.put(thirdKey, value); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns the number of key-value mappings in this map for the first key. |
| 151 | * |
| 152 | * @return Returns the number of key-value mappings in this map for the first key. |
| 153 | */ |
| 154 | public int size() { |
| 155 | return map.size(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the number of key-value mappings in this map for the second key. |
| 160 | * |
| 161 | * @return Returns the number of key-value mappings in this map for the second key. |
| 162 | */ |
| 163 | public int size(final K1 firstKey) { |
| 164 | // existence check on inner map |
| 165 | final HashMap<K2, HashMap<K3, V>> innerMap = map.get(firstKey); |
| 166 | if( innerMap == null ) { return 0; } |
| 167 | return innerMap.size(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the number of key-value mappings in this map for the third key. |
| 172 | * |
| 173 | * @return Returns the number of key-value mappings in this map for the third key. |
| 174 | */ |
| 175 | public int size(final K1 firstKey, final K2 secondKey) { |
| 176 | // existence check on inner map |
| 177 | final HashMap<K2, HashMap<K3, V>> innerMap1 = map.get(firstKey); |
| 178 | if( innerMap1 == null ) { return 0; } |
| 179 | |
| 180 | // existence check on inner map1 |
| 181 | final HashMap<K3, V> innerMap2 = innerMap1.get(secondKey); |
| 182 | if( innerMap2 == null ) { return 0; } |
| 183 | return innerMap2.size(); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns a set of the keys of the outermost map. |
| 188 | */ |
| 189 | public Set<K1> keySet() { |
| 190 | return map.keySet(); |
| 191 | } |
| 192 | |
| 193 | } |