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