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 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
9 | *
|
---|
10 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | *########################################################################
|
---|
26 | */
|
---|
27 | package org.greenstone.gatherer.util;
|
---|
28 |
|
---|
29 | /**************************************************************************************
|
---|
30 | * Written: 20/08/02
|
---|
31 | * Revised:
|
---|
32 | **************************************************************************************/
|
---|
33 | import java.util.HashMap;
|
---|
34 | import java.util.Iterator;
|
---|
35 | import org.greenstone.gatherer.msm.Metadata;
|
---|
36 |
|
---|
37 | /** Provides a HashMap implementation that indexes by two keys. Perfect for the storage of metadata references based on their metadata element and assigned value.
|
---|
38 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
39 | * @version 2.3
|
---|
40 | */
|
---|
41 | public class HashMap3D
|
---|
42 | extends HashMap {
|
---|
43 |
|
---|
44 | private int capacity = 4;
|
---|
45 |
|
---|
46 | private int size = 0;
|
---|
47 |
|
---|
48 | private Object last_key_one = null;
|
---|
49 | private Object last_key_two = null;
|
---|
50 | private Object last_value = null;
|
---|
51 |
|
---|
52 | /** Default constructor.
|
---|
53 | */
|
---|
54 | public HashMap3D() {
|
---|
55 | super();
|
---|
56 | }
|
---|
57 | /** Constructor with a specific initial capacity, as we should already have a good idea based on the number of Metadata Elements.
|
---|
58 | * @param capacity The initial capacity as an <i>int</i>.
|
---|
59 | */
|
---|
60 | public HashMap3D(int capacity) {
|
---|
61 | super(capacity);
|
---|
62 | this.capacity = capacity;
|
---|
63 | }
|
---|
64 | /** Completely remove the contents of this HashMap and its child HashMaps. */
|
---|
65 | public void clear() {
|
---|
66 | size = 0;
|
---|
67 | Iterator iterator = values().iterator();
|
---|
68 | while(iterator.hasNext()) {
|
---|
69 | HashMap inner_mapping = (HashMap) iterator.next();
|
---|
70 | inner_mapping.clear();
|
---|
71 | inner_mapping = null;
|
---|
72 | }
|
---|
73 | iterator = null;
|
---|
74 | super.clear();
|
---|
75 | }
|
---|
76 | /** 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.
|
---|
77 | * @param key_one The first key as an <strong>Object</strong>.
|
---|
78 | * @param key_two The second key as an <strong>Object</strong>.
|
---|
79 | * @return <i>true</i> if such an entry exists, <i>false</i> otherwise.
|
---|
80 | */
|
---|
81 | public boolean contains(Object key_one, Object key_two) {
|
---|
82 | boolean result = false;
|
---|
83 | // Retrieve the hash mapping at key_one.
|
---|
84 | HashMap map = (HashMap) get(key_one);
|
---|
85 | // If there is such a map then retrieve the value at key_two.
|
---|
86 | if(map != null) {
|
---|
87 | last_value = map.get(key_two);
|
---|
88 | if(last_value != null) {
|
---|
89 | last_key_one = key_one;
|
---|
90 | last_key_two = key_two;
|
---|
91 | result = true;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return result;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Retrieve an entry from this three dimensional hash mapping.
|
---|
98 | * @param key_one The first key as an <strong>Object</strong>.
|
---|
99 | * @param key_two The second key as an <strong>Object</strong>.
|
---|
100 | * @return The value <strong>Object</strong> located at (key_one, key_two), or <i>null</i> if no such value.
|
---|
101 | */
|
---|
102 | public Object get(Object key_one, Object key_two) {
|
---|
103 | Object result = null;
|
---|
104 | if(key_one.equals(last_key_one) && key_two.equals(last_key_two)) {
|
---|
105 | result = last_value;
|
---|
106 | }
|
---|
107 | else {
|
---|
108 | // Retrieve the hash mapping at key_one.
|
---|
109 | HashMap map = (HashMap) get(key_one);
|
---|
110 | // If there is such a map then retrieve the value at key_two.
|
---|
111 | if(map != null) {
|
---|
112 | result = map.get(key_two);
|
---|
113 | if(result != null) {
|
---|
114 | last_key_one = key_one;
|
---|
115 | last_key_two = key_two;
|
---|
116 | last_value = result;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 | return result;
|
---|
121 | }
|
---|
122 | /** -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.
|
---|
123 | * @param metadata The <strong>Metadata</strong> for whom we are trying to find the first unique instance.
|
---|
124 | * @return The first unique instance of the target <strong>Metadata</strong> which may in fact be the same metadata given as a paramater.
|
---|
125 | */
|
---|
126 | /* private Metadata locate(Metadata metadata) {
|
---|
127 | Metadata result = null;
|
---|
128 | if(metadata != null) {
|
---|
129 | // Locate the appropriate value->metadata hashmap.
|
---|
130 | String element_name = metadata.getElement().toString();
|
---|
131 | HashMap inner_mapping = (HashMap) get(element_name);
|
---|
132 | if(inner_mapping == null) {
|
---|
133 | inner_mapping = new HashMap(4); // Small initial capacity.
|
---|
134 | put(element_name, inner_mapping);
|
---|
135 | }
|
---|
136 | element_name = null;
|
---|
137 | // Locate the appropriate metadata
|
---|
138 | String value_name = metadata.getValueNode().getFullPath(false);
|
---|
139 | result = (Metadata) inner_mapping.get(value_name);
|
---|
140 | if(result == null) {
|
---|
141 | result = metadata;
|
---|
142 | inner_mapping.put(value_name, metadata);
|
---|
143 | }
|
---|
144 | value_name = null;
|
---|
145 | inner_mapping = null;
|
---|
146 | }
|
---|
147 | return result;
|
---|
148 | } */
|
---|
149 | /** Put an entry into this three dimensional hash mapping.
|
---|
150 | * @param key_one The first key, to store this value under, as an <strong>Object</strong>.
|
---|
151 | * @param key_two The second key, to store this value under, as an <strong>Object</strong>.
|
---|
152 | * @param value The value <strong>Object</strong> itself.
|
---|
153 | */
|
---|
154 | public void put(Object key_one, Object key_two, Object value) {
|
---|
155 | // Retrieve the hash mapping at key_one, or if none exists create one.
|
---|
156 | HashMap map = (HashMap) get(key_one);
|
---|
157 | if(map == null) {
|
---|
158 | map = new HashMap(capacity);
|
---|
159 | put(key_one, map);
|
---|
160 | }
|
---|
161 | // Now add the value to this mapping.
|
---|
162 | map.put(key_two, value);
|
---|
163 | size++;
|
---|
164 | }
|
---|
165 | }
|
---|