harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r434365 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/EnumSet.java main/java/java/util/MiniEnumSet.java test/java/tests/api/java/util/EnumSetTest.java
Date Thu, 24 Aug 2006 09:59:00 GMT
Author: pyang
Date: Thu Aug 24 02:58:58 2006
New Revision: 434365

URL: http://svn.apache.org/viewvc?rev=434365&view=rev
Log:
Patch applied for HARMONY-1267 ([classlib][luni] new methods ComplementOf(EnumSet), java.util.EnumSet.addAll(Collection),
remove(Object), equals(Object))

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumSet.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/MiniEnumSet.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumSetTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumSet.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumSet.java?rev=434365&r1=434364&r2=434365&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumSet.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumSet.java
Thu Aug 24 02:58:58 2006
@@ -104,8 +104,13 @@
      *             if the specified enum set is null
      */
     public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E>
s) {
-        throw new NotYetImplementedException();
+        EnumSet<E> set = EnumSet.noneOf(s.elementClass);
+        set.addAll(s);
+        set.complement();
+        return set;
     }
+    
+    abstract void complement();
 
     /**
      * Creates a new enum set, containing only the specified element. There are

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/MiniEnumSet.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/MiniEnumSet.java?rev=434365&r1=434364&r2=434365&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/MiniEnumSet.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/MiniEnumSet.java
Thu Aug 24 02:58:58 2006
@@ -27,10 +27,15 @@
     
     private int size = 0;
     
+    final private E[] enums;
+
+    static final private int MAX_ELEMENTS = 64;
+    
     private long bits = 0;
     
     MiniEnumSet(Class<E> elementType) {
         super(elementType);
+        enums = elementType.getEnumConstants();
     }
 
     @Override
@@ -60,11 +65,30 @@
         }
         bits |= mask;
 
-        size = Long.bitCount(bits);
+        size++;
         return true;
     }
     
     @Override
+    public boolean addAll(Collection<? extends E> collection) {
+        if (0 == collection.size()) {
+            return false;
+        }
+        if (collection instanceof EnumSet) {
+            EnumSet<E> set = (EnumSet<E>) collection;
+            if (!isValidType(set.elementClass)) {
+                throw new ClassCastException();
+            }
+            MiniEnumSet<E> miniSet = (MiniEnumSet<E>) set;
+            long oldBits = bits;
+            bits |= miniSet.bits;
+            size = Long.bitCount(bits);
+            return (oldBits != bits);
+        }
+        return super.addAll(collection);
+    }
+    
+    @Override
     @SuppressWarnings("unchecked")
     public boolean contains(Object object) {
         if (null == object) {
@@ -88,5 +112,40 @@
             return isValidType(set.elementClass ) && ((bits & set.bits) == set.bits);
         }
         return !(collection instanceof EnumSet) && super.containsAll(collection);
 
+    }
+    
+    @Override
+    @SuppressWarnings("unchecked")
+    public boolean remove(Object object) {
+        if (!contains(object)) {
+            return false;
+        }
+        E element = (E) object;
+        int ordinal = element.ordinal();
+        bits -= (1l << ordinal);
+        size--;
+        return true;
+    }
+    
+    @Override
+    @SuppressWarnings("unchecked")
+    public boolean equals(Object object) {
+        if (!(object instanceof EnumSet)) {
+            return super.equals(object);
+        }
+        EnumSet<E> set =(EnumSet<E>)object; 
+        if( !isValidType(set.elementClass) ) {
+            return size == 0 && set.size() == 0;
+        }
+        return bits == ((MiniEnumSet)set).bits;
+    }
+    
+    @Override
+    void complement() {
+        if (0 != enums.length) {
+            bits = ~bits;
+            bits &= (-1l >>> (MAX_ELEMENTS - enums.length));
+            size = enums.length - size;
+        }
     }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumSetTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumSetTest.java?rev=434365&r1=434364&r2=434365&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumSetTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumSetTest.java
Thu Aug 24 02:58:58 2006
@@ -134,6 +134,161 @@
     }
     
     /**
+     * @tests java.util.EnumSet#addAll(Collection)
+     */
+    @SuppressWarnings( { "unchecked", "boxing" })
+    public void test_addAll_LCollection() {
+
+        Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class);
+        assertEquals("Size should be 0:", 0, set.size()); //$NON-NLS-1$
+
+        try {
+            set.addAll(null);
+            fail("Should throw NullPointerException"); //$NON-NLS-1$
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        Set emptySet = EnumSet.noneOf(EmptyEnum.class);
+        Enum[] elements = EmptyEnum.class.getEnumConstants();
+        for(int i = 0; i < elements.length; i++) {
+            emptySet.add(elements[i]);
+        }
+        boolean result = set.addAll(emptySet);
+        assertFalse(result);
+
+        Collection<EnumFoo> collection = new ArrayList<EnumFoo>();
+        collection.add(EnumFoo.a);
+        collection.add(EnumFoo.b);
+        result = set.addAll(collection);
+        assertTrue("addAll should be successful", result); //$NON-NLS-1$
+        assertEquals("Size should be 2:", 2, set.size()); //$NON-NLS-1$
+
+        set = EnumSet.noneOf(EnumFoo.class);
+
+        Collection rawCollection = new ArrayList<Integer>();
+        result = set.addAll(rawCollection);
+        assertFalse(result);
+        rawCollection.add(1);
+        try {
+            set.addAll(rawCollection);
+            fail("Should throw ClassCastException"); //$NON-NLS-1$
+        } catch (ClassCastException e) {
+            // expected
+        }
+
+        Set<EnumFoo> fullSet = EnumSet.noneOf(EnumFoo.class);
+        fullSet.add(EnumFoo.a);
+        fullSet.add(EnumFoo.b);
+        result = set.addAll(fullSet);
+        assertTrue("addAll should be successful", result); //$NON-NLS-1$
+        assertEquals("Size of set should be 2", 2, set.size()); //$NON-NLS-1$
+
+        try {
+            fullSet.addAll(null);
+            fail("Should throw NullPointerException"); //$NON-NLS-1$
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        Set fullSetWithSubclass = EnumSet.noneOf(EnumWithInnerClass.class);
+        elements = EnumWithInnerClass.class.getEnumConstants();
+        for(int i = 0; i < elements.length; i++) {
+            fullSetWithSubclass.add(elements[i]);
+        }
+        try {
+            set.addAll(fullSetWithSubclass);
+            fail("Should throw ClassCastException"); //$NON-NLS-1$
+        } catch (ClassCastException e) {
+            // expected
+        }
+        Set<EnumWithInnerClass> setWithSubclass = fullSetWithSubclass;
+        result = setWithSubclass.addAll(setWithSubclass);
+        assertFalse("Should return false", result); //$NON-NLS-1$
+
+        Set<EnumWithInnerClass> anotherSetWithSubclass = EnumSet
+                .noneOf(EnumWithInnerClass.class);
+        elements = EnumWithInnerClass.class.getEnumConstants();
+        for(int i = 0; i < elements.length; i++) {
+            anotherSetWithSubclass.add((EnumWithInnerClass) elements[i]);
+        }
+        result = setWithSubclass.addAll(anotherSetWithSubclass);
+        assertFalse("Should return false", result); //$NON-NLS-1$
+
+        anotherSetWithSubclass.remove(EnumWithInnerClass.a);
+        result = setWithSubclass.addAll(anotherSetWithSubclass);
+        assertFalse("Should return false", result); //$NON-NLS-1$
+
+    }
+    
+    /**
+     * @tests java.util.EnumSet#remove(Object)
+     */
+    public void test_remove_LOject() {
+        Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class);
+        Enum[] elements = EnumFoo.class.getEnumConstants();
+        for(int i = 0; i < elements.length; i++) {
+            set.add((EnumFoo) elements[i]);
+        }
+        
+        boolean result = set.remove(null);
+        assertFalse("'set' does not contain null", result); //$NON-NLS-1$
+
+        result = set.remove(EnumFoo.a);
+        assertTrue("Should return true", result); //$NON-NLS-1$
+        result = set.remove(EnumFoo.a);
+        assertFalse("Should return false", result); //$NON-NLS-1$
+
+        assertEquals("Size of set should be 63:", 63, set.size()); //$NON-NLS-1$
+
+        result = set.remove(EnumWithInnerClass.a);
+        assertFalse("Should return false", result); //$NON-NLS-1$
+        result = set.remove(EnumWithInnerClass.f);
+        assertFalse("Should return false", result); //$NON-NLS-1$
+    }
+    
+    /**
+     * @tests java.util.EnumSet#equals(Object)
+     */
+    public void test_equals_LObject() {
+        Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class);
+        Enum[] elements = EnumFoo.class.getEnumConstants();
+        for(int i = 0; i < elements.length; i++) {
+            set.add((EnumFoo) elements[i]);
+        }
+        
+        assertFalse("Should return false", set.equals(null)); //$NON-NLS-1$
+        assertFalse(
+                "Should return false", set.equals(new Object())); //$NON-NLS-1$
+
+        Set<EnumFoo> anotherSet = EnumSet.noneOf(EnumFoo.class);
+        elements = EnumFoo.class.getEnumConstants();
+        for(int i = 0; i < elements.length; i++) {
+            anotherSet.add((EnumFoo) elements[i]);
+        }
+        assertTrue("Should return true", set.equals(anotherSet)); //$NON-NLS-1$
+        
+        anotherSet.remove(EnumFoo.a);
+        assertFalse(
+                "Should return false", set.equals(anotherSet)); //$NON-NLS-1$
+
+        Set<EnumWithInnerClass> setWithInnerClass = EnumSet
+                .noneOf(EnumWithInnerClass.class);
+        elements = EnumWithInnerClass.class.getEnumConstants();
+        for(int i = 0; i < elements.length; i++) {
+            setWithInnerClass.add((EnumWithInnerClass) elements[i]);
+        }
+        
+        assertFalse(
+                "Should return false", set.equals(setWithInnerClass)); //$NON-NLS-1$
+
+        setWithInnerClass.clear();
+        set.clear();
+        assertTrue("Should be equal", set.equals(setWithInnerClass)); //$NON-NLS-1$
+        
+    }
+    
+    /**
      * @tests java.util.EnumSet#clear()
      */
     public void test_clear() {
@@ -155,6 +310,38 @@
         set.add(EnumFoo.a);
         set.add(EnumFoo.b);
         assertEquals("Size should be 2", 2, set.size()); //$NON-NLS-1$
+    }
+    
+    /**
+     * @tests java.util.EnumSet#complementOf(java.util.EnumSet)
+     */
+    public void test_ComplementOf_LEnumSet() {
+
+        try {
+            EnumSet.complementOf((EnumSet<EnumFoo>) null);
+            fail("Should throw NullPointerException"); //$NON-NLS-1$
+        } catch (NullPointerException npe) {
+            // expected
+        }
+
+        EnumSet<EnumWithInnerClass> set = EnumSet
+                .noneOf(EnumWithInnerClass.class);
+        set.add(EnumWithInnerClass.d);
+        set.add(EnumWithInnerClass.e);
+        set.add(EnumWithInnerClass.f);
+
+        assertEquals("Size should be 3:", 3, set.size()); //$NON-NLS-1$
+
+        EnumSet<EnumWithInnerClass> complementOfE = EnumSet.complementOf(set);
+        assertTrue(set.contains(EnumWithInnerClass.d));
+        assertEquals(
+                "complementOfE should have size 3", 3, complementOfE.size()); //$NON-NLS-1$
+        assertTrue("complementOfE should contain EnumWithSubclass.a:", //$NON-NLS-1$ 
+                complementOfE.contains(EnumWithInnerClass.a));
+        assertTrue("complementOfE should contain EnumWithSubclass.b:", //$NON-NLS-1$
+                complementOfE.contains(EnumWithInnerClass.b));
+        assertTrue("complementOfE should contain EnumWithSubclass.c:", //$NON-NLS-1$
+                complementOfE.contains(EnumWithInnerClass.c));
     }
     
     /**



Mime
View raw message