KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > data > PHashMap


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: PHashMap.java,v 1.4 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton.data;
21
22 import java.io.*;
23 import java.util.*;
24
25 /**
26  * <p>This class extends AbstractPData (which provides the
27  * parental/statemap functionality) and delegates most of
28  * the Map functionality back to an underlying HashMap
29  */

30 public class PHashMap extends AbstractPData implements PMap {
31
32     private Map map = new HashMap();
33
34
35     //--------------- PHashMap -----------------------------------
36
/**
37      * Set the underlying store (you only really need to use
38      * this method if you want to store the data in something
39      * other than a HashMap, which is the default)
40      *
41      * @param imap the Map structure to be used as the underlying
42      * store
43      */

44     public void setStore(Map imap) {
45         map = imap;
46     }
47     
48
49     //--------------- PMap ---------------------------------------
50
/**
51      * Removes all mappings from this map (optional operation).
52      */

53     public void clear() {
54         //we need to start by clearing parents for any PData values in the list
55
Iterator it = map.values().iterator();
56         while (it.hasNext()) {
57             Object JavaDoc el = it.next();
58             if (el!=null && el instanceof PData) {
59                 PData pdata = (PData) el;
60 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(null);
61
if (pdata.isInheritParents() && pdata.getParent()==this) pdata.setParent(null); //csc_012003.1
62
}
63         }
64     
65         //now clear the list
66
map.clear();
67     }
68
69     /**
70      * Returns true if this map contains a mapping for the specified key.
71      */

72     public boolean containsKey(Object JavaDoc key) {
73         return map.containsKey(key);
74     }
75
76     /**
77      * Returns true if this map maps one or more keys to the specified value.
78      */

79     public boolean containsValue(Object JavaDoc value) {
80         return map.containsValue(value);
81     }
82
83     /**
84      * Returns a set view of the mappings contained in this map.
85      */

86     public Set entrySet() {
87         return map.entrySet();
88     }
89
90     /**
91      * Returns the value to which this map maps the specified key.
92      */

93     public Object JavaDoc get(Object JavaDoc key) {
94         return map.get(key);
95     }
96
97     /**
98      * Returns true if this map contains no key-value mappings.
99      */

100     public boolean isEmpty() {
101         return map.isEmpty();
102     }
103
104     /**
105      * Returns a set view of the keys contained in this map.
106      */

107     public Set keySet() {
108         return map.keySet();
109     }
110
111     /**
112      * Associates the specified value with the specified key in this map (optional operation).
113      */

114     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
115     
116         //this check is to ensure that the parental relationship is automatically
117
//cleaned up from the item currently backing the specified key. The idea here
118
//is that if you're removing an element (which you are effectively doing
119
//via a put) then that element should no longer point to this object as its
120
//parent.
121
Object JavaDoc curEl = map.get(key);
122         if (curEl!=null && curEl instanceof PData) {
123             PData pdata = (PData) curEl;
124 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(null);
125
if (pdata.isInheritParents() && pdata.getParent()==this) pdata.setParent(null); //csc_012003.1
126
}
127
128         //this check is used to ensure the parental hierarchy is automatically
129
//maintained. If you add an element to this list and that element implements
130
//PData and that element has inheritParents=true, then this list should
131
//automatically automatically become that objects parent
132
if (value!=null && value instanceof PData) {
133             PData pdata = (PData) value;
134 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(this);
135
if (pdata.isInheritParents() && pdata.getParent()==null) pdata.setParent(this); //csc_012003.1
136
}
137     
138         //put the value in the map
139
return map.put(key, value);
140     }
141
142     /**
143      * Copies all of the mappings from the specified map to this map (optional operation).
144      */

145     public void putAll(Map imap) {
146     
147         //iterate through all the key/values in the map...if the key already
148
//exists in the current map it will be replacing data, so we need to
149
//clear the parental value on the existing data. Otherwise, treat it
150
//like a straight "set" and just update the parental value on the incoming
151
//data
152
Iterator it = imap.keySet().iterator();
153         while (it.hasNext()) {
154             Object JavaDoc key = it.next();
155             
156             //see if there is an existing value for this key
157
Object JavaDoc ovalue = map.get(key);
158             if (ovalue!=null && ovalue instanceof PData) {
159                 PData pdata = (PData) ovalue;
160 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(null);
161
if (pdata.isInheritParents() && pdata.getParent()==this) pdata.setParent(null); //csc_012003.1
162
}
163             
164             //now look at the new value for the key
165
Object JavaDoc nvalue = imap.get(key);
166             if (nvalue!=null && nvalue instanceof PData) {
167                 PData pdata = (PData) nvalue;
168 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(this);
169
if (pdata.isInheritParents() && pdata.getParent()==null) pdata.setParent(this); //csc_012003.1
170
}
171         }
172     
173         //put the map into our map
174
map.putAll(imap);
175     }
176
177     /**
178      * Removes the mapping for this key from this map if present (optional operation).
179      */

180     public Object JavaDoc remove(Object JavaDoc key) {
181         //see if there is an existing value for this key
182
Object JavaDoc value = map.get(key);
183         if (value!=null && value instanceof PData) {
184             PData pdata = (PData) value;
185 //csc_012003.1 if (pdata.isInheritParents()) pdata.setParent(null);
186
if (pdata.isInheritParents() && pdata.getParent()==this) pdata.setParent(null); //csc_012003.1
187
}
188             
189         //remove the key
190
return map.remove(key);
191     }
192
193     /**
194      * Returns the number of key-value mappings in this map.
195      */

196     public int size() {
197         return map.size();
198     }
199
200     /**
201      * Returns a collection view of the values contained in this map.
202      */

203     public Collection values() {
204         return map.values();
205     }
206
207
208     //--------------- Cloneable ----------------------------------
209
/**
210      * Returns a shallow copy of this <tt>ArrayList</tt> instance. (The
211      * elements themselves are not copied.)
212      *
213      * @return a clone of this <tt>ArrayList</tt> instance.
214      */

215     public Object JavaDoc clone() {
216         try {
217             PHashMap phm = (PHashMap) super.clone();
218             phm.map = new HashMap(map);
219             return phm;
220         } catch (CloneNotSupportedException JavaDoc e) {
221             // this shouldn't happen, since we are Cloneable
222
throw new InternalError JavaDoc();
223         }
224     }
225
226
227     //--------------- Object -------------------------------------
228
/**
229      * Check object for equality. Will return true if the incoming
230      * object is a) non-null, b) the size of the underlying list
231      * structures is the same and c) the list containsAll() the
232      * same elements
233      *
234      * @param obj the object we're comparing against
235      * @return true if the objects are equal
236      */

237     public boolean equals(Object JavaDoc obj) {
238         if (obj==null) return false;
239         if (obj==this) return true;
240         if (!(obj instanceof PMap)) return false;
241         PMap pm = (PMap) obj;
242         if (this.size()!=pm.size()) return false;
243         Iterator it = this.keySet().iterator();
244         while (it.hasNext()) {
245             if (!pm.containsKey(it.next())) return false;
246         }
247         it = this.values().iterator();
248         while (it.hasNext()) {
249             if (!pm.containsValue(it.next())) return false;
250         }
251         return true;
252     }
253
254     /**
255      * Returns the hash code value for this list.
256      */

257     public int hashCode() {
258         return map.hashCode();
259     }
260
261
262     //--------------- Utility Methods ----------------------------
263

264
265
266 }
267
268
269
Popular Tags