KickJava   Java API By Example, From Geeks To Geeks.

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


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.TFloatDoubleHashMap;
22 import gnu.trove.TFloatDoubleIterator;
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 TFloatDoubleHashMap conform to the <tt>java.util.Map</tt> API.
33  * This class simply decorates an underlying TFloatDoubleHashMap 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: TFloatDoubleHashMapDecorator.java,v 1.5 2004/11/09 15:48:50 ericdf Exp $
46  * @since trove 0.1.8
47  */

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

55     public TFloatDoubleHashMapDecorator(TFloatDoubleHashMap 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             TFloatDoubleHashMapDecorator copy = (TFloatDoubleHashMapDecorator) super.clone();
70             copy._map = (TFloatDoubleHashMap)_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 Float && val instanceof Double) {
111             float k = unwrapKey(key);
112             double 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     float k = unwrapKey(key);
137     double v = _map.get(k);
138     // 0 may be a false positive since primitive maps
139
// cannot return null, so we have to do an extra
140
// check here.
141
if (v == 0) {
142         return _map.containsKey(k) ? wrapValue(v) : null;
143     } else {
144         return wrapValue(v);
145     }
146     }
147
148
149     /**
150      * Empties the map.
151      */

152     public void clear() {
153     this._map.clear();
154     }
155
156     /**
157      * Deletes a key/value pair from the map.
158      *
159      * @param key an <code>Object</code> value
160      * @return the removed value, or Integer(0) if it was not found in the map
161      */

162     public Object remove(Object key) {
163     return wrapValue(_map.remove(unwrapKey(key)));
164     }
165
166     /**
167      * Returns a Set view on the entries of the map.
168      *
169      * @return a <code>Set</code> value
170      */

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

256     public boolean containsValue(Object val) {
257     return _map.containsValue(unwrapValue(val));
258     }
259
260     /**
261      * Checks for the present of <tt>key</tt> in the keys of the map.
262      *
263      * @param key an <code>Object</code> value
264      * @return a <code>boolean</code> value
265      */

266     public boolean containsKey(Object key) {
267     return _map.containsKey(unwrapKey(key));
268     }
269
270     /**
271      * Returns the number of entries in the map.
272      * @return the map's size.
273      */

274     public int size() {
275     return this._map.size();
276     }
277
278     /**
279      * Indicates whether map has any entries.
280      * @return true if the map is empty
281      */

282     public boolean isEmpty() {
283     return (size() == 0);
284     }
285
286     /**
287      * Copies the key/value mappings in <tt>map</tt> into this map.
288      * Note that this will be a <b>deep</b> copy, as storage is by
289      * primitive value.
290      *
291      * @param map a <code>Map</code> value
292      */

293     public void putAll(Map map) {
294     Iterator it = map.entrySet().iterator();
295     for (int i = map.size(); i-- > 0;) {
296         Map.Entry e = (Map.Entry)it.next();
297         this.put(e.getKey(), e.getValue());
298     }
299     }
300
301     /**
302      * Wraps a key
303      *
304      * @param a key in the underlying map
305      * @return an Object representation of the key
306      */

307     protected Float wrapKey(float k) {
308     return new Float(k);
309     }
310
311     /**
312      * Unwraps a key
313      *
314      * @param a wrapped key
315      * @return an unwrapped representation of the key
316      */

317     protected float unwrapKey(Object key) {
318     return ((Float)key).floatValue();
319     }
320     /**
321      * Wraps a value
322      *
323      * @param a value in the underlying map
324      * @return an Object representation of the value
325      */

326     protected Double wrapValue(double k) {
327     return new Double(k);
328     }
329
330     /**
331      * Unwraps a value
332      *
333      * @param a wrapped value
334      * @return an unwrapped representation of the value
335      */

336     protected double unwrapValue(Object value) {
337     return ((Double)value).doubleValue();
338     }
339
340 } // TFloatDoubleHashMapDecorator
341
Popular Tags