--- jsr166/src/main/java/util/AbstractMap.java 2007/09/11 15:13:59 1.25 +++ jsr166/src/main/java/util/AbstractMap.java 2008/05/18 23:47:55 1.26 @@ -82,7 +82,7 @@ public abstract class AbstractMap i *

This implementation returns entrySet().size(). */ public int size() { - return entrySet().size(); + return entrySet().size(); } /** @@ -91,7 +91,7 @@ public abstract class AbstractMap i *

This implementation returns size() == 0. */ public boolean isEmpty() { - return size() == 0; + return size() == 0; } /** @@ -107,21 +107,21 @@ public abstract class AbstractMap i * @throws NullPointerException {@inheritDoc} */ public boolean containsValue(Object value) { - Iterator> i = entrySet().iterator(); - if (value==null) { - while (i.hasNext()) { - Entry e = i.next(); - if (e.getValue()==null) - return true; - } - } else { - while (i.hasNext()) { - Entry e = i.next(); - if (value.equals(e.getValue())) - return true; - } - } - return false; + Iterator> i = entrySet().iterator(); + if (value==null) { + while (i.hasNext()) { + Entry e = i.next(); + if (e.getValue()==null) + return true; + } + } else { + while (i.hasNext()) { + Entry e = i.next(); + if (value.equals(e.getValue())) + return true; + } + } + return false; } /** @@ -138,21 +138,21 @@ public abstract class AbstractMap i * @throws NullPointerException {@inheritDoc} */ public boolean containsKey(Object key) { - Iterator> i = entrySet().iterator(); - if (key==null) { - while (i.hasNext()) { - Entry e = i.next(); - if (e.getKey()==null) - return true; - } - } else { - while (i.hasNext()) { - Entry e = i.next(); - if (key.equals(e.getKey())) - return true; - } - } - return false; + Iterator> i = entrySet().iterator(); + if (key==null) { + while (i.hasNext()) { + Entry e = i.next(); + if (e.getKey()==null) + return true; + } + } else { + while (i.hasNext()) { + Entry e = i.next(); + if (key.equals(e.getKey())) + return true; + } + } + return false; } /** @@ -169,21 +169,21 @@ public abstract class AbstractMap i * @throws NullPointerException {@inheritDoc} */ public V get(Object key) { - Iterator> i = entrySet().iterator(); - if (key==null) { - while (i.hasNext()) { - Entry e = i.next(); - if (e.getKey()==null) - return e.getValue(); - } - } else { - while (i.hasNext()) { - Entry e = i.next(); - if (key.equals(e.getKey())) - return e.getValue(); - } - } - return null; + Iterator> i = entrySet().iterator(); + if (key==null) { + while (i.hasNext()) { + Entry e = i.next(); + if (e.getKey()==null) + return e.getValue(); + } + } else { + while (i.hasNext()) { + Entry e = i.next(); + if (key.equals(e.getKey())) + return e.getValue(); + } + } + return null; } @@ -201,7 +201,7 @@ public abstract class AbstractMap i * @throws IllegalArgumentException {@inheritDoc} */ public V put(K key, V value) { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException(); } /** @@ -226,28 +226,28 @@ public abstract class AbstractMap i * @throws NullPointerException {@inheritDoc} */ public V remove(Object key) { - Iterator> i = entrySet().iterator(); - Entry correctEntry = null; - if (key==null) { - while (correctEntry==null && i.hasNext()) { - Entry e = i.next(); - if (e.getKey()==null) - correctEntry = e; - } - } else { - while (correctEntry==null && i.hasNext()) { - Entry e = i.next(); - if (key.equals(e.getKey())) - correctEntry = e; - } - } - - V oldValue = null; - if (correctEntry !=null) { - oldValue = correctEntry.getValue(); - i.remove(); - } - return oldValue; + Iterator> i = entrySet().iterator(); + Entry correctEntry = null; + if (key==null) { + while (correctEntry==null && i.hasNext()) { + Entry e = i.next(); + if (e.getKey()==null) + correctEntry = e; + } + } else { + while (correctEntry==null && i.hasNext()) { + Entry e = i.next(); + if (key.equals(e.getKey())) + correctEntry = e; + } + } + + V oldValue = null; + if (correctEntry !=null) { + oldValue = correctEntry.getValue(); + i.remove(); + } + return oldValue; } @@ -286,7 +286,7 @@ public abstract class AbstractMap i * @throws UnsupportedOperationException {@inheritDoc} */ public void clear() { - entrySet().clear(); + entrySet().clear(); } @@ -316,44 +316,44 @@ public abstract class AbstractMap i * method will not all return the same set. */ public Set keySet() { - if (keySet == null) { - keySet = new AbstractSet() { - public Iterator iterator() { - return new Iterator() { - private Iterator> i = entrySet().iterator(); - - public boolean hasNext() { - return i.hasNext(); - } - - public K next() { - return i.next().getKey(); - } - - public void remove() { - i.remove(); - } + if (keySet == null) { + keySet = new AbstractSet() { + public Iterator iterator() { + return new Iterator() { + private Iterator> i = entrySet().iterator(); + + public boolean hasNext() { + return i.hasNext(); + } + + public K next() { + return i.next().getKey(); + } + + public void remove() { + i.remove(); + } }; - } + } - public int size() { - return AbstractMap.this.size(); - } - - public boolean isEmpty() { - return AbstractMap.this.isEmpty(); - } - - public void clear() { - AbstractMap.this.clear(); - } - - public boolean contains(Object k) { - return AbstractMap.this.containsKey(k); - } - }; - } - return keySet; + public int size() { + return AbstractMap.this.size(); + } + + public boolean isEmpty() { + return AbstractMap.this.isEmpty(); + } + + public void clear() { + AbstractMap.this.clear(); + } + + public boolean contains(Object k) { + return AbstractMap.this.containsKey(k); + } + }; + } + return keySet; } /** @@ -372,44 +372,44 @@ public abstract class AbstractMap i * method will not all return the same collection. */ public Collection values() { - if (values == null) { - values = new AbstractCollection() { - public Iterator iterator() { - return new Iterator() { - private Iterator> i = entrySet().iterator(); - - public boolean hasNext() { - return i.hasNext(); - } - - public V next() { - return i.next().getValue(); - } - - public void remove() { - i.remove(); - } + if (values == null) { + values = new AbstractCollection() { + public Iterator iterator() { + return new Iterator() { + private Iterator> i = entrySet().iterator(); + + public boolean hasNext() { + return i.hasNext(); + } + + public V next() { + return i.next().getValue(); + } + + public void remove() { + i.remove(); + } }; } - public int size() { - return AbstractMap.this.size(); - } - - public boolean isEmpty() { - return AbstractMap.this.isEmpty(); - } - - public void clear() { - AbstractMap.this.clear(); - } - - public boolean contains(Object v) { - return AbstractMap.this.containsValue(v); - } - }; - } - return values; + public int size() { + return AbstractMap.this.size(); + } + + public boolean isEmpty() { + return AbstractMap.this.isEmpty(); + } + + public void clear() { + AbstractMap.this.clear(); + } + + public boolean contains(Object v) { + return AbstractMap.this.containsValue(v); + } + }; + } + return values; } public abstract Set> entrySet(); @@ -439,20 +439,20 @@ public abstract class AbstractMap i * @return true if the specified object is equal to this map */ public boolean equals(Object o) { - if (o == this) - return true; + if (o == this) + return true; - if (!(o instanceof Map)) - return false; - Map m = (Map) o; - if (m.size() != size()) - return false; + if (!(o instanceof Map)) + return false; + Map m = (Map) o; + if (m.size() != size()) + return false; try { Iterator> i = entrySet().iterator(); while (i.hasNext()) { Entry e = i.next(); - K key = e.getKey(); + K key = e.getKey(); V value = e.getValue(); if (value == null) { if (!(m.get(key)==null && m.containsKey(key))) @@ -468,7 +468,7 @@ public abstract class AbstractMap i return false; } - return true; + return true; } /** @@ -489,11 +489,11 @@ public abstract class AbstractMap i * @see Set#equals(Object) */ public int hashCode() { - int h = 0; - Iterator> i = entrySet().iterator(); - while (i.hasNext()) - h += i.next().hashCode(); - return h; + int h = 0; + Iterator> i = entrySet().iterator(); + while (i.hasNext()) + h += i.next().hashCode(); + return h; } /** @@ -509,23 +509,23 @@ public abstract class AbstractMap i * @return a string representation of this map */ public String toString() { - Iterator> i = entrySet().iterator(); - if (! i.hasNext()) - return "{}"; - - StringBuilder sb = new StringBuilder(); - sb.append('{'); - for (;;) { - Entry e = i.next(); - K key = e.getKey(); - V value = e.getValue(); - sb.append(key == this ? "(this Map)" : key); - sb.append('='); - sb.append(value == this ? "(this Map)" : value); - if (! i.hasNext()) - return sb.append('}').toString(); - sb.append(", "); - } + Iterator> i = entrySet().iterator(); + if (! i.hasNext()) + return "{}"; + + StringBuilder sb = new StringBuilder(); + sb.append('{'); + for (;;) { + Entry e = i.next(); + K key = e.getKey(); + V value = e.getValue(); + sb.append(key == this ? "(this Map)" : key); + sb.append('='); + sb.append(value == this ? "(this Map)" : value); + if (! i.hasNext()) + return sb.append('}').toString(); + sb.append(", "); + } } /** @@ -568,12 +568,12 @@ public abstract class AbstractMap i * @since 1.6 */ public static class SimpleEntry - implements Entry, java.io.Serializable + implements Entry, java.io.Serializable { - private static final long serialVersionUID = -8499721149061103585L; + private static final long serialVersionUID = -8499721149061103585L; - private final K key; - private V value; + private final K key; + private V value; /** * Creates an entry representing a mapping from the specified @@ -582,10 +582,10 @@ public abstract class AbstractMap i * @param key the key represented by this entry * @param value the value represented by this entry */ - public SimpleEntry(K key, V value) { - this.key = key; + public SimpleEntry(K key, V value) { + this.key = key; this.value = value; - } + } /** * Creates an entry representing the same mapping as the @@ -593,87 +593,87 @@ public abstract class AbstractMap i * * @param entry the entry to copy */ - public SimpleEntry(Entry entry) { - this.key = entry.getKey(); + public SimpleEntry(Entry entry) { + this.key = entry.getKey(); this.value = entry.getValue(); - } + } - /** - * Returns the key corresponding to this entry. - * - * @return the key corresponding to this entry - */ - public K getKey() { - return key; - } - - /** - * Returns the value corresponding to this entry. - * - * @return the value corresponding to this entry - */ - public V getValue() { - return value; - } - - /** - * Replaces the value corresponding to this entry with the specified - * value. - * - * @param value new value to be stored in this entry - * @return the old value corresponding to the entry - */ - public V setValue(V value) { - V oldValue = this.value; - this.value = value; - return oldValue; - } - - /** - * Compares the specified object with this entry for equality. - * Returns {@code true} if the given object is also a map entry and - * the two entries represent the same mapping. More formally, two - * entries {@code e1} and {@code e2} represent the same mapping - * if

-	 *   (e1.getKey()==null ?
-	 *    e2.getKey()==null :
-	 *    e1.getKey().equals(e2.getKey()))
-	 *   &&
-	 *   (e1.getValue()==null ?
-	 *    e2.getValue()==null :
-	 *    e1.getValue().equals(e2.getValue()))
- * This ensures that the {@code equals} method works properly across - * different implementations of the {@code Map.Entry} interface. - * - * @param o object to be compared for equality with this map entry - * @return {@code true} if the specified object is equal to this map - * entry - * @see #hashCode - */ - public boolean equals(Object o) { - if (!(o instanceof Map.Entry)) - return false; - Map.Entry e = (Map.Entry)o; - return eq(key, e.getKey()) && eq(value, e.getValue()); - } - - /** - * Returns the hash code value for this map entry. The hash code - * of a map entry {@code e} is defined to be:
-	 *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
-	 *   (e.getValue()==null ? 0 : e.getValue().hashCode())
- * This ensures that {@code e1.equals(e2)} implies that - * {@code e1.hashCode()==e2.hashCode()} for any two Entries - * {@code e1} and {@code e2}, as required by the general - * contract of {@link Object#hashCode}. - * - * @return the hash code value for this map entry - * @see #equals - */ - public int hashCode() { - return (key == null ? 0 : key.hashCode()) ^ - (value == null ? 0 : value.hashCode()); - } + /** + * Returns the key corresponding to this entry. + * + * @return the key corresponding to this entry + */ + public K getKey() { + return key; + } + + /** + * Returns the value corresponding to this entry. + * + * @return the value corresponding to this entry + */ + public V getValue() { + return value; + } + + /** + * Replaces the value corresponding to this entry with the specified + * value. + * + * @param value new value to be stored in this entry + * @return the old value corresponding to the entry + */ + public V setValue(V value) { + V oldValue = this.value; + this.value = value; + return oldValue; + } + + /** + * Compares the specified object with this entry for equality. + * Returns {@code true} if the given object is also a map entry and + * the two entries represent the same mapping. More formally, two + * entries {@code e1} and {@code e2} represent the same mapping + * if
+         *   (e1.getKey()==null ?
+         *    e2.getKey()==null :
+         *    e1.getKey().equals(e2.getKey()))
+         *   &&
+         *   (e1.getValue()==null ?
+         *    e2.getValue()==null :
+         *    e1.getValue().equals(e2.getValue()))
+ * This ensures that the {@code equals} method works properly across + * different implementations of the {@code Map.Entry} interface. + * + * @param o object to be compared for equality with this map entry + * @return {@code true} if the specified object is equal to this map + * entry + * @see #hashCode + */ + public boolean equals(Object o) { + if (!(o instanceof Map.Entry)) + return false; + Map.Entry e = (Map.Entry)o; + return eq(key, e.getKey()) && eq(value, e.getValue()); + } + + /** + * Returns the hash code value for this map entry. The hash code + * of a map entry {@code e} is defined to be:
+         *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
+         *   (e.getValue()==null ? 0 : e.getValue().hashCode())
+ * This ensures that {@code e1.equals(e2)} implies that + * {@code e1.hashCode()==e2.hashCode()} for any two Entries + * {@code e1} and {@code e2}, as required by the general + * contract of {@link Object#hashCode}. + * + * @return the hash code value for this map entry + * @see #equals + */ + public int hashCode() { + return (key == null ? 0 : key.hashCode()) ^ + (value == null ? 0 : value.hashCode()); + } /** * Returns a String representation of this map entry. This @@ -683,9 +683,9 @@ public abstract class AbstractMap i * * @return a String representation of this map entry */ - public String toString() { - return key + "=" + value; - } + public String toString() { + return key + "=" + value; + } } @@ -698,12 +698,12 @@ public abstract class AbstractMap i * @since 1.6 */ public static class SimpleImmutableEntry - implements Entry, java.io.Serializable + implements Entry, java.io.Serializable { - private static final long serialVersionUID = 7138329143949025153L; + private static final long serialVersionUID = 7138329143949025153L; - private final K key; - private final V value; + private final K key; + private final V value; /** * Creates an entry representing a mapping from the specified @@ -712,10 +712,10 @@ public abstract class AbstractMap i * @param key the key represented by this entry * @param value the value represented by this entry */ - public SimpleImmutableEntry(K key, V value) { - this.key = key; + public SimpleImmutableEntry(K key, V value) { + this.key = key; this.value = value; - } + } /** * Creates an entry representing the same mapping as the @@ -723,88 +723,88 @@ public abstract class AbstractMap i * * @param entry the entry to copy */ - public SimpleImmutableEntry(Entry entry) { - this.key = entry.getKey(); + public SimpleImmutableEntry(Entry entry) { + this.key = entry.getKey(); this.value = entry.getValue(); - } + } + + /** + * Returns the key corresponding to this entry. + * + * @return the key corresponding to this entry + */ + public K getKey() { + return key; + } + + /** + * Returns the value corresponding to this entry. + * + * @return the value corresponding to this entry + */ + public V getValue() { + return value; + } - /** - * Returns the key corresponding to this entry. - * - * @return the key corresponding to this entry - */ - public K getKey() { - return key; - } - - /** - * Returns the value corresponding to this entry. - * - * @return the value corresponding to this entry - */ - public V getValue() { - return value; - } - - /** - * Replaces the value corresponding to this entry with the specified - * value (optional operation). This implementation simply throws + /** + * Replaces the value corresponding to this entry with the specified + * value (optional operation). This implementation simply throws * UnsupportedOperationException, as this class implements * an immutable map entry. - * - * @param value new value to be stored in this entry - * @return (Does not return) - * @throws UnsupportedOperationException always + * + * @param value new value to be stored in this entry + * @return (Does not return) + * @throws UnsupportedOperationException always */ - public V setValue(V value) { + public V setValue(V value) { throw new UnsupportedOperationException(); } - /** - * Compares the specified object with this entry for equality. - * Returns {@code true} if the given object is also a map entry and - * the two entries represent the same mapping. More formally, two - * entries {@code e1} and {@code e2} represent the same mapping - * if
-	 *   (e1.getKey()==null ?
-	 *    e2.getKey()==null :
-	 *    e1.getKey().equals(e2.getKey()))
-	 *   &&
-	 *   (e1.getValue()==null ?
-	 *    e2.getValue()==null :
-	 *    e1.getValue().equals(e2.getValue()))
- * This ensures that the {@code equals} method works properly across - * different implementations of the {@code Map.Entry} interface. - * - * @param o object to be compared for equality with this map entry - * @return {@code true} if the specified object is equal to this map - * entry - * @see #hashCode - */ - public boolean equals(Object o) { - if (!(o instanceof Map.Entry)) - return false; - Map.Entry e = (Map.Entry)o; - return eq(key, e.getKey()) && eq(value, e.getValue()); - } - - /** - * Returns the hash code value for this map entry. The hash code - * of a map entry {@code e} is defined to be:
-	 *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
-	 *   (e.getValue()==null ? 0 : e.getValue().hashCode())
- * This ensures that {@code e1.equals(e2)} implies that - * {@code e1.hashCode()==e2.hashCode()} for any two Entries - * {@code e1} and {@code e2}, as required by the general - * contract of {@link Object#hashCode}. - * - * @return the hash code value for this map entry - * @see #equals - */ - public int hashCode() { - return (key == null ? 0 : key.hashCode()) ^ - (value == null ? 0 : value.hashCode()); - } + /** + * Compares the specified object with this entry for equality. + * Returns {@code true} if the given object is also a map entry and + * the two entries represent the same mapping. More formally, two + * entries {@code e1} and {@code e2} represent the same mapping + * if
+         *   (e1.getKey()==null ?
+         *    e2.getKey()==null :
+         *    e1.getKey().equals(e2.getKey()))
+         *   &&
+         *   (e1.getValue()==null ?
+         *    e2.getValue()==null :
+         *    e1.getValue().equals(e2.getValue()))
+ * This ensures that the {@code equals} method works properly across + * different implementations of the {@code Map.Entry} interface. + * + * @param o object to be compared for equality with this map entry + * @return {@code true} if the specified object is equal to this map + * entry + * @see #hashCode + */ + public boolean equals(Object o) { + if (!(o instanceof Map.Entry)) + return false; + Map.Entry e = (Map.Entry)o; + return eq(key, e.getKey()) && eq(value, e.getValue()); + } + + /** + * Returns the hash code value for this map entry. The hash code + * of a map entry {@code e} is defined to be:
+         *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
+         *   (e.getValue()==null ? 0 : e.getValue().hashCode())
+ * This ensures that {@code e1.equals(e2)} implies that + * {@code e1.hashCode()==e2.hashCode()} for any two Entries + * {@code e1} and {@code e2}, as required by the general + * contract of {@link Object#hashCode}. + * + * @return the hash code value for this map entry + * @see #equals + */ + public int hashCode() { + return (key == null ? 0 : key.hashCode()) ^ + (value == null ? 0 : value.hashCode()); + } /** * Returns a String representation of this map entry. This @@ -814,9 +814,9 @@ public abstract class AbstractMap i * * @return a String representation of this map entry */ - public String toString() { - return key + "=" + value; - } + public String toString() { + return key + "=" + value; + } }