--- jsr166/src/main/java/util/AbstractMap.java 2005/05/17 04:03:50 1.11 +++ jsr166/src/main/java/util/AbstractMap.java 2005/08/24 04:47:24 1.16 @@ -6,6 +6,7 @@ */ package java.util; +import java.util.*; // for javadoc (till 6280605 is fixed) import java.util.Map.Entry; /** @@ -387,7 +388,13 @@ public abstract class AbstractMap i // Comparison and hashing /** - * {@inheritDoc} + * Compares the specified object with this map for equality. Returns + * true if the given object is also a map and the two maps + * represent the same mappings. More formally, two maps m1 and + * m2 represent the same mappings if + * m1.entrySet().equals(m2.entrySet()). This ensures that the + * equals method works properly across different implementations + * of the Map interface. * *

This implementation first checks if the specified object is this map; * if so it returns true. Then, it checks if the specified @@ -397,6 +404,9 @@ public abstract class AbstractMap i * contains each mapping that this map contains. If the specified map * fails to contain such a mapping, false is returned. If the * iteration completes, true is returned. + * + * @param o object to be compared for equality with this map + * @return true if the specified object is equal to this map */ public boolean equals(Object o) { if (o == this) @@ -404,8 +414,8 @@ public abstract class AbstractMap i if (!(o instanceof Map)) return false; - Map t = (Map) o; - if (t.size() != size()) + Map m = (Map) o; + if (m.size() != size()) return false; try { @@ -415,10 +425,10 @@ public abstract class AbstractMap i K key = e.getKey(); V value = e.getValue(); if (value == null) { - if (!(t.get(key)==null && t.containsKey(key))) + if (!(m.get(key)==null && m.containsKey(key))) return false; } else { - if (!value.equals(t.get(key))) + if (!value.equals(m.get(key))) return false; } } @@ -432,14 +442,19 @@ public abstract class AbstractMap i } /** - * {@inheritDoc} + * Returns the hash code value for this map. The hash code of a map is + * defined to be the sum of the hash codes of each entry in the map's + * entrySet() view. This ensures that m1.equals(m2) + * implies that m1.hashCode()==m2.hashCode() for any two maps + * m1 and m2, as required by the general contract of + * {@link Object#hashCode}. * *

This implementation iterates over entrySet(), calling - * hashCode() on each element (entry) in the set, and - * adding up the results. + * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the + * set, and adding up the results. * + * @return the hash code value for this map * @see Map.Entry#hashCode() - * @see Object#hashCode() * @see Object#equals(Object) * @see Set#equals(Object) */ @@ -471,8 +486,8 @@ public abstract class AbstractMap i * @return a String representation of this map */ public String toString() { - StringBuffer buf = new StringBuffer(); - buf.append("{"); + StringBuilder sb = new StringBuilder(); + sb.append("{"); Iterator> i = entrySet().iterator(); boolean hasNext = i.hasNext(); @@ -481,21 +496,21 @@ public abstract class AbstractMap i K key = e.getKey(); V value = e.getValue(); if (key == this) - buf.append("(this Map)"); + sb.append("(this Map)"); else - buf.append(key); - buf.append("="); + sb.append(key); + sb.append("="); if (value == this) - buf.append("(this Map)"); + sb.append("(this Map)"); else - buf.append(value); + sb.append(value); hasNext = i.hasNext(); if (hasNext) - buf.append(", "); + sb.append(", "); } - buf.append("}"); - return buf.toString(); + sb.append("}"); + return sb.toString(); } /** @@ -516,7 +531,7 @@ public abstract class AbstractMap i * Test for equality, checking for nulls. */ private static boolean eq(Object o1, Object o2) { - return (o1 == null ? o2 == null : o1.equals(o2)); + return o1 == null ? o2 == null : o1.equals(o2); } // Implementation Note: SimpleEntry and SimpleImmutableEntry @@ -533,9 +548,15 @@ public abstract class AbstractMap i * facilitates the process of building custom map * implementations. For example, it may be convenient to return * arrays of SimpleEntry instances in method - * Map.entrySet().toArray + * Map.entrySet().toArray. + * + * @since 1.6 */ - public static class SimpleEntry implements Entry { + public static class SimpleEntry + implements Entry, java.io.Serializable + { + private static final long serialVersionUID = -8499721149061103585L; + private final K key; private V value; @@ -601,8 +622,8 @@ public abstract class AbstractMap i } public int hashCode() { - return ((key == null) ? 0 : key.hashCode()) ^ - ((value == null) ? 0 : value.hashCode()); + return (key == null ? 0 : key.hashCode()) ^ + (value == null ? 0 : value.hashCode()); } /** @@ -620,12 +641,18 @@ public abstract class AbstractMap i } /** - * An Entry maintaining an immutable key and value, This class + * An Entry maintaining an immutable key and value. This class * does not support method setValue. This class may be * convenient in methods that return thread-safe snapshots of * key-value mappings. + * + * @since 1.6 */ - public static class SimpleImmutableEntry implements Entry { + public static class SimpleImmutableEntry + implements Entry, java.io.Serializable + { + private static final long serialVersionUID = 7138329143949025153L; + private final K key; private final V value; @@ -692,8 +719,8 @@ public abstract class AbstractMap i } public int hashCode() { - return ((key == null) ? 0 : key.hashCode()) ^ - ((value == null) ? 0 : value.hashCode()); + return (key == null ? 0 : key.hashCode()) ^ + (value == null ? 0 : value.hashCode()); } /**