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

Last change on this file since 15862 was 7985, checked in by mdewsnip, 20 years ago

Removed some dead code.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 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 * 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 */
27package org.greenstone.gatherer.util;
28
29/**************************************************************************************
30 * Written: 20/08/02
31 * Revised:
32 **************************************************************************************/
33import java.util.HashMap;
34import java.util.Iterator;
35
36/** Provides a HashMap implementation that indexes by two keys. Perfect for the storage of metadata references based on their metadata element and assigned value.
37 * @author John Thompson, Greenstone Digital Library, University of Waikato
38 * @version 2.3
39 */
40public class HashMap3D
41 extends HashMap {
42
43 private int capacity = 4;
44
45 private int size = 0;
46
47 private Object last_key_one = null;
48 private Object last_key_two = null;
49 private Object last_value = null;
50
51 /** Default constructor.
52 */
53 public HashMap3D() {
54 super();
55 }
56 /** Constructor with a specific initial capacity.
57 * @param capacity The initial capacity as an <i>int</i>.
58 */
59 public HashMap3D(int capacity) {
60 super(capacity);
61 this.capacity = capacity;
62 }
63 /** Completely remove the contents of this HashMap and its child HashMaps. */
64 public void clear() {
65 size = 0;
66 Iterator iterator = values().iterator();
67 while(iterator.hasNext()) {
68 HashMap inner_mapping = (HashMap) iterator.next();
69 inner_mapping.clear();
70 inner_mapping = null;
71 }
72 iterator = null;
73 super.clear();
74 }
75 /** 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.
76 * @param key_one The first key as an <strong>Object</strong>.
77 * @param key_two The second key as an <strong>Object</strong>.
78 * @return <i>true</i> if such an entry exists, <i>false</i> otherwise.
79 */
80 public boolean contains(Object key_one, Object key_two) {
81 boolean result = false;
82 // Retrieve the hash mapping at key_one.
83 HashMap map = (HashMap) get(key_one);
84 // If there is such a map then retrieve the value at key_two.
85 if(map != null) {
86 last_value = map.get(key_two);
87 if(last_value != null) {
88 last_key_one = key_one;
89 last_key_two = key_two;
90 result = true;
91 }
92 }
93 return result;
94 }
95
96 /** Retrieve an entry from this three dimensional hash mapping.
97 * @param key_one The first key as an <strong>Object</strong>.
98 * @param key_two The second key as an <strong>Object</strong>.
99 * @return The value <strong>Object</strong> located at (key_one, key_two), or <i>null</i> if no such value.
100 */
101 public Object get(Object key_one, Object key_two) {
102 Object result = null;
103 if(key_one.equals(last_key_one) && key_two.equals(last_key_two)) {
104 result = last_value;
105 }
106 else {
107 // Retrieve the hash mapping at key_one.
108 HashMap map = (HashMap) get(key_one);
109 // If there is such a map then retrieve the value at key_two.
110 if(map != null) {
111 result = map.get(key_two);
112 if(result != null) {
113 last_key_one = key_one;
114 last_key_two = key_two;
115 last_value = result;
116 }
117 }
118 }
119 return result;
120 }
121
122 /** Put an entry into this three dimensional hash mapping.
123 * @param key_one The first key, to store this value under, as an <strong>Object</strong>.
124 * @param key_two The second key, to store this value under, as an <strong>Object</strong>.
125 * @param value The value <strong>Object</strong> itself.
126 */
127 public void put(Object key_one, Object key_two, Object value) {
128 // Retrieve the hash mapping at key_one, or if none exists create one.
129 HashMap map = (HashMap) get(key_one);
130 if(map == null) {
131 map = new HashMap(capacity);
132 put(key_one, map);
133 }
134 // Now add the value to this mapping.
135 map.put(key_two, value);
136 size++;
137 }
138}
Note: See TracBrowser for help on using the repository browser.