You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2007/06/12 08:13:29 UTC

svn commit: r546386 - in /harmony/enhanced/classlib/branches/java6/modules/luni/src: main/java/java/util/ test/java/tests/api/java/util/ test/resources/serialization/java/util/

Author: pyang
Date: Mon Jun 11 23:13:29 2007
New Revision: 546386

URL: http://svn.apache.org/viewvc?view=rev&rev=546386
Log:
Apply patch for HARMONY-4125([classlib][luni][java6] 2 new internal classes in java.util.AbstractMap for java6)

Added:
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/SimpleEntryTest.java
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/SimpleImmutableEntryTest.java
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/java/util/AbstractMapTest_SimpleEntry.golden.ser   (with props)
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser   (with props)
Modified:
    harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/AbstractMap.java

Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/AbstractMap.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/AbstractMap.java?view=diff&rev=546386&r1=546385&r2=546386
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/AbstractMap.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/AbstractMap.java Mon Jun 11 23:13:29 2007
@@ -17,6 +17,8 @@
 
 package java.util;
 
+import java.io.Serializable;
+
 /**
  * AbstractMap is an abstract implementation of the Map interface. This
  * implementation does not support adding. A subclass must implement the
@@ -32,6 +34,241 @@
     Collection<V> valuesCollection;
 
     /**
+     * An immutable key-value mapping.
+     * 
+     * @param <K>
+     *            the type of key
+     * @param <V>
+     *            the type of value
+     * 
+     * @since 1.6
+     */
+    public static class SimpleImmutableEntry<K, V> implements Map.Entry<K, V>,
+            Serializable {
+
+        private static final long serialVersionUID = 7138329143949025153L;
+
+        private K key;
+
+        private V value;
+
+        /**
+         * Constructs a new instance by key and value.
+         * 
+         * @param theKey
+         *            the key
+         * @param theValue
+         *            the value
+         */
+        public SimpleImmutableEntry(K theKey, V theValue) {
+            key = theKey;
+            value = theValue;
+        }
+
+        /**
+         * Constructs a new instance by an entry
+         * 
+         * @param entry
+         *            the entry
+         */
+        public SimpleImmutableEntry(Map.Entry<? extends K, ? extends V> entry) {
+            key = entry.getKey();
+            value = entry.getValue();
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see java.util.Map.Entry#getKey()
+         */
+        public K getKey() {
+            return key;
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see java.util.Map.Entry#getValue()
+         */
+        public V getValue() {
+            return value;
+        }
+
+        /**
+         * Throws an UnsupportedOperationException.
+         * 
+         * @param object
+         *            new value
+         * @return (Does not)
+         * @throws UnsupportedOperationException
+         *             always
+         * 
+         * @see java.util.Map.Entry#setValue(java.lang.Object)
+         */
+        public V setValue(@SuppressWarnings("unused")
+        V object) {
+            throw new UnsupportedOperationException();
+        }
+
+        /**
+         * Answers whether the object is equal to this entry. This works across
+         * all kinds of the Map.Entry interface.
+         * 
+         * @see java.lang.Object#equals(java.lang.Object)
+         */
+        @Override
+        public boolean equals(Object object) {
+            if (this == object) {
+                return true;
+            }
+            if (object instanceof Map.Entry) {
+                Map.Entry<?, ?> entry = (Map.Entry<?, ?>) object;
+                return (key == null ? entry.getKey() == null : key.equals(entry
+                        .getKey()))
+                        && (value == null ? entry.getValue() == null : value
+                                .equals(entry.getValue()));
+            }
+            return false;
+        }
+
+        /**
+         * Answers the hash code of this entry.
+         * 
+         * @see java.lang.Object#hashCode()
+         */
+        @Override
+        public int hashCode() {
+            return (key == null ? 0 : key.hashCode())
+                    ^ (value == null ? 0 : value.hashCode());
+        }
+
+        /**
+         * Answers a String representation of this entry.
+         * 
+         * @see java.lang.Object#toString()
+         */
+        @Override
+        public String toString() {
+            return key + "=" + value; //$NON-NLS-1$
+        }
+    }
+
+    /**
+     * A key-value mapping.
+     * 
+     * @param <K>
+     *            the type of key
+     * @param <V>
+     *            the type of value
+     * 
+     * @since 1.6
+     */
+    public static class SimpleEntry<K, V> implements Map.Entry<K, V>,
+            Serializable {
+
+        private static final long serialVersionUID = -8499721149061103585L;
+
+        private K key;
+
+        private V value;
+
+        /**
+         * Constructs a new instance by key and value.
+         * 
+         * @param theKey
+         *            the key
+         * @param theValue
+         *            the value
+         */
+        public SimpleEntry(K theKey, V theValue) {
+            key = theKey;
+            value = theValue;
+        }
+
+        /**
+         * Constructs a new instance by an entry
+         * 
+         * @param entry
+         *            the entry
+         */
+        public SimpleEntry(Map.Entry<? extends K, ? extends V> entry) {
+            key = entry.getKey();
+            value = entry.getValue();
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see java.util.Map.Entry#getKey()
+         */
+        public K getKey() {
+            return key;
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see java.util.Map.Entry#getValue()
+         */
+        public V getValue() {
+            return value;
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see java.util.Map.Entry#setValue(java.lang.Object)
+         */
+        public V setValue(V object) {
+            V result = value;
+            value = object;
+            return result;
+        }
+
+        /**
+         * Answers whether the object is equal to this entry. This works across
+         * all kinds of the Map.Entry interface.
+         * 
+         * @see java.lang.Object#equals(java.lang.Object)
+         */
+        @Override
+        public boolean equals(Object object) {
+            if (this == object) {
+                return true;
+            }
+            if (object instanceof Map.Entry) {
+                Map.Entry<?, ?> entry = (Map.Entry<?, ?>) object;
+                return (key == null ? entry.getKey() == null : key.equals(entry
+                        .getKey()))
+                        && (value == null ? entry.getValue() == null : value
+                                .equals(entry.getValue()));
+            }
+            return false;
+        }
+
+        /**
+         * Answers the hash code of this entry.
+         * 
+         * @see java.lang.Object#hashCode()
+         */
+        @Override
+        public int hashCode() {
+            return (key == null ? 0 : key.hashCode())
+                    ^ (value == null ? 0 : value.hashCode());
+        }
+
+        /**
+         * Answers a String representation of this entry.
+         * 
+         * @see java.lang.Object#toString()
+         */
+        @Override
+        public String toString() {
+            return key + "=" + value; //$NON-NLS-1$
+        }
+    }
+
+    /**
      * Constructs a new instance of this AbstractMap.
      */
     protected AbstractMap() {
@@ -277,7 +514,9 @@
      *                when the key or value is null and this Map does not
      *                support null keys or values
      */
-    public V put(K key, V value) {
+    public V put(@SuppressWarnings("unused")
+    K key, @SuppressWarnings("unused")
+    V value) {
         throw new UnsupportedOperationException();
     }
 

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/SimpleEntryTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/SimpleEntryTest.java?view=auto&rev=546386
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/SimpleEntryTest.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/SimpleEntryTest.java Mon Jun 11 23:13:29 2007
@@ -0,0 +1,122 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package tests.api.java.util;
+
+import java.util.AbstractMap;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.AbstractMap.SimpleEntry;
+import java.util.Map.Entry;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.testframework.serialization.SerializationTest;
+
+import tests.util.SerializationTester;
+
+public class SimpleEntryTest extends TestCase {
+    public void test_SimpleEntry_Constructor_K_V() throws Exception {
+        new AbstractMap.SimpleEntry<Integer, String>(1,"test");
+        new AbstractMap.SimpleEntry(null,null);
+    }
+    
+    public void test_SimpleEntry_Constructor_LEntry() throws Exception {
+        Map map = new TreeMap();
+        map.put(1, "test");
+        Entry entryToPut = (Entry)map.entrySet().iterator().next();
+        Entry testEntry = new AbstractMap.SimpleEntry(entryToPut);
+        assertEquals(1,testEntry.getKey());
+        assertEquals("test",testEntry.getValue());
+        map.clear();
+        map.put(null, null);
+        entryToPut = (Entry)map.entrySet().iterator().next();
+        testEntry = new AbstractMap.SimpleEntry(entryToPut);
+        assertNull(testEntry.getKey());
+        assertNull(testEntry.getValue());
+        try {
+            new AbstractMap.SimpleEntry(null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+    }
+    
+    public void test_SimpleEntry_getKey() throws Exception {
+        Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1,"test");
+        assertEquals(1,entry.getKey());
+        entry = new AbstractMap.SimpleEntry(null,null);
+        assertNull(entry.getKey());
+    }
+    
+    public void test_SimpleEntry_getValue() throws Exception {
+        Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1,"test");
+        assertEquals("test",entry.getValue());
+        entry = new AbstractMap.SimpleEntry(null,null);
+        assertNull(entry.getValue());
+    }
+    
+    public void test_SimpleEntry_setValue() throws Exception {
+        Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1,"test");
+        assertEquals("test",entry.getValue());
+        entry.setValue("Another String");
+        assertEquals("Another String",entry.getValue());
+        entry = new AbstractMap.SimpleEntry(null,null);
+        assertNull(entry.getKey());
+    }
+    
+    public void test_SimpleEntry_equals() throws Exception {
+        Entry entry = new AbstractMap.SimpleEntry<Integer, String>(1,"test");
+        Map map = new TreeMap();
+        map.put(1, "test");
+        Entry entryToPut = (Entry)map.entrySet().iterator().next();
+        Entry testEntry = new AbstractMap.SimpleEntry(entryToPut);
+        assertEquals(entry,testEntry);
+        Entry ent = new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test");
+        assertEquals(entry,ent);
+    }
+    
+    public void test_SimpleEntry_hashCode() throws Exception {
+        Entry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
+        assertEquals((e.getKey() == null ? 0 : e.getKey().hashCode())
+                ^ (e.getValue() == null ? 0 : e.getValue().hashCode()), e
+                .hashCode());
+    }
+    
+    public void test_SimpleEntry_toString() throws Exception {
+        Entry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
+        assertEquals(e.getKey()+"="+e.getValue(),e.toString());
+    }
+    
+    /**
+     * @tests serialization/deserialization.
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void testSerializationSelf_SimpleEntry() throws Exception {
+        Entry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test");
+        SerializationTest.verifySelf(e);
+    }
+
+    /**
+     * @tests serialization/deserialization compatibility with RI.
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void testSerializationCompatibility_SimpleEntry() throws Exception {
+        SimpleEntry e = new AbstractMap.SimpleEntry<Integer, String>(1, "test");        
+        SerializationTester.assertCompabilityEquals(e, "serialization/java/util/AbstractMapTest_SimpleEntry.golden.ser");
+    }
+}

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/SimpleImmutableEntryTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/SimpleImmutableEntryTest.java?view=auto&rev=546386
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/SimpleImmutableEntryTest.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/SimpleImmutableEntryTest.java Mon Jun 11 23:13:29 2007
@@ -0,0 +1,135 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package tests.api.java.util;
+
+import java.lang.reflect.Array;
+import java.util.AbstractMap;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.AbstractMap.SimpleImmutableEntry;
+import java.util.Map.Entry;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.testframework.serialization.SerializationTest;
+
+import tests.util.SerializationTester;
+
+public class SimpleImmutableEntryTest extends TestCase {
+    public void test_SimpleImmutableEntry_Constructor_K_V() throws Exception {
+        new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test");
+        new AbstractMap.SimpleImmutableEntry(null,null);        
+    }
+    
+    public void test_SimpleImmutableEntry_Constructor_LEntry() throws Exception {
+        Map map = new TreeMap();
+        map.put(1, "test");
+        Entry entryToPut = (Entry)map.entrySet().iterator().next();
+        Entry testEntry = new AbstractMap.SimpleImmutableEntry(entryToPut);
+        assertEquals(1,testEntry.getKey());
+        assertEquals("test",testEntry.getValue());
+        map.clear();
+        map.put(null, null);
+        entryToPut = (Entry)map.entrySet().iterator().next();
+        testEntry = new AbstractMap.SimpleImmutableEntry(entryToPut);
+        assertNull(testEntry.getKey());
+        assertNull(testEntry.getValue());
+        try {
+            new AbstractMap.SimpleImmutableEntry(null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+    }
+    
+    public void test_SimpleImmutableEntry_getKey() throws Exception {
+        Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test");
+        assertEquals(1,entry.getKey());
+        entry = new AbstractMap.SimpleImmutableEntry(null,null);
+        assertNull(entry.getKey());
+    }
+    
+    public void test_SimpleImmutableEntry_getValue() throws Exception {
+        Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test");
+        assertEquals("test",entry.getValue());
+        entry = new AbstractMap.SimpleImmutableEntry(null,null);
+        assertNull(entry.getValue());
+    }
+    
+    public void test_SimpleImmutableEntry_setValue() throws Exception {
+        Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test");
+        assertEquals("test",entry.getValue());
+        try{
+            entry.setValue("Another String");
+            fail("should throw UnsupportedOperationException");
+        } catch (UnsupportedOperationException e){
+            // expected
+        }
+        assertEquals("test",entry.getValue());
+        try{
+            entry.setValue(null);
+            fail("should throw UnsupportedOperationException");
+        } catch (UnsupportedOperationException e){
+            // expected
+        }
+    }
+    
+    public void test_SimpleImmutableEntry_equals() throws Exception {
+        Entry entry = new AbstractMap.SimpleImmutableEntry<Integer, String>(1,"test");
+        Map map = new TreeMap();
+        map.put(1, "test");
+        Entry entryToPut = (Entry)map.entrySet().iterator().next();
+        Entry testEntry = new AbstractMap.SimpleImmutableEntry(entryToPut);
+        assertEquals(entry,testEntry);
+    }
+    
+    public void test_SimpleImmutableEntry_hashCode() throws Exception {
+        Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test");
+        assertEquals((e.getKey() == null ? 0 : e.getKey().hashCode())
+                ^ (e.getValue() == null ? 0 : e.getValue().hashCode()), e
+                .hashCode());
+    }
+    
+    public void test_SimpleImmutableEntry_toString() throws Exception {
+        Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test");
+        assertEquals(e.getKey()+"="+e.getValue(),e.toString());
+        Object array =Array.newInstance((byte[].class).getComponentType(), 10);
+        assertEquals(10,((byte[])array).length);
+    }
+    
+    /**
+     * @tests serialization/deserialization.
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void testSerializationSelf_SimpleImmutableEntry() throws Exception {
+        Entry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test");
+        SerializationTest.verifySelf(e);
+    }
+
+    /**
+     * @tests serialization/deserialization compatibility with RI.
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void testSerializationCompatibility_SimpleImmutableEntry() throws Exception {
+        SimpleImmutableEntry e = new AbstractMap.SimpleImmutableEntry<Integer, String>(1, "test");        
+        if (!(SerializationTester.readObject(e, "serialization/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser") instanceof SimpleImmutableEntry)){
+            fail("should be SimpleImmutableEntry");
+        }
+        SerializationTester.assertCompabilityEquals(e, "serialization/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser");        
+    }
+}

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/java/util/AbstractMapTest_SimpleEntry.golden.ser
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/java/util/AbstractMapTest_SimpleEntry.golden.ser?view=auto&rev=546386
==============================================================================
Binary file - no diff available.

Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/java/util/AbstractMapTest_SimpleEntry.golden.ser
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser?view=auto&rev=546386
==============================================================================
Binary file - no diff available.

Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/java/util/AbstractMapTest_SimpleImmutableEntry.golden.ser
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream