FileDocCategorySizeDatePackage
MiniEnumSet.javaAPI DocAndroid 1.5 API7263Wed May 06 22:41:04 BST 2009java.util

MiniEnumSet

public final class MiniEnumSet extends EnumSet
This is a concrete subclass of EnumSet designed specifically for enum type with less than or equal to 64 elements.

Fields Summary
private static final int
MAX_ELEMENTS
private int
size
private final E[]
enums
private long
bits
Constructors Summary
MiniEnumSet(Class elementType, E[] enums)
Constructs an instance.

param
elementType non-null; type of the elements
param
enums non-null; prepopulated array of constants in ordinal order

    
    // BEGIN android-changed
                              
        
        super(elementType);
        this.enums = enums;
    
Methods Summary
public booleanadd(E element)

        if (!isValidType(element.getDeclaringClass())) {
            throw new ClassCastException();
        }
        long mask = 1l << element.ordinal();
        if ((bits & mask) == mask) {
            return false;
        }
        bits |= mask;

        size++;
        return true;
    
public booleanaddAll(java.util.Collection collection)

        if (0 == collection.size()) {
            return false;
        }
        if (collection instanceof EnumSet) {
            EnumSet<?> set = (EnumSet)collection;
            if (!isValidType(set.elementClass)) {
                throw new ClassCastException();
            }
            MiniEnumSet<?> miniSet = (MiniEnumSet<?>) set;
            long oldBits = bits;
            bits |= miniSet.bits;
            size = Long.bitCount(bits);
            return (oldBits != bits);
        }
        return super.addAll(collection);
    
public voidclear()

        bits = 0;
        size = 0;
    
voidcomplement()

        if (0 != enums.length) {
            bits = ~bits;
            bits &= (-1l >>> (MAX_ELEMENTS - enums.length));
            size = enums.length - size;
        }
    
public booleancontains(java.lang.Object object)

        if (null == object) {
            return false;
        }
        if (!isValidType(object.getClass())) {
            return false;
        }
        Enum<?> element = (Enum<?>) object;
        int ordinal = element.ordinal();
        return (bits & (1l << ordinal)) != 0;
    
public booleancontainsAll(java.util.Collection collection)

        if (collection.size() == 0) {
            return true;
        }
        if (collection instanceof MiniEnumSet) {
            MiniEnumSet<?> set = (MiniEnumSet<?>) collection;
            return isValidType(set.elementClass ) && ((bits & set.bits) == set.bits);
        }
        return !(collection instanceof EnumSet) && super.containsAll(collection);  
    
public booleanequals(java.lang.Object object)

        if (!(object instanceof EnumSet)) {
            return super.equals(object);
        }
        EnumSet<?> set =(EnumSet<?>)object; 
        if( !isValidType(set.elementClass) ) {
            return size == 0 && set.size() == 0;
        }
        return bits == ((MiniEnumSet<?>)set).bits;
    
public java.util.Iteratoriterator()

        return new MiniEnumSetIterator();
    
public booleanremove(java.lang.Object object)

        if (!contains(object)) {
            return false;
        }
        Enum<?> element = (Enum<?>) object;
        int ordinal = element.ordinal();
        bits -= (1l << ordinal);
        size--;
        return true;
    
public booleanremoveAll(java.util.Collection collection)

        if (0 == collection.size()) {
            return false;
        }
        if (collection instanceof EnumSet) {
            EnumSet<E> set = (EnumSet<E>) collection;
            boolean removeSuccessful = false;
            if (isValidType(set.elementClass)) {
                long mask = bits & ((MiniEnumSet<E>) set).bits;
                if (mask != 0) {
                    bits -= mask;
                    size = Long.bitCount(bits);
                    removeSuccessful = true;
                }
            }
            return removeSuccessful;
        }
        return super.removeAll(collection);
    
public booleanretainAll(java.util.Collection collection)

        if (collection instanceof EnumSet) {
            EnumSet<E> set = (EnumSet<E>) collection;
            if (!isValidType(set.elementClass)) {
                clear();
                return true;
            }
            boolean retainSuccessful = false;
            long oldBits = bits;
            bits &= ((MiniEnumSet<E>)set).bits;
            if (oldBits != bits) {
                size = Long.bitCount(bits);
                retainSuccessful = true;
            }
            return retainSuccessful;
        }
        return super.retainAll(collection);
    
voidsetRange(E start, E end)

        int length = end.ordinal() - start.ordinal() + 1;
        long range = (-1l >>> (MAX_ELEMENTS - length)) << start.ordinal();
        bits |= range;
        size = Long.bitCount(bits);
    
public intsize()

        return size;