source: trunk/gli/src/org/greenstone/gatherer/util/HashMap3D.java@ 4364

Last change on this file since 4364 was 4364, checked in by mdewsnip, 21 years ago

Fixed tabbing.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37
38
39
40
41
42
43/* GPL_HEADER */
44package org.greenstone.gatherer.util;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Company: The University of Waikato
49 * Written: 20/08/02
50 * Revised:
51 * @author John Thompson, 9826509
52 * @version 2.3
53 **************************************************************************************/
54import java.util.HashMap;
55import java.util.Iterator;
56import org.greenstone.gatherer.msm.Metadata;
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. */
58public class HashMap3D
59 extends HashMap {
60
61 private int capacity = 4;
62
63 private Object last_key_one = null;
64 private Object last_key_two = null;
65 private Object last_value = null;
66
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
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 // Retrieve the hash mapping at key_one.
123 HashMap map = (HashMap) get(key_one);
124 // If there is such a map then retrieve the value at key_two.
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 // Locate the appropriate value->metadata hashmap.
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 // Locate the appropriate metadata
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}
Note: See TracBrowser for help on using the repository browser.