AbstractMap.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /* AbstractMap.java -- Abstract implementation of most of Map
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util;
  33. import gnu.java.lang.CPStringBuilder;
  34. import java.io.Serializable;
  35. /**
  36. * An abstract implementation of Map to make it easier to create your own
  37. * implementations. In order to create an unmodifiable Map, subclass
  38. * AbstractMap and implement the <code>entrySet</code> (usually via an
  39. * AbstractSet). To make it modifiable, also implement <code>put</code>,
  40. * and have <code>entrySet().iterator()</code> support <code>remove</code>.
  41. * <p>
  42. *
  43. * It is recommended that classes which extend this support at least the
  44. * no-argument constructor, and a constructor which accepts another Map.
  45. * Further methods in this class may be overridden if you have a more
  46. * efficient implementation.
  47. *
  48. * @author Original author unknown
  49. * @author Bryce McKinlay
  50. * @author Eric Blake (ebb9@email.byu.edu)
  51. * @see Map
  52. * @see Collection
  53. * @see HashMap
  54. * @see LinkedHashMap
  55. * @see TreeMap
  56. * @see WeakHashMap
  57. * @see IdentityHashMap
  58. * @since 1.2
  59. * @status updated to 1.4
  60. */
  61. public abstract class AbstractMap<K, V> implements Map<K, V>
  62. {
  63. /**
  64. * A class containing an immutable key and value. The
  65. * implementation of {@link Entry#setValue(V)} for this class
  66. * simply throws an {@link UnsupportedOperationException},
  67. * thus preventing changes being made. This is useful when
  68. * a static thread-safe view of a map is required.
  69. *
  70. * @since 1.6
  71. */
  72. public static class SimpleImmutableEntry<K, V>
  73. implements Entry<K, V>, Serializable
  74. {
  75. /**
  76. * Compatible with JDK 1.6
  77. */
  78. private static final long serialVersionUID = 7138329143949025153L;
  79. K key;
  80. V value;
  81. public SimpleImmutableEntry(K key, V value)
  82. {
  83. this.key = key;
  84. this.value = value;
  85. }
  86. public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry)
  87. {
  88. this(entry.getKey(), entry.getValue());
  89. }
  90. public K getKey()
  91. {
  92. return key;
  93. }
  94. public V getValue()
  95. {
  96. return value;
  97. }
  98. public V setValue(V value)
  99. {
  100. throw new UnsupportedOperationException("setValue not supported on immutable entry");
  101. }
  102. }
  103. /** An "enum" of iterator types. */
  104. // Package visible for use by subclasses.
  105. static final int KEYS = 0,
  106. VALUES = 1,
  107. ENTRIES = 2;
  108. /**
  109. * The cache for {@link #keySet()}.
  110. */
  111. // Package visible for use by subclasses.
  112. Set<K> keys;
  113. /**
  114. * The cache for {@link #values()}.
  115. */
  116. // Package visible for use by subclasses.
  117. Collection<V> values;
  118. /**
  119. * The main constructor, for use by subclasses.
  120. */
  121. protected AbstractMap()
  122. {
  123. }
  124. /**
  125. * Returns a set view of the mappings in this Map. Each element in the
  126. * set must be an implementation of Map.Entry. The set is backed by
  127. * the map, so that changes in one show up in the other. Modifications
  128. * made while an iterator is in progress cause undefined behavior. If
  129. * the set supports removal, these methods must be valid:
  130. * <code>Iterator.remove</code>, <code>Set.remove</code>,
  131. * <code>removeAll</code>, <code>retainAll</code>, and <code>clear</code>.
  132. * Element addition is not supported via this set.
  133. *
  134. * @return the entry set
  135. * @see Map.Entry
  136. */
  137. public abstract Set<Map.Entry<K, V>> entrySet();
  138. /**
  139. * Remove all entries from this Map (optional operation). This default
  140. * implementation calls entrySet().clear(). NOTE: If the entry set does
  141. * not permit clearing, then this will fail, too. Subclasses often
  142. * override this for efficiency. Your implementation of entrySet() should
  143. * not call <code>AbstractMap.clear</code> unless you want an infinite loop.
  144. *
  145. * @throws UnsupportedOperationException if <code>entrySet().clear()</code>
  146. * does not support clearing.
  147. * @see Set#clear()
  148. */
  149. public void clear()
  150. {
  151. entrySet().clear();
  152. }
  153. /**
  154. * Create a shallow copy of this Map, no keys or values are copied. The
  155. * default implementation simply calls <code>super.clone()</code>.
  156. *
  157. * @return the shallow clone
  158. * @throws CloneNotSupportedException if a subclass is not Cloneable
  159. * @see Cloneable
  160. * @see Object#clone()
  161. */
  162. protected Object clone() throws CloneNotSupportedException
  163. {
  164. AbstractMap<K, V> copy = (AbstractMap<K, V>) super.clone();
  165. // Clear out the caches; they are stale.
  166. copy.keys = null;
  167. copy.values = null;
  168. return copy;
  169. }
  170. /**
  171. * Returns true if this contains a mapping for the given key. This
  172. * implementation does a linear search, O(n), over the
  173. * <code>entrySet()</code>, returning <code>true</code> if a match
  174. * is found, <code>false</code> if the iteration ends. Many subclasses
  175. * can implement this more efficiently.
  176. *
  177. * @param key the key to search for
  178. * @return true if the map contains the key
  179. * @throws NullPointerException if key is <code>null</code> but the map
  180. * does not permit null keys
  181. * @see #containsValue(Object)
  182. */
  183. public boolean containsKey(Object key)
  184. {
  185. Iterator<Map.Entry<K, V>> entries = entrySet().iterator();
  186. int pos = size();
  187. while (--pos >= 0)
  188. if (equals(key, entries.next().getKey()))
  189. return true;
  190. return false;
  191. }
  192. /**
  193. * Returns true if this contains at least one mapping with the given value.
  194. * This implementation does a linear search, O(n), over the
  195. * <code>entrySet()</code>, returning <code>true</code> if a match
  196. * is found, <code>false</code> if the iteration ends. A match is
  197. * defined as a value, v, where <code>(value == null ? v == null :
  198. * value.equals(v))</code>. Subclasses are unlikely to implement
  199. * this more efficiently.
  200. *
  201. * @param value the value to search for
  202. * @return true if the map contains the value
  203. * @see #containsKey(Object)
  204. */
  205. public boolean containsValue(Object value)
  206. {
  207. Iterator<Map.Entry<K, V>> entries = entrySet().iterator();
  208. int pos = size();
  209. while (--pos >= 0)
  210. if (equals(value, entries.next().getValue()))
  211. return true;
  212. return false;
  213. }
  214. /**
  215. * Compares the specified object with this map for equality. Returns
  216. * <code>true</code> if the other object is a Map with the same mappings,
  217. * that is,<br>
  218. * <code>o instanceof Map && entrySet().equals(((Map) o).entrySet();</code>
  219. *
  220. * @param o the object to be compared
  221. * @return true if the object equals this map
  222. * @see Set#equals(Object)
  223. */
  224. public boolean equals(Object o)
  225. {
  226. return (o == this
  227. || (o instanceof Map
  228. && entrySet().equals(((Map<K, V>) o).entrySet())));
  229. }
  230. /**
  231. * Returns the value mapped by the given key. Returns <code>null</code> if
  232. * there is no mapping. However, in Maps that accept null values, you
  233. * must rely on <code>containsKey</code> to determine if a mapping exists.
  234. * This iteration takes linear time, searching entrySet().iterator() of
  235. * the key. Many implementations override this method.
  236. *
  237. * @param key the key to look up
  238. * @return the value associated with the key, or null if key not in map
  239. * @throws NullPointerException if this map does not accept null keys
  240. * @see #containsKey(Object)
  241. */
  242. public V get(Object key)
  243. {
  244. Iterator<Map.Entry<K, V>> entries = entrySet().iterator();
  245. int pos = size();
  246. while (--pos >= 0)
  247. {
  248. Map.Entry<K, V> entry = entries.next();
  249. if (equals(key, entry.getKey()))
  250. return entry.getValue();
  251. }
  252. return null;
  253. }
  254. /**
  255. * Returns the hash code for this map. As defined in Map, this is the sum
  256. * of all hashcodes for each Map.Entry object in entrySet, or basically
  257. * entrySet().hashCode().
  258. *
  259. * @return the hash code
  260. * @see Map.Entry#hashCode()
  261. * @see Set#hashCode()
  262. */
  263. public int hashCode()
  264. {
  265. return entrySet().hashCode();
  266. }
  267. /**
  268. * Returns true if the map contains no mappings. This is implemented by
  269. * <code>size() == 0</code>.
  270. *
  271. * @return true if the map is empty
  272. * @see #size()
  273. */
  274. public boolean isEmpty()
  275. {
  276. return size() == 0;
  277. }
  278. /**
  279. * Returns a set view of this map's keys. The set is backed by the map,
  280. * so changes in one show up in the other. Modifications while an iteration
  281. * is in progress produce undefined behavior. The set supports removal
  282. * if entrySet() does, but does not support element addition.
  283. * <p>
  284. *
  285. * This implementation creates an AbstractSet, where the iterator wraps
  286. * the entrySet iterator, size defers to the Map's size, and contains
  287. * defers to the Map's containsKey. The set is created on first use, and
  288. * returned on subsequent uses, although since no synchronization occurs,
  289. * there is a slight possibility of creating two sets.
  290. *
  291. * @return a Set view of the keys
  292. * @see Set#iterator()
  293. * @see #size()
  294. * @see #containsKey(Object)
  295. * @see #values()
  296. */
  297. public Set<K> keySet()
  298. {
  299. if (keys == null)
  300. keys = new AbstractSet<K>()
  301. {
  302. /**
  303. * Retrieves the number of keys in the backing map.
  304. *
  305. * @return The number of keys.
  306. */
  307. public int size()
  308. {
  309. return AbstractMap.this.size();
  310. }
  311. /**
  312. * Returns true if the backing map contains the
  313. * supplied key.
  314. *
  315. * @param key The key to search for.
  316. * @return True if the key was found, false otherwise.
  317. */
  318. public boolean contains(Object key)
  319. {
  320. return containsKey(key);
  321. }
  322. /**
  323. * Returns an iterator which iterates over the keys
  324. * in the backing map, using a wrapper around the
  325. * iterator returned by <code>entrySet()</code>.
  326. *
  327. * @return An iterator over the keys.
  328. */
  329. public Iterator<K> iterator()
  330. {
  331. return new Iterator<K>()
  332. {
  333. /**
  334. * The iterator returned by <code>entrySet()</code>.
  335. */
  336. private final Iterator<Map.Entry<K, V>> map_iterator
  337. = entrySet().iterator();
  338. /**
  339. * Returns true if a call to <code>next()</code> will
  340. * return another key.
  341. *
  342. * @return True if the iterator has not yet reached
  343. * the last key.
  344. */
  345. public boolean hasNext()
  346. {
  347. return map_iterator.hasNext();
  348. }
  349. /**
  350. * Returns the key from the next entry retrieved
  351. * by the underlying <code>entrySet()</code> iterator.
  352. *
  353. * @return The next key.
  354. */
  355. public K next()
  356. {
  357. return map_iterator.next().getKey();
  358. }
  359. /**
  360. * Removes the map entry which has a key equal
  361. * to that returned by the last call to
  362. * <code>next()</code>.
  363. *
  364. * @throws UnsupportedOperationException if the
  365. * map doesn't support removal.
  366. */
  367. public void remove()
  368. {
  369. map_iterator.remove();
  370. }
  371. };
  372. }
  373. };
  374. return keys;
  375. }
  376. /**
  377. * Associates the given key to the given value (optional operation). If the
  378. * map already contains the key, its value is replaced. This implementation
  379. * simply throws an UnsupportedOperationException. Be aware that in a map
  380. * that permits <code>null</code> values, a null return does not always
  381. * imply that the mapping was created.
  382. *
  383. * @param key the key to map
  384. * @param value the value to be mapped
  385. * @return the previous value of the key, or null if there was no mapping
  386. * @throws UnsupportedOperationException if the operation is not supported
  387. * @throws ClassCastException if the key or value is of the wrong type
  388. * @throws IllegalArgumentException if something about this key or value
  389. * prevents it from existing in this map
  390. * @throws NullPointerException if the map forbids null keys or values
  391. * @see #containsKey(Object)
  392. */
  393. public V put(K key, V value)
  394. {
  395. throw new UnsupportedOperationException();
  396. }
  397. /**
  398. * Copies all entries of the given map to this one (optional operation). If
  399. * the map already contains a key, its value is replaced. This implementation
  400. * simply iterates over the map's entrySet(), calling <code>put</code>,
  401. * so it is not supported if puts are not.
  402. *
  403. * @param m the mapping to load into this map
  404. * @throws UnsupportedOperationException if the operation is not supported
  405. * by this map.
  406. * @throws ClassCastException if a key or value is of the wrong type for
  407. * adding to this map.
  408. * @throws IllegalArgumentException if something about a key or value
  409. * prevents it from existing in this map.
  410. * @throws NullPointerException if the map forbids null keys or values.
  411. * @throws NullPointerException if <code>m</code> is null.
  412. * @see #put(Object, Object)
  413. */
  414. public void putAll(Map<? extends K, ? extends V> m)
  415. {
  416. // FIXME: bogus circumlocution.
  417. Iterator entries2 = m.entrySet().iterator();
  418. Iterator<Map.Entry<? extends K, ? extends V>> entries
  419. = (Iterator<Map.Entry<? extends K, ? extends V>>) entries2;
  420. int pos = m.size();
  421. while (--pos >= 0)
  422. {
  423. Map.Entry<? extends K, ? extends V> entry = entries.next();
  424. put(entry.getKey(), entry.getValue());
  425. }
  426. }
  427. /**
  428. * Removes the mapping for this key if present (optional operation). This
  429. * implementation iterates over the entrySet searching for a matching
  430. * key, at which point it calls the iterator's <code>remove</code> method.
  431. * It returns the result of <code>getValue()</code> on the entry, if found,
  432. * or null if no entry is found. Note that maps which permit null values
  433. * may also return null if the key was removed. If the entrySet does not
  434. * support removal, this will also fail. This is O(n), so many
  435. * implementations override it for efficiency.
  436. *
  437. * @param key the key to remove
  438. * @return the value the key mapped to, or null if not present.
  439. * Null may also be returned if null values are allowed
  440. * in the map and the value of this mapping is null.
  441. * @throws UnsupportedOperationException if deletion is unsupported
  442. * @see Iterator#remove()
  443. */
  444. public V remove(Object key)
  445. {
  446. Iterator<Map.Entry<K, V>> entries = entrySet().iterator();
  447. int pos = size();
  448. while (--pos >= 0)
  449. {
  450. Map.Entry<K, V> entry = entries.next();
  451. if (equals(key, entry.getKey()))
  452. {
  453. // Must get the value before we remove it from iterator.
  454. V r = entry.getValue();
  455. entries.remove();
  456. return r;
  457. }
  458. }
  459. return null;
  460. }
  461. /**
  462. * Returns the number of key-value mappings in the map. If there are more
  463. * than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE. This is
  464. * implemented as <code>entrySet().size()</code>.
  465. *
  466. * @return the number of mappings
  467. * @see Set#size()
  468. */
  469. public int size()
  470. {
  471. return entrySet().size();
  472. }
  473. /**
  474. * Returns a String representation of this map. This is a listing of the
  475. * map entries (which are specified in Map.Entry as being
  476. * <code>getKey() + "=" + getValue()</code>), separated by a comma and
  477. * space (", "), and surrounded by braces ('{' and '}'). This implementation
  478. * uses a StringBuffer and iterates over the entrySet to build the String.
  479. * Note that this can fail with an exception if underlying keys or
  480. * values complete abruptly in toString().
  481. *
  482. * @return a String representation
  483. * @see Map.Entry#toString()
  484. */
  485. public String toString()
  486. {
  487. Iterator<Map.Entry<K, V>> entries = entrySet().iterator();
  488. CPStringBuilder r = new CPStringBuilder("{");
  489. for (int pos = size(); pos > 0; pos--)
  490. {
  491. Map.Entry<K, V> entry = entries.next();
  492. r.append(entry.getKey());
  493. r.append('=');
  494. r.append(entry.getValue());
  495. if (pos > 1)
  496. r.append(", ");
  497. }
  498. r.append("}");
  499. return r.toString();
  500. }
  501. /**
  502. * Returns a collection or bag view of this map's values. The collection
  503. * is backed by the map, so changes in one show up in the other.
  504. * Modifications while an iteration is in progress produce undefined
  505. * behavior. The collection supports removal if entrySet() does, but
  506. * does not support element addition.
  507. * <p>
  508. *
  509. * This implementation creates an AbstractCollection, where the iterator
  510. * wraps the entrySet iterator, size defers to the Map's size, and contains
  511. * defers to the Map's containsValue. The collection is created on first
  512. * use, and returned on subsequent uses, although since no synchronization
  513. * occurs, there is a slight possibility of creating two collections.
  514. *
  515. * @return a Collection view of the values
  516. * @see Collection#iterator()
  517. * @see #size()
  518. * @see #containsValue(Object)
  519. * @see #keySet()
  520. */
  521. public Collection<V> values()
  522. {
  523. if (values == null)
  524. values = new AbstractCollection<V>()
  525. {
  526. /**
  527. * Returns the number of values stored in
  528. * the backing map.
  529. *
  530. * @return The number of values.
  531. */
  532. public int size()
  533. {
  534. return AbstractMap.this.size();
  535. }
  536. /**
  537. * Returns true if the backing map contains
  538. * the supplied value.
  539. *
  540. * @param value The value to search for.
  541. * @return True if the value was found, false otherwise.
  542. */
  543. public boolean contains(Object value)
  544. {
  545. return containsValue(value);
  546. }
  547. /**
  548. * Returns an iterator which iterates over the
  549. * values in the backing map, by using a wrapper
  550. * around the iterator returned by <code>entrySet()</code>.
  551. *
  552. * @return An iterator over the values.
  553. */
  554. public Iterator<V> iterator()
  555. {
  556. return new Iterator<V>()
  557. {
  558. /**
  559. * The iterator returned by <code>entrySet()</code>.
  560. */
  561. private final Iterator<Map.Entry<K, V>> map_iterator
  562. = entrySet().iterator();
  563. /**
  564. * Returns true if a call to <code>next()</call> will
  565. * return another value.
  566. *
  567. * @return True if the iterator has not yet reached
  568. * the last value.
  569. */
  570. public boolean hasNext()
  571. {
  572. return map_iterator.hasNext();
  573. }
  574. /**
  575. * Returns the value from the next entry retrieved
  576. * by the underlying <code>entrySet()</code> iterator.
  577. *
  578. * @return The next value.
  579. */
  580. public V next()
  581. {
  582. return map_iterator.next().getValue();
  583. }
  584. /**
  585. * Removes the map entry which has a key equal
  586. * to that returned by the last call to
  587. * <code>next()</code>.
  588. *
  589. * @throws UnsupportedOperationException if the
  590. * map doesn't support removal.
  591. */
  592. public void remove()
  593. {
  594. map_iterator.remove();
  595. }
  596. };
  597. }
  598. };
  599. return values;
  600. }
  601. /**
  602. * Compare two objects according to Collection semantics.
  603. *
  604. * @param o1 the first object
  605. * @param o2 the second object
  606. * @return o1 == o2 || (o1 != null && o1.equals(o2))
  607. */
  608. // Package visible for use throughout java.util.
  609. // It may be inlined since it is final.
  610. static final boolean equals(Object o1, Object o2)
  611. {
  612. return o1 == o2 || (o1 != null && o1.equals(o2));
  613. }
  614. /**
  615. * Hash an object according to Collection semantics.
  616. *
  617. * @param o the object to hash
  618. * @return o1 == null ? 0 : o1.hashCode()
  619. */
  620. // Package visible for use throughout java.util.
  621. // It may be inlined since it is final.
  622. static final int hashCode(Object o)
  623. {
  624. return o == null ? 0 : o.hashCode();
  625. }
  626. /**
  627. * A class which implements Map.Entry. It is shared by HashMap, TreeMap,
  628. * Hashtable, and Collections. It is not specified by the JDK, but makes
  629. * life much easier.
  630. *
  631. * @author Jon Zeppieri
  632. * @author Eric Blake (ebb9@email.byu.edu)
  633. *
  634. * @since 1.6
  635. */
  636. public static class SimpleEntry<K, V> implements Entry<K, V>, Serializable
  637. {
  638. /**
  639. * Compatible with JDK 1.6
  640. */
  641. private static final long serialVersionUID = -8499721149061103585L;
  642. /**
  643. * The key. Package visible for direct manipulation.
  644. */
  645. K key;
  646. /**
  647. * The value. Package visible for direct manipulation.
  648. */
  649. V value;
  650. /**
  651. * Basic constructor initializes the fields.
  652. * @param newKey the key
  653. * @param newValue the value
  654. */
  655. public SimpleEntry(K newKey, V newValue)
  656. {
  657. key = newKey;
  658. value = newValue;
  659. }
  660. public SimpleEntry(Entry<? extends K, ? extends V> entry)
  661. {
  662. this(entry.getKey(), entry.getValue());
  663. }
  664. /**
  665. * Compares the specified object with this entry. Returns true only if
  666. * the object is a mapping of identical key and value. In other words,
  667. * this must be:<br>
  668. * <pre>(o instanceof Map.Entry)
  669. * && (getKey() == null ? ((HashMap) o).getKey() == null
  670. * : getKey().equals(((HashMap) o).getKey()))
  671. * && (getValue() == null ? ((HashMap) o).getValue() == null
  672. * : getValue().equals(((HashMap) o).getValue()))</pre>
  673. *
  674. * @param o the object to compare
  675. * @return <code>true</code> if it is equal
  676. */
  677. public boolean equals(Object o)
  678. {
  679. if (! (o instanceof Map.Entry))
  680. return false;
  681. // Optimize for our own entries.
  682. if (o instanceof SimpleEntry)
  683. {
  684. SimpleEntry e = (SimpleEntry) o;
  685. return (AbstractMap.equals(key, e.key)
  686. && AbstractMap.equals(value, e.value));
  687. }
  688. Map.Entry e = (Map.Entry) o;
  689. return (AbstractMap.equals(key, e.getKey())
  690. && AbstractMap.equals(value, e.getValue()));
  691. }
  692. /**
  693. * Get the key corresponding to this entry.
  694. *
  695. * @return the key
  696. */
  697. public K getKey()
  698. {
  699. return key;
  700. }
  701. /**
  702. * Get the value corresponding to this entry. If you already called
  703. * Iterator.remove(), the behavior undefined, but in this case it works.
  704. *
  705. * @return the value
  706. */
  707. public V getValue()
  708. {
  709. return value;
  710. }
  711. /**
  712. * Returns the hash code of the entry. This is defined as the exclusive-or
  713. * of the hashcodes of the key and value (using 0 for null). In other
  714. * words, this must be:<br>
  715. * <pre>(getKey() == null ? 0 : getKey().hashCode())
  716. * ^ (getValue() == null ? 0 : getValue().hashCode())</pre>
  717. *
  718. * @return the hash code
  719. */
  720. public int hashCode()
  721. {
  722. return (AbstractMap.hashCode(key) ^ AbstractMap.hashCode(value));
  723. }
  724. /**
  725. * Replaces the value with the specified object. This writes through
  726. * to the map, unless you have already called Iterator.remove(). It
  727. * may be overridden to restrict a null value.
  728. *
  729. * @param newVal the new value to store
  730. * @return the old value
  731. * @throws NullPointerException if the map forbids null values.
  732. * @throws UnsupportedOperationException if the map doesn't support
  733. * <code>put()</code>.
  734. * @throws ClassCastException if the value is of a type unsupported
  735. * by the map.
  736. * @throws IllegalArgumentException if something else about this
  737. * value prevents it being stored in the map.
  738. */
  739. public V setValue(V newVal)
  740. {
  741. V r = value;
  742. value = newVal;
  743. return r;
  744. }
  745. /**
  746. * This provides a string representation of the entry. It is of the form
  747. * "key=value", where string concatenation is used on key and value.
  748. *
  749. * @return the string representation
  750. */
  751. public String toString()
  752. {
  753. return key + "=" + value;
  754. }
  755. } // class SimpleEntry
  756. }