KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > decorator > TDoubleObjectHashMapDecorator


1 ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2002, Eric D. Friedman All Rights Reserved.
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
12
// GNU 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 program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
///////////////////////////////////////////////////////////////////////////////
18

19 package gnu.trove.decorator;
20
21 import gnu.trove.TDoubleObjectHashMap;
22 import gnu.trove.TDoubleObjectIterator;
23 import java.util.AbstractMap;
24 import java.util.AbstractSet;
25 import java.util.Collection;
26 import java.util.Iterator;
27 import java.util.Map.Entry;
28 import java.util.Map;
29 import java.util.Set;
30
31 /**
32  * Wrapper class to make a TDoubleObjectHashMap conform to the <tt>java.util.Map</tt> API.
33  * This class simply decorates an underlying TDoubleObjectHashMap and translates the Object-based
34  * APIs into their Trove primitive analogs.
35  *
36  * <p>
37  * Note that wrapping and unwrapping primitive values is extremely inefficient. If
38  * possible, users of this class should override the appropriate methods in this class
39  * and use a table of canonical values.
40  * </p>
41  *
42  * Created: Mon Sep 23 22:07:40 PDT 2002
43  *
44  * @author Eric D. Friedman
45  * @version $Id: TDoubleObjectHashMapDecorator.java,v 1.5 2004/11/09 15:48:50 ericdf Exp $
46  * @since trove 0.1.8
47  */

48 public class TDoubleObjectHashMapDecorator extends AbstractMap implements Map, Cloneable {
49     /** the wrapped primitive map */
50     protected TDoubleObjectHashMap _map;
51
52     /**
53      * Creates a wrapper that decorates the specified primitive map.
54      */

55     public TDoubleObjectHashMapDecorator(TDoubleObjectHashMap map) {
56     super();
57     this._map = map;
58     }
59
60     /**
61      * Clones the underlying trove collection and returns the clone wrapped in a new
62      * decorator instance. This is a shallow clone except where primitives are
63      * concerned.
64      *
65      * @return a copy of the receiver
66      */

67     public Object clone() {
68         try {
69             TDoubleObjectHashMapDecorator copy = (TDoubleObjectHashMapDecorator) super.clone();
70             copy._map = (TDoubleObjectHashMap)_map.clone();
71             return copy;
72         } catch (CloneNotSupportedException e) {
73             // assert(false);
74
throw new InternalError(); // we are cloneable, so this does not happen
75
}
76     }
77
78     /**
79      * Inserts a key/value pair into the map.
80      *
81      * @param key an <code>Object</code> value
82      * @param value an <code>Object</code> value
83      * @return the previous value associated with <tt>key</tt>,
84      * or Integer(0) if none was found.
85      */

86     public Object put(Object key, Object value) {
87     return wrapValue(_map.put(unwrapKey(key), unwrapValue(value)));
88     }
89
90     /**
91      * Compares this map with another map for equality of their stored
92      * entries.
93      *
94      * @param other an <code>Object</code> value
95      * @return true if the maps are identical
96      */

97     public boolean equals(Object other) {
98     if (_map.equals(other)) {
99         return true; // comparing two trove maps
100
} else if (other instanceof Map) {
101         Map that = (Map)other;
102         if (that.size() != _map.size()) {
103         return false; // different sizes, no need to compare
104
} else { // now we have to do it the hard way
105
Iterator it = that.entrySet().iterator();
106         for (int i = that.size(); i-- > 0;) {
107             Map.Entry e = (Map.Entry)it.next();
108             Object key = e.getKey();
109             Object val = e.getValue();
110             if (key instanceof Double && val instanceof Object) {
111             double k = unwrapKey(key);
112             Object v = unwrapValue(val);
113             if (_map.containsKey(k) && v == _map.get(k)) {
114                 // match, ok to continue
115
} else {
116                 return false; // no match: we're done
117
}
118             } else {
119             return false; // different type in other map
120
}
121         }
122         return true; // all entries match
123
}
124     } else {
125         return false;
126     }
127     }
128
129     /**
130      * Retrieves the value for <tt>key</tt>
131      *
132      * @param key an <code>Object</code> value
133      * @return the value of <tt>key</tt> or null if no such mapping exists.
134      */

135     public Object get(Object key) {
136         return _map.get(unwrapKey(key));
137     }
138
139
140     /**
141      * Empties the map.
142      */

143     public void clear() {
144     this._map.clear();
145     }
146
147     /**
148      * Deletes a key/value pair from the map.
149      *
150      * @param key an <code>Object</code> value
151      * @return the removed value, or Integer(0) if it was not found in the map
152      */

153     public Object remove(Object key) {
154     return wrapValue(_map.remove(unwrapKey(key)));
155     }
156
157     /**
158      * Returns a Set view on the entries of the map.
159      *
160      * @return a <code>Set</code> value
161      */

162     public Set entrySet() {
163         return new AbstractSet() {
164         public int size() {
165             return _map.size();
166         }
167         public boolean isEmpty() {
168             return TDoubleObjectHashMapDecorator.this.isEmpty();
169         }
170         public boolean contains(Object o) {
171             if (o instanceof Map.Entry) {
172             Object k = ((Map.Entry)o).getKey();
173             Object v = ((Map.Entry)o).getValue();
174             return (TDoubleObjectHashMapDecorator.this.containsKey(k) &&
175                 TDoubleObjectHashMapDecorator.this.get(k).equals(v));
176             } else {
177             return false;
178             }
179         }
180         public Iterator iterator() {
181             return new Iterator() {
182                 private final TDoubleObjectIterator it = _map.iterator();
183                 
184                 public Object next() {
185                 it.advance();
186                 final Object key = wrapKey(it.key());
187                 final Object v = wrapValue(it.value());
188                 return new Map.Entry() {
189                     private Object val = v;
190                     public boolean equals(Object o) {
191                         return ((o instanceof Map.Entry) &&
192                             ((Map.Entry)o).getKey().equals(key) &&
193                             ((Map.Entry)o).getValue().equals(val));
194                     }
195                     public Object getKey() {
196                         return key;
197                     }
198                     public Object getValue() {
199                         return val;
200                     }
201                     public int hashCode() {
202                         return key.hashCode() + val.hashCode();
203                     }
204                     public Object setValue(Object value) {
205                         val = value;
206                         return put(key, value);
207                     }
208                     };
209                 }
210
211                 public boolean hasNext() {
212                 return it.hasNext();
213                 }
214
215                 public void remove() {
216                 it.remove();
217                 }
218             };
219         }
220         public boolean add(Object o) {
221             throw new UnsupportedOperationException();
222         }
223         public boolean remove(Object o) {
224             throw new UnsupportedOperationException();
225         }
226         public boolean addAll(Collection c) {
227             throw new UnsupportedOperationException();
228         }
229         public boolean retainAll(Collection c) {
230             throw new UnsupportedOperationException();
231         }
232         public boolean removeAll(Collection c) {
233             throw new UnsupportedOperationException();
234         }
235         public void clear() {
236             TDoubleObjectHashMapDecorator.this.clear();
237         }
238         };
239     }
240
241     /**
242      * Checks for the presence of <tt>val</tt> in the values of the map.
243      *
244      * @param val an <code>Object</code> value
245      * @return a <code>boolean</code> value
246      */

247     public boolean containsValue(Object val) {
248     return _map.containsValue(unwrapValue(val));
249     }
250
251     /**
252      * Checks for the present of <tt>key</tt> in the keys of the map.
253      *
254      * @param key an <code>Object</code> value
255      * @return a <code>boolean</code> value
256      */

257     public boolean containsKey(Object key) {
258     return _map.containsKey(unwrapKey(key));
259     }
260
261     /**
262      * Returns the number of entries in the map.
263      * @return the map's size.
264      */

265     public int size() {
266     return this._map.size();
267     }
268
269     /**
270      * Indicates whether map has any entries.
271      * @return true if the map is empty
272      */

273     public boolean isEmpty() {
274     return (size() == 0);
275     }
276
277     /**
278      * Copies the key/value mappings in <tt>map</tt> into this map.
279      * Note that this will be a <b>deep</b> copy, as storage is by
280      * primitive value.
281      *
282      * @param map a <code>Map</code> value
283      */

284     public void putAll(Map map) {
285     Iterator it = map.entrySet().iterator();
286     for (int i = map.size(); i-- > 0;) {
287         Map.Entry e = (Map.Entry)it.next();
288         this.put(e.getKey(), e.getValue());
289     }
290     }
291
292     /**
293      * Wraps a key
294      *
295      * @param a key in the underlying map
296      * @return an Object representation of the key
297      */

298     protected Double wrapKey(double k) {
299     return new Double(k);
300     }
301
302     /**
303      * Unwraps a key
304      *
305      * @param a wrapped key
306      * @return an unwrapped representation of the key
307      */

308     protected double unwrapKey(Object key) {
309     return ((Double)key).doubleValue();
310     }
311     /**
312      * Wraps a value
313      *
314      * @param a value in the underlying map
315      * @return an Object representation of the value
316      */

317     protected final Object wrapValue(Object o) {
318     return o;
319     }
320
321     /**
322      * Unwraps a value
323      *
324      * @param a wrapped value
325      * @return an unwrapped representation of the value
326      */

327     protected final Object unwrapValue(Object value) {
328     return value;
329     }
330
331 } // TDoubleObjectHashMapDecorator
332
Popular Tags