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 2006/08/31 07:48:50 UTC
svn commit: r438809 - 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
Author: pyang
Date: Wed Aug 30 22:48:50 2006
New Revision: 438809
URL: http://svn.apache.org/viewvc?rev=438809&view=rev
Log:
Patch applied for HARMONY-1344 ([classlib][luni] new methods java.util.EnumSet.of, copyOf, range)
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=438809&r1=438808&r2=438809&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 Wed Aug 30 22:48:50 2006
@@ -74,7 +74,9 @@
* if the specified enum set is null
*/
public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) {
- throw new NotYetImplementedException();
+ EnumSet<E> set = EnumSet.noneOf(s.elementClass);
+ set.addAll(s);
+ return set;
}
/**
@@ -91,7 +93,20 @@
* if the specified collection is null
*/
public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c) {
- throw new NotYetImplementedException();
+ if (c instanceof EnumSet) {
+ return copyOf((EnumSet<E>) c);
+ }
+ if (0 == c.size()) {
+ throw new IllegalArgumentException();
+ }
+ Iterator<E> iterator = c.iterator();
+ E element = iterator.next();
+ EnumSet<E> set = EnumSet.noneOf(element.getDeclaringClass());
+ set.add(element);
+ while (iterator.hasNext()) {
+ set.add(iterator.next());
+ }
+ return set;
}
/**
@@ -127,7 +142,9 @@
* if the specified element is null
*/
public static <E extends Enum<E>> EnumSet<E> of(E e) {
- throw new NotYetImplementedException();
+ EnumSet<E> set = EnumSet.noneOf(e.getDeclaringClass());
+ set.add(e);
+ return set;
}
/**
@@ -145,7 +162,9 @@
* if any of the specified elements is null
*/
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) {
- throw new NotYetImplementedException();
+ EnumSet<E> set = of(e1);
+ set.add(e2);
+ return set;
}
/**
@@ -165,7 +184,9 @@
* if any of the specified elements is null
*/
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3) {
- throw new NotYetImplementedException();
+ EnumSet<E> set = of(e1, e2);
+ set.add(e3);
+ return set;
}
/**
@@ -187,7 +208,9 @@
* if any of the specified elements is null
*/
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4) {
- throw new NotYetImplementedException();
+ EnumSet<E> set = of(e1, e2, e3);
+ set.add(e4);
+ return set;
}
/**
@@ -211,7 +234,9 @@
* if any of the specified elements is null
*/
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5) {
- throw new NotYetImplementedException();
+ EnumSet<E> set = of(e1, e2, e3, e4);
+ set.add(e5);
+ return set;
}
/**
@@ -228,7 +253,11 @@
* if any of the specified elements is null
*/
public static <E extends Enum<E>> EnumSet<E> of(E start, E... others) {
- throw new NotYetImplementedException();
+ EnumSet<E> set = of(start);
+ for (E e : others) {
+ set.add(e);
+ }
+ return set;
}
/**
@@ -246,8 +275,15 @@
* if start is behind end
*/
public static <E extends Enum<E>> EnumSet<E> range(E start, E end) {
- throw new NotYetImplementedException();
+ if (start.compareTo(end) > 0) {
+ throw new IllegalArgumentException();
+ }
+ EnumSet<E> set = EnumSet.noneOf(start.getDeclaringClass());
+ set.addRange(start, end);
+ return set;
}
+
+ abstract void addRange(E start, E end);
/**
* Creates a new enum set with the same elements as those contained in this
@@ -256,10 +292,12 @@
* @return a new enum set with the same elements as those contained in this
* enum set
*/
+ @Override
public EnumSet<E> clone() {
throw new NotYetImplementedException();
}
+ @SuppressWarnings("unchecked")
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=438809&r1=438808&r2=438809&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 Wed Aug 30 22:48:50 2006
@@ -218,7 +218,7 @@
if( !isValidType(set.elementClass) ) {
return size == 0 && set.size() == 0;
}
- return bits == ((MiniEnumSet)set).bits;
+ return bits == ((MiniEnumSet<E>)set).bits;
}
@Override
@@ -228,5 +228,13 @@
bits &= (-1l >>> (MAX_ELEMENTS - enums.length));
size = enums.length - size;
}
+ }
+
+ @Override
+ void addRange(E start, E end) {
+ int length = end.ordinal() - start.ordinal() + 1;
+ long range = (-1l >>> (MAX_ELEMENTS - length)) << start.ordinal();
+ bits |= range;
+ size += length;
}
}
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=438809&r1=438808&r2=438809&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 Wed Aug 30 22:48:50 2006
@@ -506,6 +506,97 @@
}
/**
+ * @tests java.util.EnumSet#copyOf(java.util.Collection)
+ */
+ @SuppressWarnings("unchecked")
+ public void test_CopyOf_LCollection() {
+ try {
+ EnumSet.copyOf((Collection) null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ Collection collection = new ArrayList();
+ try {
+ EnumSet.copyOf(collection);
+ fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ collection.add(new Object());
+ try {
+ EnumSet.copyOf(collection);
+ fail("Should throw ClassCastException"); //$NON-NLS-1$
+ } catch (ClassCastException e) {
+ // expected
+ }
+
+ Collection<EnumFoo> enumCollection = new ArrayList<EnumFoo>();
+ enumCollection.add(EnumFoo.b);
+
+ EnumSet<EnumFoo> copyOfEnumCollection = EnumSet.copyOf(enumCollection);
+ assertEquals("Size of copyOfEnumCollection should be 1:", //$NON-NLS-1$
+ 1, copyOfEnumCollection.size());
+ assertTrue("copyOfEnumCollection should contain EnumFoo.b:", //$NON-NLS-1$
+ copyOfEnumCollection.contains(EnumFoo.b));
+
+ enumCollection.add(null);
+ assertEquals("Size of enumCollection should be 2:", //$NON-NLS-1$
+ 2, enumCollection.size());
+
+ try {
+ copyOfEnumCollection = EnumSet.copyOf(enumCollection);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ Collection rawEnumCollection = new ArrayList();
+ rawEnumCollection.add(EnumFoo.a);
+ rawEnumCollection.add(EnumWithInnerClass.a);
+ try {
+ EnumSet.copyOf(rawEnumCollection);
+ fail("Should throw ClassCastException"); //$NON-NLS-1$
+ } catch(ClassCastException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.util.EnumSet#copyOf(java.util.EnumSet)
+ */
+ @SuppressWarnings("unchecked")
+ public void test_CopyOf_LEnumSet() {
+ EnumSet<EnumWithInnerClass> enumSet = EnumSet
+ .noneOf(EnumWithInnerClass.class);
+ enumSet.add(EnumWithInnerClass.a);
+ enumSet.add(EnumWithInnerClass.f);
+ EnumSet<EnumWithInnerClass> copyOfE = EnumSet.copyOf(enumSet);
+ assertEquals("Size of enumSet and copyOfE should be equal", //$NON-NLS-1$
+ enumSet.size(), copyOfE.size());
+
+ assertTrue("EnumWithSubclass.a should be contained in copyOfE", //$NON-NLS-1$
+ copyOfE.contains(EnumWithInnerClass.a));
+ assertTrue("EnumWithSubclass.f should be contained in copyOfE", //$NON-NLS-1$
+ copyOfE.contains(EnumWithInnerClass.f));
+
+ Object[] enumValue = copyOfE.toArray();
+ assertSame("enumValue[0] should be identical with EnumWithSubclass.a", //$NON-NLS-1$
+ enumValue[0], EnumWithInnerClass.a);
+ assertSame("enumValue[1] should be identical with EnumWithSubclass.f", //$NON-NLS-1$
+ enumValue[1], EnumWithInnerClass.f);
+
+ try {
+ EnumSet.copyOf((EnumSet) null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+ }
+
+ /**
* @tests java.util.EnumSet#removeAll(Collection)
*/
@SuppressWarnings("unchecked")
@@ -716,7 +807,7 @@
Iterator<EnumFoo> iterator = set.iterator();
Iterator<EnumFoo> anotherIterator = set.iterator();
- assertNotSame(iterator, anotherIterator);
+ assertNotSame("Should not be same", iterator, anotherIterator); //$NON-NLS-1$
try {
iterator.remove();
fail("Should throw IllegalStateException"); //$NON-NLS-1$
@@ -829,5 +920,194 @@
// RI's bug, EnumFoo.b should not exist at the moment.
assertFalse("Should return false", set.contains(EnumFoo.b)); //$NON-NLS-1$
+ }
+
+ /**
+ * @tests java.util.EnumSet#of(E)
+ */
+ public void test_Of_E() {
+ EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a);
+ assertEquals("enumSet should have length 1:", 1, enumSet.size()); //$NON-NLS-1$
+
+ assertTrue("enumSet should contain EnumWithSubclass.a:", //$NON-NLS-1$
+ enumSet.contains(EnumWithInnerClass.a));
+
+ try {
+ EnumSet.of((EnumWithInnerClass) null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.util.EnumSet#of(E, E)
+ */
+ public void test_Of_EE() {
+ EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a,
+ EnumWithInnerClass.b);
+ assertEquals("enumSet should have length 2:", 2, enumSet.size()); //$NON-NLS-1$
+
+ assertTrue("enumSet should contain EnumWithSubclass.a:", //$NON-NLS-1$
+ enumSet.contains(EnumWithInnerClass.a));
+ assertTrue("enumSet should contain EnumWithSubclass.b:", //$NON-NLS-1$
+ enumSet.contains(EnumWithInnerClass.b));
+
+ try {
+ EnumSet.of((EnumWithInnerClass) null, EnumWithInnerClass.a);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ try {
+ EnumSet.of( EnumWithInnerClass.a, (EnumWithInnerClass) null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ try {
+ EnumSet.of( (EnumWithInnerClass) null, (EnumWithInnerClass) null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ enumSet = EnumSet.of(EnumWithInnerClass.a, EnumWithInnerClass.a);
+ assertEquals("Size of enumSet should be 1", //$NON-NLS-1$
+ 1, enumSet.size());
+ }
+
+ /**
+ * @tests java.util.EnumSet#of(E, E, E)
+ */
+ public void test_Of_EEE() {
+ EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a,
+ EnumWithInnerClass.b, EnumWithInnerClass.c);
+ assertEquals("Size of enumSet should be 3:", 3, enumSet.size()); //$NON-NLS-1$
+
+ assertTrue(
+ "enumSet should contain EnumWithSubclass.a:", enumSet.contains(EnumWithInnerClass.a)); //$NON-NLS-1$
+ assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.c)); //$NON-NLS-1$
+
+ try {
+ EnumSet.of((EnumWithInnerClass) null, null, null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ enumSet = EnumSet.of(EnumWithInnerClass.a, EnumWithInnerClass.b,
+ EnumWithInnerClass.b);
+ assertEquals("enumSet should contain 2 elements:", 2, enumSet.size()); //$NON-NLS-1$
+ }
+
+ /**
+ * @tests java.util.EnumSet#of(E, E, E, E)
+ */
+ public void test_Of_EEEE() {
+ EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a,
+ EnumWithInnerClass.b, EnumWithInnerClass.c,
+ EnumWithInnerClass.d);
+ assertEquals("Size of enumSet should be 4", 4, enumSet.size()); //$NON-NLS-1$
+
+ assertTrue(
+ "enumSet should contain EnumWithSubclass.a:", enumSet.contains(EnumWithInnerClass.a)); //$NON-NLS-1$
+ assertTrue("enumSet should contain EnumWithSubclass.d:", enumSet //$NON-NLS-1$
+ .contains(EnumWithInnerClass.d));
+
+ try {
+ EnumSet.of((EnumWithInnerClass) null, null, null, null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.util.EnumSet#of(E, E, E, E, E)
+ */
+ public void test_Of_EEEEE() {
+ EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a,
+ EnumWithInnerClass.b, EnumWithInnerClass.c,
+ EnumWithInnerClass.d, EnumWithInnerClass.e);
+ assertEquals("Size of enumSet should be 5:", 5, enumSet.size()); //$NON-NLS-1$
+
+ assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.a)); //$NON-NLS-1$
+ assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.e)); //$NON-NLS-1$
+
+ try {
+ EnumSet.of((EnumWithInnerClass) null, null, null, null, null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.util.EnumSet#of(E, E...)
+ */
+ public void test_Of_EEArray() {
+ EnumWithInnerClass[] enumArray = new EnumWithInnerClass[] {
+ EnumWithInnerClass.b, EnumWithInnerClass.c };
+ EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a,
+ enumArray);
+ assertEquals("Should be equal", 3, enumSet.size()); //$NON-NLS-1$
+
+ assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.a)); //$NON-NLS-1$
+ assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.c)); //$NON-NLS-1$
+
+ try {
+ EnumSet.of(EnumWithInnerClass.a, (EnumWithInnerClass[])null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ EnumFoo[] foos = {EnumFoo.a, EnumFoo.c, EnumFoo.d};
+ EnumSet<EnumFoo> set = EnumSet.of(EnumFoo.c, foos);
+ assertEquals("size of set should be 1", 3, set.size()); //$NON-NLS-1$
+ assertTrue("Should contain EnumFoo.a", set.contains(EnumFoo.a)); //$NON-NLS-1$
+ assertTrue("Should contain EnumFoo.c", set.contains(EnumFoo.c)); //$NON-NLS-1$
+ assertTrue("Should contain EnumFoo.d", set.contains(EnumFoo.d)); //$NON-NLS-1$
+ }
+
+ /**
+ * @tests java.util.EnumSet#range(E, E)
+ */
+ public void test_Range_EE() {
+ try {
+ EnumSet.range(EnumWithInnerClass.c, null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ try {
+ EnumSet.range(null, EnumWithInnerClass.c);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ try {
+ EnumSet.range(null, (EnumWithInnerClass) null);
+ fail("Should throw NullPointerException"); //$NON-NLS-1$
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ try {
+ EnumSet.range(EnumWithInnerClass.b, EnumWithInnerClass.a);
+ fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ EnumSet<EnumWithInnerClass> enumSet = EnumSet.range(
+ EnumWithInnerClass.a, EnumWithInnerClass.a);
+ assertEquals("Size of enumSet should be 1", 1, enumSet.size()); //$NON-NLS-1$
+
}
}