harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r433135 - 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 Mon, 21 Aug 2006 03:27:07 GMT
Author: pyang
Date: Sun Aug 20 20:27:05 2006
New Revision: 433135

URL: http://svn.apache.org/viewvc?rev=433135&view=rev
Log:
Patch applied for HARMONY-1219 ([classlib][luni] new method(s) java.util.EnumSet.add(E) -
inherited from java.util.AbstractCollection, java.util.EnumSet.clear, java.util.EnumSet.size)

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=433135&r1=433134&r2=433135&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
Sun Aug 20 20:27:05 2006
@@ -250,4 +250,8 @@
     public EnumSet<E> clone() {
         throw new NotYetImplementedException();
     }
+    
+    boolean isValidType(Class cls) {
+        return cls == elementClass || cls.getSuperclass() == elementClass;
+    }
 }

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=433135&r1=433134&r2=433135&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
Sun Aug 20 20:27:05 2006
@@ -22,8 +22,13 @@
  * with less than or equal to 64 elements.
  * 
  */
+@SuppressWarnings("serial")
 final class MiniEnumSet<E extends Enum<E>> extends EnumSet<E> {
     
+    private int size = 0;
+    
+    private long bits = 0;
+    
     MiniEnumSet(Class<E> elementType) {
         super(elementType);
     }
@@ -35,6 +40,27 @@
 
     @Override
     public int size() {
-        throw new NotYetImplementedException();
+        return size;
+    }
+    
+    @Override
+    public void clear() {
+        bits = 0;
+        size = 0;
+    }
+    
+    @Override
+    public boolean add(E element) {
+        if (!isValidType(element.getDeclaringClass())) {
+            throw new ClassCastException();
+        }
+        int mask = 1 << element.ordinal();
+        if ((bits & mask) == mask) {
+            return false;
+        }
+        bits |= mask;
+
+        size = Long.bitCount(bits);
+        return true;
     }
 }

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=433135&r1=433134&r2=433135&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
Sun Aug 20 20:27:05 2006
@@ -16,15 +16,25 @@
 package tests.api.java.util;
 
 import java.util.EnumSet;
+import java.util.Set;
 
 import junit.framework.TestCase;
 
 public class EnumSetTest extends TestCase {
+    
+    static enum EnumWithInnerClass {
+        a, b, c, d, e, f {
+        },
+    }
 
     enum EnumWithAllInnerClass {
         a {},
         b {},
     }
+    
+    static enum EnumFoo {
+        a, b,
+    }
 
     /**
      * @tests java.util.EnumSet#noneOf(java.lang.Class)
@@ -57,5 +67,87 @@
         EnumSet<EnumWithAllInnerClass> setWithInnerClass = EnumSet
                 .noneOf(EnumWithAllInnerClass.class);
         assertNotNull(setWithInnerClass);
+    }
+    
+    /**
+     * @tests java.util.EnumSet#add(E)
+     */
+    @SuppressWarnings("unchecked")
+    public void test_add_E() {
+        Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class);
+        set.add(EnumFoo.a);
+        set.add(EnumFoo.b);
+        
+        try {
+            set.add(null);
+            fail("Should throw NullPointerException"); //$NON-NLS-1$
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        set.clear();
+        try {
+            set.add(null);
+            fail("Should throw NullPointerException"); //$NON-NLS-1$
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        boolean result = set.add(EnumFoo.a);
+        assertEquals("Size should be 1:", 1, set.size()); //$NON-NLS-1$
+        assertTrue("Return value should be true", result); //$NON-NLS-1$
+
+        result = set.add(EnumFoo.a);
+        assertEquals("Size should be 1:", 1, set.size()); //$NON-NLS-1$
+        assertFalse("Return value should be false", result); //$NON-NLS-1$
+
+        set.add(EnumFoo.b);
+        assertEquals("Size should be 2:", 2, set.size()); //$NON-NLS-1$
+        
+        Set rawSet = set;
+        try {
+            rawSet.add(EnumWithAllInnerClass.a);
+            fail("Should throw ClassCastException"); //$NON-NLS-1$
+        } catch(ClassCastException e) {
+            // expected
+        }
+        
+        try {
+            rawSet.add(EnumWithInnerClass.a);
+            fail("Should throw ClassCastException"); //$NON-NLS-1$
+        } catch(ClassCastException e) {
+            // expected
+        }
+        
+        try {
+            rawSet.add(new Object());
+            fail("Should throw ClassCastException"); //$NON-NLS-1$
+        } catch(ClassCastException e) {
+            // expected
+        }
+    }
+    
+    /**
+     * @tests java.util.EnumSet#clear()
+     */
+    public void test_clear() {
+        Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class);
+        set.add(EnumFoo.a);
+        set.add(EnumFoo.b);
+        assertEquals("Size should be 2", 2, set.size()); //$NON-NLS-1$
+
+        set.clear();
+
+        assertEquals("Size should be 0", 0, set.size()); //$NON-NLS-1$
+    }
+    
+    /**
+     * @tests java.util.EnumSet#size()
+     */
+    public void test_size() {
+        Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class);
+        set.add(EnumFoo.a);
+        set.add(EnumFoo.b);
+        assertEquals("Size should be 2", 2, set.size()); //$NON-NLS-1$
     }
 }



Mime
View raw message