harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r548355 - in /harmony/enhanced/classlib/branches/java6/modules/luni/src: main/java/java/util/ test/java/tests/api/java/util/ test/resources/serialization/java/util/
Date Mon, 18 Jun 2007 13:29:17 GMT
Author: tellison
Date: Mon Jun 18 06:29:16 2007
New Revision: 548355

URL: http://svn.apache.org/viewvc?view=rev&rev=548355
Log:
Apply patch HARMONY-4198 ([classlib][luni][java6] New methods in java.util.Collections for
java6)

Added:
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/java/util/Collections_asLifoQueue.golden.ser
  (with props)
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/java/util/Collections_newSetFromMap.golden.ser
  (with props)
Modified:
    harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Collections.java
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/CollectionsTest.java

Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Collections.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Collections.java?view=diff&rev=548355&r1=548354&r2=548355
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Collections.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Collections.java
Mon Jun 18 06:29:16 2007
@@ -18,6 +18,7 @@
 package java.util;
 
 import java.io.IOException;
+import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.lang.reflect.Array;
@@ -2654,7 +2655,236 @@
         }
         return obj;
     }
+    
+    /**
+     * Answers a set backed by a map. And the map must be empty when this method
+     * is called.
+     * 
+     * @param <E>
+     *            type of elements in set
+     * @param map
+     *            the backing map
+     * @return the set from the map
+     * @throws IllegalArgumentException
+     *             if the map is not empty
+     * @since 1.6
+     */
+    public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
+        if (map.isEmpty()) {
+            return new SetFromMap<E>(map);
+        }
+        throw new IllegalArgumentException();
+    }
+
+    /**
+     * Answers a LIFO Queue as a view of a Deque. Methods in the returned Queue
+     * need to be re-written to implement the LIFO feature.
+     * 
+     * @param <T>
+     *            type of elements
+     * @param deque
+     *            the Deque
+     * @return the LIFO Queue
+     * @since 1.6
+     */
+    public static <T> Queue<T> asLifoQueue(Deque<T> deque) {
+        return new AsLIFOQueue<T>(deque);
+    }
+
+    private static class SetFromMap<E> extends AbstractSet<E> implements
+            Serializable {
+        private static final long serialVersionUID = 2454657854757543876L;
+
+        // must named as it, to pass serialization compatibility test.
+        private Map<E, Boolean> m;
+
+        private transient Set<E> backingSet;
+
+        SetFromMap(final Map<E, Boolean> map) {
+            super();
+            m = map;
+            backingSet = map.keySet();
+        }
+
+        @Override
+        public boolean equals(Object object) {
+            return backingSet.equals(object);
+        }
 
+        @Override
+        public int hashCode() {
+            return backingSet.hashCode();
+        }
+
+        @Override
+        public boolean add(E object) {
+            return m.put(object, Boolean.TRUE) == null;
+        }
+
+        @Override
+        public void clear() {
+            m.clear();
+        }
+
+        @Override
+        public String toString() {
+            return backingSet.toString();
+        }
+
+        @Override
+        public boolean contains(Object object) {
+            return backingSet.contains(object);
+        }
+
+        @Override
+        public boolean containsAll(Collection<?> collection) {
+            return backingSet.containsAll(collection);
+        }
+
+        @Override
+        public boolean isEmpty() {
+            return m.isEmpty();
+        }
+
+        @Override
+        public boolean remove(Object object) {
+            return m.remove(object) != null;
+        }
+
+        @Override
+        public boolean retainAll(Collection<?> collection) {
+            return backingSet.retainAll(collection);
+        }
+
+        @Override
+        public Object[] toArray() {
+            return backingSet.toArray();
+        }
+
+        @Override
+        public <T> T[] toArray(T[] contents) {
+            return backingSet.toArray(contents);
+        }
+
+        @Override
+        public Iterator<E> iterator() {
+            return backingSet.iterator();
+        }
+
+        @Override
+        public int size() {
+            return m.size();
+        }
+
+        @SuppressWarnings("unchecked")
+        private void readObject(ObjectInputStream stream) throws IOException,
+                ClassNotFoundException {
+            stream.defaultReadObject();
+            backingSet = m.keySet();
+        }
+    }
+
+    private static class AsLIFOQueue<E> extends AbstractQueue<E> implements
+            Serializable {
+        private static final long serialVersionUID = 1802017725587941708L;
+
+        // must named as it, to pass serialization compatibility test.
+        private final Deque<E> q;
+
+        AsLIFOQueue(final Deque<E> deque) {
+            super();
+            this.q = deque;
+        }
+
+        @Override
+        public Iterator<E> iterator() {
+            return q.iterator();
+        }
+
+        @Override
+        public int size() {
+            return q.size();
+        }
+
+        public boolean offer(E o) {
+            return q.offerFirst(o);
+        }
+
+        public E peek() {
+            return q.peekFirst();
+        }
+
+        public E poll() {
+            return q.pollFirst();
+        }
+
+        @Override
+        public boolean add(E o) {
+            q.push(o);
+            return true;
+        }
+
+        @Override
+        public void clear() {
+            q.clear();
+        }
+
+        @Override
+        public E element() {
+            return q.getFirst();
+        }
+
+        @Override
+        public E remove() {
+            return q.pop();
+        }
+
+        @Override
+        public boolean contains(Object object) {
+            return q.contains(object);
+        }
+
+        @Override
+        public boolean containsAll(Collection<?> collection) {
+            return q.containsAll(collection);
+        }
+
+        @Override
+        public boolean isEmpty() {
+            return q.isEmpty();
+        }
+
+        @Override
+        public boolean remove(Object object) {
+            return q.remove(object);
+        }
+
+        @Override
+        public boolean removeAll(Collection<?> collection) {
+            return q.removeAll(collection);
+        }
+
+        @Override
+        public boolean retainAll(Collection<?> collection) {
+            return q.retainAll(collection);
+        }
+
+        @Override
+        public Object[] toArray() {
+            return q.toArray();
+        }
+
+        @Override
+        public <T> T[] toArray(T[] contents) {
+            return q.toArray(contents);
+        }
+
+        @Override
+        public String toString() {
+            return q.toString();
+        }
+    }
+    
     /**
      * Class represents a dynamically typesafe view of the specified collection.
      */

Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/CollectionsTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/CollectionsTest.java?view=diff&rev=548355&r1=548354&r2=548355
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/CollectionsTest.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/CollectionsTest.java
Mon Jun 18 06:29:16 2007
@@ -17,12 +17,15 @@
 
 package tests.api.java.util;
 
+import java.io.Serializable;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Deque;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -31,6 +34,7 @@
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Map;
+import java.util.Queue;
 import java.util.Random;
 import java.util.RandomAccess;
 import java.util.Set;
@@ -39,12 +43,15 @@
 import java.util.TreeSet;
 
 import org.apache.harmony.luni.internal.nls.Messages;
+import org.apache.harmony.testframework.serialization.SerializationTest;
+import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
 
 import tests.support.Support_CollectionTest;
 import tests.support.Support_ListTest;
 import tests.support.Support_SetTest;
 import tests.support.Support_UnmodifiableCollectionTest;
 import tests.support.Support_UnmodifiableMapTest;
+import tests.util.SerializationTester;
 
 public class CollectionsTest extends junit.framework.TestCase {
 
@@ -1891,7 +1898,8 @@
         m.put("one", "1");
         m.put("two", "2");
         Map um = Collections.unmodifiableMap(m);
-        assertEquals("{one=1, two=2}", um.toString());
+        assertTrue(-1 != um.toString().indexOf("two=2"));
+        assertTrue(-1 != um.toString().indexOf("one=1"));
     }
     
     /**
@@ -1914,6 +1922,181 @@
 
 
     /**
+     * @tests {@link java.util.Collections#newSetFromMap(Map)}
+     */
+    public void test_newSetFromMap_LMap() throws Exception {
+        Integer testInt[] = new Integer[100];
+        for (int i = 0; i < testInt.length; i++) {
+            testInt[i] = new Integer(i);
+        }       
+        Map<Integer,Boolean> map = new HashMap<Integer,Boolean>();
+        Set<Integer> set = Collections.newSetFromMap(map);
+        for (int i = 0; i < testInt.length; i++) {
+            map.put(testInt[i], true);
+        }
+        // operater on map successed
+        map.put(testInt[1], false);
+        assertTrue(map.containsKey(testInt[1]));
+        assertEquals(100, map.size());
+        assertFalse(map.get(testInt[1]));
+        assertEquals(100, set.size());
+        assertTrue(set.contains(testInt[16]));
+        Iterator setIter = set.iterator();
+        Iterator mapIter = map.keySet().iterator();
+        int i = 0;
+        // in the same order
+        while(setIter.hasNext()){
+            assertEquals(mapIter.next(),setIter.next());
+        }
+        
+        // operator on set successed
+        Integer testInt101 = new Integer(101);
+        Integer testInt102 = new Integer(102);
+        set.add(testInt101);
+        assertTrue(set.contains(testInt101));
+        assertTrue(map.get(testInt101));
+        
+        // operator on map still passes
+        map.put(testInt102, false);
+        assertTrue(set.contains(testInt102));
+        assertFalse(map.get(testInt102));
+        
+        // exception thrown
+        try {
+            Collections.newSetFromMap(map);
+            fail ("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests serialization/deserialization.
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void testSerializationSelf_newSetFromMap() throws Exception {
+        Integer testInt[] = new Integer[100];
+        for (int i = 0; i < testInt.length; i++) {
+            testInt[i] = new Integer(i);
+        }       
+        Map<Integer,Boolean> map = new HashMap<Integer,Boolean>();
+        Set<Integer> set = Collections.newSetFromMap(map);
+        for (int i = 0; i < testInt.length; i++) {
+            map.put(testInt[i], true);
+        }
+        SerializationTest.verifySelf(set);
+    }
+
+    /**
+     * @tests serialization/deserialization compatibility with RI.
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void testSerializationCompatibility_newSetFromMap() throws Exception {
+        Integer testInt[] = new Integer[100];
+        for (int i = 0; i < testInt.length; i++) {
+            testInt[i] = new Integer(i);
+        }       
+        Map<Integer,Boolean> map = new HashMap<Integer,Boolean>();
+        Set<Integer> set = Collections.newSetFromMap(map);
+        for (int i = 0; i < testInt.length; i++) {
+            map.put(testInt[i], true);
+        }
+        assertTrue(SerializationTester.assertCompabilityEquals(set, "serialization/java/util/Collections_newSetFromMap.golden.ser"));
+    }
+    
+    /**
+     * @tests {@link java.util.Collections#asLifoQueue(Deque)
+     */
+    public void test_asLifoQueue() throws Exception {
+        Integer testInt[] = new Integer[100];
+        Integer test101 = new Integer(101);
+        for (int i = 0; i < testInt.length; i++) {
+            testInt[i] = new Integer(i);
+        }    
+        Deque deque = new ArrayDeque<Integer>();
+        Queue<Integer> que = Collections.asLifoQueue(deque);
+        for (int i = 0; i < testInt.length; i++) {
+            que.add(testInt[i]);
+        }        
+        assertEquals(100, deque.size());
+        assertEquals(100, que.size());
+        for (int i = testInt.length-1; i >=0 ; i--) {
+            assertEquals(testInt[i], deque.pop());
+        }                
+        assertEquals(0, deque.size());
+        assertEquals(0, que.size());
+        for (int i = 0; i < testInt.length; i++) {
+            deque.push(testInt[i]);
+        }        
+        assertEquals(100, deque.size());
+        assertEquals(100, que.size());
+        Collection col = new LinkedList<Integer>();
+        col.add(test101);
+        que.addAll(col);
+        assertEquals(test101, que.remove());
+        for (int i = testInt.length-1; i >=0 ; i--) {
+            assertEquals(testInt[i], que.remove());
+        }          
+        assertEquals(0, deque.size());
+        assertEquals(0, que.size());
+    }
+    
+    /**
+     * @tests serialization/deserialization.
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void testSerializationSelf_asLifoQueue() throws Exception {
+        Integer testInt[] = new Integer[100];
+        Integer test101 = new Integer(101);
+        for (int i = 0; i < testInt.length; i++) {
+            testInt[i] = new Integer(i);
+        }    
+        Deque deque = new ArrayDeque<Integer>();
+        Queue<Integer> que = Collections.asLifoQueue(deque);
+        for (int i = 0; i < testInt.length; i++) {
+            que.add(testInt[i]);
+        }        
+        SerializationTest.verifySelf(que, new SerializableAssert(){
+            public void assertDeserialized(Serializable initial, Serializable deserialized)
{
+                Queue<Integer> initque = (Queue) initial;
+                Queue<Integer> deserque = (Queue) deserialized;
+                while (!initque.isEmpty()){
+                    assertEquals(initque.remove(),deserque.remove());
+                }
+            }            
+        });
+    }
+
+    /**
+     * @tests serialization/deserialization compatibility with RI.
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void testSerializationCompatibility_asLifoQueue() throws Exception {
+        Integer testInt[] = new Integer[100];
+        Integer test101 = new Integer(101);
+        for (int i = 0; i < testInt.length; i++) {
+            testInt[i] = new Integer(i);
+        }    
+        Deque deque = new ArrayDeque<Integer>();
+        Queue<Integer> que = Collections.asLifoQueue(deque);
+        for (int i = 0; i < testInt.length; i++) {
+            que.add(testInt[i]);
+        }        
+        Queue deserQue = (Queue)SerializationTester.readObject(que, "serialization/java/util/Collections_asLifoQueue.golden.ser");
+        while(!deserQue.isEmpty()){
+            assertEquals(que.remove(),deserQue.remove());
+        }
+    }
+    
+    /**
+     * @tests java.util.Collections#emptyList()
+     */
+    public void test_emptyList() {
+    	List<String> list = Collections.emptyList();
+    	assertTrue("should be true", list.isEmpty());
+    }
+
+    /**
      * Sets up the fixture, for example, open a network connection. This method
      * is called before a test is executed.
      */
@@ -1936,8 +2119,8 @@
 			hm.put(objArray[i].toString(), objArray[i]);
 		}
 	}
-
-	/**
+    
+    /**
 	 * Tears down the fixture, for example, close a network connection. This
 	 * method is called after a test is executed.
 	 */

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

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

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

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



Mime
View raw message