KickJava   Java API By Example, From Geeks To Geeks.

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


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

71 public class MapToObjectKeyLongMapAdapter extends AbstractObjectKeyLongMap implements ObjectKeyLongMap {
72
73     /** The underlying map. */
74     protected Map map;
75
76     /** The value corresponding to the last key found by containsKey(). */
77     protected Long JavaDoc lastValue;
78
79     /**
80      * Creates a new adaption to a map from object
81      * values to long values.
82      *
83      * @param map
84      * the underlying map. This map must
85      * consist of
86      * values of class
87      * {@link Long Long}. Otherwise a
88      * {@link ClassCastException ClassCastException}
89      * will be thrown by some methods.
90      *
91      * @throws NullPointerException
92      * if <tt>map</tt> is <tt>null</tt>.
93      */

94     public MapToObjectKeyLongMapAdapter(Map map) {
95         if (map == null)
96             Exceptions.nullArgument("map");
97         this.map = map;
98         lastValue = null;
99     }
100
101     /**
102      * Creates a new adaption to a map from object
103      * values to long values. The map to adapt is optionally validated.
104      *
105      * @param map
106      * the underlying map. This map must
107      * consist of
108      * values of class
109      * {@link Long Long}. Otherwise a
110      * {@link ClassCastException ClassCastException}
111      * will be thrown by some methods.
112      *
113      * @param validate
114      * indicates whether <tt>map</tt> should
115      * be checked for illegal values.
116      *
117      * @throws NullPointerException
118      * if <tt>map</tt> is <tt>null</tt>.
119      *
120      * @throws IllegalStateException
121      * if <tt>validate</tt> is <tt>true</tt> and
122      * <tt>map</tt> contains a <tt>null</tt> value,
123      * or a value that is not of class
124      * {@link Long Long}.
125      */

126     public MapToObjectKeyLongMapAdapter(Map map, boolean validate) {
127         if (map == null)
128             Exceptions.nullArgument("map");
129         this.map = map;
130         lastValue = null;
131         if (validate)
132             evalidate();
133     }
134
135     public void clear()
136     { map.clear(); }
137
138     public boolean containsKey(Object JavaDoc key) {
139         lastValue = (Long JavaDoc)map.get(key);
140         return lastValue != null;
141     }
142
143     public boolean containsValue(long value)
144     { return map.containsValue(new Long JavaDoc(value)); }
145
146     public ObjectKeyLongMapIterator entries() {
147         return new ObjectKeyLongMapIterator() {
148             Iterator i = map.entrySet().iterator();
149             Map.Entry lastEntry = null;
150
151             public boolean hasNext()
152             { return i.hasNext(); }
153
154             public void next()
155             { lastEntry = (Map.Entry)i.next(); }
156
157             public Object JavaDoc getKey() {
158                 if (lastEntry == null)
159                     Exceptions.noElementToGet();
160                 return lastEntry.getKey();
161             }
162
163             public long getValue() {
164                 if (lastEntry == null)
165                     Exceptions.noElementToGet();
166                 return ((Long JavaDoc)lastEntry.getValue()).longValue();
167             }
168
169             public void remove() {
170                 i.remove();
171                 lastEntry = null;
172             }
173         };
174     }
175
176     public long get(Object JavaDoc key) {
177         Long JavaDoc value = (Long JavaDoc)map.get(key);
178         return value == null ? MapDefaults.defaultLong() : value.longValue();
179     }
180
181     public Set keySet()
182     { return map.keySet(); }
183
184     public long lget() {
185         if (lastValue == null)
186             Exceptions.noLastElement();
187         return lastValue.longValue();
188     }
189
190     public long put(Object JavaDoc key, long value) {
191         Long JavaDoc oldValue = (Long JavaDoc)map.put(key, new Long JavaDoc(value));
192         return oldValue == null ? MapDefaults.defaultLong() : oldValue.longValue();
193     }
194
195     public long remove(Object JavaDoc key) {
196         Long JavaDoc value = (Long JavaDoc)map.remove(key);
197         return value == null ? MapDefaults.defaultLong() : value.longValue();
198     }
199
200     public int size()
201     { return map.size(); }
202
203     public LongCollection values()
204     { return new CollectionToLongCollectionAdapter(map.values()); }
205
206     public long tget(Object JavaDoc key) {
207         Long JavaDoc value = (Long JavaDoc)map.get(key);
208         if (value == null)
209             Exceptions.noSuchMapping(key);
210         return value.longValue();
211     }
212
213     /**
214      * Indicates whether the underlying map is valid for
215      * this adapter. For the underlying map to be valid it
216      * can contain no <tt>null</tt>
217      * values and only {@link Long Long} values.
218      *
219      * @return <tt>true</tt> if the underlying map is
220      * valid; returns <tt>false</tt> otherwise.
221      */

222     public boolean validate()
223     { return Adapter.isObjectKeyLongAdaptable(map); }
224
225     /**
226      * Validates the map underlying this adapter and throws
227      * an exception if it is invalid. For the underlying map to be valid it
228      * can contain no <tt>null</tt>
229      * values and only {@link Long Long} values.
230      *
231      * @throws IllegalStateException
232      * if the underlying map is invalid.
233      */

234     public void evalidate() {
235         if (!validate())
236             Exceptions.cannotAdapt("map");
237     }
238
239 }
Popular Tags