KickJava   Java API By Example, From Geeks To Geeks.

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


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.Adapter;
22 import bak.pcj.IntIterator;
23 import bak.pcj.map.IntKeyMap;
24 import bak.pcj.map.AbstractIntKeyMap;
25 import bak.pcj.map.IntKeyMapIterator;
26 import bak.pcj.set.IntSet;
27 import bak.pcj.util.Exceptions;
28
29 import java.util.Map JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Collection JavaDoc;
32
33 /**
34  * This class represents adaptions of Java Collections Framework
35  * maps to primitive maps from int values to objects.
36  * The adapter is implemented as a wrapper around the map.
37  * Thus, changes to the underlying map are reflected by this
38  * map and vice versa.
39  *
40  * <p>
41  * Adapters from JCF maps to primitive map will
42  * fail if the JCF collection contains <tt>null</tt> keys or
43  * keys of the wrong class. However, adapters are not fast
44  * failing in the case that the underlying map should
45  * contain illegal keys. To implement fast failure would require
46  * every operation to check every key of the underlying
47  * map before doing anything. Instead validation methods
48  * are provided. They can be called using the assertion facility
49  * in the client code:
50  * <pre>
51  * MapToIntKeyMapAdapter s;
52  * ...
53  * <b>assert</b> s.validate();
54  * </pre>
55  * or by letting the adapter throw an exception on illegal values:
56  * <pre>
57  * MapToIntKeyMapAdapter s;
58  * ...
59  * s.evalidate(); // Throws an exception on illegal values
60  * </pre>
61  * Either way, validation must be invoked directly by the client
62  * code.
63  *
64  * @author S&oslash;ren Bak
65  * @version 1.2 21-08-2003 19:10
66  * @since 1.0
67  */

68 public class MapToIntKeyMapAdapter extends AbstractIntKeyMap implements IntKeyMap {
69
70     /** The underlying map. */
71     protected Map map;
72
73     /**
74      * Creates a new adaption to a map from int
75      * values to objects.
76      *
77      * @param map
78      * the underlying map. This map must
79      * consist of keys of class
80      * {@link Integer Integer}. Otherwise a
81      * {@link ClassCastException ClassCastException}
82      * will be thrown by some methods.
83      *
84      * @throws NullPointerException
85      * if <tt>map</tt> is <tt>null</tt>.
86      */

87     public MapToIntKeyMapAdapter(Map map) {
88         this(map, false);
89     }
90
91     /**
92      * Creates a new adaption to a map from int
93      * values to objects. The map to adapt is optionally validated.
94      *
95      * @param map
96      * the underlying map. This map must
97      * consist of keys of class
98      * {@link Integer Integer}. Otherwise a
99      * {@link ClassCastException ClassCastException}
100      * will be thrown by some methods.
101      *
102      * @param validate
103      * indicates whether <tt>map</tt> should
104      * be checked for illegal keys.
105      *
106      * @throws NullPointerException
107      * if <tt>map</tt> is <tt>null</tt>.
108      *
109      * @throws IllegalStateException
110      * if <tt>validate</tt> is <tt>true</tt> and
111      * <tt>map</tt> contains a <tt>null</tt> key
112      * or a key that is not of class
113      * {@link Integer Integer}.
114      */

115     public MapToIntKeyMapAdapter(Map map, boolean validate) {
116         if (map == null)
117             Exceptions.nullArgument("map");
118         this.map = map;
119         if (validate)
120             evalidate();
121     }
122
123     public void clear()
124     { map.clear(); }
125
126     public boolean containsKey(int key) {
127         return map.get(new Integer JavaDoc(key)) != null;
128     }
129
130     public boolean containsValue(Object JavaDoc value)
131     { return map.containsValue(value); }
132
133     public IntKeyMapIterator entries() {
134         return new IntKeyMapIterator() {
135             Iterator i = map.entrySet().iterator();
136             Map.Entry lastEntry = null;
137
138             public boolean hasNext()
139             { return i.hasNext(); }
140
141             public void next()
142             { lastEntry = (Map.Entry)i.next(); }
143
144             public int getKey() {
145                 if (lastEntry == null)
146                     Exceptions.noElementToGet();
147                 return ((Integer JavaDoc)lastEntry.getKey()).intValue();
148             }
149
150             public Object JavaDoc getValue() {
151                 if (lastEntry == null)
152                     Exceptions.noElementToGet();
153                 return lastEntry.getValue();
154             }
155
156             public void remove() {
157                 i.remove();
158                 lastEntry = null;
159             }
160         };
161     }
162
163     public Object JavaDoc get(int key) {
164         return map.get(new Integer JavaDoc(key));
165     }
166
167     public IntSet keySet()
168     { return new SetToIntSetAdapter(map.keySet()); }
169
170     public Object JavaDoc put(int key, Object JavaDoc value) {
171         return map.put(new Integer JavaDoc(key), value);
172     }
173
174     public Object JavaDoc remove(int key) {
175         return map.remove(new Integer JavaDoc(key));
176     }
177
178     public int size()
179     { return map.size(); }
180
181     public Collection JavaDoc values()
182     { return map.values(); }
183
184     /**
185      * Indicates whether the underlying map is valid for
186      * this adapter. For the underlying map to be valid, it
187      * can only contain {@link Integer Integer} keys and no <tt>null</tt>
188      * keys.
189      *
190      * @return <tt>true</tt> if the underlying map is
191      * valid; returns <tt>false</tt> otherwise.
192      */

193     public boolean validate()
194     { return Adapter.isIntKeyAdaptable(map); }
195
196     /**
197      * Validates the map underlying this adapter and throws
198      * an exception if it is invalid. For the underlying map
199      * to be valid, it can only contain {@link Integer Integer}
200      * keys and no <tt>null</tt> keys.
201      *
202      * @throws IllegalStateException
203      * if the underlying map is invalid.
204      */

205     public void evalidate() {
206         if (!validate())
207             Exceptions.cannotAdapt("map");
208     }
209
210 }
Popular Tags