2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / util / AbstractMap.java
blob4be5f3dbd942605dbced7039b9ab6057de8a2289
1 /* AbstractMap.java -- Abstract implementation of most of Map
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.util;
41 /**
42 * An abstract implementation of Map to make it easier to create your own
43 * implementations. In order to create an unmodifiable Map, subclass
44 * AbstractMap and implement the <code>entrySet</code> (usually via an
45 * AbstractSet). To make it modifiable, also implement <code>put</code>,
46 * and have <code>entrySet().iterator()</code> support <code>remove</code>.
47 * <p>
49 * It is recommended that classes which extend this support at least the
50 * no-argument constructor, and a constructor which accepts another Map.
51 * Further methods in this class may be overridden if you have a more
52 * efficient implementation.
54 * @author Original author unknown
55 * @author Bryce McKinlay
56 * @author Eric Blake <ebb9@email.byu.edu>
57 * @see Map
58 * @see Collection
59 * @see HashMap
60 * @see LinkedHashMap
61 * @see TreeMap
62 * @see WeakHashMap
63 * @see IdentityHashMap
64 * @since 1.2
65 * @status updated to 1.4
67 public abstract class AbstractMap implements Map
69 /** An "enum" of iterator types. */
70 // Package visible for use by subclasses.
71 static final int KEYS = 0,
72 VALUES = 1,
73 ENTRIES = 2;
75 /**
76 * The cache for {@link #keySet()}.
78 // Package visible for use by subclasses.
79 Set keys;
81 /**
82 * The cache for {@link #values()}.
84 // Package visible for use by subclasses.
85 Collection values;
87 /**
88 * The main constructor, for use by subclasses.
90 protected AbstractMap()
94 /**
95 * Remove all entries from this Map (optional operation). This default
96 * implementation calls entrySet().clear(). NOTE: If the entry set does
97 * not permit clearing, then this will fail, too. Subclasses often
98 * override this for efficiency. Your implementation of entrySet() should
99 * not call <code>AbstractMap.clear</code> unless you want an infinite loop.
101 * @throws UnsupportedOperationException if <code>entrySet().clear()</code>
102 * does not support clearing.
103 * @see Set#clear()
105 public void clear()
107 entrySet().clear();
111 * Create a shallow copy of this Map, no keys or values are copied. The
112 * default implementation simply calls <code>super.clone()</code>.
114 * @return the shallow clone
115 * @throws CloneNotSupportedException if a subclass is not Cloneable
116 * @see Cloneable
117 * @see Object#clone()
119 protected Object clone() throws CloneNotSupportedException
121 AbstractMap copy = (AbstractMap) super.clone();
122 // Clear out the caches; they are stale.
123 copy.keys = null;
124 copy.values = null;
125 return copy;
129 * Returns true if this contains a mapping for the given key. This
130 * implementation does a linear search, O(n), over the
131 * <code>entrySet()</code>, returning <code>true</code> if a match
132 * is found, <code>false</code> if the iteration ends. Many subclasses
133 * can implement this more efficiently.
135 * @param key the key to search for
136 * @return true if the map contains the key
137 * @throws NullPointerException if key is <code>null</code> but the map
138 * does not permit null keys
139 * @see #containsValue(Object)
141 public boolean containsKey(Object key)
143 Iterator entries = entrySet().iterator();
144 int pos = size();
145 while (--pos >= 0)
146 if (equals(key, ((Map.Entry) entries.next()).getKey()))
147 return true;
148 return false;
152 * Returns true if this contains at least one mapping with the given value.
153 * This implementation does a linear search, O(n), over the
154 * <code>entrySet()</code>, returning <code>true</code> if a match
155 * is found, <code>false</code> if the iteration ends. A match is
156 * defined as <code>(value == null ? v == null : value.equals(v))</code>
157 * Subclasses are unlikely to implement this more efficiently.
159 * @param value the value to search for
160 * @return true if the map contains the value
161 * @see #containsKey(Object)
163 public boolean containsValue(Object value)
165 Iterator entries = entrySet().iterator();
166 int pos = size();
167 while (--pos >= 0)
168 if (equals(value, ((Map.Entry) entries.next()).getValue()))
169 return true;
170 return false;
174 * Returns a set view of the mappings in this Map. Each element in the
175 * set must be an implementation of Map.Entry. The set is backed by
176 * the map, so that changes in one show up in the other. Modifications
177 * made while an iterator is in progress cause undefined behavior. If
178 * the set supports removal, these methods must be valid:
179 * <code>Iterator.remove</code>, <code>Set.remove</code>,
180 * <code>removeAll</code>, <code>retainAll</code>, and <code>clear</code>.
181 * Element addition is not supported via this set.
183 * @return the entry set
184 * @see Map.Entry
186 public abstract Set entrySet();
189 * Compares the specified object with this map for equality. Returns
190 * <code>true</code> if the other object is a Map with the same mappings,
191 * that is,<br>
192 * <code>o instanceof Map && entrySet().equals(((Map) o).entrySet();</code>
194 * @param o the object to be compared
195 * @return true if the object equals this map
196 * @see Set#equals(Object)
198 public boolean equals(Object o)
200 return (o == this ||
201 (o instanceof Map &&
202 entrySet().equals(((Map) o).entrySet())));
206 * Returns the value mapped by the given key. Returns <code>null</code> if
207 * there is no mapping. However, in Maps that accept null values, you
208 * must rely on <code>containsKey</code> to determine if a mapping exists.
209 * This iteration takes linear time, searching entrySet().iterator() of
210 * the key. Many implementations override this method.
212 * @param key the key to look up
213 * @return the value associated with the key, or null if key not in map
214 * @throws NullPointerException if this map does not accept null keys
215 * @see #containsKey(Object)
217 public Object get(Object key)
219 Iterator entries = entrySet().iterator();
220 int pos = size();
221 while (--pos >= 0)
223 Map.Entry entry = (Map.Entry) entries.next();
224 if (equals(key, entry.getKey()))
225 return entry.getValue();
227 return null;
231 * Returns the hash code for this map. As defined in Map, this is the sum
232 * of all hashcodes for each Map.Entry object in entrySet, or basically
233 * entrySet().hashCode().
235 * @return the hash code
236 * @see Map.Entry#hashCode()
237 * @see Set#hashCode()
239 public int hashCode()
241 return entrySet().hashCode();
245 * Returns true if the map contains no mappings. This is implemented by
246 * <code>size() == 0</code>.
248 * @return true if the map is empty
249 * @see #size()
251 public boolean isEmpty()
253 return size() == 0;
257 * Returns a set view of this map's keys. The set is backed by the map,
258 * so changes in one show up in the other. Modifications while an iteration
259 * is in progress produce undefined behavior. The set supports removal
260 * if entrySet() does, but does not support element addition.
261 * <p>
263 * This implementation creates an AbstractSet, where the iterator wraps
264 * the entrySet iterator, size defers to the Map's size, and contains
265 * defers to the Map's containsKey. The set is created on first use, and
266 * returned on subsequent uses, although since no synchronization occurs,
267 * there is a slight possibility of creating two sets.
269 * @return a Set view of the keys
270 * @see Set#iterator()
271 * @see #size()
272 * @see #containsKey(Object)
273 * @see #values()
275 public Set keySet()
277 if (keys == null)
278 keys = new AbstractSet()
280 public int size()
282 return AbstractMap.this.size();
285 public boolean contains(Object key)
287 return containsKey(key);
290 public Iterator iterator()
292 return new Iterator()
294 private final Iterator map_iterator = entrySet().iterator();
296 public boolean hasNext()
298 return map_iterator.hasNext();
301 public Object next()
303 return ((Map.Entry) map_iterator.next()).getKey();
306 public void remove()
308 map_iterator.remove();
313 return keys;
317 * Associates the given key to the given value (optional operation). If the
318 * map already contains the key, its value is replaced. This implementation
319 * simply throws an UnsupportedOperationException. Be aware that in a map
320 * that permits <code>null</code> values, a null return does not always
321 * imply that the mapping was created.
323 * @param key the key to map
324 * @param value the value to be mapped
325 * @return the previous value of the key, or null if there was no mapping
326 * @throws UnsupportedOperationException if the operation is not supported
327 * @throws ClassCastException if the key or value is of the wrong type
328 * @throws IllegalArgumentException if something about this key or value
329 * prevents it from existing in this map
330 * @throws NullPointerException if the map forbids null keys or values
331 * @see #containsKey(Object)
333 public Object put(Object key, Object value)
335 throw new UnsupportedOperationException();
339 * Copies all entries of the given map to this one (optional operation). If
340 * the map already contains a key, its value is replaced. This implementation
341 * simply iterates over the map's entrySet(), calling <code>put</code>,
342 * so it is not supported if puts are not.
344 * @param m the mapping to load into this map
345 * @throws UnsupportedOperationException if the operation is not supported
346 * @throws ClassCastException if a key or value is of the wrong type
347 * @throws IllegalArgumentException if something about a key or value
348 * prevents it from existing in this map
349 * @throws NullPointerException if the map forbids null keys or values, or
350 * if <code>m</code> is null.
351 * @see #put(Object, Object)
353 public void putAll(Map m)
355 Iterator entries = m.entrySet().iterator();
356 int pos = m.size();
357 while (--pos >= 0)
359 Map.Entry entry = (Map.Entry) entries.next();
360 put(entry.getKey(), entry.getValue());
365 * Removes the mapping for this key if present (optional operation). This
366 * implementation iterates over the entrySet searching for a matching
367 * key, at which point it calls the iterator's <code>remove</code> method.
368 * It returns the result of <code>getValue()</code> on the entry, if found,
369 * or null if no entry is found. Note that maps which permit null values
370 * may also return null if the key was removed. If the entrySet does not
371 * support removal, this will also fail. This is O(n), so many
372 * implementations override it for efficiency.
374 * @param key the key to remove
375 * @return the value the key mapped to, or null if not present
376 * @throws UnsupportedOperationException if deletion is unsupported
377 * @see Iterator#remove()
379 public Object remove(Object key)
381 Iterator entries = entrySet().iterator();
382 int pos = size();
383 while (--pos >= 0)
385 Map.Entry entry = (Map.Entry) entries.next();
386 if (equals(key, entry.getKey()))
388 // Must get the value before we remove it from iterator.
389 Object r = entry.getValue();
390 entries.remove();
391 return r;
394 return null;
398 * Returns the number of key-value mappings in the map. If there are more
399 * than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE. This is
400 * implemented as <code>entrySet().size()</code>.
402 * @return the number of mappings
403 * @see Set#size()
405 public int size()
407 return entrySet().size();
411 * Returns a String representation of this map. This is a listing of the
412 * map entries (which are specified in Map.Entry as being
413 * <code>getKey() + "=" + getValue()</code>), separated by a comma and
414 * space (", "), and surrounded by braces ('{' and '}'). This implementation
415 * uses a StringBuffer and iterates over the entrySet to build the String.
416 * Note that this can fail with an exception if underlying keys or
417 * values complete abruptly in toString().
419 * @return a String representation
420 * @see Map.Entry#toString()
422 public String toString()
424 Iterator entries = entrySet().iterator();
425 StringBuffer r = new StringBuffer("{");
426 for (int pos = size(); pos > 0; pos--)
428 Map.Entry entry = (Map.Entry) entries.next();
429 r.append(entry.getKey());
430 r.append('=');
431 r.append(entry.getValue());
432 if (pos > 1)
433 r.append(", ");
435 r.append("}");
436 return r.toString();
440 * Returns a collection or bag view of this map's values. The collection
441 * is backed by the map, so changes in one show up in the other.
442 * Modifications while an iteration is in progress produce undefined
443 * behavior. The collection supports removal if entrySet() does, but
444 * does not support element addition.
445 * <p>
447 * This implementation creates an AbstractCollection, where the iterator
448 * wraps the entrySet iterator, size defers to the Map's size, and contains
449 * defers to the Map's containsValue. The collection is created on first
450 * use, and returned on subsequent uses, although since no synchronization
451 * occurs, there is a slight possibility of creating two collections.
453 * @return a Collection view of the values
454 * @see Collection#iterator()
455 * @see #size()
456 * @see #containsValue(Object)
457 * @see #keySet()
459 public Collection values()
461 if (values == null)
462 values = new AbstractCollection()
464 public int size()
466 return AbstractMap.this.size();
469 public boolean contains(Object value)
471 return containsValue(value);
474 public Iterator iterator()
476 return new Iterator()
478 private final Iterator map_iterator = entrySet().iterator();
480 public boolean hasNext()
482 return map_iterator.hasNext();
485 public Object next()
487 return ((Map.Entry) map_iterator.next()).getValue();
490 public void remove()
492 map_iterator.remove();
497 return values;
501 * Compare two objects according to Collection semantics.
503 * @param o1 the first object
504 * @param o2 the second object
505 * @return o1 == null ? o2 == null : o1.equals(o2)
507 // Package visible for use throughout java.util.
508 // It may be inlined since it is final.
509 static final boolean equals(Object o1, Object o2)
511 return o1 == null ? o2 == null : o1.equals(o2);
515 * Hash an object according to Collection semantics.
517 * @param o the object to hash
518 * @return o1 == null ? 0 : o1.hashCode()
520 // Package visible for use throughout java.util.
521 // It may be inlined since it is final.
522 static final int hashCode(Object o)
524 return o == null ? 0 : o.hashCode();
528 * A class which implements Map.Entry. It is shared by HashMap, TreeMap,
529 * Hashtable, and Collections. It is not specified by the JDK, but makes
530 * life much easier.
532 * @author Jon Zeppieri
533 * @author Eric Blake <ebb9@email.byu.edu>
535 // XXX - FIXME Use fully qualified implements as gcj 3.1 workaround.
536 static class BasicMapEntry implements Map.Entry
539 * The key. Package visible for direct manipulation.
541 Object key;
544 * The value. Package visible for direct manipulation.
546 Object value;
549 * Basic constructor initializes the fields.
550 * @param newKey the key
551 * @param newValue the value
553 BasicMapEntry(Object newKey, Object newValue)
555 key = newKey;
556 value = newValue;
560 * Compares the specified object with this entry. Returns true only if
561 * the object is a mapping of identical key and value. In other words,
562 * this must be:<br>
563 * <pre>(o instanceof Map.Entry)
564 * && (getKey() == null ? ((HashMap) o).getKey() == null
565 * : getKey().equals(((HashMap) o).getKey()))
566 * && (getValue() == null ? ((HashMap) o).getValue() == null
567 * : getValue().equals(((HashMap) o).getValue()))</pre>
569 * @param o the object to compare
570 * @return <code>true</code> if it is equal
572 public final boolean equals(Object o)
574 if (! (o instanceof Map.Entry))
575 return false;
576 // Optimize for our own entries.
577 if (o instanceof BasicMapEntry)
579 BasicMapEntry e = (BasicMapEntry) o;
580 return (AbstractMap.equals(key, e.key)
581 && AbstractMap.equals(value, e.value));
583 Map.Entry e = (Map.Entry) o;
584 return (AbstractMap.equals(key, e.getKey())
585 && AbstractMap.equals(value, e.getValue()));
589 * Get the key corresponding to this entry.
591 * @return the key
593 public final Object getKey()
595 return key;
599 * Get the value corresponding to this entry. If you already called
600 * Iterator.remove(), the behavior undefined, but in this case it works.
602 * @return the value
604 public final Object getValue()
606 return value;
610 * Returns the hash code of the entry. This is defined as the exclusive-or
611 * of the hashcodes of the key and value (using 0 for null). In other
612 * words, this must be:<br>
613 * <pre>(getKey() == null ? 0 : getKey().hashCode())
614 * ^ (getValue() == null ? 0 : getValue().hashCode())</pre>
616 * @return the hash code
618 public final int hashCode()
620 return (AbstractMap.hashCode(key) ^ AbstractMap.hashCode(value));
624 * Replaces the value with the specified object. This writes through
625 * to the map, unless you have already called Iterator.remove(). It
626 * may be overridden to restrict a null value.
628 * @param newVal the new value to store
629 * @return the old value
630 * @throws NullPointerException if the map forbids null values
632 public Object setValue(Object newVal)
634 Object r = value;
635 value = newVal;
636 return r;
640 * This provides a string representation of the entry. It is of the form
641 * "key=value", where string concatenation is used on key and value.
643 * @return the string representation
645 public final String toString()
647 return key + "=" + value;
649 } // class BasicMapEntry