Search in sources :

Example 16 with EnumSet

use of java.util.EnumSet in project guava by google.

the class TypeVisitorTest method testVisitRecursiveTypeBounds.

public <E extends Enum<E>> void testVisitRecursiveTypeBounds() {
    Type type = new TypeCapture<EnumSet<E>>() {
    }.capture();
    assertVisited(type);
    new BaseTypeVisitor() {

        @Override
        void visitParameterizedType(ParameterizedType t) {
            visit(t.getActualTypeArguments());
        }

        @Override
        void visitTypeVariable(TypeVariable<?> t) {
            visit(t.getBounds());
        }
    }.visit(type);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) EnumSet(java.util.EnumSet)

Example 17 with EnumSet

use of java.util.EnumSet in project guava by google.

the class SetsTest method testImmutableEnumSet_deserializationMakesDefensiveCopy.

// java serialization not supported in GWT.
@GwtIncompatible
public void testImmutableEnumSet_deserializationMakesDefensiveCopy() throws Exception {
    ImmutableSet<SomeEnum> original = Sets.immutableEnumSet(SomeEnum.A, SomeEnum.B);
    int handleOffset = 6;
    byte[] serializedForm = serializeWithBackReference(original, handleOffset);
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedForm));
    ImmutableSet<?> deserialized = (ImmutableSet<?>) in.readObject();
    EnumSet<?> delegate = (EnumSet<?>) in.readObject();
    assertEquals(original, deserialized);
    assertTrue(delegate.remove(SomeEnum.A));
    assertTrue(deserialized.contains(SomeEnum.A));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Sets.newEnumSet(com.google.common.collect.Sets.newEnumSet) EnumSet(java.util.EnumSet) ObjectInputStream(java.io.ObjectInputStream) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 18 with EnumSet

use of java.util.EnumSet in project j2objc by google.

the class EnumSetTest method test_removeAll_LCollection.

/**
     * @tests java.util.EnumSet#removeAll(Collection)
     */
@SuppressWarnings("unchecked")
public void test_removeAll_LCollection() {
    Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class);
    try {
        set.removeAll(null);
        //$NON-NLS-1$
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    set = EnumSet.allOf(EnumFoo.class);
    //$NON-NLS-1$
    assertEquals("Size of set should be 64:", 64, set.size());
    try {
        set.removeAll(null);
        //$NON-NLS-1$
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    Collection<EnumFoo> collection = new ArrayList<EnumFoo>();
    collection.add(EnumFoo.a);
    boolean result = set.removeAll(collection);
    //$NON-NLS-1$
    assertTrue("Should return true", result);
    //$NON-NLS-1$
    assertEquals("Size of set should be 63", 63, set.size());
    collection = new ArrayList();
    result = set.removeAll(collection);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    Set<EmptyEnum> emptySet = EnumSet.noneOf(EmptyEnum.class);
    result = set.removeAll(emptySet);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    EnumSet<EnumFoo> emptyFooSet = EnumSet.noneOf(EnumFoo.class);
    result = set.removeAll(emptyFooSet);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    emptyFooSet.add(EnumFoo.a);
    result = set.removeAll(emptyFooSet);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    Set<EnumWithInnerClass> setWithSubclass = EnumSet.noneOf(EnumWithInnerClass.class);
    result = set.removeAll(setWithSubclass);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    setWithSubclass.add(EnumWithInnerClass.a);
    result = set.removeAll(setWithSubclass);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    Set<EnumFoo> anotherSet = EnumSet.noneOf(EnumFoo.class);
    anotherSet.add(EnumFoo.a);
    set = EnumSet.allOf(EnumFoo.class);
    result = set.removeAll(anotherSet);
    //$NON-NLS-1$
    assertTrue("Should return true", result);
    //$NON-NLS-1$
    assertEquals("Size of set should be 63:", 63, set.size());
    Set<EnumWithInnerClass> setWithInnerClass = EnumSet.noneOf(EnumWithInnerClass.class);
    setWithInnerClass.add(EnumWithInnerClass.a);
    setWithInnerClass.add(EnumWithInnerClass.b);
    Set<EnumWithInnerClass> anotherSetWithInnerClass = EnumSet.noneOf(EnumWithInnerClass.class);
    anotherSetWithInnerClass.add(EnumWithInnerClass.c);
    anotherSetWithInnerClass.add(EnumWithInnerClass.d);
    result = anotherSetWithInnerClass.removeAll(setWithInnerClass);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    anotherSetWithInnerClass.add(EnumWithInnerClass.a);
    result = anotherSetWithInnerClass.removeAll(setWithInnerClass);
    //$NON-NLS-1$
    assertTrue("Should return true", result);
    assertEquals(//$NON-NLS-1$
    "Size of anotherSetWithInnerClass should remain 2", 2, anotherSetWithInnerClass.size());
    anotherSetWithInnerClass.remove(EnumWithInnerClass.c);
    anotherSetWithInnerClass.remove(EnumWithInnerClass.d);
    result = anotherSetWithInnerClass.remove(setWithInnerClass);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    Set rawSet = EnumSet.allOf(EnumWithAllInnerClass.class);
    result = rawSet.removeAll(EnumSet.allOf(EnumFoo.class));
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    setWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class);
    anotherSetWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class);
    setWithInnerClass.remove(EnumWithInnerClass.a);
    anotherSetWithInnerClass.remove(EnumWithInnerClass.f);
    result = setWithInnerClass.removeAll(anotherSetWithInnerClass);
    //$NON-NLS-1$
    assertTrue("Should return true", result);
    //$NON-NLS-1$
    assertEquals("Size of setWithInnerClass should be 1", 1, setWithInnerClass.size());
    result = setWithInnerClass.contains(EnumWithInnerClass.f);
    //$NON-NLS-1$
    assertTrue("Should return true", result);
    // test enum type with more than 64 elements
    Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class);
    Collection<HugeEnum> hugeCollection = new ArrayList<HugeEnum>();
    hugeCollection.add(HugeEnum.a);
    result = hugeSet.removeAll(hugeCollection);
    assertTrue(result);
    assertEquals(64, hugeSet.size());
    collection = new ArrayList();
    result = hugeSet.removeAll(collection);
    assertFalse(result);
    Set<HugeEnum> emptyHugeSet = EnumSet.noneOf(HugeEnum.class);
    result = hugeSet.removeAll(emptyHugeSet);
    assertFalse(result);
    Set<HugeEnumWithInnerClass> hugeSetWithSubclass = EnumSet.noneOf(HugeEnumWithInnerClass.class);
    result = hugeSet.removeAll(hugeSetWithSubclass);
    assertFalse(result);
    hugeSetWithSubclass.add(HugeEnumWithInnerClass.a);
    result = hugeSet.removeAll(hugeSetWithSubclass);
    assertFalse(result);
    Set<HugeEnum> anotherHugeSet = EnumSet.noneOf(HugeEnum.class);
    anotherHugeSet.add(HugeEnum.a);
    hugeSet = EnumSet.allOf(HugeEnum.class);
    result = hugeSet.removeAll(anotherHugeSet);
    assertTrue(result);
    assertEquals(63, set.size());
    Set<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet.noneOf(HugeEnumWithInnerClass.class);
    hugeSetWithInnerClass.add(HugeEnumWithInnerClass.a);
    hugeSetWithInnerClass.add(HugeEnumWithInnerClass.b);
    Set<HugeEnumWithInnerClass> anotherHugeSetWithInnerClass = EnumSet.noneOf(HugeEnumWithInnerClass.class);
    anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.c);
    anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.d);
    result = anotherHugeSetWithInnerClass.removeAll(setWithInnerClass);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.a);
    result = anotherHugeSetWithInnerClass.removeAll(hugeSetWithInnerClass);
    assertTrue(result);
    assertEquals(2, anotherHugeSetWithInnerClass.size());
    anotherHugeSetWithInnerClass.remove(HugeEnumWithInnerClass.c);
    anotherHugeSetWithInnerClass.remove(HugeEnumWithInnerClass.d);
    result = anotherHugeSetWithInnerClass.remove(hugeSetWithInnerClass);
    assertFalse(result);
    rawSet = EnumSet.allOf(HugeEnumWithInnerClass.class);
    result = rawSet.removeAll(EnumSet.allOf(HugeEnum.class));
    assertFalse(result);
    hugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class);
    anotherHugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class);
    hugeSetWithInnerClass.remove(HugeEnumWithInnerClass.a);
    anotherHugeSetWithInnerClass.remove(HugeEnumWithInnerClass.f);
    result = hugeSetWithInnerClass.removeAll(anotherHugeSetWithInnerClass);
    assertTrue(result);
    assertEquals(1, hugeSetWithInnerClass.size());
    result = hugeSetWithInnerClass.contains(HugeEnumWithInnerClass.f);
    assertTrue(result);
}
Also used : Set(java.util.Set) EnumSet(java.util.EnumSet) ArrayList(java.util.ArrayList)

Example 19 with EnumSet

use of java.util.EnumSet in project j2objc by google.

the class EnumSetTest method test_addAll_LCollection.

/**
     * @tests java.util.EnumSet#addAll(Collection)
     */
@SuppressWarnings({ "unchecked", "boxing" })
public void test_addAll_LCollection() {
    Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class);
    //$NON-NLS-1$
    assertEquals("Size should be 0:", 0, set.size());
    try {
        set.addAll(null);
        //$NON-NLS-1$
        fail("Should throw NullPointerException");
    } 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);
    //$NON-NLS-1$
    assertTrue("addAll should be successful", result);
    //$NON-NLS-1$
    assertEquals("Size should be 2:", 2, set.size());
    set = EnumSet.noneOf(EnumFoo.class);
    Collection rawCollection = new ArrayList<Integer>();
    result = set.addAll(rawCollection);
    assertFalse(result);
    rawCollection.add(1);
    try {
        set.addAll(rawCollection);
        //$NON-NLS-1$
        fail("Should throw ClassCastException");
    } catch (ClassCastException e) {
    // expected
    }
    Set<EnumFoo> fullSet = EnumSet.noneOf(EnumFoo.class);
    fullSet.add(EnumFoo.a);
    fullSet.add(EnumFoo.b);
    result = set.addAll(fullSet);
    //$NON-NLS-1$
    assertTrue("addAll should be successful", result);
    //$NON-NLS-1$
    assertEquals("Size of set should be 2", 2, set.size());
    try {
        fullSet.addAll(null);
        //$NON-NLS-1$
        fail("Should throw NullPointerException");
    } 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);
        //$NON-NLS-1$
        fail("Should throw ClassCastException");
    } catch (ClassCastException e) {
    // expected
    }
    Set<EnumWithInnerClass> setWithSubclass = fullSetWithSubclass;
    result = setWithSubclass.addAll(setWithSubclass);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    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);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    anotherSetWithSubclass.remove(EnumWithInnerClass.a);
    result = setWithSubclass.addAll(anotherSetWithSubclass);
    //$NON-NLS-1$
    assertFalse("Should return false", result);
    // test enum type with more than 64 elements
    Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class);
    assertEquals(0, hugeSet.size());
    try {
        hugeSet.addAll(null);
        //$NON-NLS-1$
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    hugeSet = EnumSet.allOf(HugeEnum.class);
    result = hugeSet.addAll(hugeSet);
    assertFalse(result);
    hugeSet = EnumSet.noneOf(HugeEnum.class);
    Collection<HugeEnum> hugeCollection = new ArrayList<HugeEnum>();
    hugeCollection.add(HugeEnum.a);
    hugeCollection.add(HugeEnum.b);
    result = hugeSet.addAll(hugeCollection);
    assertTrue(result);
    assertEquals(2, set.size());
    hugeSet = EnumSet.noneOf(HugeEnum.class);
    rawCollection = new ArrayList<Integer>();
    result = hugeSet.addAll(rawCollection);
    assertFalse(result);
    rawCollection.add(1);
    try {
        hugeSet.addAll(rawCollection);
        //$NON-NLS-1$
        fail("Should throw ClassCastException");
    } catch (ClassCastException e) {
    // expected
    }
    EnumSet<HugeEnum> aHugeSet = EnumSet.noneOf(HugeEnum.class);
    aHugeSet.add(HugeEnum.a);
    aHugeSet.add(HugeEnum.b);
    result = hugeSet.addAll(aHugeSet);
    assertTrue(result);
    assertEquals(2, hugeSet.size());
    try {
        aHugeSet.addAll(null);
        //$NON-NLS-1$
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    Set hugeSetWithSubclass = EnumSet.allOf(HugeEnumWithInnerClass.class);
    try {
        hugeSet.addAll(hugeSetWithSubclass);
        //$NON-NLS-1$
        fail("Should throw ClassCastException");
    } catch (ClassCastException e) {
    // expected
    }
    Set<HugeEnumWithInnerClass> hugeSetWithInnerSubclass = hugeSetWithSubclass;
    result = hugeSetWithInnerSubclass.addAll(hugeSetWithInnerSubclass);
    assertFalse(result);
    Set<HugeEnumWithInnerClass> anotherHugeSetWithSubclass = EnumSet.allOf(HugeEnumWithInnerClass.class);
    result = hugeSetWithSubclass.addAll(anotherHugeSetWithSubclass);
    assertFalse(result);
    anotherHugeSetWithSubclass.remove(HugeEnumWithInnerClass.a);
    result = setWithSubclass.addAll(anotherSetWithSubclass);
    assertFalse(result);
}
Also used : Set(java.util.Set) EnumSet(java.util.EnumSet) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 20 with EnumSet

use of java.util.EnumSet in project guava by hceylan.

the class SetsTest method testImmutableEnumSet_deserializationMakesDefensiveCopy.

@GwtIncompatible("java serialization not supported in GWT.")
public void testImmutableEnumSet_deserializationMakesDefensiveCopy() throws Exception {
    ImmutableSet<SomeEnum> original = Sets.immutableEnumSet(SomeEnum.A, SomeEnum.B);
    int handleOffset = 6;
    byte[] serializedForm = serializeWithBackReference(original, handleOffset);
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedForm));
    ImmutableSet<?> deserialized = (ImmutableSet<?>) in.readObject();
    EnumSet<?> delegate = (EnumSet<?>) in.readObject();
    assertEquals(original, deserialized);
    assertTrue(delegate.remove(SomeEnum.A));
    assertTrue(deserialized.contains(SomeEnum.A));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Sets.newEnumSet(com.google.common.collect.Sets.newEnumSet) EnumSet(java.util.EnumSet) ObjectInputStream(java.io.ObjectInputStream) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Aggregations

EnumSet (java.util.EnumSet)58 Set (java.util.Set)18 Map (java.util.Map)16 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)11 TreeSet (java.util.TreeSet)7 Test (org.junit.Test)7 ParseUtils.unexpectedElement (org.jboss.as.controller.parsing.ParseUtils.unexpectedElement)6 Collection (java.util.Collection)5 LinkedHashSet (java.util.LinkedHashSet)5 List (java.util.List)5 TreeMap (java.util.TreeMap)5 StateStorage.toStringStringSetMap (org.apache.karaf.features.internal.service.StateStorage.toStringStringSetMap)5 PathAddress (org.jboss.as.controller.PathAddress)5 PathElement (org.jboss.as.controller.PathElement)5 ModelNode (org.jboss.dmr.ModelNode)5 FeatureState (org.apache.karaf.features.FeatureState)4 Nullable (com.android.annotations.Nullable)2 ResourceUrl (com.android.ide.common.resources.ResourceUrl)2