- Timestamp:
- 2003-05-27T15:40:47+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/util/HashMap3D.java
r4293 r4364 57 57 /** Provides a HashMap implementation that indexes by two keys. Perfect for the storage of metadata references based on their metadata element and assigned value. */ 58 58 public class HashMap3D 59 59 extends HashMap { 60 60 61 61 private int capacity = 4; 62 62 63 64 65 63 private Object last_key_one = null; 64 private Object last_key_two = null; 65 private Object last_value = null; 66 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 67 /** Default constructor. 68 */ 69 public HashMap3D() { 70 super(); 71 } 72 /** Constructor with a specific initial capacity, as we should already have a good idea based on the number of Metadata Elements. 73 * @param capacity The initial capacity as an <i>int</i>. 74 */ 75 public HashMap3D(int capacity) { 76 super(capacity); 77 this.capacity = capacity; 78 } 79 /** Completely remove the contents of this HashMap and its child HashMaps. */ 80 public void clear() { 81 Iterator iterator = values().iterator(); 82 while(iterator.hasNext()) { 83 HashMap inner_mapping = (HashMap) iterator.next(); 84 inner_mapping.clear(); 85 inner_mapping = null; 86 } 87 iterator = null; 88 super.clear(); 89 } 90 /** Determine if this hash map contains an entry for the given keys. Also cache this entry because theres a good chance the next get call will ask for this entry. 91 * @param key_one The first key as an <strong>Object</strong>. 92 * @param key_two The second key as an <strong>Object</strong>. 93 * @return <i>true</i> if such an entry exists, <i>false</i> otherwise. 94 */ 95 public boolean contains(Object key_one, Object key_two) { 96 boolean result = false; 97 // Retrieve the hash mapping at key_one. 98 HashMap map = (HashMap) get(key_one); 99 // If there is such a map then retrieve the value at key_two. 100 if(map != null) { 101 last_value = map.get(key_two); 102 if(last_value != null) { 103 last_key_one = key_one; 104 last_key_two = key_two; 105 result = true; 106 } 107 } 108 return result; 109 } 110 110 111 112 113 114 115 116 117 118 119 120 121 111 /** Retrieve an entry from this three dimensional hash mapping. 112 * @param key_one The first key as an <strong>Object</strong>. 113 * @param key_two The second key as an <strong>Object</strong>. 114 * @return The value <strong>Object</strong> located at (key_one, key_two), or <i>null</i> if no such value. 115 */ 116 public Object get(Object key_one, Object key_two) { 117 Object result = null; 118 if(key_one.equals(last_key_one) && key_two.equals(last_key_two)) { 119 result = last_value; 120 } 121 else { 122 122 // Retrieve the hash mapping at key_one. 123 123 HashMap map = (HashMap) get(key_one); 124 124 // If there is such a map then retrieve the value at key_two. 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 125 if(map != null) { 126 result = map.get(key_two); 127 if(result != null) { 128 last_key_one = key_one; 129 last_key_two = key_two; 130 last_value = result; 131 } 132 } 133 } 134 return result; 135 } 136 /** -SPECIAL - Attempts to retrieve a previous instance of metadata with the same parameters as the given one. If such a metadata is found, it is returned, otherwise the given metadata is added then returned as being the first unique instance. 137 * @param metadata The <strong>Metadata</strong> for whom we are trying to find the first unique instance. 138 * @return The first unique instance of the target <strong>Metadata</strong> which may in fact be the same metadata given as a paramater. 139 */ 140 public Metadata locate(Metadata metadata) { 141 Metadata result = null; 142 if(metadata != null) { 143 143 // Locate the appropriate value->metadata hashmap. 144 145 146 147 148 149 150 144 String element_name = metadata.getElement().toString(); 145 HashMap inner_mapping = (HashMap) get(element_name); 146 if(inner_mapping == null) { 147 inner_mapping = new HashMap(4); // Small initial capacity. 148 put(element_name, inner_mapping); 149 } 150 element_name = null; 151 151 // Locate the appropriate metadata 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 152 String value_name = metadata.getValueNode().getFullPath(); 153 result = (Metadata) inner_mapping.get(value_name); 154 if(result == null) { 155 result = metadata; 156 inner_mapping.put(value_name, metadata); 157 } 158 value_name = null; 159 inner_mapping = null; 160 } 161 return result; 162 } 163 /** Put an entry into this three dimensional hash mapping. 164 * @param key_one The first key, to store this value under, as an <strong>Object</strong>. 165 * @param key_two The second key, to store this value under, as an <strong>Object</strong>. 166 * @param value The value <strong>Object</strong> itself. 167 */ 168 public void put(Object key_one, Object key_two, Object value) { 169 // Retrieve the hash mapping at key_one, or if none exists create one. 170 HashMap map = (HashMap) get(key_one); 171 if(map == null) { 172 map = new HashMap(capacity); 173 put(key_one, map); 174 } 175 // Now add the value to this mapping. 176 map.put(key_two, value); 177 } 178 178 }
Note:
See TracChangeset
for help on using the changeset viewer.