KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002 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.ObjectKeyLongMap;
22 import bak.pcj.map.ObjectKeyLongMapIterator;
23 import bak.pcj.map.MapDefaults;
24 import bak.pcj.hash.DefaultLongHashFunction;
25 import bak.pcj.util.Exceptions;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.AbstractSet JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 /**
34  * This class represents adapters of primitive maps from
35  * object values to <S> values to Java Collections
36  * Framework maps. The adapter is implemented as a wrapper
37  * around a primitive map. Thus,
38  * changes to the underlying map are reflected by this
39  * map and vice versa.
40  *
41  * @see ObjectKeyLongMap
42  * @see java.util.Map
43  *
44  * @author S&oslash;ren Bak
45  * @version 1.1 20-08-2003 23:09
46  * @since 1.1
47  */

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

63     public ObjectKeyLongMapToMapAdapter(ObjectKeyLongMap map) throws NullPointerException JavaDoc /* Exception marked to work around bug in Javadoc 1.3 */ {
64         if (map == null)
65             Exceptions.nullArgument("map");
66         this.map = map;
67     }
68
69     /**
70      * Clears this map. The underlying map is cleared.
71      *
72      * @throws UnsupportedOperationException
73      * if the operation is not supported by the
74      * underlying map.
75      */

76     public void clear()
77     { map.clear(); }
78
79     /**
80      * Indicates whether this map contains a mapping from a specified
81      * key. This is so, only if the underlying collection contains
82      * the unwrapped key.
83      *
84      * @param key
85      * the key to test for.
86      *
87      * @return <tt>true</tt> if this map contains a mapping from
88      * the specified key; returns <tt>false</tt>
89      * otherwise.
90      */

91     public boolean containsKey(Object JavaDoc key)
92     { return map.containsKey(key); }
93
94     /**
95      * Indicates whether this map contains a mapping to a specified
96      * value. For this map to contain an object, the
97      * underlying map must contain its unwrapped value.
98      * <p>Note that this map can never contain <tt>null</tt>
99      * values or values of other classes than {@link Long Long}.
100      * In those cases, this method will return <tt>false</tt>.
101      *
102      * @param value
103      * the value to test for.
104      *
105      * @return <tt>true</tt> if this map contains at least one
106      * mapping to the specified value; returns
107      * <tt>false</tt> otherwise.
108      */

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

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

137     public boolean equals(Object JavaDoc obj) {
138         if (!(obj instanceof Map))
139             return false;
140         Map m = (Map)obj;
141         if (m.size() != map.size())
142             return false;
143         Iterator i = m.entrySet().iterator();
144         while (i.hasNext()) {
145             Map.Entry e = (Map.Entry)i.next();
146             if (e.getValue() == null)
147                 return false;
148             if ( !get(e.getKey()).equals(e.getValue()) )
149                 return false;
150         }
151         return true;
152     }
153
154     /**
155      * Maps a specified key to a value. Returns <tt>null</tt>
156      * if no mapping exists for the specified key.
157      * The returned value will always be of class {@link <JS> <JS>}.
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     public Object JavaDoc get(Object JavaDoc key) {
166         long v = map.get(key);
167         if (v == MapDefaults.defaultLong())
168             if (!map.containsKey(key))
169                 return null;
170         return new Long JavaDoc(v);
171     }
172
173     /**
174      * Returns a hash code value for this map. The hash code
175      * returned is that of the underlying map.
176      *
177      * @return a hash code value for this map.
178      */

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

188     public boolean isEmpty()
189     { return map.isEmpty(); }
190
191     /**
192      * Returns a set view of the keys of this map. Removals from the
193      * returned set removes the corresponding entries in this map.
194      * Changes to the map are reflected in the set.
195      *
196      * @return a set view of the keys of this map.
197      */

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

227     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) throws NullPointerException JavaDoc, ClassCastException JavaDoc /* Exception marked to work around bug in Javadoc 1.3 */ {
228         Object JavaDoc result = get(key);
229         map.put(key, ((Long JavaDoc)value).longValue());
230         return result;
231     }
232
233     /**
234      * Adds all mappings from a specified map to this map. Any
235      * existing mappings whose keys collide with a new mapping is
236      * overwritten by the new mapping. The mappings are
237      * added to the underlying map.
238      *
239      * @param map
240      * the map whose mappings to add to this map.
241      *
242      * @throws NullPointerException
243      * if <tt>map</tt> is <tt>null</tt>.
244      *
245      * @throws UnsupportedOperationException
246      * if the operation is not supported by this map.
247      *
248      * @throws NullPointerException
249      * if a value in <tt>map</tt> is <tt>null</tt>.
250      *
251      * @throws ClassCastException
252      * if a value in <tt>value</tt> is not of class {@link Long Long}.
253      */

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

277     public Object JavaDoc remove(Object JavaDoc key) {
278         Object JavaDoc result = get(key);
279         map.remove(key);
280         return result;
281     }
282
283     /**
284      * Returns the size of this map. The size is defined as the
285      * number of mappings from keys to values. The size is that
286      * of the underlying map.
287      *
288      * @return the size of this map.
289      */

290     public int size()
291     { return map.size(); }
292
293     /**
294      * Returns a collection view of the values in this map. The
295      * collection is not modifiable, but changes to the map are
296      * reflected in the collection. All elements
297      * in the returned set is of class {@link Long Long}.
298      *
299      * @return a collection view of the values in this map.
300      */

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