001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2002, 2003 Søren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package bak.pcj.adapter;
020:
021: import bak.pcj.Adapter;
022: import bak.pcj.LongIterator;
023: import bak.pcj.LongCollection;
024: import bak.pcj.map.LongKeyLongMap;
025: import bak.pcj.map.AbstractLongKeyLongMap;
026: import bak.pcj.map.LongKeyLongMapIterator;
027: import bak.pcj.map.MapDefaults;
028: import bak.pcj.map.NoSuchMappingException;
029: import bak.pcj.set.LongSet;
030: import bak.pcj.util.Exceptions;
031:
032: import java.util.Map;
033: import java.util.Iterator;
034:
035: /**
036: * This class represents adaptions of Java Collections Framework
037: * maps to primitive maps from long values to long values.
038: * The adapter is implemented as a wrapper around the map.
039: * Thus, changes to the underlying map are reflected by this
040: * map and vice versa.
041: *
042: * <p>
043: * Adapters from JCF maps to primitive map will
044: * fail if the JCF collection contains <tt>null</tt> keys/values or
045: * keys/values of the wrong class. However, adapters are not fast
046: * failing in the case that the underlying map should
047: * contain illegal keys or values. To implement fast failure would require
048: * every operation to check every key and value of the underlying
049: * map before doing anything. Instead validation methods
050: * are provided. They can be called using the assertion facility
051: * in the client code:
052: * <pre>
053: * MapToLongKeyLongMapAdapter s;
054: * ...
055: * <b>assert</b> s.validate();
056: * </pre>
057: * or by letting the adapter throw an exception on illegal values:
058: * <pre>
059: * MapToLongKeyLongMapAdapter s;
060: * ...
061: * s.evalidate(); // Throws an exception on illegal values
062: * </pre>
063: * Either way, validation must be invoked directly by the client
064: * code.
065: *
066: * @author Søren Bak
067: * @version 1.3 21-08-2003 19:09
068: * @since 1.0
069: */
070: public class MapToLongKeyLongMapAdapter extends AbstractLongKeyLongMap
071: implements LongKeyLongMap {
072:
073: /** The underlying map. */
074: protected Map map;
075:
076: /** The value corresponding to the last key found by containsKey(). */
077: protected Long lastValue;
078:
079: /**
080: * Creates a new adaption to a map from long
081: * values to long values.
082: *
083: * @param map
084: * the underlying map. This map must
085: * consist of keys of class
086: * {@link Long Long}.
087: * values of class
088: * {@link Long Long}. Otherwise a
089: * {@link ClassCastException ClassCastException}
090: * will be thrown by some methods.
091: *
092: * @throws NullPointerException
093: * if <tt>map</tt> is <tt>null</tt>.
094: */
095: public MapToLongKeyLongMapAdapter(Map map) {
096: if (map == null)
097: Exceptions.nullArgument("map");
098: this .map = map;
099: lastValue = null;
100: }
101:
102: /**
103: * Creates a new adaption to a map from long
104: * values to long values. The map to adapt is optionally validated.
105: *
106: * @param map
107: * the underlying map. This map must
108: * consist of keys of class
109: * {@link Long Long}.
110: * values of class
111: * {@link Long Long}. Otherwise a
112: * {@link ClassCastException ClassCastException}
113: * will be thrown by some methods.
114: *
115: * @param validate
116: * indicates whether <tt>map</tt> should
117: * be checked for illegal values.
118: *
119: * @throws NullPointerException
120: * if <tt>map</tt> is <tt>null</tt>.
121: *
122: * @throws IllegalStateException
123: * if <tt>validate</tt> is <tt>true</tt> and
124: * <tt>map</tt> contains a <tt>null</tt> key/value,
125: * a key that is not of class
126: * {@link Long Long},
127: * or a value that is not of class
128: * {@link Long Long}.
129: */
130: public MapToLongKeyLongMapAdapter(Map map, boolean validate) {
131: if (map == null)
132: Exceptions.nullArgument("map");
133: this .map = map;
134: lastValue = null;
135: if (validate)
136: evalidate();
137: }
138:
139: public void clear() {
140: map.clear();
141: }
142:
143: public boolean containsKey(long key) {
144: lastValue = (Long) map.get(new Long(key));
145: return lastValue != null;
146: }
147:
148: public boolean containsValue(long value) {
149: return map.containsValue(new Long(value));
150: }
151:
152: public LongKeyLongMapIterator entries() {
153: return new LongKeyLongMapIterator() {
154: Iterator i = map.entrySet().iterator();
155: Map.Entry lastEntry = null;
156:
157: public boolean hasNext() {
158: return i.hasNext();
159: }
160:
161: public void next() {
162: lastEntry = (Map.Entry) i.next();
163: }
164:
165: public long getKey() {
166: if (lastEntry == null)
167: Exceptions.noElementToGet();
168: return ((Long) lastEntry.getKey()).longValue();
169: }
170:
171: public long getValue() {
172: if (lastEntry == null)
173: Exceptions.noElementToGet();
174: return ((Long) lastEntry.getValue()).longValue();
175: }
176:
177: public void remove() {
178: i.remove();
179: lastEntry = null;
180: }
181: };
182: }
183:
184: public long get(long key) {
185: Long value = (Long) map.get(new Long(key));
186: return value == null ? MapDefaults.defaultLong() : value
187: .longValue();
188: }
189:
190: public LongSet keySet() {
191: return new SetToLongSetAdapter(map.keySet());
192: }
193:
194: public long lget() {
195: if (lastValue == null)
196: Exceptions.noLastElement();
197: return lastValue.longValue();
198: }
199:
200: public long put(long key, long value) {
201: Long oldValue = (Long) map.put(new Long(key), new Long(value));
202: return oldValue == null ? MapDefaults.defaultLong() : oldValue
203: .longValue();
204: }
205:
206: public long remove(long key) {
207: Long value = (Long) map.remove(new Long(key));
208: return value == null ? MapDefaults.defaultLong() : value
209: .longValue();
210: }
211:
212: public int size() {
213: return map.size();
214: }
215:
216: public LongCollection values() {
217: return new CollectionToLongCollectionAdapter(map.values());
218: }
219:
220: public long tget(long key) {
221: Long value = (Long) map.get(new Long(key));
222: if (value == null)
223: Exceptions.noSuchMapping(String.valueOf(key));
224: return value.longValue();
225: }
226:
227: /**
228: * Indicates whether the underlying map is valid for
229: * this adapter. For the underlying map to be valid, it
230: * can only contain {@link Long Long} keys, no <tt>null</tt>
231: * keys/values, and only {@link Long Long} values.
232: *
233: * @return <tt>true</tt> if the underlying map is
234: * valid; returns <tt>false</tt> otherwise.
235: */
236: public boolean validate() {
237: return Adapter.isLongKeyLongAdaptable(map);
238: }
239:
240: /**
241: * Validates the map underlying this adapter and throws
242: * an exception if it is invalid. For the underlying map
243: * to be valid, it
244: * can only contain {@link Long Long} keys, no <tt>null</tt>
245: * keys/values, and only {@link Long Long} values.
246: *
247: * @throws IllegalStateException
248: * if the underlying map is invalid.
249: */
250: public void evalidate() {
251: if (!validate())
252: Exceptions.cannotAdapt("map");
253: }
254:
255: }
|