KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > adapter > FloatKeyMapToMapAdapter


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2003 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.adapter;
20
21 import bak.pcj.map.FloatKeyMap;
22 import bak.pcj.map.FloatKeyMapIterator;
23 import bak.pcj.hash.DefaultFloatHashFunction;
24 import bak.pcj.util.Exceptions;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.AbstractSet JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 /**
33  * This class represents adapters of primitive maps from
34  * float values to objects to Java Collections
35  * Framework maps. The adapter is implemented as a wrapper
36  * around a primitive map. Thus,
37  * changes to the underlying map are reflected by this
38  * map and vice versa.
39  *
40  * @see FloatKeyMap
41  * @see java.util.Map
42  *
43  * @author Søren Bak
44  * @version 1.1 20-08-2003 23:03
45  * @since 1.0
46  */

47 public class FloatKeyMapToMapAdapter implements Map {
48
49     /** The underlying primitive map. */
50     protected FloatKeyMap map;
51
52     /**
53      * Creates a new adaption of a primitive map of float
54      * keys to a Java Collections Framework map.
55      *
56      * @param map
57      * the underlying primitive map.
58      *
59      * @throws NullPointerException
60      * if <tt>map</tt> is <tt>null</tt>.
61      */

62     public FloatKeyMapToMapAdapter(FloatKeyMap map) {
63         if (map == null)
64             Exceptions.nullArgument("map");
65         this.map = map;
66     }
67
68     /**
69      * Clears this map. The underlying map is cleared.
70      *
71      * @throws UnsupportedOperationException
72      * if the operation is not supported by the
73      * underlying map.
74      */

75     public void clear()
76     { map.clear(); }
77
78     /**
79      * Indicates whether this map contains a mapping from a specified
80      * key. This is so, only if the underlying collection contains
81      * the unwrapped key.
82      *
83      * @param key
84      * the key to test for.
85      *
86      * @return <tt>true</tt> if this map contains a mapping from
87      * the specified key; returns <tt>false</tt>
88      * otherwise.
89      *
90      * @throws NullPointerException
91      * if <tt>key</tt> is <tt>null</tt>.
92      *
93      * @throws ClassCastException
94      * if <tt>key</tt> is not of class {@link Float Float}.
95      */

96     public boolean containsKey(Object JavaDoc key)
97     { return map.containsKey(((Float JavaDoc)key).floatValue()); }
98
99     /**
100      * Indicates whether this map contains a mapping to a specified
101      * value.
102      *
103      * @param value
104      * the value to test for.
105      *
106      * @return <tt>true</tt> if this map contains at least one
107      * mapping to the specified value; returns
108      * <tt>false</tt> otherwise.
109      */

110     public boolean containsValue(Object JavaDoc value) {
111         return map.containsValue(value);
112     }
113
114     /**
115      * Returns a set view of the entries of this map. The returned
116      * set is a view, so changes to this map are reflected by the
117      * returned set and vice versa. All elements of the returned
118      * set implements {@link java.util.Map.Entry java.util.Map.Entry}.
119      *
120      * @return a set view of the entries of this map.
121      */

122     public Set JavaDoc entrySet() {
123         return new EntrySet();
124     }
125
126     /**
127      * Indicates whether this map is equal to some object.
128      *
129      * @param obj
130      * the object with which to compare this map.
131      *
132      * @return <tt>true</tt> if this map is equal to the
133      * specified object; returns <tt>false</tt>
134      * otherwise.
135      */

136     public boolean equals(Object JavaDoc obj) {
137         if (!(obj instanceof Map))
138             return false;
139         Map m = (Map)obj;
140         if (m.size() != map.size())
141             return false;
142         Iterator i = m.entrySet().iterator();
143         while (i.hasNext()) {
144             Map.Entry e = (Map.Entry)i.next();
145             if (e.getKey() == null)
146                 return false;
147             if (e.getValue() == null)
148                 return false;
149             if ( !get(e.getKey()).equals(e.getValue()) )
150                 return false;
151         }
152         return true;
153     }
154
155     /**
156      * Maps a specified key to a value. Returns <tt>null</tt>
157      * if no mapping exists for the specified key.
158      *
159      * @param key
160      * the key to map to a value.
161      *
162      * @return the value that the specified key maps to, or
163      * <tt>null</tt>, if no such mapping exists.
164      *
165      * @throws NullPointerException
166      * if <tt>key</tt> is <tt>null</tt>.
167      *
168      * @throws ClassCastException
169      * if <tt>key</tt> is not of class {@link Float Float}.
170      */

171     public Object JavaDoc get(Object JavaDoc key) {
172         float k = ((Float JavaDoc)key).floatValue();
173         return map.get(k);
174     }
175
176     /**
177      * Returns a hash code value for this map. The hash code
178      * returned is that of the underlying map.
179      *
180      * @return a hash code value for this map.
181      */

182     public int hashCode()
183     { return map.hashCode(); }
184
185     /**
186      * Indicates whether this map is empty.
187      *
188      * @return <tt>true</tt> if this map is empty; returns
189      * <tt>false</tt> otherwise.
190      */

191     public boolean isEmpty()
192     { return map.isEmpty(); }
193
194     /**
195      * Returns a set view of the keys of this map. Removals from the
196      * returned set removes the corresponding entries in this map.
197      * Changes to the map are reflected in the set. All elements
198      * if the returned set is of class {@link Float Float}.
199      *
200      * @return a set view of the keys of this map.
201      */

202     public Set JavaDoc keySet() {
203         return new FloatSetToSetAdapter(map.keySet());
204     }
205
206     /**
207      * Adds a mapping from a specified key to a specified value to
208      * this map. If a mapping already exists for the specified key
209      * it is overwritten by the new mapping. The mapping is
210      * added to the underlying map.
211      *
212      * @param key
213      * the key of the mapping to add to this map.
214      *
215      * @param value
216      * the value of the mapping to add to this map.
217      *
218      * @return the old value if a
219      * mapping from the specified key already existed
220      * in this map; returns <tt>null</tt> otherwise.
221      *
222      * @throws UnsupportedOperationException
223      * if the operation is not supported by this map.
224      *
225      * @throws NullPointerException
226      * if <tt>key</tt> is <tt>null</tt>;
227      * if <tt>value</tt> is <tt>null</tt>.
228      *
229      * @throws ClassCastException
230      * if <tt>key</tt> is not of class {@link Float Float}.
231      */

232     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
233         float k = ((Float JavaDoc)key).floatValue();
234         return map.put(k, value);
235     }
236
237     /**
238      * Adds all mappings from a specified map to this map. Any
239      * existing mappings whose keys collide with a new mapping is
240      * overwritten by the new mapping. The mappings are
241      * added to the underlying map.
242      *
243      * @param map
244      * the map whose mappings to add to this map.
245      *
246      * @throws NullPointerException
247      * if <tt>map</tt> is <tt>null</tt>.
248      *
249      * @throws UnsupportedOperationException
250      * if the operation is not supported by this map.
251      *
252      * @throws NullPointerException
253      * if a key in <tt>map</tt> is <tt>null</tt>.
254      *
255      * @throws ClassCastException
256      * if a key in <tt>map</tt> is not of class {@link Float Float}.
257      */

258     public void putAll(Map map) {
259         Iterator i = map.entrySet().iterator();
260         while (i.hasNext()) {
261             Map.Entry e = (Map.Entry)i.next();
262             put(e.getKey(), e.getValue());
263         }
264     }
265
266     /**
267      * Removes the mapping from a specified key from this map.
268      * The mapping is removed from the underlying map.
269      *
270      * @param key
271      * the key whose mapping to remove from this map.
272      *
273      * @return the old value if a
274      * mapping from the specified key already existed
275      * in this map; returns <tt>null</tt> otherwise.
276      *
277      * @throws UnsupportedOperationException
278      * if the operation is not supported by the
279      * underlying map.
280      */

281     public Object JavaDoc remove(Object JavaDoc key) {
282         if (key == null)
283             return null;
284         if (!(key instanceof Float JavaDoc))
285             return null;
286         float k = ((Float JavaDoc)key).floatValue();
287         return map.remove(k);
288     }
289
290     /**
291      * Returns the size of this map. The size is defined as the
292      * number of mappings from keys to values. The size is that
293      * of the underlying map.
294      *
295      * @return the size of this map.
296      */

297     public int size()
298     { return map.size(); }
299
300     /**
301      * Returns a collection view of the values in this map. The
302      * collection is not modifiable, but changes to the map are
303      * reflected in the collection.
304      *
305      * @return a collection view of the values in this map.
306      */

307     public Collection JavaDoc values() {
308         return map.values();
309     }
310
311     class EntrySet extends AbstractSet JavaDoc {
312
313         public Iterator iterator() {
314             return new Iterator() {
315                 FloatKeyMapIterator i = map.entries();
316
317                 public boolean hasNext()
318                 { return i.hasNext(); }
319
320                 public Object JavaDoc next() {
321                     i.next();
322                     return new Entry(i.getKey(), i.getValue());
323                 }
324
325                 public void remove()
326                 { i.remove(); }
327             };
328         }
329
330         public boolean add(Object JavaDoc obj) {
331             Map.Entry e = (Map.Entry)obj;
332             if (contains(e))
333                 return false;
334             put(e.getKey(), e.getValue());
335             return true;
336         }
337
338         public int size()
339         { return map.size(); }
340     }
341
342     class Entry implements Map.Entry {
343         Float JavaDoc key;
344         Object JavaDoc value;
345
346         Entry(float key, Object JavaDoc value) {
347             this.key = new Float JavaDoc(key);
348             this.value = value;
349         }
350
351         public Object JavaDoc getKey()
352         { return key; }
353
354         public Object JavaDoc getValue()
355         { return value; }
356
357         public Object JavaDoc setValue(Object JavaDoc value)
358         { return put(key, value); }
359
360         public int hashCode()
361         { return DefaultFloatHashFunction.INSTANCE.hash(key.floatValue()) ^ value.hashCode(); }
362
363         public boolean equals(Object JavaDoc obj) {
364             if (!(obj instanceof Map.Entry))
365                 return false;
366             Map.Entry e = (Map.Entry)obj;
367             return key.equals(e.getKey()) && value.equals(e.getValue());
368         }
369     }
370
371
372 }
Popular Tags