KickJava   Java API By Example, From Geeks To Geeks.

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


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.IntCollection;
22 import bak.pcj.map.BooleanKeyIntMap;
23 import bak.pcj.map.BooleanKeyIntMapIterator;
24 import bak.pcj.map.MapDefaults;
25 import bak.pcj.hash.DefaultBooleanHashFunction;
26 import bak.pcj.hash.DefaultIntHashFunction;
27 import bak.pcj.util.Exceptions;
28
29 import java.util.Iterator JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.AbstractSet JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35 /**
36  * This class represents adapters of primitive maps from
37  * boolean values to int values to Java Collections
38  * Framework maps. The adapter is implemented as a wrapper
39  * around a primitive map. Thus,
40  * changes to the underlying map are reflected by this
41  * map and vice versa.
42  *
43  * @see BooleanKeyIntMap
44  * @see java.util.Map
45  *
46  * @author Søren Bak
47  * @version 1.4 20-08-2003 23:03
48  * @since 1.0
49  */

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

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

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

99     public boolean containsKey(Object JavaDoc key) throws NullPointerException JavaDoc, ClassCastException JavaDoc /* Exceptions marked to work around bug in Javadoc 1.3 */
100     { return map.containsKey(((Boolean JavaDoc)key).booleanValue()); }
101
102     /**
103      * Indicates whether this map contains a mapping to a specified
104      * value. For this map to contain an object, the
105      * underlying map must contain its unwrapped value.
106      * <p>Note that this map can never contain <tt>null</tt>
107      * values or values of other classes than {@link Integer Integer}.
108      * In those cases, this method will return <tt>false</tt>.
109      *
110      * @param value
111      * the value to test for.
112      *
113      * @return <tt>true</tt> if this map contains at least one
114      * mapping to the specified value; returns
115      * <tt>false</tt> otherwise.
116      */

117     public boolean containsValue(Object JavaDoc value) {
118         if (value == null)
119             return false;
120         return map.containsValue(((Integer JavaDoc)value).intValue());
121     }
122
123     /**
124      * Returns a set view of the entries of this map. The returned
125      * set is a view, so changes to this map are reflected by the
126      * returned set and vice versa. All elements of the returned
127      * set implements {@link java.util.Map.Entry java.util.Map.Entry}.
128      *
129      * @return a set view of the entries of this map.
130      */

131     public Set JavaDoc entrySet() {
132         return new EntrySet();
133     }
134
135     /**
136      * Indicates whether this map is equal to some object.
137      *
138      * @param obj
139      * the object with which to compare this map.
140      *
141      * @return <tt>true</tt> if this map is equal to the
142      * specified object; returns <tt>false</tt>
143      * otherwise.
144      */

145     public boolean equals(Object JavaDoc obj) {
146         if (!(obj instanceof Map))
147             return false;
148         Map m = (Map)obj;
149         if (m.size() != map.size())
150             return false;
151         Iterator i = m.entrySet().iterator();
152         while (i.hasNext()) {
153             Map.Entry e = (Map.Entry)i.next();
154             if (e.getKey() == null)
155                 return false;
156             if (e.getValue() == null)
157                 return false;
158             if ( !get(e.getKey()).equals(e.getValue()) )
159                 return false;
160         }
161         return true;
162     }
163
164     /**
165      * Maps a specified key to a value. Returns <tt>null</tt>
166      * if no mapping exists for the specified key.
167      * The returned value will always be of class {@link Integer Integer}.
168      *
169      * @param key
170      * the key to map to a value.
171      *
172      * @return the value that the specified key maps to, or
173      * <tt>null</tt>, if no such mapping exists.
174      *
175      * @throws NullPointerException
176      * if <tt>key</tt> is <tt>null</tt>.
177      *
178      * @throws ClassCastException
179      * if <tt>key</tt> is not of class {@link Boolean Boolean}.
180      */

181     public Object JavaDoc get(Object JavaDoc key) throws NullPointerException JavaDoc, ClassCastException JavaDoc /* Exceptions marked to work around bug in Javadoc 1.3 */ {
182         boolean k = ((Boolean JavaDoc)key).booleanValue();
183         int v = map.get(k);
184         if (v == MapDefaults.defaultInt())
185             if (!map.containsKey(k))
186                 return null;
187         return new Integer JavaDoc(v);
188     }
189
190     /**
191      * Returns a hash code value for this map. The hash code
192      * returned is that of the underlying map.
193      *
194      * @return a hash code value for this map.
195      */

196     public int hashCode()
197     { return map.hashCode(); }
198
199     /**
200      * Indicates whether this map is empty.
201      *
202      * @return <tt>true</tt> if this map is empty; returns
203      * <tt>false</tt> otherwise.
204      */

205     public boolean isEmpty()
206     { return map.isEmpty(); }
207
208     /**
209      * Returns a set view of the keys of this map. Removals from the
210      * returned set removes the corresponding entries in this map.
211      * Changes to the map are reflected in the set. All elements
212      * if the returned set is of class {@link Boolean Boolean}.
213      *
214      * @return a set view of the keys of this map.
215      */

216     public Set JavaDoc keySet() {
217         return new BooleanSetToSetAdapter(map.keySet());
218     }
219
220     /**
221      * Adds a mapping from a specified key to a specified value to
222      * this map. If a mapping already exists for the specified key
223      * it is overwritten by the new mapping. The mapping is
224      * added to the underlying map.
225      *
226      * @param key
227      * the key of the mapping to add to this map.
228      *
229      * @param value
230      * the value of the mapping to add to this map.
231      *
232      * @return the old value if a
233      * mapping from the specified key already existed
234      * in this map; returns <tt>null</tt> otherwise.
235      *
236      * @throws UnsupportedOperationException
237      * if the operation is not supported by this map.
238      *
239      * @throws NullPointerException
240      * if <tt>key</tt> is <tt>null</tt>;
241      * if <tt>value</tt> is <tt>null</tt>.
242      *
243      * @throws ClassCastException
244      * if <tt>key</tt> is not of class {@link Boolean Boolean};
245      * if <tt>value</tt> is not of class {@link Integer Integer}.
246      */

247     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 */ {
248         Object JavaDoc result = get(key);
249         boolean k = ((Boolean JavaDoc)key).booleanValue();
250         map.put(k, ((Integer JavaDoc)value).intValue());
251         return result;
252     }
253
254     /**
255      * Adds all mappings from a specified map to this map. Any
256      * existing mappings whose keys collide with a new mapping is
257      * overwritten by the new mapping. The mappings are
258      * added to the underlying map.
259      *
260      * @param map
261      * the map whose mappings to add to this map.
262      *
263      * @throws NullPointerException
264      * if <tt>map</tt> is <tt>null</tt>.
265      *
266      * @throws UnsupportedOperationException
267      * if the operation is not supported by this map.
268      *
269      * @throws NullPointerException
270      * if a key in <tt>map</tt> is <tt>null</tt>;
271      * if a value in <tt>map</tt> is <tt>null</tt>.
272      *
273      * @throws ClassCastException
274      * if a key in <tt>map</tt> is not of class {@link Boolean Boolean};
275      * if a value in <tt>value</tt> is not of class {@link Integer Integer}.
276      */

277     public void putAll(Map map) throws NullPointerException JavaDoc, ClassCastException JavaDoc /* Exceptions marked to work around bug in Javadoc 1.3 */ {
278         Iterator i = map.entrySet().iterator();
279         while (i.hasNext()) {
280             Map.Entry e = (Map.Entry)i.next();
281             put(e.getKey(), e.getValue());
282         }
283     }
284
285     /**
286      * Removes the mapping from a specified key from this map.
287      * The mapping is removed from the underlying map.
288      *
289      * @param key
290      * the key whose mapping to remove from this map.
291      *
292      * @return the old value if a
293      * mapping from the specified key already existed
294      * in this map; returns <tt>null</tt> otherwise.
295      *
296      * @throws UnsupportedOperationException
297      * if the operation is not supported by the
298      * underlying map.
299      */

300     public Object JavaDoc remove(Object JavaDoc key) {
301         if (key == null)
302             return null;
303         if (!(key instanceof Boolean JavaDoc))
304             return null;
305         Object JavaDoc result = get(key);
306         boolean k = ((Boolean JavaDoc)key).booleanValue();
307         map.remove(k);
308         return result;
309     }
310
311     /**
312      * Returns the size of this map. The size is defined as the
313      * number of mappings from keys to values. The size is that
314      * of the underlying map.
315      *
316      * @return the size of this map.
317      */

318     public int size()
319     { return map.size(); }
320
321     /**
322      * Returns a collection view of the values in this map. The
323      * collection is not modifiable, but changes to the map are
324      * reflected in the collection. All elements
325      * in the returned set is of class {@link Integer Integer}.
326      *
327      * @return a collection view of the values in this map.
328      */

329     public Collection values() {
330         return new IntCollectionToCollectionAdapter(map.values());
331     }
332
333     class EntrySet extends AbstractSet JavaDoc {
334
335         public Iterator iterator() {
336             return new Iterator() {
337                 BooleanKeyIntMapIterator i = map.entries();
338
339                 public boolean hasNext()
340                 { return i.hasNext(); }
341
342                 public Object JavaDoc next() {
343                     i.next();
344                     return new Entry(i.getKey(), i.getValue());
345                 }
346
347                 public void remove()
348                 { i.remove(); }
349             };
350         }
351
352         public boolean add(Object JavaDoc obj) {
353             Map.Entry e = (Map.Entry)obj;
354             if (contains(e))
355                 return false;
356             put(e.getKey(), e.getValue());
357             return true;
358         }
359
360         public int size()
361         { return map.size(); }
362     }
363
364     class Entry implements Map.Entry {
365         Boolean JavaDoc key;
366         Integer JavaDoc value;
367
368         Entry(boolean key, int value) {
369             this.key = new Boolean JavaDoc(key);
370             this.value = new Integer JavaDoc(value);
371         }
372
373         public Object JavaDoc getKey()
374         { return key; }
375
376         public Object JavaDoc getValue()
377         { return value; }
378
379         public Object JavaDoc setValue(Object JavaDoc value)
380         { return put(key, value); }
381
382         public int hashCode()
383         { return DefaultBooleanHashFunction.INSTANCE.hash(key.booleanValue()) ^ DefaultIntHashFunction.INSTANCE.hash(value.intValue()); }
384
385         public boolean equals(Object JavaDoc obj) {
386             if (!(obj instanceof Map.Entry))
387                 return false;
388             Map.Entry e = (Map.Entry)obj;
389             return key.equals(e.getKey()) && value.equals(e.getValue());
390         }
391     }
392
393
394 }
Popular Tags