libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / util / AbstractMap.java
blob5d59db9a666947244007cc0a4cafb3bd10ed629b
1 /* AbstractMap.java -- Abstract implementation of most of Map
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package java.util;
42 import gnu.java.lang.CPStringBuilder;
44 import java.io.Serializable;
46 /**
47 * An abstract implementation of Map to make it easier to create your own
48 * implementations. In order to create an unmodifiable Map, subclass
49 * AbstractMap and implement the <code>entrySet</code> (usually via an
50 * AbstractSet). To make it modifiable, also implement <code>put</code>,
51 * and have <code>entrySet().iterator()</code> support <code>remove</code>.
52 * <p>
54 * It is recommended that classes which extend this support at least the
55 * no-argument constructor, and a constructor which accepts another Map.
56 * Further methods in this class may be overridden if you have a more
57 * efficient implementation.
59 * @author Original author unknown
60 * @author Bryce McKinlay
61 * @author Eric Blake (ebb9@email.byu.edu)
62 * @see Map
63 * @see Collection
64 * @see HashMap
65 * @see LinkedHashMap
66 * @see TreeMap
67 * @see WeakHashMap
68 * @see IdentityHashMap
69 * @since 1.2
70 * @status updated to 1.4
72 public abstract class AbstractMap<K, V> implements Map<K, V>
74 /**
75 * A class containing an immutable key and value. The
76 * implementation of {@link Entry#setValue(V)} for this class
77 * simply throws an {@link UnsupportedOperationException},
78 * thus preventing changes being made. This is useful when
79 * a static thread-safe view of a map is required.
81 * @since 1.6
83 public static class SimpleImmutableEntry<K, V>
84 implements Entry<K, V>, Serializable
86 /**
87 * Compatible with JDK 1.6
89 private static final long serialVersionUID = 7138329143949025153L;
91 K key;
92 V value;
94 public SimpleImmutableEntry(K key, V value)
96 this.key = key;
97 this.value = value;
100 public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry)
102 this(entry.getKey(), entry.getValue());
105 public K getKey()
107 return key;
110 public V getValue()
112 return value;
115 public V setValue(V value)
117 throw new UnsupportedOperationException("setValue not supported on immutable entry");
121 /** An "enum" of iterator types. */
122 // Package visible for use by subclasses.
123 static final int KEYS = 0,
124 VALUES = 1,
125 ENTRIES = 2;
128 * The cache for {@link #keySet()}.
130 // Package visible for use by subclasses.
131 Set<K> keys;
134 * The cache for {@link #values()}.
136 // Package visible for use by subclasses.
137 Collection<V> values;
140 * The main constructor, for use by subclasses.
142 protected AbstractMap()
147 * Returns a set view of the mappings in this Map. Each element in the
148 * set must be an implementation of Map.Entry. The set is backed by
149 * the map, so that changes in one show up in the other. Modifications
150 * made while an iterator is in progress cause undefined behavior. If
151 * the set supports removal, these methods must be valid:
152 * <code>Iterator.remove</code>, <code>Set.remove</code>,
153 * <code>removeAll</code>, <code>retainAll</code>, and <code>clear</code>.
154 * Element addition is not supported via this set.
156 * @return the entry set
157 * @see Map.Entry
159 public abstract Set<Map.Entry<K, V>> entrySet();
162 * Remove all entries from this Map (optional operation). This default
163 * implementation calls entrySet().clear(). NOTE: If the entry set does
164 * not permit clearing, then this will fail, too. Subclasses often
165 * override this for efficiency. Your implementation of entrySet() should
166 * not call <code>AbstractMap.clear</code> unless you want an infinite loop.
168 * @throws UnsupportedOperationException if <code>entrySet().clear()</code>
169 * does not support clearing.
170 * @see Set#clear()
172 public void clear()
174 entrySet().clear();
178 * Create a shallow copy of this Map, no keys or values are copied. The
179 * default implementation simply calls <code>super.clone()</code>.
181 * @return the shallow clone
182 * @throws CloneNotSupportedException if a subclass is not Cloneable
183 * @see Cloneable
184 * @see Object#clone()
186 protected Object clone() throws CloneNotSupportedException
188 AbstractMap<K, V> copy = (AbstractMap<K, V>) super.clone();
189 // Clear out the caches; they are stale.
190 copy.keys = null;
191 copy.values = null;
192 return copy;
196 * Returns true if this contains a mapping for the given key. This
197 * implementation does a linear search, O(n), over the
198 * <code>entrySet()</code>, returning <code>true</code> if a match
199 * is found, <code>false</code> if the iteration ends. Many subclasses
200 * can implement this more efficiently.
202 * @param key the key to search for
203 * @return true if the map contains the key
204 * @throws NullPointerException if key is <code>null</code> but the map
205 * does not permit null keys
206 * @see #containsValue(Object)
208 public boolean containsKey(Object key)
210 Iterator<Map.Entry<K, V>> entries = entrySet().iterator();
211 int pos = size();
212 while (--pos >= 0)
213 if (equals(key, entries.next().getKey()))
214 return true;
215 return false;
219 * Returns true if this contains at least one mapping with the given value.
220 * This implementation does a linear search, O(n), over the
221 * <code>entrySet()</code>, returning <code>true</code> if a match
222 * is found, <code>false</code> if the iteration ends. A match is
223 * defined as a value, v, where <code>(value == null ? v == null :
224 * value.equals(v))</code>. Subclasses are unlikely to implement
225 * this more efficiently.
227 * @param value the value to search for
228 * @return true if the map contains the value
229 * @see #containsKey(Object)
231 public boolean containsValue(Object value)
233 Iterator<Map.Entry<K, V>> entries = entrySet().iterator();
234 int pos = size();
235 while (--pos >= 0)
236 if (equals(value, entries.next().getValue()))
237 return true;
238 return false;
242 * Compares the specified object with this map for equality. Returns
243 * <code>true</code> if the other object is a Map with the same mappings,
244 * that is,<br>
245 * <code>o instanceof Map && entrySet().equals(((Map) o).entrySet();</code>
247 * @param o the object to be compared
248 * @return true if the object equals this map
249 * @see Set#equals(Object)
251 public boolean equals(Object o)
253 return (o == this
254 || (o instanceof Map
255 && entrySet().equals(((Map<K, V>) o).entrySet())));
259 * Returns the value mapped by the given key. Returns <code>null</code> if
260 * there is no mapping. However, in Maps that accept null values, you
261 * must rely on <code>containsKey</code> to determine if a mapping exists.
262 * This iteration takes linear time, searching entrySet().iterator() of
263 * the key. Many implementations override this method.
265 * @param key the key to look up
266 * @return the value associated with the key, or null if key not in map
267 * @throws NullPointerException if this map does not accept null keys
268 * @see #containsKey(Object)
270 public V get(Object key)
272 Iterator<Map.Entry<K, V>> entries = entrySet().iterator();
273 int pos = size();
274 while (--pos >= 0)
276 Map.Entry<K, V> entry = entries.next();
277 if (equals(key, entry.getKey()))
278 return entry.getValue();
280 return null;
284 * Returns the hash code for this map. As defined in Map, this is the sum
285 * of all hashcodes for each Map.Entry object in entrySet, or basically
286 * entrySet().hashCode().
288 * @return the hash code
289 * @see Map.Entry#hashCode()
290 * @see Set#hashCode()
292 public int hashCode()
294 return entrySet().hashCode();
298 * Returns true if the map contains no mappings. This is implemented by
299 * <code>size() == 0</code>.
301 * @return true if the map is empty
302 * @see #size()
304 public boolean isEmpty()
306 return size() == 0;
310 * Returns a set view of this map's keys. The set is backed by the map,
311 * so changes in one show up in the other. Modifications while an iteration
312 * is in progress produce undefined behavior. The set supports removal
313 * if entrySet() does, but does not support element addition.
314 * <p>
316 * This implementation creates an AbstractSet, where the iterator wraps
317 * the entrySet iterator, size defers to the Map's size, and contains
318 * defers to the Map's containsKey. The set is created on first use, and
319 * returned on subsequent uses, although since no synchronization occurs,
320 * there is a slight possibility of creating two sets.
322 * @return a Set view of the keys
323 * @see Set#iterator()
324 * @see #size()
325 * @see #containsKey(Object)
326 * @see #values()
328 public Set<K> keySet()
330 if (keys == null)
331 keys = new AbstractSet<K>()
334 * Retrieves the number of keys in the backing map.
336 * @return The number of keys.
338 public int size()
340 return AbstractMap.this.size();
344 * Returns true if the backing map contains the
345 * supplied key.
347 * @param key The key to search for.
348 * @return True if the key was found, false otherwise.
350 public boolean contains(Object key)
352 return containsKey(key);
356 * Returns an iterator which iterates over the keys
357 * in the backing map, using a wrapper around the
358 * iterator returned by <code>entrySet()</code>.
360 * @return An iterator over the keys.
362 public Iterator<K> iterator()
364 return new Iterator<K>()
367 * The iterator returned by <code>entrySet()</code>.
369 private final Iterator<Map.Entry<K, V>> map_iterator
370 = entrySet().iterator();
373 * Returns true if a call to <code>next()</code> will
374 * return another key.
376 * @return True if the iterator has not yet reached
377 * the last key.
379 public boolean hasNext()
381 return map_iterator.hasNext();
385 * Returns the key from the next entry retrieved
386 * by the underlying <code>entrySet()</code> iterator.
388 * @return The next key.
390 public K next()
392 return map_iterator.next().getKey();
396 * Removes the map entry which has a key equal
397 * to that returned by the last call to
398 * <code>next()</code>.
400 * @throws UnsupportedOperationException if the
401 * map doesn't support removal.
403 public void remove()
405 map_iterator.remove();
410 return keys;
414 * Associates the given key to the given value (optional operation). If the
415 * map already contains the key, its value is replaced. This implementation
416 * simply throws an UnsupportedOperationException. Be aware that in a map
417 * that permits <code>null</code> values, a null return does not always
418 * imply that the mapping was created.
420 * @param key the key to map
421 * @param value the value to be mapped
422 * @return the previous value of the key, or null if there was no mapping
423 * @throws UnsupportedOperationException if the operation is not supported
424 * @throws ClassCastException if the key or value is of the wrong type
425 * @throws IllegalArgumentException if something about this key or value
426 * prevents it from existing in this map
427 * @throws NullPointerException if the map forbids null keys or values
428 * @see #containsKey(Object)
430 public V put(K key, V value)
432 throw new UnsupportedOperationException();
436 * Copies all entries of the given map to this one (optional operation). If
437 * the map already contains a key, its value is replaced. This implementation
438 * simply iterates over the map's entrySet(), calling <code>put</code>,
439 * so it is not supported if puts are not.
441 * @param m the mapping to load into this map
442 * @throws UnsupportedOperationException if the operation is not supported
443 * by this map.
444 * @throws ClassCastException if a key or value is of the wrong type for
445 * adding to this map.
446 * @throws IllegalArgumentException if something about a key or value
447 * prevents it from existing in this map.
448 * @throws NullPointerException if the map forbids null keys or values.
449 * @throws NullPointerException if <code>m</code> is null.
450 * @see #put(Object, Object)
452 public void putAll(Map<? extends K, ? extends V> m)
454 // FIXME: bogus circumlocution.
455 Iterator entries2 = m.entrySet().iterator();
456 Iterator<Map.Entry<? extends K, ? extends V>> entries
457 = (Iterator<Map.Entry<? extends K, ? extends V>>) entries2;
458 int pos = m.size();
459 while (--pos >= 0)
461 Map.Entry<? extends K, ? extends V> entry = entries.next();
462 put(entry.getKey(), entry.getValue());
467 * Removes the mapping for this key if present (optional operation). This
468 * implementation iterates over the entrySet searching for a matching
469 * key, at which point it calls the iterator's <code>remove</code> method.
470 * It returns the result of <code>getValue()</code> on the entry, if found,
471 * or null if no entry is found. Note that maps which permit null values
472 * may also return null if the key was removed. If the entrySet does not
473 * support removal, this will also fail. This is O(n), so many
474 * implementations override it for efficiency.
476 * @param key the key to remove
477 * @return the value the key mapped to, or null if not present.
478 * Null may also be returned if null values are allowed
479 * in the map and the value of this mapping is null.
480 * @throws UnsupportedOperationException if deletion is unsupported
481 * @see Iterator#remove()
483 public V remove(Object key)
485 Iterator<Map.Entry<K, V>> entries = entrySet().iterator();
486 int pos = size();
487 while (--pos >= 0)
489 Map.Entry<K, V> entry = entries.next();
490 if (equals(key, entry.getKey()))
492 // Must get the value before we remove it from iterator.
493 V r = entry.getValue();
494 entries.remove();
495 return r;
498 return null;
502 * Returns the number of key-value mappings in the map. If there are more
503 * than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE. This is
504 * implemented as <code>entrySet().size()</code>.
506 * @return the number of mappings
507 * @see Set#size()
509 public int size()
511 return entrySet().size();
515 * Returns a String representation of this map. This is a listing of the
516 * map entries (which are specified in Map.Entry as being
517 * <code>getKey() + "=" + getValue()</code>), separated by a comma and
518 * space (", "), and surrounded by braces ('{' and '}'). This implementation
519 * uses a StringBuffer and iterates over the entrySet to build the String.
520 * Note that this can fail with an exception if underlying keys or
521 * values complete abruptly in toString().
523 * @return a String representation
524 * @see Map.Entry#toString()
526 public String toString()
528 Iterator<Map.Entry<K, V>> entries = entrySet().iterator();
529 CPStringBuilder r = new CPStringBuilder("{");
530 for (int pos = size(); pos > 0; pos--)
532 Map.Entry<K, V> entry = entries.next();
533 r.append(entry.getKey());
534 r.append('=');
535 r.append(entry.getValue());
536 if (pos > 1)
537 r.append(", ");
539 r.append("}");
540 return r.toString();
544 * Returns a collection or bag view of this map's values. The collection
545 * is backed by the map, so changes in one show up in the other.
546 * Modifications while an iteration is in progress produce undefined
547 * behavior. The collection supports removal if entrySet() does, but
548 * does not support element addition.
549 * <p>
551 * This implementation creates an AbstractCollection, where the iterator
552 * wraps the entrySet iterator, size defers to the Map's size, and contains
553 * defers to the Map's containsValue. The collection is created on first
554 * use, and returned on subsequent uses, although since no synchronization
555 * occurs, there is a slight possibility of creating two collections.
557 * @return a Collection view of the values
558 * @see Collection#iterator()
559 * @see #size()
560 * @see #containsValue(Object)
561 * @see #keySet()
563 public Collection<V> values()
565 if (values == null)
566 values = new AbstractCollection<V>()
569 * Returns the number of values stored in
570 * the backing map.
572 * @return The number of values.
574 public int size()
576 return AbstractMap.this.size();
580 * Returns true if the backing map contains
581 * the supplied value.
583 * @param value The value to search for.
584 * @return True if the value was found, false otherwise.
586 public boolean contains(Object value)
588 return containsValue(value);
592 * Returns an iterator which iterates over the
593 * values in the backing map, by using a wrapper
594 * around the iterator returned by <code>entrySet()</code>.
596 * @return An iterator over the values.
598 public Iterator<V> iterator()
600 return new Iterator<V>()
603 * The iterator returned by <code>entrySet()</code>.
605 private final Iterator<Map.Entry<K, V>> map_iterator
606 = entrySet().iterator();
609 * Returns true if a call to <code>next()</call> will
610 * return another value.
612 * @return True if the iterator has not yet reached
613 * the last value.
615 public boolean hasNext()
617 return map_iterator.hasNext();
621 * Returns the value from the next entry retrieved
622 * by the underlying <code>entrySet()</code> iterator.
624 * @return The next value.
626 public V next()
628 return map_iterator.next().getValue();
632 * Removes the map entry which has a key equal
633 * to that returned by the last call to
634 * <code>next()</code>.
636 * @throws UnsupportedOperationException if the
637 * map doesn't support removal.
639 public void remove()
641 map_iterator.remove();
646 return values;
650 * Compare two objects according to Collection semantics.
652 * @param o1 the first object
653 * @param o2 the second object
654 * @return o1 == o2 || (o1 != null && o1.equals(o2))
656 // Package visible for use throughout java.util.
657 // It may be inlined since it is final.
658 static final boolean equals(Object o1, Object o2)
660 return o1 == o2 || (o1 != null && o1.equals(o2));
664 * Hash an object according to Collection semantics.
666 * @param o the object to hash
667 * @return o1 == null ? 0 : o1.hashCode()
669 // Package visible for use throughout java.util.
670 // It may be inlined since it is final.
671 static final int hashCode(Object o)
673 return o == null ? 0 : o.hashCode();
677 * A class which implements Map.Entry. It is shared by HashMap, TreeMap,
678 * Hashtable, and Collections. It is not specified by the JDK, but makes
679 * life much easier.
681 * @author Jon Zeppieri
682 * @author Eric Blake (ebb9@email.byu.edu)
684 * @since 1.6
686 public static class SimpleEntry<K, V> implements Entry<K, V>, Serializable
690 * Compatible with JDK 1.6
692 private static final long serialVersionUID = -8499721149061103585L;
695 * The key. Package visible for direct manipulation.
697 K key;
700 * The value. Package visible for direct manipulation.
702 V value;
705 * Basic constructor initializes the fields.
706 * @param newKey the key
707 * @param newValue the value
709 public SimpleEntry(K newKey, V newValue)
711 key = newKey;
712 value = newValue;
715 public SimpleEntry(Entry<? extends K, ? extends V> entry)
717 this(entry.getKey(), entry.getValue());
721 * Compares the specified object with this entry. Returns true only if
722 * the object is a mapping of identical key and value. In other words,
723 * this must be:<br>
724 * <pre>(o instanceof Map.Entry)
725 * && (getKey() == null ? ((HashMap) o).getKey() == null
726 * : getKey().equals(((HashMap) o).getKey()))
727 * && (getValue() == null ? ((HashMap) o).getValue() == null
728 * : getValue().equals(((HashMap) o).getValue()))</pre>
730 * @param o the object to compare
731 * @return <code>true</code> if it is equal
733 public boolean equals(Object o)
735 if (! (o instanceof Map.Entry))
736 return false;
737 // Optimize for our own entries.
738 if (o instanceof SimpleEntry)
740 SimpleEntry e = (SimpleEntry) o;
741 return (AbstractMap.equals(key, e.key)
742 && AbstractMap.equals(value, e.value));
744 Map.Entry e = (Map.Entry) o;
745 return (AbstractMap.equals(key, e.getKey())
746 && AbstractMap.equals(value, e.getValue()));
750 * Get the key corresponding to this entry.
752 * @return the key
754 public K getKey()
756 return key;
760 * Get the value corresponding to this entry. If you already called
761 * Iterator.remove(), the behavior undefined, but in this case it works.
763 * @return the value
765 public V getValue()
767 return value;
771 * Returns the hash code of the entry. This is defined as the exclusive-or
772 * of the hashcodes of the key and value (using 0 for null). In other
773 * words, this must be:<br>
774 * <pre>(getKey() == null ? 0 : getKey().hashCode())
775 * ^ (getValue() == null ? 0 : getValue().hashCode())</pre>
777 * @return the hash code
779 public int hashCode()
781 return (AbstractMap.hashCode(key) ^ AbstractMap.hashCode(value));
785 * Replaces the value with the specified object. This writes through
786 * to the map, unless you have already called Iterator.remove(). It
787 * may be overridden to restrict a null value.
789 * @param newVal the new value to store
790 * @return the old value
791 * @throws NullPointerException if the map forbids null values.
792 * @throws UnsupportedOperationException if the map doesn't support
793 * <code>put()</code>.
794 * @throws ClassCastException if the value is of a type unsupported
795 * by the map.
796 * @throws IllegalArgumentException if something else about this
797 * value prevents it being stored in the map.
799 public V setValue(V newVal)
801 V r = value;
802 value = newVal;
803 return r;
807 * This provides a string representation of the entry. It is of the form
808 * "key=value", where string concatenation is used on key and value.
810 * @return the string representation
812 public String toString()
814 return key + "=" + value;
816 } // class SimpleEntry