1.1 --- a/emul/compact/src/main/java/java/util/AbstractMap.java Tue Feb 26 14:55:55 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,822 +0,0 @@
1.4 -/*
1.5 - * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 - *
1.8 - * This code is free software; you can redistribute it and/or modify it
1.9 - * under the terms of the GNU General Public License version 2 only, as
1.10 - * published by the Free Software Foundation. Oracle designates this
1.11 - * particular file as subject to the "Classpath" exception as provided
1.12 - * by Oracle in the LICENSE file that accompanied this code.
1.13 - *
1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 - * version 2 for more details (a copy is included in the LICENSE file that
1.18 - * accompanied this code).
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License version
1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 - *
1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 - * or visit www.oracle.com if you need additional information or have any
1.26 - * questions.
1.27 - */
1.28 -
1.29 -package java.util;
1.30 -import java.util.Map.Entry;
1.31 -
1.32 -/**
1.33 - * This class provides a skeletal implementation of the <tt>Map</tt>
1.34 - * interface, to minimize the effort required to implement this interface.
1.35 - *
1.36 - * <p>To implement an unmodifiable map, the programmer needs only to extend this
1.37 - * class and provide an implementation for the <tt>entrySet</tt> method, which
1.38 - * returns a set-view of the map's mappings. Typically, the returned set
1.39 - * will, in turn, be implemented atop <tt>AbstractSet</tt>. This set should
1.40 - * not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator
1.41 - * should not support the <tt>remove</tt> method.
1.42 - *
1.43 - * <p>To implement a modifiable map, the programmer must additionally override
1.44 - * this class's <tt>put</tt> method (which otherwise throws an
1.45 - * <tt>UnsupportedOperationException</tt>), and the iterator returned by
1.46 - * <tt>entrySet().iterator()</tt> must additionally implement its
1.47 - * <tt>remove</tt> method.
1.48 - *
1.49 - * <p>The programmer should generally provide a void (no argument) and map
1.50 - * constructor, as per the recommendation in the <tt>Map</tt> interface
1.51 - * specification.
1.52 - *
1.53 - * <p>The documentation for each non-abstract method in this class describes its
1.54 - * implementation in detail. Each of these methods may be overridden if the
1.55 - * map being implemented admits a more efficient implementation.
1.56 - *
1.57 - * <p>This class is a member of the
1.58 - * <a href="{@docRoot}/../technotes/guides/collections/index.html">
1.59 - * Java Collections Framework</a>.
1.60 - *
1.61 - * @param <K> the type of keys maintained by this map
1.62 - * @param <V> the type of mapped values
1.63 - *
1.64 - * @author Josh Bloch
1.65 - * @author Neal Gafter
1.66 - * @see Map
1.67 - * @see Collection
1.68 - * @since 1.2
1.69 - */
1.70 -
1.71 -public abstract class AbstractMap<K,V> implements Map<K,V> {
1.72 - /**
1.73 - * Sole constructor. (For invocation by subclass constructors, typically
1.74 - * implicit.)
1.75 - */
1.76 - protected AbstractMap() {
1.77 - }
1.78 -
1.79 - // Query Operations
1.80 -
1.81 - /**
1.82 - * {@inheritDoc}
1.83 - *
1.84 - * <p>This implementation returns <tt>entrySet().size()</tt>.
1.85 - */
1.86 - public int size() {
1.87 - return entrySet().size();
1.88 - }
1.89 -
1.90 - /**
1.91 - * {@inheritDoc}
1.92 - *
1.93 - * <p>This implementation returns <tt>size() == 0</tt>.
1.94 - */
1.95 - public boolean isEmpty() {
1.96 - return size() == 0;
1.97 - }
1.98 -
1.99 - /**
1.100 - * {@inheritDoc}
1.101 - *
1.102 - * <p>This implementation iterates over <tt>entrySet()</tt> searching
1.103 - * for an entry with the specified value. If such an entry is found,
1.104 - * <tt>true</tt> is returned. If the iteration terminates without
1.105 - * finding such an entry, <tt>false</tt> is returned. Note that this
1.106 - * implementation requires linear time in the size of the map.
1.107 - *
1.108 - * @throws ClassCastException {@inheritDoc}
1.109 - * @throws NullPointerException {@inheritDoc}
1.110 - */
1.111 - public boolean containsValue(Object value) {
1.112 - Iterator<Entry<K,V>> i = entrySet().iterator();
1.113 - if (value==null) {
1.114 - while (i.hasNext()) {
1.115 - Entry<K,V> e = i.next();
1.116 - if (e.getValue()==null)
1.117 - return true;
1.118 - }
1.119 - } else {
1.120 - while (i.hasNext()) {
1.121 - Entry<K,V> e = i.next();
1.122 - if (value.equals(e.getValue()))
1.123 - return true;
1.124 - }
1.125 - }
1.126 - return false;
1.127 - }
1.128 -
1.129 - /**
1.130 - * {@inheritDoc}
1.131 - *
1.132 - * <p>This implementation iterates over <tt>entrySet()</tt> searching
1.133 - * for an entry with the specified key. If such an entry is found,
1.134 - * <tt>true</tt> is returned. If the iteration terminates without
1.135 - * finding such an entry, <tt>false</tt> is returned. Note that this
1.136 - * implementation requires linear time in the size of the map; many
1.137 - * implementations will override this method.
1.138 - *
1.139 - * @throws ClassCastException {@inheritDoc}
1.140 - * @throws NullPointerException {@inheritDoc}
1.141 - */
1.142 - public boolean containsKey(Object key) {
1.143 - Iterator<Map.Entry<K,V>> i = entrySet().iterator();
1.144 - if (key==null) {
1.145 - while (i.hasNext()) {
1.146 - Entry<K,V> e = i.next();
1.147 - if (e.getKey()==null)
1.148 - return true;
1.149 - }
1.150 - } else {
1.151 - while (i.hasNext()) {
1.152 - Entry<K,V> e = i.next();
1.153 - if (key.equals(e.getKey()))
1.154 - return true;
1.155 - }
1.156 - }
1.157 - return false;
1.158 - }
1.159 -
1.160 - /**
1.161 - * {@inheritDoc}
1.162 - *
1.163 - * <p>This implementation iterates over <tt>entrySet()</tt> searching
1.164 - * for an entry with the specified key. If such an entry is found,
1.165 - * the entry's value is returned. If the iteration terminates without
1.166 - * finding such an entry, <tt>null</tt> is returned. Note that this
1.167 - * implementation requires linear time in the size of the map; many
1.168 - * implementations will override this method.
1.169 - *
1.170 - * @throws ClassCastException {@inheritDoc}
1.171 - * @throws NullPointerException {@inheritDoc}
1.172 - */
1.173 - public V get(Object key) {
1.174 - Iterator<Entry<K,V>> i = entrySet().iterator();
1.175 - if (key==null) {
1.176 - while (i.hasNext()) {
1.177 - Entry<K,V> e = i.next();
1.178 - if (e.getKey()==null)
1.179 - return e.getValue();
1.180 - }
1.181 - } else {
1.182 - while (i.hasNext()) {
1.183 - Entry<K,V> e = i.next();
1.184 - if (key.equals(e.getKey()))
1.185 - return e.getValue();
1.186 - }
1.187 - }
1.188 - return null;
1.189 - }
1.190 -
1.191 -
1.192 - // Modification Operations
1.193 -
1.194 - /**
1.195 - * {@inheritDoc}
1.196 - *
1.197 - * <p>This implementation always throws an
1.198 - * <tt>UnsupportedOperationException</tt>.
1.199 - *
1.200 - * @throws UnsupportedOperationException {@inheritDoc}
1.201 - * @throws ClassCastException {@inheritDoc}
1.202 - * @throws NullPointerException {@inheritDoc}
1.203 - * @throws IllegalArgumentException {@inheritDoc}
1.204 - */
1.205 - public V put(K key, V value) {
1.206 - throw new UnsupportedOperationException();
1.207 - }
1.208 -
1.209 - /**
1.210 - * {@inheritDoc}
1.211 - *
1.212 - * <p>This implementation iterates over <tt>entrySet()</tt> searching for an
1.213 - * entry with the specified key. If such an entry is found, its value is
1.214 - * obtained with its <tt>getValue</tt> operation, the entry is removed
1.215 - * from the collection (and the backing map) with the iterator's
1.216 - * <tt>remove</tt> operation, and the saved value is returned. If the
1.217 - * iteration terminates without finding such an entry, <tt>null</tt> is
1.218 - * returned. Note that this implementation requires linear time in the
1.219 - * size of the map; many implementations will override this method.
1.220 - *
1.221 - * <p>Note that this implementation throws an
1.222 - * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
1.223 - * iterator does not support the <tt>remove</tt> method and this map
1.224 - * contains a mapping for the specified key.
1.225 - *
1.226 - * @throws UnsupportedOperationException {@inheritDoc}
1.227 - * @throws ClassCastException {@inheritDoc}
1.228 - * @throws NullPointerException {@inheritDoc}
1.229 - */
1.230 - public V remove(Object key) {
1.231 - Iterator<Entry<K,V>> i = entrySet().iterator();
1.232 - Entry<K,V> correctEntry = null;
1.233 - if (key==null) {
1.234 - while (correctEntry==null && i.hasNext()) {
1.235 - Entry<K,V> e = i.next();
1.236 - if (e.getKey()==null)
1.237 - correctEntry = e;
1.238 - }
1.239 - } else {
1.240 - while (correctEntry==null && i.hasNext()) {
1.241 - Entry<K,V> e = i.next();
1.242 - if (key.equals(e.getKey()))
1.243 - correctEntry = e;
1.244 - }
1.245 - }
1.246 -
1.247 - V oldValue = null;
1.248 - if (correctEntry !=null) {
1.249 - oldValue = correctEntry.getValue();
1.250 - i.remove();
1.251 - }
1.252 - return oldValue;
1.253 - }
1.254 -
1.255 -
1.256 - // Bulk Operations
1.257 -
1.258 - /**
1.259 - * {@inheritDoc}
1.260 - *
1.261 - * <p>This implementation iterates over the specified map's
1.262 - * <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt>
1.263 - * operation once for each entry returned by the iteration.
1.264 - *
1.265 - * <p>Note that this implementation throws an
1.266 - * <tt>UnsupportedOperationException</tt> if this map does not support
1.267 - * the <tt>put</tt> operation and the specified map is nonempty.
1.268 - *
1.269 - * @throws UnsupportedOperationException {@inheritDoc}
1.270 - * @throws ClassCastException {@inheritDoc}
1.271 - * @throws NullPointerException {@inheritDoc}
1.272 - * @throws IllegalArgumentException {@inheritDoc}
1.273 - */
1.274 - public void putAll(Map<? extends K, ? extends V> m) {
1.275 - for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1.276 - put(e.getKey(), e.getValue());
1.277 - }
1.278 -
1.279 - /**
1.280 - * {@inheritDoc}
1.281 - *
1.282 - * <p>This implementation calls <tt>entrySet().clear()</tt>.
1.283 - *
1.284 - * <p>Note that this implementation throws an
1.285 - * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
1.286 - * does not support the <tt>clear</tt> operation.
1.287 - *
1.288 - * @throws UnsupportedOperationException {@inheritDoc}
1.289 - */
1.290 - public void clear() {
1.291 - entrySet().clear();
1.292 - }
1.293 -
1.294 -
1.295 - // Views
1.296 -
1.297 - /**
1.298 - * Each of these fields are initialized to contain an instance of the
1.299 - * appropriate view the first time this view is requested. The views are
1.300 - * stateless, so there's no reason to create more than one of each.
1.301 - */
1.302 - transient volatile Set<K> keySet = null;
1.303 - transient volatile Collection<V> values = null;
1.304 -
1.305 - /**
1.306 - * {@inheritDoc}
1.307 - *
1.308 - * <p>This implementation returns a set that subclasses {@link AbstractSet}.
1.309 - * The subclass's iterator method returns a "wrapper object" over this
1.310 - * map's <tt>entrySet()</tt> iterator. The <tt>size</tt> method
1.311 - * delegates to this map's <tt>size</tt> method and the
1.312 - * <tt>contains</tt> method delegates to this map's
1.313 - * <tt>containsKey</tt> method.
1.314 - *
1.315 - * <p>The set is created the first time this method is called,
1.316 - * and returned in response to all subsequent calls. No synchronization
1.317 - * is performed, so there is a slight chance that multiple calls to this
1.318 - * method will not all return the same set.
1.319 - */
1.320 - public Set<K> keySet() {
1.321 - if (keySet == null) {
1.322 - keySet = new AbstractSet<K>() {
1.323 - public Iterator<K> iterator() {
1.324 - return new Iterator<K>() {
1.325 - private Iterator<Entry<K,V>> i = entrySet().iterator();
1.326 -
1.327 - public boolean hasNext() {
1.328 - return i.hasNext();
1.329 - }
1.330 -
1.331 - public K next() {
1.332 - return i.next().getKey();
1.333 - }
1.334 -
1.335 - public void remove() {
1.336 - i.remove();
1.337 - }
1.338 - };
1.339 - }
1.340 -
1.341 - public int size() {
1.342 - return AbstractMap.this.size();
1.343 - }
1.344 -
1.345 - public boolean isEmpty() {
1.346 - return AbstractMap.this.isEmpty();
1.347 - }
1.348 -
1.349 - public void clear() {
1.350 - AbstractMap.this.clear();
1.351 - }
1.352 -
1.353 - public boolean contains(Object k) {
1.354 - return AbstractMap.this.containsKey(k);
1.355 - }
1.356 - };
1.357 - }
1.358 - return keySet;
1.359 - }
1.360 -
1.361 - /**
1.362 - * {@inheritDoc}
1.363 - *
1.364 - * <p>This implementation returns a collection that subclasses {@link
1.365 - * AbstractCollection}. The subclass's iterator method returns a
1.366 - * "wrapper object" over this map's <tt>entrySet()</tt> iterator.
1.367 - * The <tt>size</tt> method delegates to this map's <tt>size</tt>
1.368 - * method and the <tt>contains</tt> method delegates to this map's
1.369 - * <tt>containsValue</tt> method.
1.370 - *
1.371 - * <p>The collection is created the first time this method is called, and
1.372 - * returned in response to all subsequent calls. No synchronization is
1.373 - * performed, so there is a slight chance that multiple calls to this
1.374 - * method will not all return the same collection.
1.375 - */
1.376 - public Collection<V> values() {
1.377 - if (values == null) {
1.378 - values = new AbstractCollection<V>() {
1.379 - public Iterator<V> iterator() {
1.380 - return new Iterator<V>() {
1.381 - private Iterator<Entry<K,V>> i = entrySet().iterator();
1.382 -
1.383 - public boolean hasNext() {
1.384 - return i.hasNext();
1.385 - }
1.386 -
1.387 - public V next() {
1.388 - return i.next().getValue();
1.389 - }
1.390 -
1.391 - public void remove() {
1.392 - i.remove();
1.393 - }
1.394 - };
1.395 - }
1.396 -
1.397 - public int size() {
1.398 - return AbstractMap.this.size();
1.399 - }
1.400 -
1.401 - public boolean isEmpty() {
1.402 - return AbstractMap.this.isEmpty();
1.403 - }
1.404 -
1.405 - public void clear() {
1.406 - AbstractMap.this.clear();
1.407 - }
1.408 -
1.409 - public boolean contains(Object v) {
1.410 - return AbstractMap.this.containsValue(v);
1.411 - }
1.412 - };
1.413 - }
1.414 - return values;
1.415 - }
1.416 -
1.417 - public abstract Set<Entry<K,V>> entrySet();
1.418 -
1.419 -
1.420 - // Comparison and hashing
1.421 -
1.422 - /**
1.423 - * Compares the specified object with this map for equality. Returns
1.424 - * <tt>true</tt> if the given object is also a map and the two maps
1.425 - * represent the same mappings. More formally, two maps <tt>m1</tt> and
1.426 - * <tt>m2</tt> represent the same mappings if
1.427 - * <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the
1.428 - * <tt>equals</tt> method works properly across different implementations
1.429 - * of the <tt>Map</tt> interface.
1.430 - *
1.431 - * <p>This implementation first checks if the specified object is this map;
1.432 - * if so it returns <tt>true</tt>. Then, it checks if the specified
1.433 - * object is a map whose size is identical to the size of this map; if
1.434 - * not, it returns <tt>false</tt>. If so, it iterates over this map's
1.435 - * <tt>entrySet</tt> collection, and checks that the specified map
1.436 - * contains each mapping that this map contains. If the specified map
1.437 - * fails to contain such a mapping, <tt>false</tt> is returned. If the
1.438 - * iteration completes, <tt>true</tt> is returned.
1.439 - *
1.440 - * @param o object to be compared for equality with this map
1.441 - * @return <tt>true</tt> if the specified object is equal to this map
1.442 - */
1.443 - public boolean equals(Object o) {
1.444 - if (o == this)
1.445 - return true;
1.446 -
1.447 - if (!(o instanceof Map))
1.448 - return false;
1.449 - Map<K,V> m = (Map<K,V>) o;
1.450 - if (m.size() != size())
1.451 - return false;
1.452 -
1.453 - try {
1.454 - Iterator<Entry<K,V>> i = entrySet().iterator();
1.455 - while (i.hasNext()) {
1.456 - Entry<K,V> e = i.next();
1.457 - K key = e.getKey();
1.458 - V value = e.getValue();
1.459 - if (value == null) {
1.460 - if (!(m.get(key)==null && m.containsKey(key)))
1.461 - return false;
1.462 - } else {
1.463 - if (!value.equals(m.get(key)))
1.464 - return false;
1.465 - }
1.466 - }
1.467 - } catch (ClassCastException unused) {
1.468 - return false;
1.469 - } catch (NullPointerException unused) {
1.470 - return false;
1.471 - }
1.472 -
1.473 - return true;
1.474 - }
1.475 -
1.476 - /**
1.477 - * Returns the hash code value for this map. The hash code of a map is
1.478 - * defined to be the sum of the hash codes of each entry in the map's
1.479 - * <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt>
1.480 - * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
1.481 - * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
1.482 - * {@link Object#hashCode}.
1.483 - *
1.484 - * <p>This implementation iterates over <tt>entrySet()</tt>, calling
1.485 - * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the
1.486 - * set, and adding up the results.
1.487 - *
1.488 - * @return the hash code value for this map
1.489 - * @see Map.Entry#hashCode()
1.490 - * @see Object#equals(Object)
1.491 - * @see Set#equals(Object)
1.492 - */
1.493 - public int hashCode() {
1.494 - int h = 0;
1.495 - Iterator<Entry<K,V>> i = entrySet().iterator();
1.496 - while (i.hasNext())
1.497 - h += i.next().hashCode();
1.498 - return h;
1.499 - }
1.500 -
1.501 - /**
1.502 - * Returns a string representation of this map. The string representation
1.503 - * consists of a list of key-value mappings in the order returned by the
1.504 - * map's <tt>entrySet</tt> view's iterator, enclosed in braces
1.505 - * (<tt>"{}"</tt>). Adjacent mappings are separated by the characters
1.506 - * <tt>", "</tt> (comma and space). Each key-value mapping is rendered as
1.507 - * the key followed by an equals sign (<tt>"="</tt>) followed by the
1.508 - * associated value. Keys and values are converted to strings as by
1.509 - * {@link String#valueOf(Object)}.
1.510 - *
1.511 - * @return a string representation of this map
1.512 - */
1.513 - public String toString() {
1.514 - Iterator<Entry<K,V>> i = entrySet().iterator();
1.515 - if (! i.hasNext())
1.516 - return "{}";
1.517 -
1.518 - StringBuilder sb = new StringBuilder();
1.519 - sb.append('{');
1.520 - for (;;) {
1.521 - Entry<K,V> e = i.next();
1.522 - K key = e.getKey();
1.523 - V value = e.getValue();
1.524 - sb.append(key == this ? "(this Map)" : key);
1.525 - sb.append('=');
1.526 - sb.append(value == this ? "(this Map)" : value);
1.527 - if (! i.hasNext())
1.528 - return sb.append('}').toString();
1.529 - sb.append(',').append(' ');
1.530 - }
1.531 - }
1.532 -
1.533 - /**
1.534 - * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys
1.535 - * and values themselves are not cloned.
1.536 - *
1.537 - * @return a shallow copy of this map
1.538 - */
1.539 - protected Object clone() throws CloneNotSupportedException {
1.540 - AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();
1.541 - result.keySet = null;
1.542 - result.values = null;
1.543 - return result;
1.544 - }
1.545 -
1.546 - /**
1.547 - * Utility method for SimpleEntry and SimpleImmutableEntry.
1.548 - * Test for equality, checking for nulls.
1.549 - */
1.550 - private static boolean eq(Object o1, Object o2) {
1.551 - return o1 == null ? o2 == null : o1.equals(o2);
1.552 - }
1.553 -
1.554 - // Implementation Note: SimpleEntry and SimpleImmutableEntry
1.555 - // are distinct unrelated classes, even though they share
1.556 - // some code. Since you can't add or subtract final-ness
1.557 - // of a field in a subclass, they can't share representations,
1.558 - // and the amount of duplicated code is too small to warrant
1.559 - // exposing a common abstract class.
1.560 -
1.561 -
1.562 - /**
1.563 - * An Entry maintaining a key and a value. The value may be
1.564 - * changed using the <tt>setValue</tt> method. This class
1.565 - * facilitates the process of building custom map
1.566 - * implementations. For example, it may be convenient to return
1.567 - * arrays of <tt>SimpleEntry</tt> instances in method
1.568 - * <tt>Map.entrySet().toArray</tt>.
1.569 - *
1.570 - * @since 1.6
1.571 - */
1.572 - public static class SimpleEntry<K,V>
1.573 - implements Entry<K,V>, java.io.Serializable
1.574 - {
1.575 - private static final long serialVersionUID = -8499721149061103585L;
1.576 -
1.577 - private final K key;
1.578 - private V value;
1.579 -
1.580 - /**
1.581 - * Creates an entry representing a mapping from the specified
1.582 - * key to the specified value.
1.583 - *
1.584 - * @param key the key represented by this entry
1.585 - * @param value the value represented by this entry
1.586 - */
1.587 - public SimpleEntry(K key, V value) {
1.588 - this.key = key;
1.589 - this.value = value;
1.590 - }
1.591 -
1.592 - /**
1.593 - * Creates an entry representing the same mapping as the
1.594 - * specified entry.
1.595 - *
1.596 - * @param entry the entry to copy
1.597 - */
1.598 - public SimpleEntry(Entry<? extends K, ? extends V> entry) {
1.599 - this.key = entry.getKey();
1.600 - this.value = entry.getValue();
1.601 - }
1.602 -
1.603 - /**
1.604 - * Returns the key corresponding to this entry.
1.605 - *
1.606 - * @return the key corresponding to this entry
1.607 - */
1.608 - public K getKey() {
1.609 - return key;
1.610 - }
1.611 -
1.612 - /**
1.613 - * Returns the value corresponding to this entry.
1.614 - *
1.615 - * @return the value corresponding to this entry
1.616 - */
1.617 - public V getValue() {
1.618 - return value;
1.619 - }
1.620 -
1.621 - /**
1.622 - * Replaces the value corresponding to this entry with the specified
1.623 - * value.
1.624 - *
1.625 - * @param value new value to be stored in this entry
1.626 - * @return the old value corresponding to the entry
1.627 - */
1.628 - public V setValue(V value) {
1.629 - V oldValue = this.value;
1.630 - this.value = value;
1.631 - return oldValue;
1.632 - }
1.633 -
1.634 - /**
1.635 - * Compares the specified object with this entry for equality.
1.636 - * Returns {@code true} if the given object is also a map entry and
1.637 - * the two entries represent the same mapping. More formally, two
1.638 - * entries {@code e1} and {@code e2} represent the same mapping
1.639 - * if<pre>
1.640 - * (e1.getKey()==null ?
1.641 - * e2.getKey()==null :
1.642 - * e1.getKey().equals(e2.getKey()))
1.643 - * &&
1.644 - * (e1.getValue()==null ?
1.645 - * e2.getValue()==null :
1.646 - * e1.getValue().equals(e2.getValue()))</pre>
1.647 - * This ensures that the {@code equals} method works properly across
1.648 - * different implementations of the {@code Map.Entry} interface.
1.649 - *
1.650 - * @param o object to be compared for equality with this map entry
1.651 - * @return {@code true} if the specified object is equal to this map
1.652 - * entry
1.653 - * @see #hashCode
1.654 - */
1.655 - public boolean equals(Object o) {
1.656 - if (!(o instanceof Map.Entry))
1.657 - return false;
1.658 - Map.Entry e = (Map.Entry)o;
1.659 - return eq(key, e.getKey()) && eq(value, e.getValue());
1.660 - }
1.661 -
1.662 - /**
1.663 - * Returns the hash code value for this map entry. The hash code
1.664 - * of a map entry {@code e} is defined to be: <pre>
1.665 - * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
1.666 - * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
1.667 - * This ensures that {@code e1.equals(e2)} implies that
1.668 - * {@code e1.hashCode()==e2.hashCode()} for any two Entries
1.669 - * {@code e1} and {@code e2}, as required by the general
1.670 - * contract of {@link Object#hashCode}.
1.671 - *
1.672 - * @return the hash code value for this map entry
1.673 - * @see #equals
1.674 - */
1.675 - public int hashCode() {
1.676 - return (key == null ? 0 : key.hashCode()) ^
1.677 - (value == null ? 0 : value.hashCode());
1.678 - }
1.679 -
1.680 - /**
1.681 - * Returns a String representation of this map entry. This
1.682 - * implementation returns the string representation of this
1.683 - * entry's key followed by the equals character ("<tt>=</tt>")
1.684 - * followed by the string representation of this entry's value.
1.685 - *
1.686 - * @return a String representation of this map entry
1.687 - */
1.688 - public String toString() {
1.689 - return key + "=" + value;
1.690 - }
1.691 -
1.692 - }
1.693 -
1.694 - /**
1.695 - * An Entry maintaining an immutable key and value. This class
1.696 - * does not support method <tt>setValue</tt>. This class may be
1.697 - * convenient in methods that return thread-safe snapshots of
1.698 - * key-value mappings.
1.699 - *
1.700 - * @since 1.6
1.701 - */
1.702 - public static class SimpleImmutableEntry<K,V>
1.703 - implements Entry<K,V>, java.io.Serializable
1.704 - {
1.705 - private static final long serialVersionUID = 7138329143949025153L;
1.706 -
1.707 - private final K key;
1.708 - private final V value;
1.709 -
1.710 - /**
1.711 - * Creates an entry representing a mapping from the specified
1.712 - * key to the specified value.
1.713 - *
1.714 - * @param key the key represented by this entry
1.715 - * @param value the value represented by this entry
1.716 - */
1.717 - public SimpleImmutableEntry(K key, V value) {
1.718 - this.key = key;
1.719 - this.value = value;
1.720 - }
1.721 -
1.722 - /**
1.723 - * Creates an entry representing the same mapping as the
1.724 - * specified entry.
1.725 - *
1.726 - * @param entry the entry to copy
1.727 - */
1.728 - public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {
1.729 - this.key = entry.getKey();
1.730 - this.value = entry.getValue();
1.731 - }
1.732 -
1.733 - /**
1.734 - * Returns the key corresponding to this entry.
1.735 - *
1.736 - * @return the key corresponding to this entry
1.737 - */
1.738 - public K getKey() {
1.739 - return key;
1.740 - }
1.741 -
1.742 - /**
1.743 - * Returns the value corresponding to this entry.
1.744 - *
1.745 - * @return the value corresponding to this entry
1.746 - */
1.747 - public V getValue() {
1.748 - return value;
1.749 - }
1.750 -
1.751 - /**
1.752 - * Replaces the value corresponding to this entry with the specified
1.753 - * value (optional operation). This implementation simply throws
1.754 - * <tt>UnsupportedOperationException</tt>, as this class implements
1.755 - * an <i>immutable</i> map entry.
1.756 - *
1.757 - * @param value new value to be stored in this entry
1.758 - * @return (Does not return)
1.759 - * @throws UnsupportedOperationException always
1.760 - */
1.761 - public V setValue(V value) {
1.762 - throw new UnsupportedOperationException();
1.763 - }
1.764 -
1.765 - /**
1.766 - * Compares the specified object with this entry for equality.
1.767 - * Returns {@code true} if the given object is also a map entry and
1.768 - * the two entries represent the same mapping. More formally, two
1.769 - * entries {@code e1} and {@code e2} represent the same mapping
1.770 - * if<pre>
1.771 - * (e1.getKey()==null ?
1.772 - * e2.getKey()==null :
1.773 - * e1.getKey().equals(e2.getKey()))
1.774 - * &&
1.775 - * (e1.getValue()==null ?
1.776 - * e2.getValue()==null :
1.777 - * e1.getValue().equals(e2.getValue()))</pre>
1.778 - * This ensures that the {@code equals} method works properly across
1.779 - * different implementations of the {@code Map.Entry} interface.
1.780 - *
1.781 - * @param o object to be compared for equality with this map entry
1.782 - * @return {@code true} if the specified object is equal to this map
1.783 - * entry
1.784 - * @see #hashCode
1.785 - */
1.786 - public boolean equals(Object o) {
1.787 - if (!(o instanceof Map.Entry))
1.788 - return false;
1.789 - Map.Entry e = (Map.Entry)o;
1.790 - return eq(key, e.getKey()) && eq(value, e.getValue());
1.791 - }
1.792 -
1.793 - /**
1.794 - * Returns the hash code value for this map entry. The hash code
1.795 - * of a map entry {@code e} is defined to be: <pre>
1.796 - * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
1.797 - * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
1.798 - * This ensures that {@code e1.equals(e2)} implies that
1.799 - * {@code e1.hashCode()==e2.hashCode()} for any two Entries
1.800 - * {@code e1} and {@code e2}, as required by the general
1.801 - * contract of {@link Object#hashCode}.
1.802 - *
1.803 - * @return the hash code value for this map entry
1.804 - * @see #equals
1.805 - */
1.806 - public int hashCode() {
1.807 - return (key == null ? 0 : key.hashCode()) ^
1.808 - (value == null ? 0 : value.hashCode());
1.809 - }
1.810 -
1.811 - /**
1.812 - * Returns a String representation of this map entry. This
1.813 - * implementation returns the string representation of this
1.814 - * entry's key followed by the equals character ("<tt>=</tt>")
1.815 - * followed by the string representation of this entry's value.
1.816 - *
1.817 - * @return a String representation of this map entry
1.818 - */
1.819 - public String toString() {
1.820 - return key + "=" + value;
1.821 - }
1.822 -
1.823 - }
1.824 -
1.825 -}