changeset 6386:62f516761797

Remove java.util.Mapping and revert back to Map.Entry. Reviewed-by: briangoetz, psandoz
author henryjen
date Mon, 12 Nov 2012 10:55:20 +0100
parents 94d64473e8e6
children 969458b5ee9d
files src/share/classes/java/util/Comparators.java src/share/classes/java/util/HashMap.java src/share/classes/java/util/Map.java src/share/classes/java/util/Mapping.java src/share/classes/java/util/streams/Sink.java test-ng/tests/org/openjdk/tests/java/util/ComparatorsTest.java
diffstat 6 files changed, 45 insertions(+), 150 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Comparators.java	Sun Nov 11 17:44:44 2012 -0500
+++ b/src/share/classes/java/util/Comparators.java	Mon Nov 12 10:55:20 2012 +0100
@@ -109,29 +109,29 @@
     }
 
     /**
-     * A comparator compares Mapping in natural order on Key.
+     * A comparator compares Map.Entry in natural order on Key.
      *
      * @param <K> Comparable Key type
      * @param <V> Value type
      */
-    public static <K extends Comparable<? super K>, V> Comparator<Mapping<K,V>> naturalOrderKeys() {
-        return (Comparator<Mapping<K, V>> & Serializable)
+    public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> naturalOrderKeys() {
+        return (Comparator<Map.Entry<K, V>> & Serializable)
             (c1, c2) -> c1.getKey().compareTo(c2.getKey());
     }
 
     /**
-     * A comparator compares Mapping in natural order on Value.
+     * A comparator compares Map.Entry in natural order on Value.
      *
      * @param <K> Key type
      * @param <V> Comparable Value type
      */
-    public static <K, V extends Comparable<? super V>> Comparator<Mapping<K,V>> naturalOrderValues() {
-        return (Comparator<Mapping<K, V>> & Serializable)
+    public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> naturalOrderValues() {
+        return (Comparator<Map.Entry<K, V>> & Serializable)
             (c1, c2) -> c1.getValue().compareTo(c2.getValue());
     }
 
     /**
-     * A comparator compares Mapping by Key using the given Comparator.
+     * A comparator compares Map.Entry by Key using the given Comparator.
      *
      * <p>The returned comparator is serializable assuming the specified
      * comparators are also serializable.
@@ -140,22 +140,22 @@
      * @param <V> Value type
      * @param cmp The key comparator
      */
-    public static <K, V> Comparator<Mapping<K, V>> byKey(Comparator<? super K> cmp) {
+    public static <K, V> Comparator<Map.Entry<K, V>> byKey(Comparator<? super K> cmp) {
         Objects.requireNonNull(cmp);
-        return (Comparator<Mapping<K, V>> & Serializable)
+        return (Comparator<Map.Entry<K, V>> & Serializable)
             (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
     }
 
     /**
-     * A comparator compares Mapping by Value using the given Comparator.
+     * A comparator compares Map.Entry by Value using the given Comparator.
      *
      * @param <K> Key type
      * @param <V> Value type
      * @param cmp The value comparator
      */
-    public static <K, V> Comparator<Mapping<K, V>> byValue(Comparator<? super V> cmp) {
+    public static <K, V> Comparator<Map.Entry<K, V>> byValue(Comparator<? super V> cmp) {
         Objects.requireNonNull(cmp);
-        return (Comparator<Mapping<K, V>> & Serializable)
+        return (Comparator<Map.Entry<K, V>> & Serializable)
             (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
     }
 
--- a/src/share/classes/java/util/HashMap.java	Sun Nov 11 17:44:44 2012 -0500
+++ b/src/share/classes/java/util/HashMap.java	Mon Nov 12 10:55:20 2012 +0100
@@ -703,7 +703,7 @@
         return result;
     }
 
-    static class Entry<K,V> extends Mapping.AbstractMapping<K,V> implements Map.Entry<K,V> {
+    static class Entry<K,V> implements Map.Entry<K,V> {
         final K key;
         V value;
         Entry<K,V> next;
@@ -733,6 +733,26 @@
             return oldValue;
         }
 
+        public final boolean equals(Object o) {
+            if (!(o instanceof Map.Entry))
+                return false;
+            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
+            Object k1 = getKey();
+            Object k2 = e.getKey();
+            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
+                Object v1 = getValue();
+                Object v2 = e.getValue();
+                if (v1 == v2 || (v1 != null && v1.equals(v2)))
+                    return true;
+            }
+            return false;
+        }
+
+        public final int hashCode() {
+            return (key==null   ? 0 : key.hashCode()) ^
+                   (value==null ? 0 : value.hashCode());
+        }
+
         public final String toString() {
             return getKey() + "=" + getValue();
         }
@@ -835,7 +855,7 @@
 
     private final class ValueIterator extends HashIterator<V> {
         public V next() {
-            return nextEntry().getValue();
+            return nextEntry().value;
         }
     }
 
--- a/src/share/classes/java/util/Map.java	Sun Nov 11 17:44:44 2012 -0500
+++ b/src/share/classes/java/util/Map.java	Mon Nov 12 10:55:20 2012 +0100
@@ -365,7 +365,7 @@
      * @see Map#entrySet()
      * @since 1.2
      */
-    interface Entry<K,V> extends Mapping<K,V> {
+    interface Entry<K,V> {
         /**
          * Returns the key corresponding to this entry.
          *
@@ -374,7 +374,6 @@
          *         required to, throw this exception if the entry has been
          *         removed from the backing map.
          */
-        @Override
         K getKey();
 
         /**
@@ -387,7 +386,6 @@
          *         required to, throw this exception if the entry has been
          *         removed from the backing map.
          */
-        @Override
         V getValue();
 
         /**
--- a/src/share/classes/java/util/Mapping.java	Sun Nov 11 17:44:44 2012 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,123 +0,0 @@
-/*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package java.util;
-
-/**
- * A tuple of two values named {@code key} and {@code value}.
- *
- */
-public interface Mapping<K, V> {
-
-    /**
-     * Return the key portion of the mapping.
-     *
-     * @return the key portion of the mapping.
-     */
-    K getKey();
-
-    /**
-     * Return the value portion of the mapping.
-     *
-     * @return the value portion of the mapping.
-     */
-    V getValue();
-
-    /**
-     * Provides {@code equals()} and {@code hashCode()} implementations.
-     */
-    public abstract static class AbstractMapping<K, V> implements Mapping<K, V> {
-
-        /**
-         * 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<pre>
-         *     Objects.equals(e1.left(), e2.left()) &amp; &amp;
-         * Objects.equals(e1.right(), e2.right())
-         * </pre> This ensures that the {@code equals} method works properly across
-         * different implementations of the {@code Map.Entry} interface.
-         *
-         * @param other object to be compared for equality with this map entry
-         * @return {@code true} if the specified object is equal to this map entry
-         */
-        @Override
-        public final boolean equals(Object other) {
-            if (other instanceof Mapping) {
-                @SuppressWarnings("raw")
-                Mapping likeMe = (Mapping) other;
-                return Objects.equals(getKey(), likeMe.getKey())
-                        && Objects.equals(getValue(), likeMe.getValue());
-            }
-
-            return false;
-        }
-
-        /**
-         * Returns the hash code value for this map entry. The hash code of a map
-         * entry <tt>e</tt> is defined to be:
-         * <pre>
-         *     Objects.hashCode(left()) ^ Objects.hashCode(right());
-         * </pre> This ensures that <tt>e1.equals(e2)</tt> implies that
-         * <tt>e1.hashCode()==e2.hashCode()</tt> for any two entries {@code e1} and
-         * {@code e2}, as required by the general contract of
-         * {@code Object.hashCode}
-         *
-         * @return the hash code value for this mapping
-         * @see Object#hashCode()
-         * @see Object#equals(Object)
-         * @see #equals(Object)
-         */
-        @Override
-        public final int hashCode() {
-            return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue());
-        }
-    }
-
-    /**
-     * A simple {@code Mapping} instance.
-     * @param <K>
-     * @param <V>
-     */
-    public static class MappingValue<K, V> extends AbstractMapping<K, V> {
-        private final K k;
-        private final V v;
-
-        public MappingValue(K k, V v) {
-            System.out.println("Someone called new MappingValue");
-            this.k = k;
-            this.v = v;
-        }
-
-        @Override
-        public K getKey() {
-            return k;
-        }
-
-        @Override
-        public V getValue() {
-            return v;
-        }
-    }
-}
--- a/src/share/classes/java/util/streams/Sink.java	Sun Nov 11 17:44:44 2012 -0500
+++ b/src/share/classes/java/util/streams/Sink.java	Mon Nov 12 10:55:20 2012 +0100
@@ -24,7 +24,6 @@
  */
 package java.util.streams;
 
-import java.util.Mapping;
 import java.util.Objects;
 import java.util.functions.BiBlock;
 import java.util.functions.Block;
--- a/test-ng/tests/org/openjdk/tests/java/util/ComparatorsTest.java	Sun Nov 11 17:44:44 2012 -0500
+++ b/test-ng/tests/org/openjdk/tests/java/util/ComparatorsTest.java	Mon Nov 12 10:55:20 2012 +0100
@@ -26,7 +26,8 @@
 
 import java.util.Comparator;
 import java.util.Comparators;
-import java.util.Mapping;
+import java.util.AbstractMap;
+import java.util.Map;
 import org.testng.annotations.Test;
 
 import java.util.functions.DoubleMapper;
@@ -128,7 +129,7 @@
         assertComparisons(things, comp, comparisons);
     }
 
-    public void testMappingComparator() {
+    public void testComparing() {
         Thing[] things = new Thing[doubleValues.length];
         for (int i=0; i<doubleValues.length; i++)
             things[i] = new Thing(0, 0L, 0.0, stringValues[i]);
@@ -188,12 +189,12 @@
     }
 
     private<K, V> void assertComparison(K k1, V v1, K k2, V v2,
-                                        Comparator<Mapping<K, V>> ck,
-                                        Comparator<Mapping<K, V>> cv) {
-        final Mapping<K, V> p11 = new Mapping.MappingValue<>(k1, v1);
-        final Mapping<K, V> p12 = new Mapping.MappingValue<>(k1, v2);
-        final Mapping<K, V> p21 = new Mapping.MappingValue<>(k2, v1);
-        final Mapping<K, V> p22 = new Mapping.MappingValue<>(k2, v2);
+                                        Comparator<Map.Entry<K, V>> ck,
+                                        Comparator<Map.Entry<K, V>> cv) {
+        final Map.Entry<K, V> p11 = new AbstractMap.SimpleImmutableEntry<>(k1, v1);
+        final Map.Entry<K, V> p12 = new AbstractMap.SimpleImmutableEntry<>(k1, v2);
+        final Map.Entry<K, V> p21 = new AbstractMap.SimpleImmutableEntry<>(k2, v1);
+        final Map.Entry<K, V> p22 = new AbstractMap.SimpleImmutableEntry<>(k2, v2);
 
         assertTrue(ck.compare(p11, p11) == 0);
         assertTrue(ck.compare(p12, p11) == 0);
@@ -211,7 +212,7 @@
         assertTrue(cv.compare(p21, p11) == 0);
         assertTrue(cv.compare(p21, p12) < 0);
 
-        Comparator<Mapping<K, V>> cmp = Comparators.compose(ck, cv);
+        Comparator<Map.Entry<K, V>> cmp = Comparators.compose(ck, cv);
         assertTrue(cmp.compare(p11, p11) == 0);
         assertTrue(cmp.compare(p12, p11) > 0);
         assertTrue(cmp.compare(p11, p12) < 0);